RobotManualStepViewModel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. #region 方法
  117. void ExecuteLoadedCommand()
  118. {
  119. }
  120. /// <summary>
  121. /// 连接机器人
  122. /// </summary>
  123. private async void ExecuteConnectCommand()
  124. {
  125. if (Robot == null || !Robot.IsConnected) return;
  126. try
  127. {
  128. await Robot.ConnectAsync();
  129. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.连接成功}!", Duration = 1 });
  130. }
  131. catch (Exception ex)
  132. {
  133. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  134. }
  135. }
  136. /// <summary>
  137. /// 断开机器人
  138. /// </summary>
  139. private void ExecuteDisConnectCommand()
  140. {
  141. if (Robot == null || !Robot.IsConnected) return;
  142. try
  143. {
  144. Robot.Disconnect();
  145. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.已断开}!", Duration = 1 });
  146. }
  147. catch (Exception ex)
  148. {
  149. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  150. }
  151. }
  152. /// <summary>
  153. /// 电机
  154. /// </summary>
  155. /// <param name="obj"></param>
  156. private async void ExecuteMotorCommand(string obj)
  157. {
  158. if (Robot == null || !Robot.IsConnected) return;
  159. try
  160. {
  161. await Robot.MotorAsync(Convert.ToBoolean(obj));
  162. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.成功}!", Duration = 0.4 });
  163. }
  164. catch (Exception ex)
  165. {
  166. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  167. }
  168. }
  169. /// <summary>
  170. /// 重置
  171. /// </summary>
  172. private async void ExecuteResetCommand()
  173. {
  174. if (Robot == null || !Robot.IsConnected) return;
  175. try
  176. {
  177. await Robot.ResetAsync();
  178. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.成功}!", Duration = 0.4 });
  179. }
  180. catch (Exception ex)
  181. {
  182. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  183. }
  184. }
  185. /// <summary>
  186. /// 点动
  187. /// </summary>
  188. /// <param name="obj"></param>
  189. private async void ExecuteJogCommand(object obj)
  190. {
  191. if (Robot == null || !Robot.IsConnected) return;
  192. try
  193. {
  194. Robot.Timeout = 6000000;
  195. var items = ((string)obj).Split(':');
  196. switch (items[0])
  197. {
  198. case "X+":
  199. await Robot.JogAsync("X", double.Parse(items[1]));
  200. break;
  201. case "X-":
  202. await Robot.JogAsync("X", -double.Parse(items[1]));
  203. break;
  204. case "Y+":
  205. await Robot.JogAsync("Y", double.Parse(items[1]));
  206. break;
  207. case "Y-":
  208. await Robot.JogAsync("Y", -double.Parse(items[1]));
  209. break;
  210. case "Z+":
  211. await Robot.JogAsync("Z", double.Parse(items[1]));
  212. break;
  213. case "Z-":
  214. await Robot.JogAsync("Z", -double.Parse(items[1]));
  215. break;
  216. case "U+":
  217. await Robot.JogAsync("U", double.Parse(items[1]));
  218. break;
  219. case "U-":
  220. await Robot.JogAsync("U", -double.Parse(items[1]));
  221. break;
  222. case "V+":
  223. await Robot.JogAsync("V", double.Parse(items[1]));
  224. break;
  225. case "V-":
  226. await Robot.JogAsync("V", -double.Parse(items[1]));
  227. break;
  228. case "W+":
  229. await Robot.JogAsync("W", double.Parse(items[1]));
  230. break;
  231. case "W-":
  232. await Robot.JogAsync("W", -double.Parse(items[1]));
  233. break;
  234. default:
  235. break;
  236. }
  237. Robot.Timeout = 5000;
  238. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.成功}!", Duration = 0.4 });
  239. }
  240. catch (Exception ex)
  241. {
  242. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  243. }
  244. }
  245. /// <summary>
  246. /// 锁定刹车
  247. /// </summary>
  248. private async void ExecuteSLockCommand()
  249. {
  250. if (Robot == null || !Robot.IsConnected) return;
  251. try
  252. {
  253. await Robot.SLockAsync();
  254. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.成功}!", Duration = 0.4 });
  255. }
  256. catch (Exception ex)
  257. {
  258. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  259. }
  260. }
  261. /// <summary>
  262. /// 释放刹车
  263. /// </summary>
  264. private async void ExecutesFreeCommand()
  265. {
  266. if (Robot == null || !Robot.IsConnected) return;
  267. try
  268. {
  269. await Robot.SFreeAsync();
  270. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.成功}!", Duration = 0.4 });
  271. }
  272. catch (Exception ex)
  273. {
  274. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  275. }
  276. }
  277. #endregion
  278. #region 继承
  279. /// <summary>
  280. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  281. /// </summary>
  282. /// <param name="navigationContext"></param>
  283. /// <param name="continuationCallback"></param>
  284. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  285. {
  286. continuationCallback(true);
  287. }
  288. /// <summary>
  289. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  290. /// </summary>
  291. /// <param name="navigationContext"></param>
  292. /// <exception cref="NotImplementedException"></exception>
  293. public async void OnNavigatedTo(NavigationContext navigationContext)
  294. {
  295. var robotInfo = navigationContext.Parameters["RobotInfo"] as Models.RobotInfo;
  296. if (robotInfo != null)
  297. {
  298. Robot = _robotService.GetRobot(robotInfo.Id);
  299. Robot.EnterDebugMode= true;
  300. }
  301. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  302. {
  303. IsAllowEdit = false;
  304. }
  305. else
  306. {
  307. IsAllowEdit = true;
  308. }
  309. }
  310. /// <summary>
  311. /// 是否允许导航到此视图模型。
  312. /// </summary>
  313. /// <param name="navigationContext"></param>
  314. /// <returns></returns>
  315. public bool IsNavigationTarget(NavigationContext navigationContext)
  316. {
  317. return true;
  318. }
  319. /// <summary>
  320. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  321. /// </summary>
  322. /// <param name="navigationContext"></param>
  323. public void OnNavigatedFrom(NavigationContext navigationContext)
  324. {
  325. if (Robot != null)
  326. Robot.EnterDebugMode = false;
  327. }
  328. #endregion
  329. private bool _IsAllowEdit = false;
  330. public bool IsAllowEdit
  331. {
  332. get { return _IsAllowEdit; }
  333. set { SetProperty(ref _IsAllowEdit, value); }
  334. }
  335. private void LoginChange(Models.User user)
  336. {
  337. if (user.userPart == Enums.UserPart.Operator)
  338. {
  339. IsAllowEdit = false;
  340. }
  341. else
  342. {
  343. IsAllowEdit = true;
  344. }
  345. }
  346. }
  347. }