SettingViewModel.MotionCard.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Windows;
  6. using TeamAAS.Motion;
  7. using TeamAAS.Motion.Models;
  8. using TeamAAS.Views;
  9. namespace TeamAAS.ViewModels
  10. {
  11. /// <summary>
  12. /// 设置页 - 控制卡管理(列表 / 连接 / 属性)。
  13. /// 调试操作已下沉到库内 MotionDebugView。
  14. /// </summary>
  15. public partial class SettingViewModel
  16. {
  17. #region 控制卡管理
  18. private readonly MotionDebugPageFactory _motionDebugFactory = new MotionDebugPageFactory();
  19. public ObservableCollection<MotionDeviceConfig> MotionCards { get; }
  20. private MotionDeviceConfig _selectedMotionCard;
  21. public MotionDeviceConfig SelectedMotionCard
  22. {
  23. get => _selectedMotionCard;
  24. set
  25. {
  26. if (SetProperty(ref _selectedMotionCard, value))
  27. RefreshMotionDebugHost();
  28. RaisePropertyChanged(nameof(HasSelectedMotionCard));
  29. }
  30. }
  31. private object _currentMotionDebugPage;
  32. public object CurrentMotionDebugPage
  33. {
  34. get => _currentMotionDebugPage;
  35. private set => SetProperty(ref _currentMotionDebugPage, value);
  36. }
  37. private void RefreshMotionDebugHost()
  38. {
  39. if (_selectedMotionCard == null)
  40. {
  41. CurrentMotionDebugPage = null;
  42. return;
  43. }
  44. try
  45. {
  46. var motionCard = _motionManager.GetMotion(_selectedMotionCard.Name);
  47. if (motionCard == null) return;
  48. CurrentMotionDebugPage = _motionDebugFactory.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. return;
  66. var motionInfo = dialog.Result;
  67. try
  68. {
  69. if (!_motionManager.AddDevice(motionInfo))
  70. {
  71. DialogHelper.ShowError("添加控制卡失败,名称可能重复或适配器不可用");
  72. return;
  73. }
  74. MotionCards.Add(motionInfo);
  75. SelectedMotionCard = motionInfo;
  76. DialogHelper.Success($"已添加 {motionInfo.Name}");
  77. }
  78. catch (Exception ex)
  79. {
  80. DialogHelper.ShowError("添加控制卡失败", ex);
  81. }
  82. }
  83. private void DeleteMotionCard()
  84. {
  85. if (SelectedMotionCard == null) return;
  86. if (!DialogHelper.ConfirmYesNo(
  87. $"确认删除 \"{SelectedMotionCard.Name}\" 吗?",
  88. "确认删除"))
  89. return;
  90. var cardToRemove = SelectedMotionCard;
  91. try
  92. {
  93. _motionManager.RemoveDevice(cardToRemove);
  94. MotionCards.Remove(cardToRemove);
  95. SelectedMotionCard = null;
  96. CurrentMotionDebugPage = null;
  97. }
  98. catch (Exception ex)
  99. {
  100. DialogHelper.ShowError("删除控制卡失败", ex);
  101. }
  102. }
  103. private Task ConnectMotionCardAsync()
  104. {
  105. if (SelectedMotionCard == null) return Task.CompletedTask;
  106. try
  107. {
  108. var motion = _motionManager.GetMotion(SelectedMotionCard.Name);
  109. if (motion == null)
  110. {
  111. var dev = _motionManager.CreateDevice(SelectedMotionCard);
  112. if (dev != null)
  113. _motionManager.Devices.Add(dev);
  114. motion = _motionManager.GetMotion(SelectedMotionCard.Name);
  115. }
  116. if (motion == null)
  117. {
  118. DialogHelper.ShowWarning("无法获取控制卡实例");
  119. return Task.CompletedTask;
  120. }
  121. if (motion.Open())
  122. {
  123. DialogHelper.Success("控制卡连接成功");
  124. RefreshMotionDebugHost();
  125. }
  126. else
  127. {
  128. DialogHelper.ShowError("控制卡连接失败");
  129. }
  130. }
  131. catch (Exception ex)
  132. {
  133. DialogHelper.ShowError("连接异常", ex);
  134. }
  135. return Task.CompletedTask;
  136. }
  137. private void DisconnectMotionCard()
  138. {
  139. if (SelectedMotionCard == null) return;
  140. try
  141. {
  142. var motion = _motionManager.GetMotion(SelectedMotionCard.Name);
  143. motion?.Close();
  144. RefreshMotionDebugHost();
  145. DialogHelper.Info("已断开控制卡");
  146. }
  147. catch (Exception ex)
  148. {
  149. DialogHelper.ShowError("断开异常", ex);
  150. }
  151. }
  152. #endregion
  153. #region 配置
  154. public void ApplyMotionCardEdit(MotionDeviceConfig edited)
  155. {
  156. try
  157. {
  158. if (edited == null) return;
  159. _motionManager.SaveConfig();
  160. RefreshMotionDebugHost();
  161. DialogHelper.Success("控制卡属性已更新并保存");
  162. }
  163. catch (Exception ex)
  164. {
  165. DialogHelper.ShowError("应用控制卡属性失败", ex);
  166. }
  167. }
  168. private void RefreshMotionCardList()
  169. {
  170. MotionCards.Clear();
  171. foreach (var cfg in _motionManager.Configs)
  172. MotionCards.Add(cfg);
  173. SelectedMotionCard = MotionCards.FirstOrDefault();
  174. }
  175. private void SaveMotionCardConfig()
  176. {
  177. try
  178. {
  179. if (_motionManager.SaveConfig())
  180. DialogHelper.Success("控制卡配置已保存");
  181. else
  182. DialogHelper.ShowError("控制卡配置保存失败,请查看日志");
  183. }
  184. catch (Exception ex)
  185. {
  186. DialogHelper.ShowError("保存控制卡配置失败", ex);
  187. }
  188. }
  189. #endregion
  190. }
  191. }