SettingViewModel.Robot.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Threading.Tasks;
  5. using System.Windows;
  6. using TeamAAS.Robot.Models;
  7. using TeamAAS.Robot.UI;
  8. using TeamAAS.Views;
  9. namespace TeamAAS.ViewModels
  10. {
  11. /// <summary>
  12. /// 设置页 - 机器人管理(列表 / 连接 / 属性)。
  13. /// 调试操作(jog / 电机 / 复位 / 刹车 / Tool / 坐标)已下沉到库内 DefaultRobotDebugPage。
  14. /// </summary>
  15. public partial class SettingViewModel
  16. {
  17. #region 机器人管理
  18. public ObservableCollection<RobotInfo> Robots { get; }
  19. private RobotInfo _selectedRobot;
  20. public RobotInfo SelectedRobot
  21. {
  22. get { return _selectedRobot; }
  23. set
  24. {
  25. if (SetProperty(ref _selectedRobot, value))
  26. RefreshRobotDebugHost();
  27. RaisePropertyChanged(nameof(HasSelectedRobot));
  28. }
  29. }
  30. private object _currentRobotDebugPage;
  31. /// <summary>右侧调试页内容:RobotDebugPageFactory 创建(品牌专属页或库内默认页),选中机器人时恒非空。</summary>
  32. public object CurrentRobotDebugPage
  33. {
  34. get { return _currentRobotDebugPage; }
  35. private set { SetProperty(ref _currentRobotDebugPage, value); }
  36. }
  37. /// <summary>按选中的机器人刷新右侧调试页(工厂恒返回页面并绑定设备实例)。</summary>
  38. private void RefreshRobotDebugHost()
  39. {
  40. (_currentRobotDebugPage as IRobotDebugPage)?.UnbindRobot();
  41. CurrentRobotDebugPage = null;
  42. if (_selectedRobot == null) return;
  43. try
  44. {
  45. var robot = _robotManager.GetRobot(_selectedRobot.Id);
  46. if (robot == null) return;
  47. CurrentRobotDebugPage = RobotDebugPageFactory.Create(robot);
  48. }
  49. catch (Exception ex)
  50. {
  51. System.Diagnostics.Debug.WriteLine($"加载机器人调试页失败: {ex.Message}");
  52. }
  53. }
  54. public bool HasSelectedRobot => _selectedRobot != null;
  55. #endregion
  56. #region 机器人操作(列表管理 + 连接)
  57. private void AddRobot()
  58. {
  59. var dialog = new AddRobotDialog(Robots.Count)
  60. {
  61. Owner = Application.Current.MainWindow
  62. };
  63. if (dialog.ShowDialog() == true && dialog.Result != null)
  64. {
  65. var robotInfo = dialog.Result;
  66. robotInfo.RobotNo = Robots.Count + 1;
  67. try
  68. {
  69. _robotManager.CreateRobot(robotInfo.Id, robotInfo);
  70. Robots.Add(robotInfo);
  71. SelectedRobot = robotInfo;
  72. DialogHelper.Success($"已添加 {robotInfo.RobotName}");
  73. }
  74. catch (Exception ex)
  75. {
  76. DialogHelper.ShowError("添加机器人失败", ex);
  77. }
  78. }
  79. }
  80. private void DeleteRobot()
  81. {
  82. if (SelectedRobot == null) return;
  83. if (!DialogHelper.ConfirmYesNo(
  84. $"确认删除 \"{SelectedRobot.RobotName}\" 吗?",
  85. "确认删除"))
  86. {
  87. return;
  88. }
  89. var robotToRemove = SelectedRobot;
  90. try
  91. {
  92. _robotManager.RemoveRobot(robotToRemove.Id);
  93. }
  94. catch (Exception ex)
  95. {
  96. System.Diagnostics.Debug.WriteLine($"释放机器人资源异常: {ex.Message}");
  97. }
  98. Robots.Remove(robotToRemove);
  99. for (int i = 0; i < Robots.Count; i++)
  100. {
  101. Robots[i].RobotNo = i + 1;
  102. }
  103. SelectedRobot = null;
  104. }
  105. private async Task ConnectRobotAsync()
  106. {
  107. if (SelectedRobot == null) return;
  108. try
  109. {
  110. var robot = _robotManager.GetRobot(SelectedRobot.Id);
  111. if (robot == null)
  112. {
  113. _robotManager.CreateRobot(SelectedRobot.Id, SelectedRobot);
  114. robot = _robotManager.GetRobot(SelectedRobot.Id);
  115. }
  116. if (robot == null)
  117. {
  118. DialogHelper.ShowWarning("无法获取机器人实例");
  119. return;
  120. }
  121. await robot.ConnectAsync();
  122. if (robot.IsConnected) DialogHelper.Success("机器人连接成功");
  123. else DialogHelper.ShowError("机器人连接失败");
  124. }
  125. catch (Exception ex)
  126. {
  127. DialogHelper.ShowError("连接异常", ex);
  128. }
  129. }
  130. private void DisconnectRobot()
  131. {
  132. if (SelectedRobot == null) return;
  133. try
  134. {
  135. var robot = _robotManager.GetRobot(SelectedRobot.Id);
  136. if (robot == null) return;
  137. robot.Disconnect();
  138. DialogHelper.Info("已断开机器人");
  139. }
  140. catch (Exception ex)
  141. {
  142. DialogHelper.ShowError("断开异常", ex);
  143. }
  144. }
  145. #endregion
  146. #region 机器人列表刷新 & 配置保存 & 属性编辑
  147. public void ApplyRobotEdit(RobotInfo edited)
  148. {
  149. try
  150. {
  151. var infos = _robotManager.GetAllRobotInfos();
  152. var list = new List<RobotInfo>(infos);
  153. int idx = list.FindIndex(r => r.Id == edited.Id);
  154. if (idx >= 0) list[idx] = edited;
  155. _robotManager.InitializeAllRobots(list.ToArray());
  156. _robotManager.SaveConfig();
  157. RefreshRobotList();
  158. DialogHelper.Success("机器人属性已更新并保存");
  159. }
  160. catch (Exception ex)
  161. {
  162. DialogHelper.ShowError("应用机器人属性失败", ex);
  163. }
  164. }
  165. private void RefreshRobotList()
  166. {
  167. Robots.Clear();
  168. var infos = _robotManager.GetAllRobotInfos();
  169. foreach (var info in infos)
  170. {
  171. Robots.Add(info);
  172. }
  173. }
  174. private void SaveRobotConfig()
  175. {
  176. try
  177. {
  178. if (_robotManager.SaveConfig())
  179. {
  180. RefreshRobotList();
  181. DialogHelper.Success("机器人配置已保存");
  182. }
  183. else
  184. DialogHelper.ShowError("机器人配置保存失败,请查看日志");
  185. }
  186. catch (Exception ex)
  187. {
  188. DialogHelper.ShowError("保存机器人配置失败", ex);
  189. }
  190. }
  191. #endregion
  192. }
  193. }