RobotManualStepViewModel.cs 14 KB

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