CalibrationBasicsViewModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 System;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.Linq;
  11. using System.Windows;
  12. using TeamAAS_VP.Events;
  13. using TeamAAS_VP.Core;
  14. using TeamAAS_VP.Interfaces;
  15. using TeamAAS_VP.Models;
  16. using TeamAAS_VP;
  17. using System.ComponentModel;
  18. using TeamAAS_VP.Enums;
  19. using TeamAAS_VP.Models.Calibration;
  20. using Team.FFFeederService.Interfaces;
  21. namespace TeamAAS_VP.ViewModels.Calibration
  22. {
  23. public class CalibrationBasicsViewModel : BindableBase, IDataErrorInfo, IConfirmNavigationRequest
  24. {
  25. IRegionManager _regionManager;
  26. IRegionNavigationService _regionNavigationService;
  27. IContainerProvider _container;
  28. IDialogService _dialogService;
  29. IEventAggregator _eventAggregator;
  30. IConfigService _configService;
  31. IRobotService _robotService;
  32. ICameraService _cameraService;
  33. IFeederService _feederService;
  34. ISystemDatabaseService _systemDatabaseService;
  35. #region 属性
  36. private ObservableCollection<CameraInfo> _Cameras;
  37. public ObservableCollection<CameraInfo> Cameras
  38. {
  39. get { return _Cameras; }
  40. set { SetProperty(ref _Cameras, value); }
  41. }
  42. private ObservableCollection<RobotInfo> _Robots;
  43. public ObservableCollection<RobotInfo> Robots
  44. {
  45. get { return _Robots; }
  46. set
  47. {
  48. SetProperty(ref _Robots, value);
  49. RaisePropertyChanged(nameof(Error));
  50. }
  51. }
  52. private ObservableCollection<FeederInfo> _Feeders;
  53. public ObservableCollection<FeederInfo> Feeders
  54. {
  55. get { return _Feeders; }
  56. set { SetProperty(ref _Feeders, value);
  57. }
  58. }
  59. private CalibrationInfo _SelectCalibration;
  60. public CalibrationInfo SelectCalibration
  61. {
  62. get { return _SelectCalibration; }
  63. set
  64. {
  65. SetProperty(ref _SelectCalibration, value);
  66. if (value != null)
  67. {
  68. Robots = new ObservableCollection<RobotInfo>(_configService.GetAllRobots());
  69. Feeders = new ObservableCollection<FeederInfo>(_configService.GetAllFeeders());
  70. SelectRobot = Robots.FirstOrDefault(r => r.Id == value.RobotId);
  71. SelectFeeder = Feeders.FirstOrDefault(r => r.Id == value.FeederId);
  72. }
  73. }
  74. }
  75. private RobotInfo _SelectRobot;
  76. public RobotInfo SelectRobot
  77. {
  78. get { return _SelectRobot; }
  79. set
  80. {
  81. SetProperty(ref _SelectRobot, value);
  82. if (value != null && SelectCalibration != null)
  83. {
  84. SelectCalibration.RobotId = value.Id;
  85. }
  86. }
  87. }
  88. private FeederInfo _SelectFeeder;
  89. public FeederInfo SelectFeeder
  90. {
  91. get { return _SelectFeeder; }
  92. set
  93. {
  94. SetProperty(ref _SelectFeeder, value);
  95. if (value != null && SelectCalibration != null)
  96. {
  97. SelectCalibration.FeederId = value.Id;
  98. }
  99. }
  100. }
  101. private CameraInfo _SelectCamera;
  102. public CameraInfo SelectCamera
  103. {
  104. get { return _SelectCamera; }
  105. set
  106. {
  107. SetProperty(ref _SelectCamera, value);
  108. if (value != null && SelectCalibration != null)
  109. {
  110. SelectCalibration.CameraID = value.Id;
  111. SelectCalibration.CameraName = value.CameraName;
  112. }
  113. else
  114. {
  115. SelectCalibration.CameraID = Guid.Empty;
  116. SelectCalibration.CameraName = "";
  117. }
  118. }
  119. }
  120. private Management _management;
  121. public Management management
  122. {
  123. get { return _management; }
  124. set { SetProperty(ref _management, value); }
  125. }
  126. #endregion
  127. #region 命令
  128. private DelegateCommand _LoadedCommand;
  129. public DelegateCommand LoadedCommand =>
  130. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  131. private DelegateCommand _NextCommand;
  132. public DelegateCommand NextCommand =>
  133. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand));
  134. private DelegateCommand _BackCommand;
  135. public DelegateCommand BackCommand =>
  136. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
  137. private DelegateCommand _SelectCameraMountChangedCommand;
  138. public DelegateCommand SelectCameraMountChangedCommand =>
  139. _SelectCameraMountChangedCommand ?? (_SelectCameraMountChangedCommand = new DelegateCommand(ExecuteSelectCameraMountChangedCommand));
  140. #endregion
  141. #region 事件
  142. #endregion
  143. public CalibrationBasicsViewModel(IRegionManager regionManager, IEventAggregator ea, IRegionNavigationService regionNavigationService, IContainerProvider container, IDialogService dialogService,
  144. IConfigService configService,IRobotService robotService,IFeederService feederService,ICameraService cameraService, ISystemDatabaseService systemDatabaseService)
  145. {
  146. _regionManager = regionManager;
  147. _regionNavigationService = regionNavigationService;
  148. _container = container;
  149. _dialogService = dialogService;
  150. _eventAggregator = ea;
  151. _configService = configService;
  152. _robotService = robotService;
  153. _feederService = feederService;
  154. _cameraService = cameraService;
  155. _systemDatabaseService = systemDatabaseService;
  156. management = _container.Resolve<Management>();
  157. }
  158. #region 方法
  159. void ExecuteLoadedCommand()
  160. {
  161. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  162. {
  163. IsAllowEdit = false;
  164. }
  165. else
  166. {
  167. IsAllowEdit = true;
  168. }
  169. }
  170. /// <summary>
  171. /// 下一步
  172. /// </summary>
  173. async void ExecuteNextCommand()
  174. {
  175. try
  176. {
  177. if (SelectCalibration != null)
  178. {
  179. if (SelectCalibration.Pick != null && (SelectCalibration.Pick.Inhal > -1 || SelectCalibration.Pick.Blow > -1))
  180. {
  181. IRobot Robot = _robotService.GetRobot(SelectCalibration.RobotId);
  182. if (Robot.IsConnected)
  183. {
  184. //发送校准的参数
  185. await Robot.CalibParameAsync(SelectCalibration.Pick, SelectCalibration.Speed, SelectCalibration.Accel, SelectCalibration.Power, SelectCalibration.WaitSuction, SelectCalibration.WaitBlow);
  186. await Robot.CalibOutIOAsync(true);
  187. }
  188. }
  189. if (SelectCalibration.FeederId != Guid.Empty)
  190. {
  191. var feeder = _feederService.GetFeeder(SelectCalibration.FeederId);
  192. if (feeder.IsConnected)
  193. {
  194. await feeder.OpenLightAsync();
  195. }
  196. }
  197. }
  198. }
  199. catch (Exception ex)
  200. {
  201. LogHelper.WriteLogError("相机校准参数配置点击下一步时出错!", ex);
  202. MessageBox.Show(ex.Message);
  203. }
  204. NavigationParameters param = new NavigationParameters();
  205. param.Add("SelectCalibration", SelectCalibration);
  206. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationCameraParams", param);
  207. }
  208. /// <summary>
  209. /// 上一步
  210. /// </summary>
  211. void ExecuteBackCommand()
  212. {
  213. _regionManager.RequestNavigate("CalibrationRegionContext", "CalibrationHome");
  214. }
  215. void ExecuteSelectCameraMountChangedCommand()
  216. {
  217. ValidateProperty(nameof(SelectCalibration));
  218. RaisePropertyChanged(nameof(Error));
  219. }
  220. // 手动触发验证
  221. public string ValidateProperty(string propertyName)
  222. {
  223. return this[propertyName]; // 主动调用索引器
  224. }
  225. #endregion
  226. #region 继承
  227. /// <summary>
  228. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  229. /// </summary>
  230. /// <param name="navigationContext"></param>
  231. /// <param name="continuationCallback"></param>
  232. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  233. {
  234. continuationCallback(true);
  235. }
  236. /// <summary>
  237. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  238. /// </summary>
  239. /// <param name="navigationContext"></param>
  240. /// <exception cref="NotImplementedException"></exception>
  241. public async void OnNavigatedTo(NavigationContext navigationContext)
  242. {
  243. SelectCalibration = navigationContext.Parameters.GetValue<CalibrationInfo>("SelectCalibration");
  244. Robots = new ObservableCollection<RobotInfo>(_configService.GetAllRobots());
  245. Feeders = new ObservableCollection<FeederInfo>(_configService.GetAllFeeders());
  246. Cameras = new ObservableCollection<CameraInfo>(_configService.GetAllCameras());
  247. if (SelectCalibration.CameraID != Guid.Empty)
  248. {
  249. SelectCamera = _configService.GetAllCameras().FirstOrDefault(c => c.Id == SelectCalibration.CameraID);
  250. }
  251. if (SelectCalibration.RobotId != Guid.Empty)
  252. {
  253. IRobot Robot = _robotService.GetRobot(SelectCalibration.RobotId);
  254. if (Robot!=null)
  255. {
  256. Robot.EnterDebugMode = true;
  257. }
  258. }
  259. }
  260. /// <summary>
  261. /// 是否允许导航到此视图模型。
  262. /// </summary>
  263. /// <param name="navigationContext"></param>
  264. /// <returns></returns>
  265. public bool IsNavigationTarget(NavigationContext navigationContext)
  266. {
  267. return true;
  268. }
  269. /// <summary>
  270. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  271. /// </summary>
  272. /// <param name="navigationContext"></param>
  273. public void OnNavigatedFrom(NavigationContext navigationContext)
  274. {
  275. try
  276. {
  277. SelectCalibration = _container.Resolve<CalibrationHomeViewModel>().SelectCalibration;
  278. if (SelectCalibration != null)
  279. {
  280. if (SelectCalibration.RobotId!=Guid.Empty)
  281. {
  282. IRobot Robot = _robotService.GetRobot(SelectCalibration.RobotId);
  283. Robot.EnterDebugMode = false;
  284. }
  285. }
  286. }
  287. catch (Exception)
  288. {
  289. }
  290. }
  291. #endregion
  292. private bool _IsAllowEdit = false;
  293. public bool IsAllowEdit
  294. {
  295. get { return _IsAllowEdit; }
  296. set { SetProperty(ref _IsAllowEdit, value); }
  297. }
  298. public string Error{
  299. get
  300. {
  301. //var errors = new List<string>()
  302. //{
  303. // this[nameof(SelectCalibration)],
  304. // this[nameof(SelectRobot)],
  305. //};
  306. //return string.Join(Environment.NewLine, errors.Where(e => !string.IsNullOrEmpty(e)));
  307. string err1 = this[nameof(SelectCalibration)];
  308. string err2 = this[nameof(SelectRobot)];
  309. if (!string.IsNullOrEmpty(err1))
  310. {
  311. return err1;
  312. }
  313. if (!string.IsNullOrEmpty(err2))
  314. {
  315. return err2;
  316. }
  317. return string.Empty;
  318. }
  319. }
  320. public string this[string columnName]
  321. {
  322. get
  323. {
  324. string result = string.Empty;
  325. switch (columnName)
  326. {
  327. case nameof(SelectCalibration):
  328. if (SelectCalibration != null && SelectRobot != null)
  329. {
  330. switch (SelectRobot.RobotBrand)
  331. {
  332. case RobotBrand.Default:
  333. break;
  334. case RobotBrand.EPSON:
  335. case RobotBrand.FANUC:
  336. case RobotBrand.Schneider:
  337. if (SelectCalibration.CameraMount == CameraMount.MobileDown_XYPlatform)
  338. {
  339. result = "Unsupported camera installation method!";
  340. }
  341. break;
  342. case RobotBrand.XYZ_Platform:
  343. case RobotBrand.XYZU_Platform:
  344. if (SelectCalibration.CameraMount == CameraMount.MobileJ2 || SelectCalibration.CameraMount == CameraMount.MobileJ4)
  345. {
  346. result = "Unsupported camera installation method!";
  347. }
  348. break;
  349. default:
  350. break;
  351. }
  352. }
  353. break;
  354. case nameof(SelectRobot):
  355. if (SelectCalibration != null && SelectRobot != null)
  356. {
  357. switch (SelectRobot.RobotBrand)
  358. {
  359. case RobotBrand.Default:
  360. break;
  361. case RobotBrand.EPSON:
  362. case RobotBrand.FANUC:
  363. case RobotBrand.Schneider:
  364. if (SelectCalibration.CameraMount == CameraMount.MobileDown_XYPlatform)
  365. {
  366. result = "不支持的相机校准方式!";
  367. }
  368. break;
  369. case RobotBrand.XYZ_Platform:
  370. case RobotBrand.XYZU_Platform:
  371. if (SelectCalibration.CameraMount == CameraMount.MobileJ2 || SelectCalibration.CameraMount == CameraMount.MobileJ4)
  372. {
  373. result = "不支持的相机校准方式!";
  374. }
  375. break;
  376. default:
  377. break;
  378. }
  379. }
  380. break;
  381. //case nameof(SelectFeeder):
  382. // if (SelectFeeder == null)
  383. // {
  384. // result = "请选择喂料器";
  385. // }
  386. // break;
  387. //case nameof(SelectCamera):
  388. // if (SelectCamera == null)
  389. // {
  390. // result = "请选择相机";
  391. // }
  392. // break;
  393. }
  394. return result;
  395. }
  396. }
  397. private void LoginChange(Models.User user)
  398. {
  399. if (user.userPart == Enums.UserPart.Operator)
  400. {
  401. IsAllowEdit = false;
  402. }
  403. else
  404. {
  405. IsAllowEdit = true;
  406. }
  407. }
  408. }
  409. }