RobotManualStepViewModel.cs 14 KB

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