RobotManualStepViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. using Prism.Commands;
  2. using Prism.Ioc;
  3. using Prism.Mvvm;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using Team.FFFeederService.Interfaces;
  8. using Team.FFFeederService;
  9. using TeamAAS_VP.Core;
  10. using TeamAAS_VP.Interfaces;
  11. using System.Security.Cryptography;
  12. using TeamAAS_VP.Events;
  13. using Prism.Events;
  14. using System.Windows;
  15. using TeamAAS_VP.Resources.Languages;
  16. namespace TeamAAS_VP.ViewModels.DebugMod
  17. {
  18. public class RobotManualStepViewModel : BindableBase
  19. {
  20. IContainerProvider _container;
  21. IEventAggregator _eventAggregator;
  22. #region 属性
  23. public Management management { get; set; }
  24. public RobotService robotService { get; set; }
  25. private IRobot _Robot;
  26. public IRobot Robot
  27. {
  28. get { return _Robot; }
  29. set
  30. {
  31. SetProperty(ref _Robot, value);
  32. try
  33. {
  34. var config = FileHelper.ReadApplicationConfiguration();
  35. if (config.RobotJogSetpDistance.TryGetValue(value.Id, out double distance))
  36. {
  37. this.Distance = distance;
  38. }
  39. }
  40. catch (Exception)
  41. {
  42. }
  43. }
  44. }
  45. private double _Distance = 1;
  46. public double Distance
  47. {
  48. get { return _Distance; }
  49. set
  50. {
  51. SetProperty(ref _Distance, value);
  52. try
  53. {
  54. if (Robot == null)
  55. {
  56. return;
  57. }
  58. var config = FileHelper.ReadApplicationConfiguration();
  59. if (config.RobotJogSetpDistance.ContainsKey(Robot.Id))
  60. {
  61. config.RobotJogSetpDistance[Robot.Id] = value;
  62. }
  63. else
  64. {
  65. config.RobotJogSetpDistance.Add(Robot.Id, value);
  66. }
  67. FileHelper.SaveApplicationConfiguration(config);
  68. }
  69. catch (Exception)
  70. {
  71. }
  72. }
  73. }
  74. private bool CanExecute
  75. {
  76. get
  77. {
  78. if (Robot==null)
  79. return false;
  80. else return Robot.CanExecute;
  81. }
  82. }
  83. #endregion
  84. #region 命令
  85. private DelegateCommand _LoadedCommand;
  86. public DelegateCommand LoadedCommand =>
  87. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  88. private DelegateCommand _ConnectCommand;
  89. public DelegateCommand ConnectCommand =>
  90. _ConnectCommand ?? (_ConnectCommand = new DelegateCommand(ExecuteConnectCommand));
  91. private DelegateCommand _DisConnectCommand;
  92. public DelegateCommand DisConnectCommand =>
  93. _DisConnectCommand ?? (_DisConnectCommand = new DelegateCommand(ExecuteDisConnectCommand));
  94. private DelegateCommand<string> _MotorCommand;
  95. public DelegateCommand<string> MotorCommand =>
  96. _MotorCommand ?? (_MotorCommand = new DelegateCommand<string>(ExecuteMotorCommand).ObservesCanExecute(()=> CanExecute).ObservesProperty(()=> Robot).ObservesProperty(()=> Robot.CanExecute));
  97. private DelegateCommand _ResetCommand;
  98. public DelegateCommand ResetCommand =>
  99. _ResetCommand ?? (_ResetCommand = new DelegateCommand(ExecuteResetCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  100. private DelegateCommand<object> _JogCommand;
  101. public DelegateCommand<object> JogCommand =>
  102. _JogCommand ?? (_JogCommand = new DelegateCommand<object>(ExecuteJogCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  103. private DelegateCommand _sFreeCommand;
  104. public DelegateCommand sFreeCommand =>
  105. _sFreeCommand ?? (_sFreeCommand = new DelegateCommand(ExecutesFreeCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  106. private DelegateCommand _SLockCommand;
  107. public DelegateCommand SLockCommand =>
  108. _SLockCommand ?? (_SLockCommand = new DelegateCommand(ExecuteSLockCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  109. #endregion
  110. #region 事件
  111. #endregion
  112. public RobotManualStepViewModel(IContainerProvider container, IEventAggregator ea)
  113. {
  114. _container = container;
  115. _eventAggregator = ea;
  116. _container.Resolve<DebugViewModel>().PropertyChanged += RobotManualStepViewModel_PropertyChanged; ;
  117. }
  118. #region 方法
  119. void ExecuteLoadedCommand()
  120. {
  121. try
  122. {
  123. if (_container.Resolve<Management>().CurrentUser.userPart == Enums.UserPart.Operator)
  124. {
  125. IsAllowEdit = false;
  126. }
  127. else
  128. {
  129. IsAllowEdit = true;
  130. }
  131. management = _container.Resolve<Management>();
  132. robotService = management.RobotService;
  133. var srobot = _container.Resolve<DebugViewModel>().SelectRobot;
  134. if (srobot != null)
  135. {
  136. Robot = robotService.GetRobot(srobot.Id);
  137. }
  138. }
  139. catch (Exception)
  140. {
  141. }
  142. }
  143. /// <summary>
  144. /// 连接机器人
  145. /// </summary>
  146. private async void ExecuteConnectCommand()
  147. {
  148. if (Robot == null || !Robot.IsConnected) return;
  149. try
  150. {
  151. await Robot.ConnectAsync();
  152. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.连接成功}!", Duration = 1 });
  153. }
  154. catch (Exception ex)
  155. {
  156. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  157. }
  158. }
  159. /// <summary>
  160. /// 断开机器人
  161. /// </summary>
  162. private void ExecuteDisConnectCommand()
  163. {
  164. if (Robot == null || !Robot.IsConnected) return;
  165. try
  166. {
  167. Robot.Disconnect();
  168. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.已断开}!", Duration = 1 });
  169. }
  170. catch (Exception ex)
  171. {
  172. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  173. }
  174. }
  175. /// <summary>
  176. /// 电机
  177. /// </summary>
  178. /// <param name="obj"></param>
  179. private async void ExecuteMotorCommand(string obj)
  180. {
  181. if (Robot == null || !Robot.IsConnected) return;
  182. try
  183. {
  184. await Robot.MotorAsync(Convert.ToBoolean(obj));
  185. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.成功}!", Duration = 0.4 });
  186. }
  187. catch (Exception ex)
  188. {
  189. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  190. }
  191. }
  192. /// <summary>
  193. /// 重置
  194. /// </summary>
  195. private async void ExecuteResetCommand()
  196. {
  197. if (Robot == null || !Robot.IsConnected) return;
  198. try
  199. {
  200. await Robot.ResetAsync();
  201. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.成功}!", Duration = 0.4 });
  202. }
  203. catch (Exception ex)
  204. {
  205. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  206. }
  207. }
  208. /// <summary>
  209. /// 点动
  210. /// </summary>
  211. /// <param name="obj"></param>
  212. private async void ExecuteJogCommand(object obj)
  213. {
  214. if (Robot == null || !Robot.IsConnected) return;
  215. try
  216. {
  217. Robot.Timeout = 6000000;
  218. var items = ((string)obj).Split(':');
  219. switch (items[0])
  220. {
  221. case "X+":
  222. await Robot.JogAsync("X", double.Parse(items[1]));
  223. break;
  224. case "X-":
  225. await Robot.JogAsync("X", -double.Parse(items[1]));
  226. break;
  227. case "Y+":
  228. await Robot.JogAsync("Y", double.Parse(items[1]));
  229. break;
  230. case "Y-":
  231. await Robot.JogAsync("Y", -double.Parse(items[1]));
  232. break;
  233. case "Z+":
  234. await Robot.JogAsync("Z", double.Parse(items[1]));
  235. break;
  236. case "Z-":
  237. await Robot.JogAsync("Z", -double.Parse(items[1]));
  238. break;
  239. case "U+":
  240. await Robot.JogAsync("U", double.Parse(items[1]));
  241. break;
  242. case "U-":
  243. await Robot.JogAsync("U", -double.Parse(items[1]));
  244. break;
  245. case "V+":
  246. await Robot.JogAsync("V", double.Parse(items[1]));
  247. break;
  248. case "V-":
  249. await Robot.JogAsync("V", -double.Parse(items[1]));
  250. break;
  251. case "W+":
  252. await Robot.JogAsync("W", double.Parse(items[1]));
  253. break;
  254. case "W-":
  255. await Robot.JogAsync("W", -double.Parse(items[1]));
  256. break;
  257. default:
  258. break;
  259. }
  260. Robot.Timeout = 5000;
  261. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.成功}!", Duration = 0.4 });
  262. }
  263. catch (Exception ex)
  264. {
  265. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  266. }
  267. }
  268. /// <summary>
  269. /// 锁定刹车
  270. /// </summary>
  271. private async void ExecuteSLockCommand()
  272. {
  273. if (Robot == null || !Robot.IsConnected) return;
  274. try
  275. {
  276. await Robot.SLockAsync();
  277. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.成功}!", Duration = 0.4 });
  278. }
  279. catch (Exception ex)
  280. {
  281. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  282. }
  283. }
  284. /// <summary>
  285. /// 释放刹车
  286. /// </summary>
  287. private async void ExecutesFreeCommand()
  288. {
  289. if (Robot == null || !Robot.IsConnected) return;
  290. try
  291. {
  292. await Robot.SFreeAsync();
  293. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.成功}!", Duration = 0.4 });
  294. }
  295. catch (Exception ex)
  296. {
  297. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  298. }
  299. }
  300. private void RobotManualStepViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  301. {
  302. if (e.PropertyName == "SelectRobot")
  303. {
  304. management = _container.Resolve<Management>();
  305. robotService = management.RobotService;
  306. var srobot = _container.Resolve<DebugViewModel>().SelectRobot;
  307. if(srobot != null)
  308. {
  309. Robot = robotService.GetRobot(srobot.Id);
  310. }
  311. }
  312. }
  313. #endregion
  314. private bool _IsAllowEdit = false;
  315. public bool IsAllowEdit
  316. {
  317. get { return _IsAllowEdit; }
  318. set { SetProperty(ref _IsAllowEdit, value); }
  319. }
  320. private void LoginChange(Models.User user)
  321. {
  322. if (user.userPart == Enums.UserPart.Operator)
  323. {
  324. IsAllowEdit = false;
  325. }
  326. else
  327. {
  328. IsAllowEdit = true;
  329. }
  330. }
  331. }
  332. }