PlcRobotManualStepViewModel.cs 20 KB

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