TorqueCheckViewModel.cs 12 KB

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