TorqueCheckViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. using NPOI.OpenXml4Net.OPC.Internal;
  2. using NPOI.SS.Formula.Functions;
  3. using Prism.Commands;
  4. using Prism.Events;
  5. using Prism.Ioc;
  6. using Prism.Mvvm;
  7. using Prism.Services.Dialogs;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using TeamAAS_VP.Core;
  17. using TeamAAS_VP.Enums;
  18. using TeamAAS_VP.Events;
  19. using TeamAAS_VP.Interfaces;
  20. using TeamAAS_VP.Models;
  21. using TeamAAS_VP.Resources.Languages;
  22. using TeamAAS_VP.Services;
  23. using FileHelper = TeamAAS_VP.Core.FileHelper;
  24. namespace TeamAAS_VP.ViewModels.Home
  25. {
  26. public class TorqueCheckViewModel : BindableBase, IDialogAware
  27. {
  28. IEventAggregator _eventAggregator;
  29. IConfigService _configService;
  30. IContainerProvider _container;
  31. IRobotService _robotService;
  32. #region 属性
  33. private Management _management;
  34. public Management management
  35. {
  36. get { return _management; }
  37. set { SetProperty(ref _management, value); }
  38. }
  39. private bool CanExecute
  40. {
  41. get
  42. {
  43. // 命令可执行仅当机器人可执行且已选择点位
  44. if (Robot != null && Robot.IsConnected && Robot.CanExecute)
  45. {
  46. return true;
  47. }
  48. return false;
  49. }
  50. }
  51. private IRobot _Robot;
  52. public IRobot Robot
  53. {
  54. get { return _Robot; }
  55. set { SetProperty(ref _Robot, value); }
  56. }
  57. private double _Distance = 1;
  58. public double Distance
  59. {
  60. get { return _Distance; }
  61. set
  62. {
  63. SetProperty(ref _Distance, value);
  64. try
  65. {
  66. if (Robot == null)
  67. {
  68. return;
  69. }
  70. _configService.UpdateRobotStepDistance(Robot.Id, value);
  71. }
  72. catch (Exception)
  73. {
  74. }
  75. }
  76. }
  77. private TorqueTest _tc = new TorqueTest();
  78. public TorqueTest Tc
  79. {
  80. get { return _tc; }
  81. set { SetProperty(ref _tc, value); }
  82. }
  83. private double _torqueValue;
  84. public double torqueValue
  85. {
  86. get { return _torqueValue; }
  87. set { SetProperty(ref _torqueValue, value); }
  88. }
  89. private string _TValue;
  90. public string TValue
  91. {
  92. get { return _TValue; }
  93. set
  94. {
  95. SetProperty(ref _TValue, value);
  96. }
  97. }
  98. private string _PValue;
  99. public string PValue
  100. {
  101. get { return _PValue; }
  102. set
  103. {
  104. SetProperty(ref _PValue, value);
  105. }
  106. }
  107. private string _SubValue;
  108. public string SubValue
  109. {
  110. get { return _SubValue; }
  111. set { SetProperty(ref _SubValue, value); }
  112. }
  113. private string _Result;
  114. public string Result
  115. {
  116. get { return _Result; }
  117. set { SetProperty(ref _Result, value); }
  118. }
  119. private string _FontColor;
  120. public string FontColor
  121. {
  122. get { return _FontColor; }
  123. set { SetProperty(ref _FontColor, value); }
  124. }
  125. #endregion
  126. #region 命令
  127. private DelegateCommand _CancelCommand;
  128. public DelegateCommand CancelCommand =>
  129. _CancelCommand ?? (_CancelCommand = new DelegateCommand(ExecuteCancelCommand));
  130. private DelegateCommand _ConfirmCommand;
  131. public DelegateCommand ConfirmCommand =>
  132. _ConfirmCommand ?? (_ConfirmCommand = new DelegateCommand(ExecuteConfirmCommand));
  133. private DelegateCommand<string> _MotorCommand;
  134. public DelegateCommand<string> MotorCommand =>
  135. _MotorCommand ?? (_MotorCommand = new DelegateCommand<string>(ExecuteMotorCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  136. private DelegateCommand<object> _JogCommand;
  137. public DelegateCommand<object> JogCommand =>
  138. _JogCommand ?? (_JogCommand = new DelegateCommand<object>(ExecuteJogCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  139. private DelegateCommand _TechPointCommand;
  140. public DelegateCommand TechPointCommand =>
  141. _TechPointCommand ?? (_TechPointCommand = new DelegateCommand(ExecuteTechPointCommand));
  142. private DelegateCommand _ModifyCommand;
  143. public DelegateCommand ModifyCommand =>
  144. _ModifyCommand ?? (_ModifyCommand = new DelegateCommand(ExecuteModifyCommand));
  145. private DelegateCommand _WorkCommand;
  146. public DelegateCommand WorkCommand =>
  147. _WorkCommand ?? (_WorkCommand = new DelegateCommand(ExecuteWorkCommand));
  148. #endregion
  149. #region 方法
  150. public TorqueCheckViewModel(IEventAggregator ea, IConfigService configService, IContainerProvider container, IRobotService robotService)
  151. {
  152. _eventAggregator = ea;
  153. _configService = configService;
  154. _container = container;
  155. _robotService = robotService;
  156. management = _container.Resolve<Management>();
  157. }
  158. void ExecuteCancelCommand()
  159. {
  160. IDialogParameters parameters = new DialogParameters();
  161. RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel, parameters));
  162. }
  163. void ExecuteConfirmCommand()
  164. {
  165. IDialogParameters parameters = new DialogParameters();
  166. RequestClose?.Invoke(new DialogResult(ButtonResult.OK, parameters));
  167. }
  168. /// <summary>
  169. /// 电机
  170. /// </summary>
  171. /// <param name="obj"></param>
  172. private async void ExecuteMotorCommand(string obj)
  173. {
  174. if (Robot == null || !Robot.IsConnected) return;
  175. try
  176. {
  177. await Robot.MotorAsync(Convert.ToBoolean(obj));
  178. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  179. }
  180. catch (Exception ex)
  181. {
  182. LogHelper.WriteLogError("机器人点击操作时出错!", ex);
  183. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  184. }
  185. }
  186. /// <summary>
  187. /// 点动
  188. /// </summary>
  189. /// <param name="obj"></param>
  190. private async void ExecuteJogCommand(object obj)
  191. {
  192. if (Robot == null || !Robot.IsConnected) return;
  193. try
  194. {
  195. var items = ((string)obj).Split(':');
  196. //如果移动距离大于10mm,则提示用户确认
  197. double distance = double.Parse(items[1]);
  198. if (Math.Abs(distance) >= 10)
  199. {
  200. if (MessageBox.Show($"确认要点动距离 {distance} mm 吗?", "确认点动", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  201. {
  202. return;
  203. }
  204. }
  205. Robot.Timeout = 6000000;
  206. switch (items[0])
  207. {
  208. case "X+":
  209. await Robot.JogAsync("X", distance);
  210. break;
  211. case "X-":
  212. await Robot.JogAsync("X", -distance);
  213. break;
  214. case "Y+":
  215. await Robot.JogAsync("Y", distance);
  216. break;
  217. case "Y-":
  218. await Robot.JogAsync("Y", -distance);
  219. break;
  220. case "Z+":
  221. await Robot.JogAsync("Z", distance);
  222. break;
  223. case "Z-":
  224. await Robot.JogAsync("Z", -distance);
  225. break;
  226. case "U+":
  227. await Robot.JogAsync("U", distance);
  228. break;
  229. case "U-":
  230. await Robot.JogAsync("U", -distance);
  231. break;
  232. case "V+":
  233. await Robot.JogAsync("V", distance);
  234. break;
  235. case "V-":
  236. await Robot.JogAsync("V", -distance);
  237. break;
  238. case "W+":
  239. await Robot.JogAsync("W", distance);
  240. break;
  241. case "W-":
  242. await Robot.JogAsync("W", -distance);
  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. LogHelper.WriteLogError("机器人点位-机器人点动时出错", ex);
  253. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  254. }
  255. }
  256. /// <summary>
  257. /// 示教点位
  258. /// </summary>
  259. async void ExecuteTechPointCommand()
  260. {
  261. //弹窗用户是否确认要示教该点位
  262. if (MessageBox.Show($"确认要示教点位吗?", "示教点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  263. {
  264. return;
  265. }
  266. var curpos = await Robot.GetRobotPosAsync();
  267. Tc.Xpoint = curpos.X;
  268. Tc.Ypoint = curpos.Y;
  269. Tc.Zpoint = curpos.Z;
  270. FileHelper.WriteJsonFile(Tc, FilePath.TorqueCheckPath);
  271. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  272. }
  273. /// <summary>
  274. /// 修改
  275. /// </summary>
  276. void ExecuteModifyCommand()
  277. {
  278. if (MessageBox.Show($"确认要修改扭力阈值吗?", "修改", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  279. {
  280. return;
  281. }
  282. Tc.TorqueValue = torqueValue;
  283. FileHelper.WriteJsonFile(Tc, FilePath.TorqueCheckPath);
  284. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 0.4 });
  285. }
  286. void ExecuteWorkCommand()
  287. {
  288. try
  289. {
  290. MoveRobot();
  291. Thread.Sleep(500);
  292. DisplayResult();
  293. }
  294. catch (Exception ex)
  295. {
  296. LogHelper.WriteLogError("执行点检时出错!", ex);
  297. }
  298. }
  299. public async void MoveRobot()
  300. {
  301. if (!File.Exists(FilePath.TorqueCheckPath))
  302. {
  303. MessageBox.Show("还未示教,请先示教");
  304. return;
  305. }
  306. var res = FileHelper.ReadJsonFile<TorqueTest>(FilePath.TorqueCheckPath);
  307. var point = new RPoint()
  308. {
  309. X = (float)res.Xpoint,
  310. Y = (float)res.Ypoint,
  311. Z = (float)res.Zpoint,
  312. };
  313. if (Robot == null || !Robot.IsConnected) return;
  314. try
  315. {
  316. //确认要执行该点位的该运动指令吗
  317. if (MessageBox.Show($"确认要进行点检吗?", "执行点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  318. {
  319. return;
  320. }
  321. await Robot.JumpAsync(point, 0);
  322. }
  323. catch (Exception ex)
  324. {
  325. LogHelper.WriteLogError("执行点检移动时出错!", ex);
  326. }
  327. }
  328. public void DisplayResult()
  329. {
  330. try
  331. {
  332. TValue = management.SignalValue;
  333. double tvalue = double.Parse(TValue.Split(' ')[0]);
  334. PValue = $"{management.Value} N.m";
  335. double pvalue = management.Value;
  336. SubValue = $"{Math.Abs(tvalue - pvalue)} N.m";
  337. double sub = Math.Abs(tvalue - pvalue);
  338. var res = FileHelper.ReadJsonFile<TorqueTest>(FilePath.TorqueCheckPath);
  339. if (res != null)
  340. {
  341. if (sub <= res.TorqueValue)
  342. {
  343. FontColor = "Green";
  344. Result = "OK";
  345. }
  346. else
  347. {
  348. FontColor = "Red";
  349. Result = "NG";
  350. }
  351. }
  352. else
  353. {
  354. if (sub <= torqueValue)
  355. {
  356. FontColor = "Green";
  357. Result = "OK";
  358. }
  359. else
  360. {
  361. FontColor = "Red";
  362. Result = "NG";
  363. }
  364. }
  365. }
  366. catch (Exception ex)
  367. {
  368. LogHelper.WriteLogError("执行点检显示时出错!", ex);
  369. }
  370. }
  371. #endregion
  372. public string Title { get; set; } = "扭力点检";
  373. public event Action<IDialogResult> RequestClose;
  374. public bool CanCloseDialog()
  375. {
  376. return true;
  377. }
  378. public void OnDialogClosed()
  379. {
  380. }
  381. public void OnDialogOpened(IDialogParameters parameters)
  382. {
  383. try
  384. {
  385. var res = FileHelper.ReadJsonFile<TorqueTest>(FilePath.TorqueCheckPath);
  386. if (res != null)
  387. {
  388. res.TorqueValue = torqueValue;
  389. }
  390. else
  391. {
  392. MessageBox.Show("还未示教,请先示教");
  393. }
  394. }
  395. catch (Exception ex)
  396. {
  397. MessageBox.Show("还未示教,请先示教");
  398. }
  399. if (Robot == null)
  400. {
  401. Robot = _robotService.GetRobotByNumber(1);
  402. }
  403. if (Robot != null && Robot.IsConnected)
  404. {
  405. this.Distance = _configService.GetRobotStepDistance(Robot.Id);
  406. Robot.EnterDebugMode = true;
  407. }
  408. else if (Robot != null)
  409. {
  410. Robot.EnterDebugMode = true;
  411. }
  412. }
  413. }
  414. }