TorqueCheckViewModel.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. using MathNet.Numerics;
  2. using NPOI.OpenXml4Net.OPC.Internal;
  3. using NPOI.SS.Formula.Functions;
  4. using Org.BouncyCastle.Bcpg.Sig;
  5. using OxyPlot;
  6. using Prism.Commands;
  7. using Prism.Events;
  8. using Prism.Ioc;
  9. using Prism.Mvvm;
  10. using Prism.Services.Dialogs;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Collections.ObjectModel;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. using System.Windows;
  20. using TeamAAS_VP.Controls;
  21. using TeamAAS_VP.Core;
  22. using TeamAAS_VP.Core.PLCs;
  23. using TeamAAS_VP.Core.ScrewDriver;
  24. using TeamAAS_VP.Data;
  25. using TeamAAS_VP.Enums;
  26. using TeamAAS_VP.Events;
  27. using TeamAAS_VP.Interfaces;
  28. using TeamAAS_VP.Models;
  29. using TeamAAS_VP.Models.PLC;
  30. using TeamAAS_VP.Models.Torque;
  31. using TeamAAS_VP.Resources.Languages;
  32. using TeamAAS_VP.Services;
  33. using FileHelper = TeamAAS_VP.Core.FileHelper;
  34. namespace TeamAAS_VP.ViewModels.Home
  35. {
  36. public class TorqueCheckViewModel : BindableBase, IDialogAware
  37. {
  38. // 点检扭矩参数存放日志地址
  39. public static readonly string CheckTorqueConfigurationPath = "..//Log//ScrewFeedersConfiguration.cfg";
  40. IEventAggregator _eventAggregator;
  41. IConfigService _configService;
  42. IContainerProvider _container;
  43. IRobotService _robotService;
  44. IPlcService _plcService;
  45. TorqueService _torqueService;
  46. IDialogService _dialogService;
  47. float Value { get; set; } = 0;
  48. #region 属性
  49. private Management _management;
  50. public Management management
  51. {
  52. get { return _management; }
  53. set { SetProperty(ref _management, value); }
  54. }
  55. private bool CanExecute
  56. {
  57. get
  58. {
  59. // 命令可执行仅当机器人可执行且已选择点位
  60. if (Robot != null && Robot.IsConnected && Robot.CanExecute)
  61. {
  62. return true;
  63. }
  64. return false;
  65. }
  66. }
  67. private IRobot _Robot;
  68. public IRobot Robot
  69. {
  70. get { return _Robot; }
  71. set { SetProperty(ref _Robot, value); }
  72. }
  73. private double _Distance = 1;
  74. public double Distance
  75. {
  76. get { return _Distance; }
  77. set
  78. {
  79. SetProperty(ref _Distance, value);
  80. try
  81. {
  82. if (Robot == null)
  83. {
  84. return;
  85. }
  86. _configService.UpdateRobotStepDistance(Robot.Id, value);
  87. }
  88. catch (Exception)
  89. {
  90. }
  91. }
  92. }
  93. private float _Z_Distance;
  94. public float Z_Distance
  95. {
  96. get { return _Z_Distance; }
  97. set { SetProperty(ref _Z_Distance, value); }
  98. }
  99. private float _torqueValue;
  100. public float TorqueValue
  101. {
  102. get { return _torqueValue; }
  103. set { SetProperty(ref _torqueValue, value); }
  104. }
  105. private string _TValue;
  106. public string TValue
  107. {
  108. get { return _TValue; }
  109. set
  110. {
  111. SetProperty(ref _TValue, value);
  112. }
  113. }
  114. private string _PValue;
  115. public string PValue
  116. {
  117. get { return _PValue; }
  118. set
  119. {
  120. SetProperty(ref _PValue, value);
  121. }
  122. }
  123. private string _SubValue;
  124. public string SubValue
  125. {
  126. get { return _SubValue; }
  127. set { SetProperty(ref _SubValue, value); }
  128. }
  129. private string _Result;
  130. public string Result
  131. {
  132. get { return _Result; }
  133. set { SetProperty(ref _Result, value); }
  134. }
  135. private string _FontColor;
  136. public string FontColor
  137. {
  138. get { return _FontColor; }
  139. set { SetProperty(ref _FontColor, value); }
  140. }
  141. private int _SelectedIndex;
  142. public int SelectedIndex
  143. {
  144. get { return _SelectedIndex; }
  145. set { SetProperty(ref _SelectedIndex, value); }
  146. }
  147. private ObservableCollection<PlcPoint_Torque> _Points = new ObservableCollection<PlcPoint_Torque>();
  148. /// <summary>
  149. /// 点位集合
  150. /// </summary>
  151. public ObservableCollection<PlcPoint_Torque> Points
  152. {
  153. get { return _Points; }
  154. set
  155. {
  156. SetProperty(ref _Points, value);
  157. AllProductParameter.TorquePoints = Points;
  158. }
  159. }
  160. private TorqueProduceModel _AllProductParameter = new TorqueProduceModel();
  161. /// <summary>
  162. /// 所有产品的参数信息
  163. /// </summary>
  164. public TorqueProduceModel AllProductParameter
  165. {
  166. get { return _AllProductParameter; }
  167. set { SetProperty(ref _AllProductParameter, value); }
  168. }
  169. private PlcPoint_Torque _selectedPointInDataGrid;
  170. public PlcPoint_Torque SelectedPointInDataGrid
  171. {
  172. get { return _selectedPointInDataGrid; }
  173. set
  174. {
  175. SetProperty(ref _selectedPointInDataGrid, value);
  176. // 可以同步到 SelectPoint 属性
  177. SelectedPoint = value;
  178. AllProductParameter.TorquePoints = Points;
  179. }
  180. }
  181. private PlcPoint_Torque _SelectedPoint;
  182. public PlcPoint_Torque SelectedPoint
  183. {
  184. get { return _SelectedPoint; }
  185. set { SetProperty(ref _SelectedPoint, value); }
  186. }
  187. #endregion
  188. #region 命令
  189. private DelegateCommand _CancelCommand;
  190. public DelegateCommand CancelCommand =>
  191. _CancelCommand ?? (_CancelCommand = new DelegateCommand(ExecuteCancelCommand));
  192. private DelegateCommand _LoadedCommand;
  193. public DelegateCommand LoadedCommand =>
  194. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  195. private DelegateCommand _ConfirmCommand;
  196. public DelegateCommand ConfirmCommand =>
  197. _ConfirmCommand ?? (_ConfirmCommand = new DelegateCommand(ExecuteConfirmCommand));
  198. private DelegateCommand<string> _MotorCommand;
  199. public DelegateCommand<string> MotorCommand =>
  200. _MotorCommand ?? (_MotorCommand = new DelegateCommand<string>(ExecuteMotorCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  201. private DelegateCommand<object> _JogCommand;
  202. public DelegateCommand<object> JogCommand =>
  203. _JogCommand ?? (_JogCommand = new DelegateCommand<object>(ExecuteJogCommand).ObservesCanExecute(() => CanExecute).ObservesProperty(() => Robot).ObservesProperty(() => Robot.CanExecute));
  204. private DelegateCommand _TechPointCommand;
  205. public DelegateCommand TechPointCommand =>
  206. _TechPointCommand ?? (_TechPointCommand = new DelegateCommand(ExecuteTechPointCommand));
  207. private DelegateCommand _MovePointCommand;
  208. public DelegateCommand MovePointCommand =>
  209. _MovePointCommand ?? (_MovePointCommand = new DelegateCommand(ExecuteMovePointCommand));
  210. private DelegateCommand _ModifyCommand;
  211. public DelegateCommand ModifyCommand =>
  212. _ModifyCommand ?? (_ModifyCommand = new DelegateCommand(ExecuteModifyCommand));
  213. private DelegateCommand _WorkCommand;
  214. public DelegateCommand WorkCommand =>
  215. _WorkCommand ?? (_WorkCommand = new DelegateCommand(ExecuteWorkCommand));
  216. private DelegateCommand _SavePointsCommand;
  217. public DelegateCommand SavePointsCommand =>
  218. _SavePointsCommand ?? (_SavePointsCommand = new DelegateCommand(ExecuteSavePointsCommand));
  219. private DelegateCommand _PickScrewTestCommand;
  220. public DelegateCommand PickScrewTestCommand =>
  221. _PickScrewTestCommand ?? (_PickScrewTestCommand = new DelegateCommand(ExecutePickScrewTestCommand));
  222. #endregion
  223. #region 方法
  224. public TorqueCheckViewModel(IEventAggregator ea, IConfigService configService, TorqueService torqueService,
  225. IContainerProvider container, IRobotService robotService, IPlcService plcService, IDialogService dialogService)
  226. {
  227. _eventAggregator = ea;
  228. _configService = configService;
  229. _container = container;
  230. _robotService = robotService;
  231. _plcService = plcService;
  232. _torqueService = torqueService;
  233. _dialogService = dialogService;
  234. management = _container.Resolve<Management>();
  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(500);
  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(50);
  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;
  529. bool ngin = (bool)nging;
  530. //电批OKNG
  531. if (okin || ngin)
  532. {
  533. //获取电批结果
  534. bool isSucced = management.ScrewDriver.GetData();
  535. LockResult lockResult = new LockResult();
  536. lockResult.LockTurns = (float)management.ScrewDriver.Truns;
  537. lockResult.LockTorque = (float)management.ScrewDriver.TorqueValue;
  538. lockResult.WaveDatas = management.ScrewDriver.WaveDatas;
  539. App.Current.Dispatcher.Invoke(() =>
  540. {
  541. _eventAggregator.GetEvent<LockFinishNotification>().Publish(lockResult);
  542. });
  543. //await _systemDatabaseService.RecordLockResultAsync(lockResult);
  544. await plc.WriteNodeAsync(addressConfig.In_WorkDone.Address, (Int16)0);
  545. //await Task.Delay(200);
  546. Value = lockResult.LockTorque;
  547. await plc.WriteNodeAsync(addressConfig.Check_ScrewTorque.Address, false);
  548. await _torqueService.BackZero_Async(Robot,Robot.GetRobotPos());
  549. return true;
  550. }
  551. }
  552. }
  553. return false;
  554. }
  555. private void ExecutePickScrewTestCommand()
  556. {
  557. var param = new DialogParameters();
  558. param.Add("PickScrewPoint", AllProductParameter);
  559. param.Add("Robot", Robot);
  560. _dialogService.ShowDialog("PickScrewAccuracyAnalyzer", param, rst =>
  561. {
  562. //对话框关闭之后的回调函数,可以在这解析结果。
  563. ButtonResult result1 = rst.Result;
  564. if (result1 == ButtonResult.OK)
  565. {
  566. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  567. }
  568. });
  569. }
  570. #endregion
  571. public string Title { get; set; } = "扭力点检";
  572. public event Action<IDialogResult> RequestClose;
  573. public bool CanCloseDialog()
  574. {
  575. return true;
  576. }
  577. public void OnDialogClosed()
  578. {
  579. }
  580. public void OnDialogOpened(IDialogParameters parameters)
  581. {
  582. try
  583. {
  584. var res = FileHelper.ReadJsonFile<TorqueProduceModel>(FilePath.TorqueCheckPath);
  585. if (res == null)
  586. {
  587. MessageBox.Show("还未示教,请先示教");
  588. InitialTorquePoints();
  589. }
  590. }
  591. catch (Exception)
  592. {
  593. MessageBox.Show("还未示教,请先示教");
  594. InitialTorquePoints();
  595. }
  596. if (Robot == null)
  597. {
  598. Robot = _robotService.GetRobotByNumber(1);
  599. }
  600. if (Robot != null && Robot.IsConnected)
  601. {
  602. this.Distance = _configService.GetRobotStepDistance(Robot.Id);
  603. Robot.EnterDebugMode = true;
  604. }
  605. else if (Robot != null)
  606. {
  607. Robot.EnterDebugMode = true;
  608. }
  609. }
  610. private void InitialTorquePoints()
  611. {
  612. TorqueProduceModel torques = new TorqueProduceModel()
  613. {
  614. TorquePoints = new ObservableCollection<PlcPoint_Torque>()
  615. {
  616. new PlcPoint_Torque(){Label="抛料点位"},
  617. new PlcPoint_Torque(){Label="取料点位"},
  618. new PlcPoint_Torque(){Label="锁螺丝点位"},
  619. new PlcPoint_Torque(){Label="安全点位"},
  620. }
  621. };
  622. FileHelper.WriteJsonFile(torques, FilePath.TorqueCheckPath);
  623. }
  624. private void ExecuteLoadedCommand()
  625. {
  626. var res = FileHelper.ReadJsonFile<TorqueProduceModel>(FilePath.TorqueCheckPath);
  627. this.Points = res.TorquePoints;
  628. this.TorqueValue = res.TorqueValue;
  629. }
  630. private void SendTaskMessage(string msg, MessageLevel level)
  631. {
  632. App.Current.Dispatcher.Invoke(() =>
  633. {
  634. _eventAggregator.GetEvent<TaskMessageNotification>().Publish(new Models.MessageStruct() { Message = msg, level = level });
  635. });
  636. }
  637. private void ExecuteSavePointsCommand()
  638. {
  639. AllProductParameter.TorqueValue = this.TorqueValue;
  640. AllProductParameter.TorquePoints = Points;
  641. FileHelper.WriteJsonFile(AllProductParameter, FilePath.TorqueCheckPath);
  642. MessageBox.Show("参数保存成功");
  643. }
  644. private async void ExecuteMovePointCommand()
  645. {
  646. if (MessageBox.Show($"确认要进行单独移动至点位吗?", "执行点位", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
  647. {
  648. return;
  649. }
  650. PlcAddressConfig addressConfig = _configService.GetPlcAddresses();
  651. OpcUaClientPLC plc = _plcService.GetPlcByNumber(addressConfig.PlcNo) as OpcUaClientPLC;
  652. await _torqueService.Signal_Move_Async(Robot, plc, addressConfig, SelectedPoint);
  653. }
  654. }
  655. }