PlcPointParamsViewModel.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. using Prism.Commands;
  2. using Prism.Events;
  3. using Prism.Ioc;
  4. using Prism.Mvvm;
  5. using Prism.Regions;
  6. using Prism.Services.Dialogs;
  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;
  13. using TeamAAS_VP.Core.PLCs;
  14. using TeamAAS_VP.Enums;
  15. using TeamAAS_VP.Events;
  16. using TeamAAS_VP.Interfaces;
  17. using TeamAAS_VP.Models;
  18. using TeamAAS_VP.Models.PLC;
  19. using TeamAAS_VP.Models.Robot;
  20. using TeamAAS_VP.Resources.Languages;
  21. namespace TeamAAS_VP.ViewModels.Product
  22. {
  23. public class PlcPointParamsViewModel : BindableBase, IConfirmNavigationRequest
  24. {
  25. IRegionManager _regionManager;
  26. IEventAggregator _eventAggregator;
  27. IContainerProvider _container;
  28. IDialogService _dialogService;
  29. IConfigService _configService;
  30. IRobotService _robotService;
  31. ISystemDatabaseService _systemDatabaseService;
  32. #region 属性
  33. private ProductModel _SelectProduct;
  34. /// <summary>
  35. /// 选中的产品
  36. /// </summary>
  37. public ProductModel SelectProduct
  38. {
  39. get { return _SelectProduct; }
  40. set { SetProperty(ref _SelectProduct, value); }
  41. }
  42. private PlcPoint _SelectedPoint;
  43. public PlcPoint SelectedPoint
  44. {
  45. get { return _SelectedPoint; }
  46. set { SetProperty(ref _SelectedPoint, value); }
  47. }
  48. private PlcPoint _selectedPointInDataGrid;
  49. public PlcPoint SelectedPointInDataGrid
  50. {
  51. get { return _selectedPointInDataGrid; }
  52. set
  53. {
  54. if (SetProperty(ref _selectedPointInDataGrid, value))
  55. {
  56. SelectedPointInComboBox = value;
  57. }
  58. // 可以同步到 SelectPoint 属性
  59. SelectedPoint = value;
  60. }
  61. }
  62. private PlcPoint _selectedPointInComboBox;
  63. public PlcPoint SelectedPointInComboBox
  64. {
  65. get { return _selectedPointInComboBox; }
  66. set
  67. {
  68. if (SetProperty(ref _selectedPointInComboBox, value))
  69. {
  70. SelectedPointInDataGrid = value;
  71. }
  72. // 可以同步到 SelectPoint 属性
  73. SelectedPoint = value;
  74. }
  75. }
  76. private ObservableCollection<PlcPoint> _Points = new ObservableCollection<PlcPoint>();
  77. /// <summary>
  78. /// 点位集合
  79. /// </summary>
  80. public ObservableCollection<PlcPoint> Points
  81. {
  82. get { return _Points; }
  83. set { SetProperty(ref _Points, value); }
  84. }
  85. private MotionCommand _SelectMotionCommand;
  86. /// <summary>
  87. /// 选中的运动指令
  88. /// </summary>
  89. public MotionCommand SelectMotionCommand
  90. {
  91. get { return _SelectMotionCommand; }
  92. set { SetProperty(ref _SelectMotionCommand, value); }
  93. }
  94. public Management management { get; set; }
  95. private IRobot _Robot;
  96. public IRobot Robot
  97. {
  98. get { return _Robot; }
  99. set
  100. {
  101. SetProperty(ref _Robot, value);
  102. }
  103. }
  104. private int _SelectedIndex;
  105. public int SelectedIndex
  106. {
  107. get { return _SelectedIndex; }
  108. set { SetProperty(ref _SelectedIndex, value); }
  109. }
  110. private double _Distance = 1;
  111. public double Distance
  112. {
  113. get { return _Distance; }
  114. set
  115. {
  116. SetProperty(ref _Distance, value);
  117. try
  118. {
  119. if (Robot == null)
  120. {
  121. return;
  122. }
  123. _configService.UpdateRobotStepDistance(Robot.Id, value);
  124. }
  125. catch (Exception)
  126. {
  127. }
  128. }
  129. }
  130. #endregion
  131. #region 命令
  132. private DelegateCommand _NextCommand;
  133. public DelegateCommand NextCommand =>
  134. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand));
  135. private DelegateCommand _BackCommand;
  136. public DelegateCommand BackCommand =>
  137. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
  138. private DelegateCommand _LoadedCommand;
  139. public DelegateCommand LoadedCommand =>
  140. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  141. private DelegateCommand<object> _ExecuteMotion;
  142. public DelegateCommand<object> ExecuteMotion =>
  143. _ExecuteMotion ?? (_ExecuteMotion = new DelegateCommand<object>(ExecuteExecuteMotion).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  144. private DelegateCommand<object> _JogCommand;
  145. public DelegateCommand<object> JogCommand =>
  146. _JogCommand ?? (_JogCommand = new DelegateCommand<object>(ExecuteJogCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  147. private DelegateCommand _TechPointCommand;
  148. public DelegateCommand TechPointCommand =>
  149. _TechPointCommand ?? (_TechPointCommand = new DelegateCommand(ExecuteTechPointCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  150. private DelegateCommand<string> _MotorCommand;
  151. public DelegateCommand<string> MotorCommand =>
  152. _MotorCommand ?? (_MotorCommand = new DelegateCommand<string>(ExecuteMotorCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  153. private DelegateCommand _ResetCommand;
  154. public DelegateCommand ResetCommand =>
  155. _ResetCommand ?? (_ResetCommand = new DelegateCommand(ExecuteResetCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  156. private DelegateCommand _sFreeCommand;
  157. public DelegateCommand sFreeCommand =>
  158. _sFreeCommand ?? (_sFreeCommand = new DelegateCommand(ExecutesFreeCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  159. private DelegateCommand _SLockCommand;
  160. public DelegateCommand SLockCommand =>
  161. _SLockCommand ?? (_SLockCommand = new DelegateCommand(ExecuteSLockCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  162. private DelegateCommand _SelectionChangedCommand;
  163. public DelegateCommand SelectionChangedCommand =>
  164. _SelectionChangedCommand ?? (_SelectionChangedCommand = new DelegateCommand(ExecuteSelectionChangedCommand));
  165. private DelegateCommand _AddPointCommand;
  166. public DelegateCommand AddPointCommand =>
  167. _AddPointCommand ?? (_AddPointCommand = new DelegateCommand(ExecuteAddPointCommand));
  168. private DelegateCommand _DeletePointCommand;
  169. public DelegateCommand DeletePointCommand =>
  170. _DeletePointCommand ?? (_DeletePointCommand = new DelegateCommand(ExecuteDeletePointCommand));
  171. #endregion
  172. #region 事件
  173. #endregion
  174. public PlcPointParamsViewModel(IRegionManager regionManager, IEventAggregator ea, IContainerProvider container, IDialogService dialogService,
  175. IConfigService configService, IRobotService robotService, ISystemDatabaseService systemDatabaseService)
  176. {
  177. _regionManager = regionManager;
  178. _eventAggregator = ea;
  179. _container = container;
  180. _dialogService = dialogService;
  181. _configService = configService;
  182. _systemDatabaseService = systemDatabaseService;
  183. //订阅权限登录事件
  184. _eventAggregator.GetEvent<UserLoginNotification>().Subscribe(LoginChange);
  185. management = _container.Resolve<Management>();
  186. _robotService = robotService;
  187. }
  188. #region 方法
  189. /// <summary>
  190. /// 下一步
  191. /// </summary>
  192. void ExecuteNextCommand()
  193. {
  194. NavigationParameters param = new NavigationParameters();
  195. param.Add("SelectProduct", SelectProduct);
  196. _regionManager.RequestNavigate("ProductRegionContext", "ProductParams", param);
  197. }
  198. /// <summary>
  199. /// 上一步
  200. /// </summary>
  201. void ExecuteBackCommand()
  202. {
  203. NavigationParameters param = new NavigationParameters();
  204. param.Add("SelectProduct", SelectProduct);
  205. param.Add("SelectedProcedure", null);
  206. _regionManager.RequestNavigate("ProductRegionContext", "ProcessBasics", param);
  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. LogHelper.WriteLogError("机器人点位-机器人点动时出错", ex);
  266. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  267. }
  268. }
  269. /// <summary>
  270. /// 执行运动
  271. /// </summary>
  272. /// <param name="obj"></param>
  273. async void ExecuteExecuteMotion(object parameter)
  274. {
  275. var values = (object[])parameter;
  276. var point = values[0] as RPoint;
  277. MotionCommand motionCmd = (MotionCommand)values[1];
  278. if (Robot == null || !Robot.IsConnected) return;
  279. try
  280. {
  281. Robot.Timeout = 6000000;
  282. switch (motionCmd)
  283. {
  284. case MotionCommand.Go:
  285. await Robot.GoAsync(point);
  286. break;
  287. case MotionCommand.Jump:
  288. await Robot.JumpAsync(point, 0);
  289. break;
  290. case MotionCommand.Move:
  291. await Robot.MoveAsync(point);
  292. break;
  293. }
  294. Robot.Timeout = 5000;
  295. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  296. }
  297. catch (Exception ex)
  298. {
  299. LogHelper.WriteLogError("机器人点位-执行点位时出错", ex);
  300. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  301. }
  302. }
  303. /// <summary>
  304. /// 示教点位
  305. /// </summary>
  306. /// <param name="point"></param>
  307. async void ExecuteTechPointCommand()
  308. {
  309. if (Robot == null || !Robot.IsConnected) return;
  310. try
  311. {
  312. var curpos = await Robot.GetRobotPosAsync();
  313. SelectedPoint.X_Position = curpos.X;
  314. SelectedPoint.Y_Position = curpos.Y;
  315. SelectedPoint.Z_Position_Start = curpos.Z;
  316. SelectedPoint.U_Position = curpos.U;
  317. SelectedPoint.R_Position = curpos.V;
  318. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  319. }
  320. catch (Exception ex)
  321. {
  322. LogHelper.WriteLogError("机器人点位-示教点位时出错", ex);
  323. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  324. }
  325. }
  326. void ExecuteLoadedCommand()
  327. {
  328. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  329. {
  330. IsAllowEdit = false;
  331. }
  332. else
  333. {
  334. IsAllowEdit = true;
  335. }
  336. }
  337. /// <summary>
  338. /// 电机
  339. /// </summary>
  340. /// <param name="obj"></param>
  341. private async void ExecuteMotorCommand(string obj)
  342. {
  343. if (Robot == null || !Robot.IsConnected) return;
  344. try
  345. {
  346. await Robot.MotorAsync(Convert.ToBoolean(obj));
  347. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  348. }
  349. catch (Exception ex)
  350. {
  351. LogHelper.WriteLogError("机器人点击操作时出错!", ex);
  352. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  353. }
  354. }
  355. /// <summary>
  356. /// 重置
  357. /// </summary>
  358. private async void ExecuteResetCommand()
  359. {
  360. if (Robot == null || !Robot.IsConnected) return;
  361. try
  362. {
  363. await Robot.ResetAsync();
  364. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  365. }
  366. catch (Exception ex)
  367. {
  368. LogHelper.WriteLogError("重置机器人时出错!", ex);
  369. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  370. }
  371. }
  372. /// <summary>
  373. /// 锁定刹车
  374. /// </summary>
  375. private async void ExecuteSLockCommand()
  376. {
  377. if (Robot == null || !Robot.IsConnected) return;
  378. try
  379. {
  380. await Robot.SLockAsync();
  381. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  382. }
  383. catch (Exception ex)
  384. {
  385. LogHelper.WriteLogError("机器人锁定刹车时出错!", ex);
  386. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  387. }
  388. }
  389. /// <summary>
  390. /// 释放刹车
  391. /// </summary>
  392. private async void ExecutesFreeCommand()
  393. {
  394. if (Robot == null || !Robot.IsConnected) return;
  395. try
  396. {
  397. await Robot.SFreeAsync();
  398. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  399. }
  400. catch (Exception ex)
  401. {
  402. LogHelper.WriteLogError("机器人释放刹车时出错!", ex);
  403. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  404. }
  405. }
  406. private bool CanExecute
  407. {
  408. get
  409. {
  410. if (Robot != null)
  411. {
  412. return Robot.CanExecute;
  413. }
  414. else
  415. {
  416. return false;
  417. }
  418. }
  419. }
  420. /// <summary>
  421. /// 点位表切换时
  422. /// </summary>
  423. void ExecuteSelectionChangedCommand()
  424. {
  425. switch (SelectedIndex)
  426. {
  427. case 0:
  428. Points = SelectProduct.PickCameraPoints;
  429. break;
  430. case 1:
  431. Points = SelectProduct.PickPoints;
  432. break;
  433. case 2:
  434. Points = SelectProduct.SecPosCameraPoints;
  435. break;
  436. case 3:
  437. Points = SelectProduct.ScrewCameraPoints;
  438. break;
  439. case 4:
  440. Points = SelectProduct.ScrewPoints;
  441. break;
  442. case 5:
  443. Points = SelectProduct.CheckCameraPoints;
  444. break;
  445. default:
  446. break;
  447. }
  448. }
  449. /// <summary>
  450. /// 增加点位
  451. /// </summary>
  452. void ExecuteAddPointCommand()
  453. {
  454. //添加点位
  455. Points.Add(new PlcPoint()
  456. {
  457. Number = Points.Count + 1,
  458. });
  459. }
  460. /// <summary>
  461. /// 移除点位
  462. /// </summary>
  463. void ExecuteDeletePointCommand()
  464. {
  465. if (SelectedPoint==null)
  466. {
  467. return;
  468. }
  469. Points.Remove(SelectedPoint);
  470. //重新编号
  471. for (int i = 1; i < Points.Count; i++)
  472. {
  473. Points[i].Number = i + 1;
  474. }
  475. }
  476. #endregion
  477. #region 继承
  478. /// <summary>
  479. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  480. /// </summary>
  481. /// <param name="navigationContext"></param>
  482. /// <param name="continuationCallback"></param>
  483. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  484. {
  485. continuationCallback(true);
  486. }
  487. /// <summary>
  488. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  489. /// </summary>
  490. /// <param name="navigationContext"></param>
  491. /// <exception cref="NotImplementedException"></exception>
  492. public async void OnNavigatedTo(NavigationContext navigationContext)
  493. {
  494. try
  495. {
  496. SelectProduct = navigationContext.Parameters.GetValue<ProductModel>("SelectProduct");
  497. SelectedIndex = 0;
  498. Points = SelectProduct.PickCameraPoints;
  499. if (Robot==null)
  500. {
  501. Robot = _robotService.GetRobotByNumber(1);
  502. }
  503. if (Robot != null && Robot.IsConnected)
  504. {
  505. this.Distance = _configService.GetRobotStepDistance(Robot.Id);
  506. Robot.EnterDebugMode = true;
  507. }
  508. else if (Robot != null)
  509. {
  510. Robot.EnterDebugMode = true;
  511. }
  512. }
  513. catch (Exception ex)
  514. {
  515. LogHelper.WriteLogError("机器人点位界面进入时出错!", ex);
  516. }
  517. }
  518. /// <summary>
  519. /// 是否允许导航到此视图模型。
  520. /// </summary>
  521. /// <param name="navigationContext"></param>
  522. /// <returns></returns>
  523. public bool IsNavigationTarget(NavigationContext navigationContext)
  524. {
  525. return true;
  526. }
  527. /// <summary>
  528. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  529. /// </summary>
  530. /// <param name="navigationContext"></param>
  531. public void OnNavigatedFrom(NavigationContext navigationContext)
  532. {
  533. //if (Robot != null)
  534. //{
  535. // Robot.EnterDebugMode = false;
  536. //}
  537. }
  538. #endregion
  539. private bool _IsAllowEdit = false;
  540. public bool IsAllowEdit
  541. {
  542. get { return _IsAllowEdit; }
  543. set { SetProperty(ref _IsAllowEdit, value); }
  544. }
  545. private void LoginChange(Models.User user)
  546. {
  547. if (user.userPart == Enums.UserPart.Operator)
  548. {
  549. IsAllowEdit = false;
  550. }
  551. else
  552. {
  553. IsAllowEdit = true;
  554. }
  555. }
  556. }
  557. }