PlcRobotManualStepViewModel.cs 16 KB

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