PlcRobotManualStepViewModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. using MathNet.Numerics;
  2. using Prism.Commands;
  3. using Prism.Events;
  4. using Prism.Ioc;
  5. using Prism.Mvvm;
  6. using Prism.Regions;
  7. using Prism.Unity;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Collections.ObjectModel;
  11. using System.Linq;
  12. using System.Windows;
  13. using TeamAAS_VP.Core.Robots;
  14. using TeamAAS_VP.Events;
  15. using TeamAAS_VP.Interfaces;
  16. using TeamAAS_VP.Resources.Languages;
  17. namespace TeamAAS_VP.ViewModels.DebugMod
  18. {
  19. public class PlcRobotManualStepViewModel : BindableBase, IConfirmNavigationRequest
  20. {
  21. IContainerProvider _container;
  22. IEventAggregator _eventAggregator;
  23. IRobotService _robotService;
  24. ISystemDatabaseService _systemDatabaseService;
  25. IConfigService _configService;
  26. public PlcRobotManualStepViewModel(IContainerProvider container, IEventAggregator ea, IRobotService robotService, ISystemDatabaseService systemDatabaseService, IConfigService configService)
  27. {
  28. _container = container;
  29. _eventAggregator = ea;
  30. _robotService = robotService;
  31. _systemDatabaseService = systemDatabaseService;
  32. _configService = configService;
  33. //订阅权限登录事件
  34. _eventAggregator.GetEvent<UserLoginNotification>().Subscribe(LoginChange);
  35. }
  36. #region 属性
  37. private ObservableCollection<AxisExtended> _AdditionalAxisList;
  38. public ObservableCollection<AxisExtended> AdditionalAxisList
  39. {
  40. get { return _AdditionalAxisList; }
  41. set { SetProperty(ref _AdditionalAxisList, value); }
  42. }
  43. private XYZU_Robot _Robot;
  44. public XYZU_Robot Robot
  45. {
  46. get { return _Robot; }
  47. set
  48. {
  49. SetProperty(ref _Robot, value);
  50. try
  51. {
  52. if (value != null)
  53. {
  54. this.Distance = _configService.GetRobotStepDistance(value.Id);
  55. }
  56. }
  57. catch (Exception)
  58. {
  59. }
  60. }
  61. }
  62. private double _Distance = 1;
  63. public double Distance
  64. {
  65. get { return _Distance; }
  66. set
  67. {
  68. SetProperty(ref _Distance, value);
  69. try
  70. {
  71. if (Robot == null)
  72. {
  73. return;
  74. }
  75. if (_configService == null)
  76. _configService = ((PrismApplication)(App.Current)).Container.Resolve<IConfigService>();
  77. _configService.UpdateRobotStepDistance(Robot.Id, value);
  78. }
  79. catch (Exception)
  80. {
  81. }
  82. }
  83. }
  84. #endregion
  85. #region 命令
  86. private DelegateCommand<string> _MotorCommand;
  87. public DelegateCommand<string> MotorCommand =>
  88. _MotorCommand ?? (_MotorCommand = new DelegateCommand<string>(ExecuteMotorCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  89. private DelegateCommand _ResetCommand;
  90. public DelegateCommand ResetCommand =>
  91. _ResetCommand ?? (_ResetCommand = new DelegateCommand(ExecuteResetCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  92. private DelegateCommand<object> _JogCommand;
  93. public DelegateCommand<object> JogCommand =>
  94. _JogCommand ?? (_JogCommand = new DelegateCommand<object>(ExecuteJogCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  95. //附加轴,轴点动+命令
  96. private DelegateCommand<object> _AdditionalAxisJogPlusCommand;
  97. public DelegateCommand<object> AdditionalAxisJogPlusCommand =>
  98. _AdditionalAxisJogPlusCommand ?? (_AdditionalAxisJogPlusCommand = new DelegateCommand<object>(ExecuteAdditionalAxisJogPlusCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  99. //附加轴,轴点动-命令
  100. private DelegateCommand<object> _AdditionalAxisJogMinusCommand;
  101. public DelegateCommand<object> AdditionalAxisJogMinusCommand =>
  102. _AdditionalAxisJogMinusCommand ?? (_AdditionalAxisJogMinusCommand = new DelegateCommand<object>(ExecuteAdditionalAxisJogMinusCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  103. private DelegateCommand<object> _AdditionalAxisMotorOnCommand;
  104. public DelegateCommand<object> AdditionalAxisMotorOnCommand =>
  105. _AdditionalAxisMotorOnCommand ?? (_AdditionalAxisMotorOnCommand = new DelegateCommand<object>(ExecuteAdditionalAxisMotorOnCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  106. private DelegateCommand<object> _AdditionalAxisMotorOffCommand;
  107. public DelegateCommand<object> AdditionalAxisMotorOffCommand =>
  108. _AdditionalAxisMotorOffCommand ?? (_AdditionalAxisMotorOffCommand = new DelegateCommand<object>(ExecuteAdditionalAxisMotorOffCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  109. #endregion
  110. #region 事件
  111. #endregion
  112. #region 方法
  113. private void ShowMsg(string msg)
  114. {
  115. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.成功}!", Duration = 0.4 });
  116. }
  117. /// <summary>
  118. /// 电机
  119. /// </summary>
  120. /// <param name="obj"></param>
  121. private async void ExecuteMotorCommand(string obj)
  122. {
  123. if (Robot == null || !Robot.IsConnected) return;
  124. try
  125. {
  126. await Robot.MotorAsync(Convert.ToBoolean(obj));
  127. ShowMsg($"{Lang.完成}!");
  128. }
  129. catch (Exception ex)
  130. {
  131. LogHelper.WriteLogError("机器人点击操作时出错!", ex);
  132. ShowMsg(ex.Message);
  133. }
  134. }
  135. /// <summary>
  136. /// 重置
  137. /// </summary>
  138. private async void ExecuteResetCommand()
  139. {
  140. if (Robot == null || !Robot.IsConnected) return;
  141. try
  142. {
  143. await Robot.ResetAsync();
  144. ShowMsg($"{Lang.完成}!");
  145. }
  146. catch (Exception ex)
  147. {
  148. LogHelper.WriteLogError("重置机器人时出错!", ex);
  149. ShowMsg(ex.Message);
  150. }
  151. }
  152. /// <summary>
  153. /// 点动
  154. /// </summary>
  155. /// <param name="obj"></param>
  156. private async void ExecuteJogCommand(object obj)
  157. {
  158. if (Robot == null || !Robot.IsConnected) return;
  159. try
  160. {
  161. var items = ((string)obj).Split(':');
  162. //如果移动距离大于10mm,则提示用户确认
  163. double distance = double.Parse(items[1]);
  164. if (Math.Abs(distance) >= 10)
  165. {
  166. if (MessageBox.Show(string.Format(Lang.确认要点动距离0mm吗,distance), Lang.确认点动, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  167. {
  168. return;
  169. }
  170. }
  171. Robot.Timeout = 6000000;
  172. switch (items[0])
  173. {
  174. case "X+":
  175. await Robot.JogAsync("X", distance);
  176. break;
  177. case "X-":
  178. await Robot.JogAsync("X", -distance);
  179. break;
  180. case "Y+":
  181. await Robot.JogAsync("Y", distance);
  182. break;
  183. case "Y-":
  184. await Robot.JogAsync("Y", -distance);
  185. break;
  186. case "Z+":
  187. await Robot.JogAsync("Z", distance);
  188. break;
  189. case "Z-":
  190. await Robot.JogAsync("Z", -distance);
  191. break;
  192. case "U+":
  193. await Robot.JogAsync("U", distance);
  194. break;
  195. case "U-":
  196. await Robot.JogAsync("U", -distance);
  197. break;
  198. case "V+":
  199. await Robot.JogAsync("V", distance);
  200. break;
  201. case "V-":
  202. await Robot.JogAsync("V", -distance);
  203. break;
  204. case "W+":
  205. await Robot.JogAsync("W", distance);
  206. break;
  207. case "W-":
  208. await Robot.JogAsync("W", -distance);
  209. break;
  210. default:
  211. break;
  212. }
  213. Robot.Timeout = 5000;
  214. ShowMsg($"{Lang.完成}!");
  215. }
  216. catch (Exception ex)
  217. {
  218. LogHelper.WriteLogError("机器点动时出错!", ex);
  219. ShowMsg(ex.Message);
  220. }
  221. }
  222. private bool CanExecute
  223. {
  224. get
  225. {
  226. if (Robot != null)
  227. {
  228. return Robot.CanExecute;
  229. }
  230. else
  231. {
  232. return false;
  233. }
  234. }
  235. }
  236. /// <summary>
  237. /// 附加轴+点动
  238. /// </summary>
  239. /// <param name="obj"></param>
  240. async void ExecuteAdditionalAxisJogPlusCommand(object obj)
  241. {
  242. if (Robot == null || !Robot.IsConnected) return;
  243. try
  244. {
  245. if (obj is AxisExtended axis)
  246. {
  247. //如果移动距离大于10mm,则提示用户确认
  248. if (Math.Abs(Distance) >= 10)
  249. {
  250. if (MessageBox.Show(string.Format(Lang.确认要点动距离0mm吗, Distance), Lang.确认点动, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  251. {
  252. return;
  253. }
  254. }
  255. await Robot.AdditionalAxisJogAsync(axis.Name, Distance);
  256. ShowMsg($"{Lang.完成}!");
  257. }
  258. }
  259. catch (Exception ex)
  260. {
  261. LogHelper.WriteLogError("附加轴点动+时出错!", ex);
  262. ShowMsg(ex.Message);
  263. }
  264. }
  265. /// <summary>
  266. /// 附加轴-点动
  267. /// </summary>
  268. /// <param name="obj"></param>
  269. async void ExecuteAdditionalAxisJogMinusCommand(object obj)
  270. {
  271. if (Robot == null || !Robot.IsConnected) return;
  272. try
  273. {
  274. if (obj is AxisExtended axis)
  275. {
  276. //如果移动距离大于10mm,则提示用户确认
  277. if (Math.Abs(Distance) >= 10)
  278. {
  279. if (MessageBox.Show(string.Format(Lang.确认要点动距离0mm吗, Distance), Lang.确认点动, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  280. {
  281. return;
  282. }
  283. }
  284. await Robot.AdditionalAxisJogNegAsync(axis.Name, Distance);
  285. ShowMsg($"{Lang.完成}!");
  286. }
  287. }
  288. catch (Exception ex)
  289. {
  290. LogHelper.WriteLogError("附加轴点动-时出错!", ex);
  291. ShowMsg(ex.Message);
  292. }
  293. }
  294. /// <summary>
  295. /// 附加轴下使能
  296. /// </summary>
  297. /// <param name="obj"></param>
  298. async void ExecuteAdditionalAxisMotorOffCommand(object obj)
  299. {
  300. if (Robot == null || !Robot.IsConnected) return;
  301. try
  302. {
  303. if (obj is AxisExtended axis)
  304. {
  305. await Robot.AdditionalAxisMotorAsync(axis.Name, false);
  306. ShowMsg($"{Lang.完成}!");
  307. }
  308. }
  309. catch (Exception ex)
  310. {
  311. LogHelper.WriteLogError("附加轴上使能时出错!", ex);
  312. ShowMsg(ex.Message);
  313. }
  314. }
  315. /// <summary>
  316. /// 附加轴上使能
  317. /// </summary>
  318. /// <param name="obj"></param>
  319. async void ExecuteAdditionalAxisMotorOnCommand(object obj)
  320. {
  321. if (Robot == null || !Robot.IsConnected) return;
  322. try
  323. {
  324. if (obj is AxisExtended axis)
  325. {
  326. await Robot.AdditionalAxisMotorAsync(axis.Name, true);
  327. ShowMsg($"{Lang.完成}!");
  328. }
  329. }
  330. catch (Exception ex)
  331. {
  332. LogHelper.WriteLogError("附加轴下使能时出错!", ex);
  333. ShowMsg(ex.Message);
  334. }
  335. }
  336. #endregion
  337. #region 继承
  338. /// <summary>
  339. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  340. /// </summary>
  341. /// <param name="navigationContext"></param>
  342. /// <param name="continuationCallback"></param>
  343. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  344. {
  345. continuationCallback(true);
  346. }
  347. /// <summary>
  348. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  349. /// </summary>
  350. /// <param name="navigationContext"></param>
  351. /// <exception cref="NotImplementedException"></exception>
  352. public async void OnNavigatedTo(NavigationContext navigationContext)
  353. {
  354. var robotInfo = navigationContext.Parameters["RobotInfo"] as Models.RobotInfo;
  355. if (robotInfo != null)
  356. {
  357. Robot = _robotService.GetRobot(robotInfo.Id) as XYZU_Robot;
  358. AdditionalAxisList = Robot.AdditionalAxisList;
  359. Robot.EnterDebugMode = true;
  360. }
  361. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  362. {
  363. IsAllowEdit = false;
  364. }
  365. else
  366. {
  367. IsAllowEdit = true;
  368. }
  369. }
  370. /// <summary>
  371. /// 是否允许导航到此视图模型。
  372. /// </summary>
  373. /// <param name="navigationContext"></param>
  374. /// <returns></returns>
  375. public bool IsNavigationTarget(NavigationContext navigationContext)
  376. {
  377. return true;
  378. }
  379. /// <summary>
  380. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  381. /// </summary>
  382. /// <param name="navigationContext"></param>
  383. public void OnNavigatedFrom(NavigationContext navigationContext)
  384. {
  385. if (Robot != null)
  386. Robot.EnterDebugMode = false;
  387. }
  388. #endregion
  389. private bool _IsAllowEdit = false;
  390. public bool IsAllowEdit
  391. {
  392. get { return _IsAllowEdit; }
  393. set { SetProperty(ref _IsAllowEdit, value); }
  394. }
  395. private void LoginChange(Models.User user)
  396. {
  397. if (user.userPart == Enums.UserPart.Operator)
  398. {
  399. IsAllowEdit = false;
  400. }
  401. else
  402. {
  403. IsAllowEdit = true;
  404. }
  405. }
  406. }
  407. }