MotorManualWindowViewModel.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System;
  2. using System.Linq;
  3. using System.Collections;
  4. using System.Windows.Controls;
  5. using System.Collections.Generic;
  6. using System.Collections.ObjectModel;
  7. using Prism.Mvvm;
  8. using Prism.Commands;
  9. using Panuon.UI.Silver;
  10. using SHJX.Service.Model.Enums;
  11. using SHJX.Service.Control.Interface;
  12. using SHJX.Service.Model.CRUDModules;
  13. using SHJX.Service.Shell.Views.Manual;
  14. using SHJX.Service.Common.Constants;
  15. namespace SHJX.Service.Shell.ViewModels.Manual
  16. {
  17. public class MotorManualWindowViewModel : BindableBase
  18. {
  19. #region Fields
  20. private readonly IManualService _service;
  21. #endregion
  22. #region Properties
  23. public ObservableCollection<EquipmentArea> Areas { get; set; }
  24. public ObservableCollection<PositionHotlist> PositionHotlists { get; set; }
  25. public ObservableCollection<MotorSpeed> SpeedNames { get; set; }
  26. public List<ComboBoxItem> AreaPoint { get; set; }
  27. private ComboBoxItem _areaCurrentPoint;
  28. public ComboBoxItem AreaCurrentPoint
  29. {
  30. get => _areaCurrentPoint;
  31. set => SetProperty(ref _areaCurrentPoint, value);
  32. }
  33. public EquipmentArea Area
  34. {
  35. get => Areas.FirstOrDefault(item => item.PointName.Equals(AreaCurrentPoint.Content));
  36. set
  37. {
  38. Areas.Where(item => item.PointName.Equals(AreaCurrentPoint.Content)).ToArray()[0] = value;
  39. RaisePropertyChanged(nameof(Area));
  40. }
  41. }
  42. #endregion
  43. public MotorManualWindowViewModel(IManualService service)
  44. {
  45. _service = service;
  46. AreaPoint = new List<ComboBoxItem>();
  47. Areas = new ObservableCollection<EquipmentArea>(_service.GetData<EquipmentArea>());
  48. PositionHotlists = new ObservableCollection<PositionHotlist>(_service.GetData<PositionHotlist>());
  49. SpeedNames = new ObservableCollection<MotorSpeed>(_service.GetData<MotorSpeed>());
  50. (from EquipmentArea item in Areas select item.PointName).ToList().ForEach(item =>
  51. {
  52. AreaPoint.Add(new ComboBoxItem() { Content = item });
  53. });
  54. AreaCurrentPoint = AreaPoint.FirstOrDefault();
  55. }
  56. #region Command
  57. private DelegateCommand<object> _forwardCommand;
  58. public DelegateCommand<object> ForwardCommand => _forwardCommand ??= new DelegateCommand<object>(ExecuteForwardCommand);
  59. private DelegateCommand<object> _inversionCommand;
  60. public DelegateCommand<object> InversionCommand => _inversionCommand ??= new DelegateCommand<object>(ExecuteInversionCommand);
  61. private DelegateCommand<object> _areaSelectionChangedCommand;
  62. public DelegateCommand<object> AreaSelectionChangedCommand => _areaSelectionChangedCommand ??= new DelegateCommand<object>((obj) =>
  63. {
  64. Area = Areas.FirstOrDefault(item => item.PointName.Equals(AreaCurrentPoint.Content));
  65. });
  66. private DelegateCommand<object> _motorGoBackCommand;
  67. public DelegateCommand<object> MotorGoBackCommand => _motorGoBackCommand ??= new DelegateCommand<object>(ExecuteMotorGoBackCommand);
  68. private DelegateCommand<object> _motorStopCommand;
  69. public DelegateCommand<object> MotorStopCommand => _motorStopCommand ??= new DelegateCommand<object>(ExecuteMotorStopCommand);
  70. private DelegateCommand<object> _updateAreaCommand;
  71. public DelegateCommand<object> UpdateAreaCommand => _updateAreaCommand ??= new DelegateCommand<object>((obj) =>
  72. {
  73. EquipmentArea area = Areas.FirstOrDefault(item => item.PointName.Equals(AreaCurrentPoint.Content));
  74. bool res = _service.UpdateData(area);
  75. PositionHotlist ph = PositionHotlists.FirstOrDefault(item => item.PositionName.Equals(AreaCurrentPoint.Content));
  76. if (ph != null)
  77. {
  78. ph.Enable = area.Enable;
  79. if (ph.Enable)
  80. ph.CurrentOccupy = "";
  81. res = _service.UpdateData(ph);
  82. }
  83. _service.UpdateAreaAllData(area);
  84. Notice.Show($"{area.PointName}区域信息更新{(res ? "成功" : "失败")}", "Info", 3, MessageBoxIcon.Info);
  85. });
  86. public DelegateCommand<object> _speedSettingCommand;
  87. public DelegateCommand<object> SpeedSettingCommand => _speedSettingCommand ??= new DelegateCommand<object>(ExecuteSpeedSettingCommand);
  88. public DelegateCommand<object> _maniManualControlCommand;
  89. public DelegateCommand<object> ManiManualControlCommand => _maniManualControlCommand ??= new DelegateCommand<object>(ExecuteManiManualControlCommand);
  90. public DelegateCommand _singleMoveCommand;
  91. public DelegateCommand SingleMoveCommand => _singleMoveCommand ??= new DelegateCommand(() =>
  92. {
  93. CustomMoveWindow customMove = new(AreaCurrentPoint.Content.ToString());
  94. customMove.ShowDialog();
  95. });
  96. #endregion
  97. #region Execute
  98. /// <summary>
  99. /// 机械抓手操作
  100. /// </summary>
  101. /// <param name="obj"></param>
  102. private void ExecuteManiManualControlCommand(object obj)
  103. {
  104. RegisterExecuteType type = obj.ToString() switch
  105. {
  106. "杯抓" => RegisterExecuteType.Open,
  107. "杯松" => RegisterExecuteType.Close,
  108. _ => throw new ArgumentNullException(obj.ToString())
  109. };
  110. _service.ManualControlRegister(EquipmentNames.Tongs, type);
  111. }
  112. /// <summary>
  113. /// 电机正转
  114. /// </summary>
  115. /// <param name="obj"></param>
  116. public async void ExecuteForwardCommand(object obj)
  117. {
  118. if (obj is null)
  119. {
  120. return;
  121. }
  122. object[] receiveObj = obj as object[];
  123. if (receiveObj is null or { Length: 0 })
  124. {
  125. return;
  126. }
  127. double receiveValue = Math.Abs(Convert.ToDouble(receiveObj[1]));
  128. (string motorName, double value) = receiveObj[0].ToString()?.Trim() switch
  129. {
  130. "X轴" => (EquipmentNames.AxisX, -1 * receiveValue),
  131. "Y轴" => (EquipmentNames.AxisY, -1 * receiveValue),
  132. "Z轴" => (EquipmentNames.AxisZ, -1 * receiveValue),
  133. _ => throw new ArgumentNullException(receiveObj[0].ToString()?.Trim())
  134. };
  135. bool res = await _service.ManualWriteMoveToMotorAsync(motorName, value, MotorManualMoveType.Forward);
  136. Notice.Show($"{receiveObj[0]}正转{(res ? "成功" : "失败")}", "Info", 3, MessageBoxIcon.Info);
  137. }
  138. /// <summary>
  139. /// 电机反转
  140. /// </summary>
  141. /// <param name="obj"></param>
  142. public async void ExecuteInversionCommand(object obj)
  143. {
  144. if (obj is null)
  145. {
  146. return;
  147. }
  148. object[] receiveObj = obj as object[];
  149. if (receiveObj is null or { Length: 0 })
  150. {
  151. return;
  152. }
  153. double receiveValue = Math.Abs(Convert.ToDouble(receiveObj[1]));
  154. (string motorName, double value) = receiveObj[0].ToString()?.Trim() switch
  155. {
  156. "X轴" => (EquipmentNames.AxisX, receiveValue),
  157. "Y轴" => (EquipmentNames.AxisY, receiveValue),
  158. "Z轴" => (EquipmentNames.AxisZ, receiveValue),
  159. _ => throw new ArgumentNullException(receiveObj[0].ToString()?.Trim())
  160. };
  161. bool res = await _service.ManualWriteMoveToMotorAsync(motorName, value, MotorManualMoveType.Inversion);
  162. Notice.Show($"{receiveObj[0]}反转{(res ? "成功" : "失败")}", "Info", 3, MessageBoxIcon.Info);
  163. }
  164. /// <summary>
  165. /// 返回原点
  166. /// </summary>
  167. /// <param name="obj"></param>
  168. private async void ExecuteMotorGoBackCommand(object obj)
  169. {
  170. string backName = obj.ToString() switch
  171. {
  172. "X轴" => EquipmentNames.AxisX,
  173. "Y轴" => EquipmentNames.AxisY,
  174. "Z轴" => EquipmentNames.AxisZ,
  175. _ => throw new ArgumentNullException(obj.ToString()),
  176. };
  177. bool res = await _service.MotorGoBackAsync(backName);
  178. Notice.Show($"{obj}返回原点{(res ? "成功" : "失败")}", "Info", 3, MessageBoxIcon.Info);
  179. }
  180. /// <summary>
  181. /// 电机停止
  182. /// </summary>
  183. /// <param name="obj"></param>
  184. private void ExecuteMotorStopCommand(object obj)
  185. {
  186. string backName = obj.ToString() switch
  187. {
  188. "X轴" => EquipmentNames.AxisX,
  189. "Y轴" => EquipmentNames.AxisY,
  190. "Z轴" => EquipmentNames.AxisZ,
  191. _ => throw new ArgumentNullException(obj.ToString()),
  192. };
  193. _service.MotorStop(backName);
  194. }
  195. public async void ExecuteSpeedSettingCommand(object obj)
  196. {
  197. if (obj is null)
  198. {
  199. return;
  200. }
  201. IList lists = obj as IList;
  202. if (lists is null or { Count: 0 })
  203. {
  204. Notice.Show($"未选择数据,无法设置!", "Error", 3, MessageBoxIcon.Error);
  205. return;
  206. }
  207. bool res = true;
  208. foreach (MotorSpeed speed in from object item in lists where item is MotorSpeed let speed = item as MotorSpeed select speed)
  209. {
  210. res = res && await _service.UpdateSpeedAsync(speed.DetailName);
  211. }
  212. Notice.Show($"速度设置{(res ? "成功" : "失败")}", "Info", 3, MessageBoxIcon.Info);
  213. }
  214. #endregion
  215. }
  216. }