CalibrationTeachCenterPointViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. public Management management { get; set; }
  50. private IRobot _Robot;
  51. public IRobot Robot
  52. {
  53. get { return _Robot; }
  54. set { SetProperty(ref _Robot, value); }
  55. }
  56. #endregion
  57. #region 命令
  58. private DelegateCommand _LoadedCommand;
  59. public DelegateCommand LoadedCommand =>
  60. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  61. private DelegateCommand _NextCommand;
  62. public DelegateCommand NextCommand =>
  63. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand));
  64. private DelegateCommand _BackCommand;
  65. public DelegateCommand BackCommand =>
  66. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
  67. private DelegateCommand _TechCenterCommand;
  68. public DelegateCommand TechCenterCommand =>
  69. _TechCenterCommand ?? (_TechCenterCommand = new DelegateCommand(ExecuteTechCenterCommand));
  70. private DelegateCommand _TechHomeCommand;
  71. public DelegateCommand TechHomeCommand =>
  72. _TechHomeCommand ?? (_TechHomeCommand = new DelegateCommand(ExecuteTechHomeCommand));
  73. private DelegateCommand<object> _BlowOrInhalControlCommand;
  74. public DelegateCommand<object> BlowOrInhalControlCommand =>
  75. _BlowOrInhalControlCommand ?? (_BlowOrInhalControlCommand = new DelegateCommand<object>(ExecuteBlowOrInhalControlCommand));
  76. private DelegateCommand _TechMarkPointCommand;
  77. public DelegateCommand TechMarkPointCommand =>
  78. _TechMarkPointCommand ?? (_TechMarkPointCommand = new DelegateCommand(ExecuteTechMarkPointCommand));
  79. #endregion
  80. #region 事件
  81. #endregion
  82. public CalibrationTeachCenterPointViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService,
  83. IConfigService configService, IRobotService robotService, ICameraService cameraService, IFeederService feederService, ISystemDatabaseService systemDatabaseService)
  84. {
  85. _regionManager = regionManager;
  86. _regionNavigationService = regionNavigationService;
  87. _container = container;
  88. _dialogService = dialogService;
  89. _eventAggregator = ea;
  90. _configService = configService;
  91. _robotService = robotService;
  92. _cameraService = cameraService;
  93. _feederService = feederService;
  94. _systemDatabaseService = systemDatabaseService;
  95. }
  96. #region 方法
  97. void ExecuteLoadedCommand()
  98. {
  99. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  100. {
  101. IsAllowEdit = false;
  102. }
  103. else
  104. {
  105. IsAllowEdit = true;
  106. }
  107. }
  108. /// <summary>
  109. /// 下一步
  110. /// </summary>
  111. void ExecuteNextCommand()
  112. {
  113. NavigationParameters param = new NavigationParameters();
  114. param.Add("SelectCalibration", SelectCalibration);
  115. if (SelectCalibration.CameraMount== Enums.CameraMount.MobileDown_XYPlatform)
  116. {
  117. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationAuto", param);
  118. }
  119. else
  120. {
  121. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationSelectTool", param);
  122. }
  123. }
  124. /// <summary>
  125. /// 上一步
  126. /// </summary>
  127. void ExecuteBackCommand()
  128. {
  129. NavigationParameters param = new NavigationParameters();
  130. param.Add("SelectCalibration", SelectCalibration);
  131. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationEditVision", param);
  132. }
  133. /// <summary>
  134. /// 示教中心点
  135. /// </summary>
  136. async void ExecuteTechCenterCommand()
  137. {
  138. if (Robot == null || !Robot.IsConnected) return;
  139. try
  140. {
  141. var curpos = await Robot.GetRobotPosAsync();
  142. if (SelectCalibration.CenterPoint==null)
  143. {
  144. SelectCalibration.CenterPoint = new RPoint();
  145. }
  146. SelectCalibration.CenterPoint.X = curpos.X;
  147. SelectCalibration.CenterPoint.Y = curpos.Y;
  148. SelectCalibration.CenterPoint.Z = curpos.Z;
  149. SelectCalibration.CenterPoint.U = curpos.U;
  150. SelectCalibration.CenterPoint.V = curpos.V;
  151. SelectCalibration.CenterPoint.W = curpos.W;
  152. SelectCalibration.CenterPoint.Hand = curpos.Hand;
  153. SelectCalibration.CenterPoint.Local = curpos.Local;
  154. SelectCalibration.CenterPoint.Tool = curpos.Tool;
  155. LogHelper.WriteLogInfo($"相机校准-示教中心点界面示教待机点:X:{SelectCalibration.CenterPoint.X} Y:{SelectCalibration.CenterPoint.Y} Z:{SelectCalibration.CenterPoint.Z} ");
  156. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"成功! X:{SelectCalibration.CenterPoint.X} Y:{SelectCalibration.CenterPoint.Y} Z:{SelectCalibration.CenterPoint.Z}", Duration = 1 });
  157. }
  158. catch (Exception ex)
  159. {
  160. LogHelper.WriteLogError("相机校准-示教中心点界面示教中心点时出错!", ex);
  161. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  162. }
  163. }
  164. /// <summary>
  165. /// 示教待机点
  166. /// </summary>
  167. async void ExecuteTechHomeCommand()
  168. {
  169. if (Robot == null || !Robot.IsConnected) return;
  170. try
  171. {
  172. var curpos = await Robot.GetRobotPosAsync();
  173. if (SelectCalibration.HomePoint == null)
  174. {
  175. SelectCalibration.HomePoint = new RPoint();
  176. }
  177. SelectCalibration.HomePoint.X = curpos.X;
  178. SelectCalibration.HomePoint.Y = curpos.Y;
  179. SelectCalibration.HomePoint.Z = curpos.Z;
  180. SelectCalibration.HomePoint.U = curpos.U;
  181. SelectCalibration.HomePoint.V = curpos.V;
  182. SelectCalibration.HomePoint.W = curpos.W;
  183. SelectCalibration.HomePoint.Hand = curpos.Hand;
  184. SelectCalibration.HomePoint.Local = curpos.Local;
  185. SelectCalibration.HomePoint.Tool = curpos.Tool;
  186. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "成功!", Duration = 1 });
  187. }
  188. catch (Exception ex)
  189. {
  190. LogHelper.WriteLogError("相机校准-示教中心点界面示教待机点时出错!", ex);
  191. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  192. }
  193. }
  194. /// <summary>
  195. /// 夹爪打开/关闭
  196. /// </summary>
  197. /// <param name="obj"></param>
  198. async void ExecuteBlowOrInhalControlCommand(object obj)
  199. {
  200. try
  201. {
  202. await Robot.CalibOutIOAsync(!(bool)obj);
  203. }
  204. catch (Exception ex)
  205. {
  206. LogHelper.WriteLogError("相机校准-夹爪打开/关闭点时出错!", ex);
  207. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  208. }
  209. }
  210. /// <summary>
  211. /// 示教P0
  212. /// </summary>
  213. async void ExecuteTechMarkPointCommand()
  214. {
  215. if (Robot == null || !Robot.IsConnected) return;
  216. try
  217. {
  218. var curpos = await Robot.GetRobotPosAsync();
  219. if (SelectCalibration.MarkPoint == null)
  220. {
  221. SelectCalibration.MarkPoint = new RPoint();
  222. }
  223. SelectCalibration.MarkPoint.X = curpos.X;
  224. SelectCalibration.MarkPoint.Y = curpos.Y;
  225. SelectCalibration.MarkPoint.Z = curpos.Z;
  226. SelectCalibration.MarkPoint.U = curpos.U;
  227. SelectCalibration.MarkPoint.V = curpos.V;
  228. SelectCalibration.MarkPoint.W = curpos.W;
  229. SelectCalibration.MarkPoint.Hand = curpos.Hand;
  230. SelectCalibration.MarkPoint.Local = curpos.Local;
  231. SelectCalibration.MarkPoint.Tool = curpos.Tool;
  232. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "成功!", Duration = 1 });
  233. }
  234. catch (Exception ex)
  235. {
  236. LogHelper.WriteLogError("相机校准-示教中心点界面示教P0时出错!", ex);
  237. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  238. }
  239. }
  240. #endregion
  241. #region 继承
  242. /// <summary>
  243. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  244. /// </summary>
  245. /// <param name="navigationContext"></param>
  246. /// <param name="continuationCallback"></param>
  247. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  248. {
  249. continuationCallback(true);
  250. }
  251. /// <summary>
  252. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  253. /// </summary>
  254. /// <param name="navigationContext"></param>
  255. /// <exception cref="NotImplementedException"></exception>
  256. public async void OnNavigatedTo(NavigationContext navigationContext)
  257. {
  258. SelectCalibration = navigationContext.Parameters.GetValue<CalibrationInfo>("SelectCalibration");
  259. if (SelectCalibration != null)
  260. {
  261. Robot = _robotService.GetRobot(SelectCalibration.RobotId);
  262. if (SelectCalibration.CameraMount == Enums.CameraMount.FixedDown)
  263. {
  264. IsShowTechHome = true;
  265. }
  266. }
  267. else
  268. {
  269. IsShowTechHome = false;
  270. }
  271. }
  272. /// <summary>
  273. /// 是否允许导航到此视图模型。
  274. /// </summary>
  275. /// <param name="navigationContext"></param>
  276. /// <returns></returns>
  277. public bool IsNavigationTarget(NavigationContext navigationContext)
  278. {
  279. return true;
  280. }
  281. /// <summary>
  282. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  283. /// </summary>
  284. /// <param name="navigationContext"></param>
  285. public void OnNavigatedFrom(NavigationContext navigationContext)
  286. {
  287. }
  288. #endregion
  289. private bool _IsAllowEdit = false;
  290. public bool IsAllowEdit
  291. {
  292. get { return _IsAllowEdit; }
  293. set { SetProperty(ref _IsAllowEdit, value); }
  294. }
  295. private void LoginChange(Models.User user)
  296. {
  297. if (user.userPart == Enums.UserPart.Operator)
  298. {
  299. IsAllowEdit = false;
  300. }
  301. else
  302. {
  303. IsAllowEdit = true;
  304. }
  305. }
  306. }
  307. }