TorqueCheckViewModel.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. using MathNet.Numerics;
  2. using NPOI.OpenXml4Net.OPC.Internal;
  3. using NPOI.SS.Formula.Functions;
  4. using OxyPlot;
  5. using Prism.Commands;
  6. using Prism.Events;
  7. using Prism.Ioc;
  8. using Prism.Mvvm;
  9. using Prism.Services.Dialogs;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Collections.ObjectModel;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using TeamAAS_VP.Controls;
  20. using TeamAAS_VP.Core;
  21. using TeamAAS_VP.Core.PLCs;
  22. using TeamAAS_VP.Core.ScrewDriver;
  23. using TeamAAS_VP.Data;
  24. using TeamAAS_VP.Enums;
  25. using TeamAAS_VP.Events;
  26. using TeamAAS_VP.Interfaces;
  27. using TeamAAS_VP.Models;
  28. using TeamAAS_VP.Models.PLC;
  29. using TeamAAS_VP.Models.Torque;
  30. using TeamAAS_VP.Resources.Languages;
  31. using TeamAAS_VP.Services;
  32. using FileHelper = TeamAAS_VP.Core.FileHelper;
  33. namespace TeamAAS_VP.ViewModels.Home
  34. {
  35. public class TorqueCheckViewModel : BindableBase, IDialogAware
  36. {
  37. // 点检扭矩参数存放日志地址
  38. public static readonly string CheckTorqueConfigurationPath = "..//Log//ScrewFeedersConfiguration.cfg";
  39. IEventAggregator _eventAggregator;
  40. IConfigService _configService;
  41. IContainerProvider _container;
  42. IRobotService _robotService;
  43. IPlcService _plcService;
  44. TorqueService _torqueService;
  45. float Value { get; set; } = 0;
  46. #region 属性
  47. private Management _management;
  48. public Management management
  49. {
  50. get { return _management; }
  51. set { SetProperty(ref _management, value); }
  52. }
  53. private bool CanExecute
  54. {
  55. get
  56. {
  57. // 命令可执行仅当机器人可执行且已选择点位
  58. if (Robot != null && Robot.IsConnected && Robot.CanExecute)
  59. {
  60. return true;
  61. }
  62. return false;
  63. }
  64. }
  65. private IRobot _Robot;
  66. public IRobot Robot
  67. {
  68. get { return _Robot; }
  69. set { SetProperty(ref _Robot, value); }
  70. }
  71. private double _Distance = 1;
  72. public double Distance
  73. {
  74. get { return _Distance; }
  75. set
  76. {
  77. SetProperty(ref _Distance, value);
  78. try
  79. {
  80. if (Robot == null)
  81. {
  82. return;
  83. }
  84. _configService.UpdateRobotStepDistance(Robot.Id, value);
  85. }
  86. catch (Exception)
  87. {
  88. }
  89. }
  90. }
  91. private float _Z_Distance;
  92. public float Z_Distance
  93. {
  94. get { return _Z_Distance; }
  95. set { SetProperty(ref _Z_Distance, value); }
  96. }
  97. private float _torqueValue;
  98. public float TorqueValue
  99. {
  100. get { return _torqueValue; }
  101. set { SetProperty(ref _torqueValue, value); }
  102. }
  103. private string _TValue;
  104. public string TValue
  105. {
  106. get { return _TValue; }
  107. set
  108. {
  109. SetProperty(ref _TValue, value);
  110. }
  111. }
  112. private string _PValue;
  113. public string PValue
  114. {
  115. get { return _PValue; }
  116. set
  117. {
  118. SetProperty(ref _PValue, value);
  119. }
  120. }
  121. private string _SubValue;
  122. public string SubValue
  123. {
  124. get { return _SubValue; }
  125. set { SetProperty(ref _SubValue, value); }
  126. }
  127. private string _Result;
  128. public string Result
  129. {
  130. get { return _Result; }
  131. set { SetProperty(ref _Result, value); }
  132. }
  133. private string _FontColor;
  134. public string FontColor
  135. {
  136. get { return _FontColor; }
  137. set { SetProperty(ref _FontColor, value); }
  138. }
  139. private int _SelectedIndex;
  140. public int SelectedIndex
  141. {
  142. get { return _SelectedIndex; }
  143. set { SetProperty(ref _SelectedIndex, value); }
  144. }
  145. private ObservableCollection<PlcPoint_Torque> _Points = new ObservableCollection<PlcPoint_Torque>();
  146. /// <summary>
  147. /// 点位集合
  148. /// </summary>
  149. public ObservableCollection<PlcPoint_Torque> Points
  150. {
  151. get { return _Points; }
  152. set { SetProperty(ref _Points, value); }
  153. }
  154. private TorqueProduceModel _AllProductParameter = new TorqueProduceModel();
  155. /// <summary>
  156. /// 所有产品的参数信息
  157. /// </summary>
  158. public TorqueProduceModel AllProductParameter
  159. {
  160. get { return _AllProductParameter; }
  161. set { SetProperty(ref _AllProductParameter, value); }
  162. }
  163. private PlcPoint_Torque _selectedPointInDataGrid;
  164. public PlcPoint_Torque SelectedPointInDataGrid
  165. {
  166. get { return _selectedPointInDataGrid; }
  167. set
  168. {
  169. SetProperty(ref _selectedPointInDataGrid, value);
  170. // 可以同步到 SelectPoint 属性
  171. SelectedPoint = value;
  172. AllProductParameter.TorquePoints = _Points;
  173. }
  174. }
  175. private PlcPoint_Torque _SelectedPoint;
  176. public PlcPoint_Torque SelectedPoint
  177. {
  178. get { return _SelectedPoint; }
  179. set { SetProperty(ref _SelectedPoint, value); }
  180. }
  181. #endregion
  182. #region 命令
  183. private DelegateCommand _CancelCommand;
  184. public DelegateCommand CancelCommand =>
  185. _CancelCommand ?? (_CancelCommand = new DelegateCommand(ExecuteCancelCommand));
  186. private DelegateCommand _LoadedCommand;
  187. public DelegateCommand LoadedCommand =>
  188. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  189. private DelegateCommand _ConfirmCommand;
  190. public DelegateCommand ConfirmCommand =>
  191. _ConfirmCommand ?? (_ConfirmCommand = new DelegateCommand(ExecuteConfirmCommand));
  192. private DelegateCommand<string> _MotorCommand;
  193. public DelegateCommand<string> MotorCommand =>
  194. _MotorCommand ?? (_MotorCommand = new DelegateCommand<string>(ExecuteMotorCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  195. private DelegateCommand<object> _JogCommand;
  196. public DelegateCommand<object> JogCommand =>
  197. _JogCommand ?? (_JogCommand = new DelegateCommand<object>(ExecuteJogCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  198. private DelegateCommand _TechPointCommand;
  199. public DelegateCommand TechPointCommand =>
  200. _TechPointCommand ?? (_TechPointCommand = new DelegateCommand(ExecuteTechPointCommand));
  201. private DelegateCommand _MovePointCommand;
  202. public DelegateCommand MovePointCommand =>
  203. _MovePointCommand ?? (_MovePointCommand = new DelegateCommand(ExecuteMovePointCommand));
  204. private DelegateCommand _ModifyCommand;
  205. public DelegateCommand ModifyCommand =>
  206. _ModifyCommand ?? (_ModifyCommand = new DelegateCommand(ExecuteModifyCommand));
  207. private DelegateCommand _WorkCommand;
  208. public DelegateCommand WorkCommand =>
  209. _WorkCommand ?? (_WorkCommand = new DelegateCommand(ExecuteWorkCommand));
  210. private DelegateCommand _AddPointCommand;
  211. public DelegateCommand AddPointCommand =>
  212. _AddPointCommand ?? (_AddPointCommand = new DelegateCommand(ExecuteAddPointCommand));
  213. private DelegateCommand _SavePointsCommand;
  214. public DelegateCommand SavePointsCommand =>
  215. _SavePointsCommand ?? (_SavePointsCommand = new DelegateCommand(ExecuteSavePointsCommand));
  216. private DelegateCommand _DeletePointCommand;
  217. public DelegateCommand DeletePointCommand =>
  218. _DeletePointCommand ?? (_DeletePointCommand = new DelegateCommand(ExecuteDeletePointCommand, CanExecuteDeletePointCommand).ObservesProperty(() => SelectedPointInDataGrid));
  219. #endregion
  220. #region 方法
  221. public TorqueCheckViewModel(IEventAggregator ea, IConfigService configService, TorqueService torqueService,
  222. IContainerProvider container, IRobotService robotService, IPlcService plcService)
  223. {
  224. _eventAggregator = ea;
  225. _configService = configService;
  226. _container = container;
  227. _robotService = robotService;
  228. _plcService = plcService;
  229. _torqueService = torqueService;
  230. management = _container.Resolve<Management>();
  231. }
  232. bool CanExecuteDeletePointCommand()
  233. {
  234. return SelectedPointInDataGrid != null;
  235. }
  236. void ExecuteCancelCommand()
  237. {
  238. IDialogParameters parameters = new DialogParameters();
  239. RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel, parameters));
  240. }
  241. void ExecuteConfirmCommand()
  242. {
  243. IDialogParameters parameters = new DialogParameters();
  244. RequestClose?.Invoke(new DialogResult(ButtonResult.OK, parameters));
  245. }
  246. /// <summary>
  247. /// 电机
  248. /// </summary>
  249. /// <param name="obj"></param>
  250. private async void ExecuteMotorCommand(string obj)
  251. {
  252. if (Robot == null || !Robot.IsConnected) return;
  253. try
  254. {
  255. await Robot.MotorAsync(Convert.ToBoolean(obj));
  256. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  257. }
  258. catch (Exception ex)
  259. {
  260. LogHelper.WriteLogError("机器人点击操作时出错!", ex);
  261. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  262. }
  263. }
  264. /// <summary>
  265. /// 点动
  266. /// </summary>
  267. /// <param name="obj"></param>
  268. private async void ExecuteJogCommand(object obj)
  269. {
  270. if (Robot == null || !Robot.IsConnected) return;
  271. try
  272. {
  273. var items = ((string)obj).Split(':');
  274. //如果移动距离大于10mm,则提示用户确认
  275. double distance = double.Parse(items[1]);
  276. if (Math.Abs(distance) >= 10)
  277. {
  278. if (MessageBox.Show($"确认要点动距离 {distance} mm 吗?", "确认点动", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  279. {
  280. return;
  281. }
  282. }
  283. Robot.Timeout = 6000000;
  284. switch (items[0])
  285. {
  286. case "X+":
  287. await Robot.JogAsync("X", distance);
  288. break;
  289. case "X-":
  290. await Robot.JogAsync("X", -distance);
  291. break;
  292. case "Y+":
  293. await Robot.JogAsync("Y", distance);
  294. break;
  295. case "Y-":
  296. await Robot.JogAsync("Y", -distance);
  297. break;
  298. case "Z+":
  299. await Robot.JogAsync("Z", distance);
  300. break;
  301. case "Z-":
  302. await Robot.JogAsync("Z", -distance);
  303. break;
  304. case "U+":
  305. await Robot.JogAsync("U", distance);
  306. break;
  307. case "U-":
  308. await Robot.JogAsync("U", -distance);
  309. break;
  310. case "V+":
  311. await Robot.JogAsync("V", distance);
  312. break;
  313. case "V-":
  314. await Robot.JogAsync("V", -distance);
  315. break;
  316. case "W+":
  317. await Robot.JogAsync("W", distance);
  318. break;
  319. case "W-":
  320. await Robot.JogAsync("W", -distance);
  321. break;
  322. default:
  323. break;
  324. }
  325. Robot.Timeout = 5000;
  326. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  327. }
  328. catch (Exception ex)
  329. {
  330. LogHelper.WriteLogError("机器人点位-机器人点动时出错", ex);
  331. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  332. }
  333. }
  334. /// <summary>
  335. /// 示教点位
  336. /// </summary>
  337. async void ExecuteTechPointCommand()
  338. {
  339. //弹窗用户是否确认要示教该点位
  340. if (MessageBox.Show($"确认要示教点位吗?", "示教点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  341. {
  342. return;
  343. }
  344. if (SelectedPoint == null)
  345. {
  346. MessageBox.Show("请选择示教点位");
  347. return;
  348. }
  349. var curpos = await Robot.GetRobotPosAsync();
  350. SelectedPoint.X_Position = curpos.X;
  351. SelectedPoint.Y_Position = curpos.Y;
  352. SelectedPoint.Z_Position_Start = curpos.Z;
  353. SelectedPoint.Z_Position_Stop = 0;
  354. AllProductParameter.TorqueValue = this.TorqueValue;
  355. AllProductParameter.TorquePoints = Points;
  356. FileHelper.WriteJsonFile(AllProductParameter, FilePath.TorqueCheckPath);
  357. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"{Lang.完成}!", Duration = 0.4 });
  358. }
  359. /// <summary>
  360. /// 修改
  361. /// </summary>
  362. async void ExecuteModifyCommand()
  363. {
  364. if (MessageBox.Show($"确认要修改扭力阈值吗?", "修改", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  365. {
  366. return;
  367. }
  368. var res = FileHelper.ReadJsonFile<TorqueProduceModel>(FilePath.TorqueCheckPath);
  369. res.TorqueValue = this.TorqueValue;
  370. try
  371. {
  372. FileHelper.WriteJsonFile(res, FilePath.TorqueCheckPath);
  373. }
  374. catch (Exception)
  375. {
  376. MessageBox.Show("修改失败");
  377. }
  378. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 0.4 });
  379. }
  380. async void ExecuteWorkCommand()
  381. {
  382. if (!File.Exists(FilePath.TorqueCheckPath))
  383. {
  384. MessageBox.Show("还未示教,请先示教");
  385. return;
  386. }
  387. if (Robot == null || !Robot.IsConnected)
  388. {
  389. return;
  390. }
  391. if (MessageBox.Show($"确认要进行点检吗?", "执行点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  392. {
  393. return;
  394. }
  395. try
  396. {
  397. if (await MoveRobot())
  398. {
  399. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 0.4 });
  400. }
  401. else
  402. {
  403. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.失败, Duration = 0.4 });
  404. }
  405. }
  406. catch (Exception ex)
  407. {
  408. LogHelper.WriteLogError("执行点检时出错!", ex);
  409. }
  410. }
  411. /// <summary>
  412. /// 点检扭矩总体流程
  413. /// </summary>
  414. /// <returns></returns>
  415. public async Task<bool> MoveRobot()
  416. {
  417. TorqueProduceModel Plc_Point = new TorqueProduceModel();
  418. PlcAddressConfig addressConfig = _configService.GetPlcAddresses();
  419. OpcUaClientPLC plc = _plcService.GetPlcByNumber(addressConfig.PlcNo) as OpcUaClientPLC;
  420. try
  421. {
  422. Plc_Point = FileHelper.ReadJsonFile<TorqueProduceModel>(FilePath.TorqueCheckPath);
  423. if (Plc_Point == null || addressConfig == null || plc == null)
  424. {
  425. return false;
  426. }
  427. //切换手动模式
  428. if (await plc.WriteNodeAsync<bool>(addressConfig.OP_AutoManual.Address, false))
  429. {
  430. //回设置的零点
  431. if (await _torqueService.Safety_Position_Async(Robot, plc, addressConfig, Plc_Point))
  432. {
  433. //执行抛螺丝动作
  434. if (await _torqueService.Throw_Screw_Async(Robot, plc, addressConfig, Plc_Point))
  435. {
  436. //执行取螺丝动作
  437. if (await _torqueService.Pick_Screw_Async(Robot, plc, addressConfig, Plc_Point))
  438. {
  439. //执行锁螺丝点位操作
  440. if (await _torqueService.Screw_Torque_Async(Robot, plc, addressConfig, Plc_Point))
  441. {
  442. //获取电批结果并显示曲线
  443. if (await WatchAsync(plc, addressConfig))
  444. {
  445. await DisplayResult();
  446. return true;
  447. }
  448. MessageBox.Show("未获取到电批结果"); return false;
  449. }
  450. MessageBox.Show("执行锁螺丝点位操作失败"); return false;
  451. }
  452. MessageBox.Show("执行取螺丝动作失败"); return false;
  453. }
  454. MessageBox.Show("执行抛螺丝动作失败"); return false;
  455. }
  456. MessageBox.Show("执行回安全点失败"); return false;
  457. }
  458. MessageBox.Show("点检失败"); return false;
  459. }
  460. catch (Exception ex)
  461. {
  462. LogHelper.WriteLogError("执行点检移动时出错!", ex);
  463. return false;
  464. }
  465. finally
  466. {
  467. await _torqueService.Safety_Position_Async(Robot, plc, addressConfig, Plc_Point);
  468. }
  469. }
  470. public async Task DisplayResult()
  471. {
  472. try
  473. {
  474. await Task.Delay(100);
  475. TValue = management.SignalValue;
  476. double tvalue = double.Parse(TValue.Split(' ')[0]);
  477. double pvalue = Value;
  478. PValue = $"{Math.Round(pvalue, 3)} kgf.cm";
  479. SubValue = $"{Math.Round(Math.Abs(tvalue - pvalue), 3)} kgf.cm";
  480. double sub = Math.Abs(tvalue - pvalue);
  481. var res = FileHelper.ReadJsonFile<TorqueProduceModel>(FilePath.TorqueCheckPath);
  482. if (res != null)
  483. {
  484. if (sub <= res.TorqueValue)
  485. {
  486. FontColor = "Green";
  487. Result = "OK";
  488. }
  489. else
  490. {
  491. FontColor = "Red";
  492. Result = "NG";
  493. }
  494. }
  495. else
  496. {
  497. if (sub <= TorqueValue)
  498. {
  499. FontColor = "Green";
  500. Result = "OK";
  501. }
  502. else
  503. {
  504. FontColor = "Red";
  505. Result = "NG";
  506. }
  507. }
  508. await _torqueService.Save_Torque_Log($"{DateTime.Now},{TValue},{PValue},{Math.Round(sub, 4)},{TorqueValue},{Result}");
  509. SendTaskMessage("点检扭矩成功", MessageLevel.Info);
  510. }
  511. catch (Exception ex)
  512. {
  513. LogHelper.WriteLogError("执行点检显示时出错!", ex);
  514. }
  515. }
  516. public async Task<bool> WatchAsync(OpcUaClientPLC plc, PlcAddressConfig addressConfig)
  517. {
  518. object oking = null;
  519. object nging = null;
  520. int count = 0;
  521. while (count++ < 20)
  522. {
  523. await Task.Delay(100);
  524. oking = plc.ReadNode(addressConfig.Screw_OK.Address);
  525. nging = plc.ReadNode(addressConfig.Screw_NG.Address);
  526. if (oking != null && nging != null)
  527. {
  528. bool okin = (bool)oking; bool ngin = (bool)nging;
  529. //电批OKNG
  530. if (okin || ngin)
  531. {
  532. //获取电批结果
  533. bool isSucced = management.ScrewDriver.GetData();
  534. LockResult lockResult = new LockResult();
  535. lockResult.LockTurns = (float)management.ScrewDriver.Truns;
  536. lockResult.LockTorque = (float)management.ScrewDriver.TorqueValue;
  537. lockResult.WaveDatas = management.ScrewDriver.WaveDatas;
  538. App.Current.Dispatcher.Invoke(() =>
  539. {
  540. _eventAggregator.GetEvent<LockFinishNotification>().Publish(lockResult);
  541. });
  542. //await _systemDatabaseService.RecordLockResultAsync(lockResult);
  543. await plc.WriteNodeAsync(addressConfig.In_WorkDone.Address, (Int16)0);
  544. Value = lockResult.LockTorque;
  545. await plc.WriteNodeAsync(addressConfig.Check_ScrewTorque.Address, false);
  546. return true;
  547. }
  548. }
  549. }
  550. return false;
  551. }
  552. #endregion
  553. public string Title { get; set; } = "扭力点检";
  554. public event Action<IDialogResult> RequestClose;
  555. public bool CanCloseDialog()
  556. {
  557. return true;
  558. }
  559. public void OnDialogClosed()
  560. {
  561. }
  562. public void OnDialogOpened(IDialogParameters parameters)
  563. {
  564. try
  565. {
  566. var res = FileHelper.ReadJsonFile<TorqueProduceModel>(FilePath.TorqueCheckPath);
  567. if (res == null)
  568. {
  569. MessageBox.Show("还未示教,请先示教");
  570. }
  571. }
  572. catch (Exception)
  573. {
  574. MessageBox.Show("还未示教,请先示教");
  575. }
  576. if (Robot == null)
  577. {
  578. Robot = _robotService.GetRobotByNumber(1);
  579. }
  580. if (Robot != null && Robot.IsConnected)
  581. {
  582. this.Distance = _configService.GetRobotStepDistance(Robot.Id);
  583. Robot.EnterDebugMode = true;
  584. }
  585. else if (Robot != null)
  586. {
  587. Robot.EnterDebugMode = true;
  588. }
  589. }
  590. private void ExecuteLoadedCommand()
  591. {
  592. var res = FileHelper.ReadJsonFile<TorqueProduceModel>(FilePath.TorqueCheckPath);
  593. this.Points = res.TorquePoints;
  594. this.TorqueValue = res.TorqueValue;
  595. }
  596. #region 添加点位与删除点位
  597. /// <summary>
  598. /// 增加点位
  599. /// </summary>
  600. private void ExecuteAddPointCommand()
  601. {
  602. //添加点位
  603. Points.Add(new PlcPoint_Torque()
  604. {
  605. Number = Points.Count + 1,
  606. });
  607. }
  608. /// <summary>
  609. /// 移除点位
  610. /// </summary>
  611. private void ExecuteDeletePointCommand()
  612. {
  613. if (SelectedPoint == null)
  614. {
  615. return;
  616. }
  617. // 确认删除
  618. if (MessageBox.Show(Lang.确认要删除选中的点位吗, Lang.删除点位, MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  619. {
  620. return;
  621. }
  622. int removedIndex = Points.IndexOf(SelectedPoint);
  623. Points.Remove(SelectedPoint);
  624. //重新编号
  625. for (int i = 0; i < Points.Count; i++)
  626. {
  627. Points[i].Number = i + 1;
  628. }
  629. // adjust selection
  630. if (Points.Count > 0)
  631. {
  632. int newIndex = Math.Min(removedIndex, Points.Count - 1);
  633. SelectedPointInDataGrid = Points[newIndex];
  634. }
  635. else
  636. {
  637. SelectedPointInDataGrid = null;
  638. }
  639. }
  640. #endregion
  641. private void SendTaskMessage(string msg, MessageLevel level)
  642. {
  643. App.Current.Dispatcher.Invoke(() =>
  644. {
  645. _eventAggregator.GetEvent<TaskMessageNotification>().Publish(new Models.MessageStruct() { Message = msg, level = level });
  646. });
  647. }
  648. private void ExecuteSavePointsCommand()
  649. {
  650. AllProductParameter.TorqueValue = this.TorqueValue;
  651. AllProductParameter.TorquePoints = Points;
  652. FileHelper.WriteJsonFile(AllProductParameter, FilePath.TorqueCheckPath);
  653. MessageBox.Show("参数保存成功");
  654. }
  655. private async void ExecuteMovePointCommand()
  656. {
  657. if (MessageBox.Show($"确认要进行单独移动至点位吗?", "执行点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  658. {
  659. return;
  660. }
  661. PlcAddressConfig addressConfig = _configService.GetPlcAddresses();
  662. OpcUaClientPLC plc = _plcService.GetPlcByNumber(addressConfig.PlcNo) as OpcUaClientPLC;
  663. await _torqueService.Signal_Move_Async(Robot, plc, addressConfig, SelectedPoint);
  664. }
  665. }
  666. }