SettingViewModel.MotionCard.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using TeamAAS.Motion;
  9. using TeamAAS.Motion.Models;
  10. using TeamAAS.Views;
  11. namespace TeamAAS.ViewModels
  12. {
  13. /// <summary>
  14. /// 设置页 - 控制卡管理(列表 / 连接 / 属性)。
  15. /// 调试操作(jog / 复位)已下沉到库内 MotionDebugView。
  16. /// </summary>
  17. public partial class SettingViewModel
  18. {
  19. #region 控制卡管理
  20. MotionDebugPageFactory Factory=new MotionDebugPageFactory();
  21. public ObservableCollection<MotionDeviceConfig> MotionCards { get; }
  22. private MotionDeviceConfig _selectedMotionCard;
  23. public MotionDeviceConfig SelectedMotionCard
  24. {
  25. get { return _selectedMotionCard; }
  26. set
  27. {
  28. if ( SetProperty(ref _selectedMotionCard, value) )
  29. RefreshMotionDebugHost();
  30. RaisePropertyChanged(nameof(HasSelectedMotionCard));
  31. }
  32. }
  33. private object _currentMotionDebugPage;
  34. /// <summary>右侧调试页内容:RobotDebugPageFactory 创建(品牌专属页或库内默认页),选中机器人时恒非空。</summary>
  35. public object CurrentMotionDebugPage
  36. {
  37. get { return _currentMotionDebugPage; }
  38. private set { SetProperty(ref _currentMotionDebugPage, value); }
  39. }
  40. /// <summary>按选中的机器人刷新右侧调试页(工厂恒返回页面并绑定设备实例)。</summary>
  41. private void RefreshMotionDebugHost()
  42. {
  43. if ( _selectedMotionCard == null ) return;
  44. try
  45. {
  46. var motionCard = _motionManager.GetMotion(_selectedMotionCard.Name);
  47. if ( motionCard == null ) return;
  48. CurrentMotionDebugPage = Factory.CreatePage(motionCard);
  49. }
  50. catch ( Exception ex )
  51. {
  52. System.Diagnostics.Debug.WriteLine($"加载控制卡调试页失败: {ex.Message}");
  53. }
  54. }
  55. public bool HasSelectedMotionCard => _selectedMotionCard != null;
  56. #endregion
  57. #region 控制卡操作(列表管理 + 连接)
  58. private void AddMotionCard()
  59. {
  60. var dialog = new AddMotionCardDialog(_motionManager)
  61. {
  62. Owner = Application.Current.MainWindow
  63. };
  64. if ( dialog.ShowDialog() == true && dialog.Result != null )
  65. {
  66. var montionInfo = dialog.Result;
  67. try
  68. {
  69. _motionManager.CreateDevice (montionInfo);
  70. MotionCards.Add(montionInfo);
  71. SelectedMotionCard = montionInfo;
  72. DialogHelper.Success($"已添加 {montionInfo.Name}");
  73. }
  74. catch ( Exception ex )
  75. {
  76. DialogHelper.ShowError("添加控制卡失败", ex);
  77. }
  78. }
  79. }
  80. private void DeleteMotionCard()
  81. {
  82. if ( SelectedMotionCard == null ) return;
  83. if ( !DialogHelper.ConfirmYesNo(
  84. $"确认删除 \"{SelectedMotionCard.Name}\" 吗?",
  85. "确认删除") )
  86. {
  87. return;
  88. }
  89. var CardToRemove = SelectedMotionCard;
  90. try
  91. {
  92. _motionManager.RemoveDevice(CardToRemove);
  93. MotionCards.Remove(CardToRemove);
  94. SelectedMotionCard = null;
  95. }
  96. catch ( Exception ex )
  97. {
  98. System.Diagnostics.Debug.WriteLine($"释放控制卡资源异常: {ex.Message}");
  99. }
  100. MotionCards.Remove(CardToRemove);
  101. SelectedRobot = null;
  102. }
  103. private async Task ConnectMotionCardAsync()
  104. {
  105. if ( SelectedMotionCard == null ) return;
  106. try
  107. {
  108. var motion = _motionManager.GetMotion(SelectedMotionCard.Name);
  109. if ( motion == null )
  110. {
  111. _motionManager.CreateDevice(SelectedMotionCard);
  112. motion = _motionManager.GetMotion(SelectedMotionCard.Name);
  113. }
  114. if ( motion == null )
  115. {
  116. DialogHelper.ShowWarning("无法获取控制卡实例");
  117. return;
  118. }
  119. motion.Connect(SelectedMotionCard.ConnectionString);
  120. if ( motion.IsConnected ) DialogHelper.Success("控制卡连接成功");
  121. else DialogHelper.ShowError("控制卡连接失败");
  122. }
  123. catch ( Exception ex )
  124. {
  125. DialogHelper.ShowError("连接异常", ex);
  126. }
  127. }
  128. private void DisconnectMotionCard()
  129. {
  130. if ( SelectedMotionCard == null ) return;
  131. try
  132. {
  133. var motion = _motionManager.GetMotion(SelectedMotionCard.Name);
  134. if ( motion == null ) return;
  135. motion.Disconnect();
  136. DialogHelper.Info("已断开控制卡");
  137. }
  138. catch ( Exception ex )
  139. {
  140. DialogHelper.ShowError("断开异常", ex);
  141. }
  142. }
  143. #endregion
  144. #region 机器人列表刷新 & 配置保存 & 属性编辑
  145. public void ApplyMotionCardEdit(MotionDeviceConfig edited)
  146. {
  147. try
  148. {
  149. DialogHelper.Success("控制卡属性已更新并保存");
  150. }
  151. catch ( Exception ex )
  152. {
  153. DialogHelper.ShowError("应用控制卡属性失败", ex);
  154. }
  155. }
  156. private void RefreshMotionCardList()
  157. {
  158. MotionCards.Clear();
  159. var infos = _motionManager.Devices;
  160. foreach ( var info in infos )
  161. {
  162. MotionCards.Add(( MotionDeviceConfig ) info);
  163. }
  164. }
  165. private void SaveMotionCardConfig()
  166. {
  167. try
  168. {
  169. if ( _motionManager.SaveConfig() )
  170. {
  171. DialogHelper.Success("控制卡配置已保存");
  172. }
  173. else
  174. DialogHelper.ShowError("控制卡配置保存失败,请查看日志");
  175. }
  176. catch ( Exception ex )
  177. {
  178. DialogHelper.ShowError("保存控制卡配置失败", ex);
  179. }
  180. }
  181. #endregion
  182. }
  183. }