CalibrationTeachCenterPointViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 if (Robot.Brand== Enums.RobotBrand.XYZ_Platform)
  120. {
  121. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationAuto", param);
  122. }
  123. else
  124. {
  125. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationSelectTool", param);
  126. }
  127. }
  128. /// <summary>
  129. /// 上一步
  130. /// </summary>
  131. void ExecuteBackCommand()
  132. {
  133. NavigationParameters param = new NavigationParameters();
  134. param.Add("SelectCalibration", SelectCalibration);
  135. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationEditVision", param);
  136. }
  137. /// <summary>
  138. /// 示教中心点
  139. /// </summary>
  140. async void ExecuteTechCenterCommand()
  141. {
  142. if (Robot == null || !Robot.IsConnected) return;
  143. try
  144. {
  145. var curpos = await Robot.GetRobotPosAsync();
  146. if (SelectCalibration.CenterPoint==null)
  147. {
  148. SelectCalibration.CenterPoint = new RPoint();
  149. }
  150. SelectCalibration.CenterPoint.X = curpos.X;
  151. SelectCalibration.CenterPoint.Y = curpos.Y;
  152. SelectCalibration.CenterPoint.Z = curpos.Z;
  153. SelectCalibration.CenterPoint.U = curpos.U;
  154. SelectCalibration.CenterPoint.V = curpos.V;
  155. SelectCalibration.CenterPoint.W = curpos.W;
  156. SelectCalibration.CenterPoint.Hand = curpos.Hand;
  157. SelectCalibration.CenterPoint.Local = curpos.Local;
  158. SelectCalibration.CenterPoint.Tool = curpos.Tool;
  159. LogHelper.WriteLogInfo($"相机校准-示教中心点界面示教待机点:X:{SelectCalibration.CenterPoint.X} Y:{SelectCalibration.CenterPoint.Y} Z:{SelectCalibration.CenterPoint.Z} ");
  160. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = $"成功! X:{SelectCalibration.CenterPoint.X} Y:{SelectCalibration.CenterPoint.Y} Z:{SelectCalibration.CenterPoint.Z}", Duration = 1 });
  161. }
  162. catch (Exception ex)
  163. {
  164. LogHelper.WriteLogError("相机校准-示教中心点界面示教中心点时出错!", ex);
  165. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  166. }
  167. }
  168. /// <summary>
  169. /// 示教待机点
  170. /// </summary>
  171. async void ExecuteTechHomeCommand()
  172. {
  173. if (Robot == null || !Robot.IsConnected) return;
  174. try
  175. {
  176. var curpos = await Robot.GetRobotPosAsync();
  177. if (SelectCalibration.HomePoint == null)
  178. {
  179. SelectCalibration.HomePoint = new RPoint();
  180. }
  181. SelectCalibration.HomePoint.X = curpos.X;
  182. SelectCalibration.HomePoint.Y = curpos.Y;
  183. SelectCalibration.HomePoint.Z = curpos.Z;
  184. SelectCalibration.HomePoint.U = curpos.U;
  185. SelectCalibration.HomePoint.V = curpos.V;
  186. SelectCalibration.HomePoint.W = curpos.W;
  187. SelectCalibration.HomePoint.Hand = curpos.Hand;
  188. SelectCalibration.HomePoint.Local = curpos.Local;
  189. SelectCalibration.HomePoint.Tool = curpos.Tool;
  190. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "成功!", Duration = 1 });
  191. }
  192. catch (Exception ex)
  193. {
  194. LogHelper.WriteLogError("相机校准-示教中心点界面示教待机点时出错!", ex);
  195. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  196. }
  197. }
  198. /// <summary>
  199. /// 夹爪打开/关闭
  200. /// </summary>
  201. /// <param name="obj"></param>
  202. async void ExecuteBlowOrInhalControlCommand(object obj)
  203. {
  204. try
  205. {
  206. await Robot.CalibOutIOAsync(!(bool)obj);
  207. }
  208. catch (Exception ex)
  209. {
  210. LogHelper.WriteLogError("相机校准-夹爪打开/关闭点时出错!", ex);
  211. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  212. }
  213. }
  214. /// <summary>
  215. /// 示教P0
  216. /// </summary>
  217. async void ExecuteTechMarkPointCommand()
  218. {
  219. if (Robot == null || !Robot.IsConnected) return;
  220. try
  221. {
  222. var curpos = await Robot.GetRobotPosAsync();
  223. if (SelectCalibration.MarkPoint == null)
  224. {
  225. SelectCalibration.MarkPoint = new RPoint();
  226. }
  227. SelectCalibration.MarkPoint.X = curpos.X;
  228. SelectCalibration.MarkPoint.Y = curpos.Y;
  229. SelectCalibration.MarkPoint.Z = curpos.Z;
  230. SelectCalibration.MarkPoint.U = curpos.U;
  231. SelectCalibration.MarkPoint.V = curpos.V;
  232. SelectCalibration.MarkPoint.W = curpos.W;
  233. SelectCalibration.MarkPoint.Hand = curpos.Hand;
  234. SelectCalibration.MarkPoint.Local = curpos.Local;
  235. SelectCalibration.MarkPoint.Tool = curpos.Tool;
  236. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = "成功!", Duration = 1 });
  237. }
  238. catch (Exception ex)
  239. {
  240. LogHelper.WriteLogError("相机校准-示教中心点界面示教P0时出错!", ex);
  241. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = ex.Message, Duration = 1 });
  242. }
  243. }
  244. #endregion
  245. #region 继承
  246. /// <summary>
  247. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  248. /// </summary>
  249. /// <param name="navigationContext"></param>
  250. /// <param name="continuationCallback"></param>
  251. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  252. {
  253. continuationCallback(true);
  254. }
  255. /// <summary>
  256. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  257. /// </summary>
  258. /// <param name="navigationContext"></param>
  259. /// <exception cref="NotImplementedException"></exception>
  260. public async void OnNavigatedTo(NavigationContext navigationContext)
  261. {
  262. SelectCalibration = navigationContext.Parameters.GetValue<CalibrationInfo>("SelectCalibration");
  263. if (SelectCalibration != null)
  264. {
  265. Robot = _robotService.GetRobot(SelectCalibration.RobotId);
  266. if (SelectCalibration.CameraMount == Enums.CameraMount.FixedDown)
  267. {
  268. IsShowTechHome = true;
  269. }
  270. }
  271. else
  272. {
  273. IsShowTechHome = false;
  274. }
  275. }
  276. /// <summary>
  277. /// 是否允许导航到此视图模型。
  278. /// </summary>
  279. /// <param name="navigationContext"></param>
  280. /// <returns></returns>
  281. public bool IsNavigationTarget(NavigationContext navigationContext)
  282. {
  283. return true;
  284. }
  285. /// <summary>
  286. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  287. /// </summary>
  288. /// <param name="navigationContext"></param>
  289. public void OnNavigatedFrom(NavigationContext navigationContext)
  290. {
  291. }
  292. #endregion
  293. private bool _IsAllowEdit = false;
  294. public bool IsAllowEdit
  295. {
  296. get { return _IsAllowEdit; }
  297. set { SetProperty(ref _IsAllowEdit, value); }
  298. }
  299. private void LoginChange(Models.User user)
  300. {
  301. if (user.userPart == Enums.UserPart.Operator)
  302. {
  303. IsAllowEdit = false;
  304. }
  305. else
  306. {
  307. IsAllowEdit = true;
  308. }
  309. }
  310. }
  311. }