CalibrationTeachCenterPointViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. using Prism.Commands;
  2. using Prism.Events;
  3. using Prism.Ioc;
  4. using Prism.Mvvm;
  5. using Prism.Regions;
  6. using Prism.Services.Dialogs;
  7. using SixLabors.Fonts.Unicode;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Security.Cryptography;
  12. using System.Windows;
  13. using Team.FFFeederService.Interfaces;
  14. using TeamAAS_VP;
  15. using TeamAAS_VP.Core;
  16. using TeamAAS_VP.Events;
  17. using TeamAAS_VP.Interfaces;
  18. using TeamAAS_VP.Models;
  19. using TeamAAS_VP.Models.Calibration;
  20. using TeamAAS_VP.Services;
  21. using TeamAAS_VP.ViewModels.Product;
  22. namespace TeamAAS_VP.ViewModels.Calibration
  23. {
  24. public class CalibrationTeachCenterPointViewModel : BindableBase, IConfirmNavigationRequest
  25. {
  26. IRegionManager _regionManager;
  27. IRegionNavigationService _regionNavigationService;
  28. IContainerProvider _container;
  29. IDialogService _dialogService;
  30. IEventAggregator _eventAggregator;
  31. IConfigService _configService;
  32. IRobotService _robotService;
  33. ICameraService _cameraService;
  34. IFeederService _feederService;
  35. ISystemDatabaseService _systemDatabaseService;
  36. #region 属性
  37. private CalibrationInfo _SelectCalibration;
  38. public CalibrationInfo SelectCalibration
  39. {
  40. get { return _SelectCalibration; }
  41. set { SetProperty(ref _SelectCalibration, value); }
  42. }
  43. private bool _IsShowTechHome = false;
  44. public bool IsShowTechHome
  45. {
  46. get { return _IsShowTechHome; }
  47. set { SetProperty(ref _IsShowTechHome, value); }
  48. }
  49. private bool _IsShowTechP0ModeSelect = true;
  50. /// <summary>
  51. /// 是否显示P0模式选择
  52. /// </summary>
  53. public bool IsShowTechP0ModeSelect
  54. {
  55. get { return _IsShowTechP0ModeSelect; }
  56. set { SetProperty(ref _IsShowTechP0ModeSelect, value); }
  57. }
  58. public Management management { get; set; }
  59. private IRobot _Robot;
  60. public IRobot Robot
  61. {
  62. get { return _Robot; }
  63. set { SetProperty(ref _Robot, value); }
  64. }
  65. #endregion
  66. #region 命令
  67. private DelegateCommand _LoadedCommand;
  68. public DelegateCommand LoadedCommand =>
  69. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  70. private DelegateCommand _NextCommand;
  71. public DelegateCommand NextCommand =>
  72. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand));
  73. private DelegateCommand _BackCommand;
  74. public DelegateCommand BackCommand =>
  75. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
  76. private DelegateCommand _TechCenterCommand;
  77. public DelegateCommand TechCenterCommand =>
  78. _TechCenterCommand ?? (_TechCenterCommand = new DelegateCommand(ExecuteTechCenterCommand));
  79. private DelegateCommand _TechHomeCommand;
  80. public DelegateCommand TechHomeCommand =>
  81. _TechHomeCommand ?? (_TechHomeCommand = new DelegateCommand(ExecuteTechHomeCommand));
  82. private DelegateCommand<object> _BlowOrInhalControlCommand;
  83. public DelegateCommand<object> BlowOrInhalControlCommand =>
  84. _BlowOrInhalControlCommand ?? (_BlowOrInhalControlCommand = new DelegateCommand<object>(ExecuteBlowOrInhalControlCommand));
  85. private DelegateCommand _TechMarkPointCommand;
  86. public DelegateCommand TechMarkPointCommand =>
  87. _TechMarkPointCommand ?? (_TechMarkPointCommand = new DelegateCommand(ExecuteTechMarkPointCommand));
  88. #endregion
  89. #region 事件
  90. #endregion
  91. public CalibrationTeachCenterPointViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService,
  92. IConfigService configService, IRobotService robotService, ICameraService cameraService, IFeederService feederService, ISystemDatabaseService systemDatabaseService)
  93. {
  94. _regionManager = regionManager;
  95. _regionNavigationService = regionNavigationService;
  96. _container = container;
  97. _dialogService = dialogService;
  98. _eventAggregator = ea;
  99. _configService = configService;
  100. _robotService = robotService;
  101. _cameraService = cameraService;
  102. _feederService = feederService;
  103. _systemDatabaseService = systemDatabaseService;
  104. }
  105. #region 方法
  106. void ExecuteLoadedCommand()
  107. {
  108. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  109. {
  110. IsAllowEdit = false;
  111. }
  112. else
  113. {
  114. IsAllowEdit = true;
  115. }
  116. }
  117. /// <summary>
  118. /// 下一步
  119. /// </summary>
  120. void ExecuteNextCommand()
  121. {
  122. NavigationParameters param = new NavigationParameters();
  123. param.Add("SelectCalibration", SelectCalibration);
  124. if (SelectCalibration.CameraMount == Enums.CameraMount.MobileDown_XYPlatform)
  125. {
  126. if (SelectCalibration.CalibTechP0Mode == Enums.CalibTechP0Mode.CameraFindP0)
  127. {
  128. _regionManager.RequestNavigate("CalibrationRegionContext", "FixedUpCameraFindP0", param);
  129. return;
  130. }
  131. else
  132. {
  133. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationAuto", param);
  134. }
  135. }
  136. else if (Robot.Brand == Enums.RobotBrand.XYZ_Platform)
  137. {
  138. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationAuto", param);
  139. }
  140. else
  141. {
  142. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationSelectTool", param);
  143. }
  144. }
  145. /// <summary>
  146. /// 上一步
  147. /// </summary>
  148. void ExecuteBackCommand()
  149. {
  150. NavigationParameters param = new NavigationParameters();
  151. param.Add("SelectCalibration", SelectCalibration);
  152. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationEditVision", param);
  153. }
  154. /// <summary>
  155. /// 示教中心点
  156. /// </summary>
  157. async void ExecuteTechCenterCommand()
  158. {
  159. if (Robot == null || !Robot.IsConnected) return;
  160. try
  161. {
  162. var curpos = await Robot.GetRobotPosAsync();
  163. if (SelectCalibration.CenterPoint == null)
  164. {
  165. SelectCalibration.CenterPoint = new RPoint();
  166. }
  167. SelectCalibration.CenterPoint.X = curpos.X;
  168. SelectCalibration.CenterPoint.Y = curpos.Y;
  169. SelectCalibration.CenterPoint.Z = curpos.Z;
  170. SelectCalibration.CenterPoint.U = curpos.U;
  171. SelectCalibration.CenterPoint.V = curpos.V;
  172. SelectCalibration.CenterPoint.W = curpos.W;
  173. SelectCalibration.CenterPoint.Hand = curpos.Hand;
  174. SelectCalibration.CenterPoint.Local = curpos.Local;
  175. SelectCalibration.CenterPoint.Tool = curpos.Tool;
  176. LogHelper.WriteLogInfo($"相机校准-示教中心点界面示教待机点:X:{SelectCalibration.CenterPoint.X} Y:{SelectCalibration.CenterPoint.Y} Z:{SelectCalibration.CenterPoint.Z} ");
  177. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"成功! X:{SelectCalibration.CenterPoint.X} Y:{SelectCalibration.CenterPoint.Y} Z:{SelectCalibration.CenterPoint.Z}", Duration = 1 });
  178. }
  179. catch (Exception ex)
  180. {
  181. LogHelper.WriteLogError("相机校准-示教中心点界面示教中心点时出错!", ex);
  182. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  183. }
  184. }
  185. /// <summary>
  186. /// 示教待机点
  187. /// </summary>
  188. async void ExecuteTechHomeCommand()
  189. {
  190. if (Robot == null || !Robot.IsConnected) return;
  191. try
  192. {
  193. var curpos = await Robot.GetRobotPosAsync();
  194. if (SelectCalibration.HomePoint == null)
  195. {
  196. SelectCalibration.HomePoint = new RPoint();
  197. }
  198. SelectCalibration.HomePoint.X = curpos.X;
  199. SelectCalibration.HomePoint.Y = curpos.Y;
  200. SelectCalibration.HomePoint.Z = curpos.Z;
  201. SelectCalibration.HomePoint.U = curpos.U;
  202. SelectCalibration.HomePoint.V = curpos.V;
  203. SelectCalibration.HomePoint.W = curpos.W;
  204. SelectCalibration.HomePoint.Hand = curpos.Hand;
  205. SelectCalibration.HomePoint.Local = curpos.Local;
  206. SelectCalibration.HomePoint.Tool = curpos.Tool;
  207. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "成功!", Duration = 1 });
  208. }
  209. catch (Exception ex)
  210. {
  211. LogHelper.WriteLogError("相机校准-示教中心点界面示教待机点时出错!", ex);
  212. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  213. }
  214. }
  215. /// <summary>
  216. /// 夹爪打开/关闭
  217. /// </summary>
  218. /// <param name="obj"></param>
  219. async void ExecuteBlowOrInhalControlCommand(object obj)
  220. {
  221. try
  222. {
  223. await Robot.CalibOutIOAsync(!(bool)obj);
  224. }
  225. catch (Exception ex)
  226. {
  227. LogHelper.WriteLogError("相机校准-夹爪打开/关闭点时出错!", ex);
  228. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  229. }
  230. }
  231. /// <summary>
  232. /// 示教P0
  233. /// </summary>
  234. async void ExecuteTechMarkPointCommand()
  235. {
  236. if (Robot == null || !Robot.IsConnected) return;
  237. try
  238. {
  239. var curpos = await Robot.GetRobotPosAsync();
  240. if (SelectCalibration.MarkPoint == null)
  241. {
  242. SelectCalibration.MarkPoint = new RPoint();
  243. }
  244. SelectCalibration.MarkPoint.X = curpos.X;
  245. SelectCalibration.MarkPoint.Y = curpos.Y;
  246. SelectCalibration.MarkPoint.Z = curpos.Z;
  247. SelectCalibration.MarkPoint.U = curpos.U;
  248. SelectCalibration.MarkPoint.V = curpos.V;
  249. SelectCalibration.MarkPoint.W = curpos.W;
  250. SelectCalibration.MarkPoint.Hand = curpos.Hand;
  251. SelectCalibration.MarkPoint.Local = curpos.Local;
  252. SelectCalibration.MarkPoint.Tool = curpos.Tool;
  253. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "成功!", Duration = 1 });
  254. }
  255. catch (Exception ex)
  256. {
  257. LogHelper.WriteLogError("相机校准-示教中心点界面示教P0时出错!", ex);
  258. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  259. }
  260. }
  261. #endregion
  262. #region 继承
  263. /// <summary>
  264. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  265. /// </summary>
  266. /// <param name="navigationContext"></param>
  267. /// <param name="continuationCallback"></param>
  268. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  269. {
  270. continuationCallback(true);
  271. }
  272. /// <summary>
  273. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  274. /// </summary>
  275. /// <param name="navigationContext"></param>
  276. /// <exception cref="NotImplementedException"></exception>
  277. public async void OnNavigatedTo(NavigationContext navigationContext)
  278. {
  279. SelectCalibration = navigationContext.Parameters.GetValue<CalibrationInfo>("SelectCalibration");
  280. if (SelectCalibration != null)
  281. {
  282. Robot = _robotService.GetRobot(SelectCalibration.RobotId);
  283. if (SelectCalibration.CameraMount == Enums.CameraMount.FixedDown)
  284. {
  285. IsShowTechHome = true;
  286. }
  287. if (SelectCalibration.CameraMount == Enums.CameraMount.FixedUp)
  288. {
  289. IsShowTechP0ModeSelect = false;
  290. }
  291. else if (SelectCalibration.CameraMount == Enums.CameraMount.FixedDown)
  292. {
  293. IsShowTechP0ModeSelect = false;
  294. }
  295. else if (SelectCalibration.CameraMount == Enums.CameraMount.MobileJ4)
  296. {
  297. IsShowTechP0ModeSelect = true;
  298. }
  299. else if (SelectCalibration.CameraMount == Enums.CameraMount.MobileJ2)
  300. {
  301. IsShowTechP0ModeSelect = true;
  302. }
  303. else if (SelectCalibration.CameraMount == Enums.CameraMount.MobileDown_XYPlatform)
  304. {
  305. IsShowTechP0ModeSelect = true;
  306. }
  307. }
  308. else
  309. {
  310. IsShowTechHome = false;
  311. }
  312. }
  313. /// <summary>
  314. /// 是否允许导航到此视图模型。
  315. /// </summary>
  316. /// <param name="navigationContext"></param>
  317. /// <returns></returns>
  318. public bool IsNavigationTarget(NavigationContext navigationContext)
  319. {
  320. return true;
  321. }
  322. /// <summary>
  323. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  324. /// </summary>
  325. /// <param name="navigationContext"></param>
  326. public void OnNavigatedFrom(NavigationContext navigationContext)
  327. {
  328. }
  329. #endregion
  330. private bool _IsAllowEdit = false;
  331. public bool IsAllowEdit
  332. {
  333. get { return _IsAllowEdit; }
  334. set { SetProperty(ref _IsAllowEdit, value); }
  335. }
  336. private void LoginChange(Models.User user)
  337. {
  338. if (user.userPart == Enums.UserPart.Operator)
  339. {
  340. IsAllowEdit = false;
  341. }
  342. else
  343. {
  344. IsAllowEdit = true;
  345. }
  346. }
  347. }
  348. }