PlcRobotManualStepViewModel.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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. //private DelegateCommand<string> _CameraAxisModerOnCommand;
  109. //public DelegateCommand<string> CameraAxisModerOnCommand =>
  110. // _CameraAxisModerOnCommand ?? (_CameraAxisModerOnCommand = new DelegateCommand<string>(ExecuteCameraAxisModerOnCommand));
  111. //private DelegateCommand<string> _CameraAxisModerOffCommand;
  112. //public DelegateCommand<string> CameraAxisModerOffCommand =>
  113. // _CameraAxisModerOffCommand ?? (_CameraAxisModerOffCommand = new DelegateCommand<string>(ExecuteCameraAxisModerOffCommand));
  114. //private DelegateCommand<string> _CameraAxisJogCommand;
  115. //public DelegateCommand<string> CameraAxisJogCommand =>
  116. // _CameraAxisJogCommand ?? (_CameraAxisJogCommand = new DelegateCommand<string>(ExecuteCameraAxisJogCommand));
  117. #endregion
  118. #region 事件
  119. #endregion
  120. #region 方法
  121. private void ShowMsg(string msg)
  122. {
  123. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.成功}!", Duration = 0.4 });
  124. }
  125. /// <summary>
  126. /// 电机
  127. /// </summary>
  128. /// <param name="obj"></param>
  129. private async void ExecuteMotorCommand(string obj)
  130. {
  131. if (Robot == null || !Robot.IsConnected) return;
  132. try
  133. {
  134. await Robot.MotorAsync(Convert.ToBoolean(obj));
  135. ShowMsg($"{Lang.完成}!");
  136. }
  137. catch (Exception ex)
  138. {
  139. LogHelper.WriteLogError("机器人点击操作时出错!", ex);
  140. ShowMsg(ex.Message);
  141. }
  142. }
  143. /// <summary>
  144. /// 重置
  145. /// </summary>
  146. private async void ExecuteResetCommand()
  147. {
  148. if (Robot == null || !Robot.IsConnected) return;
  149. try
  150. {
  151. await Robot.ResetAsync();
  152. ShowMsg($"{Lang.完成}!");
  153. }
  154. catch (Exception ex)
  155. {
  156. LogHelper.WriteLogError("重置机器人时出错!", ex);
  157. ShowMsg(ex.Message);
  158. }
  159. }
  160. /// <summary>
  161. /// 点动
  162. /// </summary>
  163. /// <param name="obj"></param>
  164. private async void ExecuteJogCommand(object obj)
  165. {
  166. if (Robot == null || !Robot.IsConnected) return;
  167. try
  168. {
  169. var items = ((string)obj).Split(':');
  170. //如果移动距离大于10mm,则提示用户确认
  171. double distance = double.Parse(items[1]);
  172. if (Math.Abs(distance) >= 10)
  173. {
  174. if (MessageBox.Show($"确认要点动距离 {distance} mm 吗?", "确认点动", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  175. {
  176. return;
  177. }
  178. }
  179. Robot.Timeout = 6000000;
  180. switch (items[0])
  181. {
  182. case "X+":
  183. await Robot.JogAsync("X", distance);
  184. break;
  185. case "X-":
  186. await Robot.JogAsync("X", -distance);
  187. break;
  188. case "Y+":
  189. await Robot.JogAsync("Y", distance);
  190. break;
  191. case "Y-":
  192. await Robot.JogAsync("Y", -distance);
  193. break;
  194. case "Z+":
  195. await Robot.JogAsync("Z", distance);
  196. break;
  197. case "Z-":
  198. await Robot.JogAsync("Z", -distance);
  199. break;
  200. case "U+":
  201. await Robot.JogAsync("U", distance);
  202. break;
  203. case "U-":
  204. await Robot.JogAsync("U", -distance);
  205. break;
  206. case "V+":
  207. await Robot.JogAsync("V", distance);
  208. break;
  209. case "V-":
  210. await Robot.JogAsync("V", -distance);
  211. break;
  212. case "W+":
  213. await Robot.JogAsync("W", distance);
  214. break;
  215. case "W-":
  216. await Robot.JogAsync("W", -distance);
  217. break;
  218. default:
  219. break;
  220. }
  221. Robot.Timeout = 5000;
  222. ShowMsg($"{Lang.完成}!");
  223. }
  224. catch (Exception ex)
  225. {
  226. LogHelper.WriteLogError("机器点动时出错!", ex);
  227. ShowMsg(ex.Message);
  228. }
  229. }
  230. private bool CanExecute
  231. {
  232. get
  233. {
  234. if (Robot != null)
  235. {
  236. return Robot.CanExecute;
  237. }
  238. else
  239. {
  240. return false;
  241. }
  242. }
  243. }
  244. /// <summary>
  245. /// 附加轴+点动
  246. /// </summary>
  247. /// <param name="obj"></param>
  248. async void ExecuteAdditionalAxisJogPlusCommand(object obj)
  249. {
  250. if (Robot == null || !Robot.IsConnected) return;
  251. try
  252. {
  253. if (obj is AxisExtended axis)
  254. {
  255. //如果移动距离大于10mm,则提示用户确认
  256. if (Math.Abs(Distance) >= 10)
  257. {
  258. if (MessageBox.Show($"确认要点动距离 {Distance} mm 吗?", "确认点动", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  259. {
  260. return;
  261. }
  262. }
  263. await Robot.AdditionalAxisJogAsync(axis.Name, Distance);
  264. ShowMsg($"{Lang.完成}!");
  265. }
  266. }
  267. catch (Exception ex)
  268. {
  269. LogHelper.WriteLogError("附加轴点动+时出错!", ex);
  270. ShowMsg(ex.Message);
  271. }
  272. }
  273. /// <summary>
  274. /// 附加轴-点动
  275. /// </summary>
  276. /// <param name="obj"></param>
  277. async void ExecuteAdditionalAxisJogMinusCommand(object obj)
  278. {
  279. if (Robot == null || !Robot.IsConnected) return;
  280. try
  281. {
  282. if (obj is AxisExtended axis)
  283. {
  284. //如果移动距离大于10mm,则提示用户确认
  285. if (Math.Abs(Distance) >= 10)
  286. {
  287. if (MessageBox.Show($"确认要点动距离 {Distance} mm 吗?", "确认点动", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  288. {
  289. return;
  290. }
  291. }
  292. await Robot.AdditionalAxisJogNegAsync(axis.Name, Distance);
  293. ShowMsg($"{Lang.完成}!");
  294. }
  295. }
  296. catch (Exception ex)
  297. {
  298. LogHelper.WriteLogError("附加轴点动-时出错!", ex);
  299. ShowMsg(ex.Message);
  300. }
  301. }
  302. /// <summary>
  303. /// 附加轴下使能
  304. /// </summary>
  305. /// <param name="obj"></param>
  306. async void ExecuteAdditionalAxisMotorOffCommand(object obj)
  307. {
  308. if (Robot == null || !Robot.IsConnected) return;
  309. try
  310. {
  311. if (obj is AxisExtended axis)
  312. {
  313. await Robot.AdditionalAxisMotorAsync(axis.Name, false);
  314. ShowMsg($"{Lang.完成}!");
  315. }
  316. }
  317. catch (Exception ex)
  318. {
  319. LogHelper.WriteLogError("附加轴上使能时出错!", ex);
  320. ShowMsg(ex.Message);
  321. }
  322. }
  323. /// <summary>
  324. /// 附加轴上使能
  325. /// </summary>
  326. /// <param name="obj"></param>
  327. async void ExecuteAdditionalAxisMotorOnCommand(object obj)
  328. {
  329. if (Robot == null || !Robot.IsConnected) return;
  330. try
  331. {
  332. if (obj is AxisExtended axis)
  333. {
  334. await Robot.AdditionalAxisMotorAsync(axis.Name, true);
  335. ShowMsg($"{Lang.完成}!");
  336. }
  337. }
  338. catch (Exception ex)
  339. {
  340. LogHelper.WriteLogError("附加轴下使能时出错!", ex);
  341. ShowMsg(ex.Message);
  342. }
  343. }
  344. #region 相机控制
  345. ///// <summary>
  346. ///// 轴电机下使能操作
  347. ///// </summary>
  348. ///// <param name="parameter"></param>
  349. //async void ExecuteCameraAxisModerOffCommand(string parameter)
  350. //{
  351. // try
  352. // {
  353. // int cameraIndex = int.Parse(parameter);
  354. // var isSuccess = await Robot.CameraMotorAsync(cameraIndex, false);
  355. // if (isSuccess)
  356. // {
  357. // ShowMsg($"{Lang.完成}!");
  358. // }
  359. // else
  360. // {
  361. // ShowMsg("相机轴下使能失败!");
  362. // }
  363. // }
  364. // catch (Exception ex)
  365. // {
  366. // LogHelper.WriteLogError("相机轴下使能时出错!", ex);
  367. // ShowMsg(ex.Message);
  368. // }
  369. //}
  370. ///// <summary>
  371. ///// 轴电机上使能操作
  372. ///// </summary>
  373. ///// <param name="parameter"></param>
  374. //async void ExecuteCameraAxisModerOnCommand(string parameter)
  375. //{
  376. // try
  377. // {
  378. // int cameraIndex = int.Parse(parameter);
  379. // var isSuccess =await Robot.CameraMotorAsync(cameraIndex, true);
  380. // if (isSuccess)
  381. // {
  382. // ShowMsg($"{Lang.完成}!");
  383. // }
  384. // else
  385. // {
  386. // ShowMsg("相机轴上使能失败!");
  387. // }
  388. // }
  389. // catch (Exception ex)
  390. // {
  391. // LogHelper.WriteLogError("相机轴上使能时出错!", ex);
  392. // ShowMsg(ex.Message);
  393. // }
  394. //}
  395. ///// <summary>
  396. ///// 相机轴点动命令
  397. ///// </summary>
  398. ///// <param name="parameter"></param>
  399. //async void ExecuteCameraAxisJogCommand(string parameter)
  400. //{
  401. // try
  402. // {
  403. // int cameraIndex = int.Parse(parameter.Split(',')[0]);
  404. // double jogDistance = Distance;
  405. // string axisName = "";
  406. // switch (parameter.Split(',')[1])
  407. // {
  408. // case "X+":
  409. // jogDistance = Distance;
  410. // axisName = "X";
  411. // break;
  412. // case "X-":
  413. // jogDistance = -Distance;
  414. // axisName = "X";
  415. // break;
  416. // case "Y+":
  417. // jogDistance = Distance;
  418. // axisName = "Y";
  419. // break;
  420. // case "Y-":
  421. // jogDistance = -Distance;
  422. // axisName = "Y";
  423. // break;
  424. // default:
  425. // break;
  426. // }
  427. // bool isSuccess = await Robot.CameraJogAsync(cameraIndex, axisName, jogDistance);
  428. // if (isSuccess)
  429. // {
  430. // ShowMsg($"{Lang.完成}!");
  431. // }
  432. // else
  433. // {
  434. // ShowMsg("相机轴点动失败!");
  435. // }
  436. // }
  437. // catch (Exception ex)
  438. // {
  439. // LogHelper.WriteLogError("相机轴点动时出错!", ex);
  440. // ShowMsg(ex.Message);
  441. // }
  442. //}
  443. #endregion
  444. #endregion
  445. #region 继承
  446. /// <summary>
  447. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  448. /// </summary>
  449. /// <param name="navigationContext"></param>
  450. /// <param name="continuationCallback"></param>
  451. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  452. {
  453. continuationCallback(true);
  454. }
  455. /// <summary>
  456. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  457. /// </summary>
  458. /// <param name="navigationContext"></param>
  459. /// <exception cref="NotImplementedException"></exception>
  460. public async void OnNavigatedTo(NavigationContext navigationContext)
  461. {
  462. var robotInfo = navigationContext.Parameters["RobotInfo"] as Models.RobotInfo;
  463. if (robotInfo != null)
  464. {
  465. Robot = _robotService.GetRobot(robotInfo.Id) as XYZU_Robot;
  466. AdditionalAxisList = Robot.AdditionalAxisList;
  467. Robot.EnterDebugMode = true;
  468. }
  469. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  470. {
  471. IsAllowEdit = false;
  472. }
  473. else
  474. {
  475. IsAllowEdit = true;
  476. }
  477. }
  478. /// <summary>
  479. /// 是否允许导航到此视图模型。
  480. /// </summary>
  481. /// <param name="navigationContext"></param>
  482. /// <returns></returns>
  483. public bool IsNavigationTarget(NavigationContext navigationContext)
  484. {
  485. return true;
  486. }
  487. /// <summary>
  488. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  489. /// </summary>
  490. /// <param name="navigationContext"></param>
  491. public void OnNavigatedFrom(NavigationContext navigationContext)
  492. {
  493. if (Robot != null)
  494. Robot.EnterDebugMode = false;
  495. }
  496. #endregion
  497. private bool _IsAllowEdit = false;
  498. public bool IsAllowEdit
  499. {
  500. get { return _IsAllowEdit; }
  501. set { SetProperty(ref _IsAllowEdit, value); }
  502. }
  503. private void LoginChange(Models.User user)
  504. {
  505. if (user.userPart == Enums.UserPart.Operator)
  506. {
  507. IsAllowEdit = false;
  508. }
  509. else
  510. {
  511. IsAllowEdit = true;
  512. }
  513. }
  514. }
  515. }