TorqueCheckViewModel.cs 13 KB

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