CalibrationBasicsViewModel.cs 16 KB

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