CalibrationBasicsViewModel.cs 16 KB

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