SettingViewModel.MotionCard.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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.Motion.Motions;
  9. using TeamAAS.Views;
  10. namespace TeamAAS.ViewModels
  11. {
  12. /// <summary>
  13. /// 设置页 - 控制卡管理(列表 / 连接 / 属性)。
  14. /// 调试操作已下沉到库内 MotionDebugView。
  15. /// </summary>
  16. public partial class SettingViewModel
  17. {
  18. #region 控制卡管理
  19. private readonly MotionDebugPageFactory _motionDebugFactory = new MotionDebugPageFactory();
  20. public ObservableCollection<MotionDeviceConfig> MotionCards { get; }
  21. private MotionDeviceConfig _selectedMotionCard;
  22. public MotionDeviceConfig SelectedMotionCard
  23. {
  24. get => _selectedMotionCard;
  25. set
  26. {
  27. if (SetProperty(ref _selectedMotionCard, value))
  28. RefreshMotionDebugHost();
  29. RaisePropertyChanged(nameof(HasSelectedMotionCard));
  30. }
  31. }
  32. private object _currentMotionDebugPage;
  33. public object CurrentMotionDebugPage
  34. {
  35. get => _currentMotionDebugPage;
  36. private set => SetProperty(ref _currentMotionDebugPage, value);
  37. }
  38. private void RefreshMotionDebugHost()
  39. {
  40. if (_selectedMotionCard == null)
  41. {
  42. CurrentMotionDebugPage = null;
  43. return;
  44. }
  45. try
  46. {
  47. var motionCard = _motionManager.GetMotion(_selectedMotionCard.Name);
  48. if (motionCard == null) return;
  49. CurrentMotionDebugPage = _motionDebugFactory.CreatePage(motionCard);
  50. }
  51. catch (Exception ex)
  52. {
  53. System.Diagnostics.Debug.WriteLine($"加载控制卡调试页失败: {ex.Message}");
  54. }
  55. }
  56. public bool HasSelectedMotionCard => _selectedMotionCard != null;
  57. #endregion
  58. #region 控制卡操作
  59. private void AddMotionCard()
  60. {
  61. var dialog = new AddMotionCardDialog(_motionManager)
  62. {
  63. Owner = Application.Current.MainWindow
  64. };
  65. if (dialog.ShowDialog() != true || dialog.Result == null)
  66. return;
  67. var motionInfo = dialog.Result;
  68. try
  69. {
  70. if (!_motionManager.AddDevice(motionInfo))
  71. {
  72. DialogHelper.ShowError("添加控制卡失败,名称可能重复或适配器不可用");
  73. return;
  74. }
  75. MotionCards.Add(motionInfo);
  76. SelectedMotionCard = motionInfo;
  77. DialogHelper.Success($"已添加 {motionInfo.Name}");
  78. }
  79. catch (Exception ex)
  80. {
  81. DialogHelper.ShowError("添加控制卡失败", ex);
  82. }
  83. }
  84. private void DeleteMotionCard()
  85. {
  86. if (SelectedMotionCard == null) return;
  87. if (!DialogHelper.ConfirmYesNo(
  88. $"确认删除 \"{SelectedMotionCard.Name}\" 吗?",
  89. "确认删除"))
  90. return;
  91. var cardToRemove = SelectedMotionCard;
  92. try
  93. {
  94. _motionManager.RemoveDevice(cardToRemove);
  95. MotionCards.Remove(cardToRemove);
  96. SelectedMotionCard = null;
  97. CurrentMotionDebugPage = null;
  98. }
  99. catch (Exception ex)
  100. {
  101. DialogHelper.ShowError("删除控制卡失败", ex);
  102. }
  103. }
  104. private Task ConnectMotionCardAsync()
  105. {
  106. if (SelectedMotionCard == null) return Task.CompletedTask;
  107. try
  108. {
  109. var motion = _motionManager.GetMotion(SelectedMotionCard.Name);
  110. if (motion == null)
  111. {
  112. var dev = _motionManager.CreateDevice(SelectedMotionCard);
  113. if (dev != null)
  114. _motionManager.Devices.Add(dev);
  115. motion = _motionManager.GetMotion(SelectedMotionCard.Name);
  116. }
  117. if (motion == null)
  118. {
  119. DialogHelper.ShowWarning("无法获取控制卡实例");
  120. return Task.CompletedTask;
  121. }
  122. if (motion.Open())
  123. {
  124. DialogHelper.Success("控制卡连接成功");
  125. RefreshMotionDebugHost();
  126. }
  127. else
  128. {
  129. DialogHelper.ShowError("控制卡连接失败");
  130. }
  131. }
  132. catch (Exception ex)
  133. {
  134. DialogHelper.ShowError("连接异常", ex);
  135. }
  136. return Task.CompletedTask;
  137. }
  138. private void DisconnectMotionCard()
  139. {
  140. if (SelectedMotionCard == null) return;
  141. try
  142. {
  143. var motion = _motionManager.GetMotion(SelectedMotionCard.Name);
  144. motion?.Close();
  145. RefreshMotionDebugHost();
  146. DialogHelper.Info("已断开控制卡");
  147. }
  148. catch (Exception ex)
  149. {
  150. DialogHelper.ShowError("断开异常", ex);
  151. }
  152. }
  153. #endregion
  154. #region 配置
  155. public void ApplyMotionCardEdit(MotionDeviceConfig edited)
  156. {
  157. try
  158. {
  159. if (edited == null) return;
  160. _motionManager.SaveConfig();
  161. ReloadMotionCardAxes(edited);
  162. RefreshMotionDebugHost();
  163. DialogHelper.Success("控制卡属性已更新并保存");
  164. }
  165. catch (Exception ex)
  166. {
  167. DialogHelper.ShowError("应用控制卡属性失败", ex);
  168. }
  169. }
  170. private void EditMotionAxes()
  171. {
  172. if (SelectedMotionCard == null) return;
  173. var current = MotionAxisCatalog.GetEffective(SelectedMotionCard);
  174. var dialog = new EditMotionAxesDialog(current)
  175. {
  176. Owner = Application.Current.MainWindow
  177. };
  178. if (dialog.ShowDialog() != true || dialog.Result == null)
  179. return;
  180. SelectedMotionCard.Axes = dialog.Result;
  181. SelectedMotionCard.AxisCount = dialog.Result.Count;
  182. _motionManager.SaveConfig();
  183. ReloadMotionCardAxes(SelectedMotionCard);
  184. RefreshMotionDebugHost();
  185. DialogHelper.Success("轴配置已保存");
  186. }
  187. private void EditMotionSafety()
  188. {
  189. if (SelectedMotionCard == null) return;
  190. var current = MotionAxisCatalog.GetEffective(SelectedMotionCard);
  191. var dialog = new EditMotionSafetyDialog(SelectedMotionCard, current)
  192. {
  193. Owner = Application.Current.MainWindow
  194. };
  195. if (dialog.ShowDialog() != true || dialog.Result == null)
  196. return;
  197. SelectedMotionCard.EstopDiIndex = dialog.EstopDiIndex;
  198. SelectedMotionCard.EstopDiBank = dialog.EstopDiBank;
  199. SelectedMotionCard.EstopActiveHigh = dialog.EstopActiveHigh;
  200. SelectedMotionCard.StopDiIndex = dialog.StopDiIndex;
  201. SelectedMotionCard.StopDiBank = dialog.StopDiBank;
  202. SelectedMotionCard.StopActiveHigh = dialog.StopActiveHigh;
  203. if (SelectedMotionCard.Axes == null || SelectedMotionCard.Axes.Count == 0)
  204. SelectedMotionCard.Axes = current;
  205. foreach (var row in dialog.Result)
  206. {
  207. var ax = SelectedMotionCard.Axes.FirstOrDefault(a => a != null && a.AxisNo == row.AxisNo);
  208. if (ax == null) continue;
  209. ax.SoftLimitEnabled = row.SoftLimitEnabled;
  210. ax.SoftLimitNeg = row.SoftLimitNeg;
  211. ax.SoftLimitPos = row.SoftLimitPos;
  212. ax.MaxVelocity = row.MaxVelocity;
  213. }
  214. _motionManager.SaveConfig();
  215. ReloadMotionCardAxes(SelectedMotionCard);
  216. RefreshMotionDebugHost();
  217. DialogHelper.Success("安全配置已保存");
  218. }
  219. private void ReloadMotionCardAxes(MotionDeviceConfig cfg)
  220. {
  221. var motion = _motionManager.GetMotion(cfg.Name);
  222. if (motion is InovanceMotionCard inovance)
  223. inovance.ReloadFromConfig();
  224. else if (motion is SimulatedMotionCard simulated)
  225. simulated.ReloadFromConfig();
  226. }
  227. private void RefreshMotionCardList()
  228. {
  229. MotionCards.Clear();
  230. foreach (var cfg in _motionManager.Configs)
  231. MotionCards.Add(cfg);
  232. SelectedMotionCard = MotionCards.FirstOrDefault();
  233. }
  234. private void SaveMotionCardConfig()
  235. {
  236. try
  237. {
  238. if (_motionManager.SaveConfig())
  239. DialogHelper.Success("控制卡配置已保存");
  240. else
  241. DialogHelper.ShowError("控制卡配置保存失败,请查看日志");
  242. }
  243. catch (Exception ex)
  244. {
  245. DialogHelper.ShowError("保存控制卡配置失败", ex);
  246. }
  247. }
  248. #endregion
  249. }
  250. }