ProductParamsViewModel.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. using MaterialDesignThemes.Wpf;
  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.Threading.Tasks;
  13. using System.Windows;
  14. using Team.FFFeederService.Interfaces;
  15. using TeamAAS_VP.Controls;
  16. using TeamAAS_VP.Core;
  17. using TeamAAS_VP.Events;
  18. using TeamAAS_VP.Interfaces;
  19. using TeamAAS_VP.Models;
  20. using TeamAAS_VP.Resources.Languages;
  21. using TeamAAS_VP.Services;
  22. namespace TeamAAS_VP.ViewModels.Product
  23. {
  24. public class ProductParamsViewModel : BindableBase, IConfirmNavigationRequest
  25. {
  26. IRegionManager _regionManager;
  27. IEventAggregator _eventAggregator;
  28. IContainerProvider _container;
  29. IDialogService _dialogService;
  30. IConfigService _configService;
  31. IRobotService _robotService;
  32. ICameraService _cameraService;
  33. IFeederService _feederService;
  34. IPlcService _plcService;
  35. IProductService _productService;
  36. ISystemDatabaseService _systemDatabaseService;
  37. #region 属性
  38. private ProductModel _SelectProduct;
  39. /// <summary>
  40. /// 选中的产品
  41. /// </summary>
  42. public ProductModel SelectProduct
  43. {
  44. get { return _SelectProduct; }
  45. set { SetProperty(ref _SelectProduct, value); }
  46. }
  47. private Management _management;
  48. public Management management
  49. {
  50. get { return _management; }
  51. set { SetProperty(ref _management, value); }
  52. }
  53. private ObservableCollection<ProcedureModel> _ProcedureModels;
  54. /// <summary>
  55. /// 流程集合
  56. /// </summary>
  57. public ObservableCollection<ProcedureModel> ProcedureModels
  58. {
  59. get { return _ProcedureModels; }
  60. set { SetProperty(ref _ProcedureModels, value); }
  61. }
  62. private ProcedureModel _FixedUpCamera1Procedure;
  63. /// <summary>
  64. /// 固定向上相机1视觉流程
  65. /// </summary>
  66. public ProcedureModel FixedUpCamera1Procedure
  67. {
  68. get { return _FixedUpCamera1Procedure; }
  69. set
  70. {
  71. SetProperty(ref _FixedUpCamera1Procedure, value);
  72. if (value != null)
  73. {
  74. SelectProduct.FixedUpCamera1ProcedureId = value.Id;
  75. }
  76. }
  77. }
  78. private ProcedureModel _FixedUpCamera2Procedure;
  79. /// <summary>
  80. /// 固定向上相机2视觉流程
  81. /// </summary>
  82. public ProcedureModel FixedUpCamera2Procedure
  83. {
  84. get { return _FixedUpCamera2Procedure; }
  85. set
  86. {
  87. SetProperty(ref _FixedUpCamera2Procedure, value);
  88. if (value != null)
  89. {
  90. SelectProduct.FixedUpCamera2ProcedureId = value.Id;
  91. }
  92. }
  93. }
  94. private ProcedureModel _FixedDownCamera1Procedure;
  95. /// <summary>
  96. /// 固定向下相机1取料流程ID
  97. /// </summary>
  98. public ProcedureModel FixedDownCamera1Procedure
  99. {
  100. get { return _FixedDownCamera1Procedure; }
  101. set { SetProperty(ref _FixedDownCamera1Procedure, value);
  102. if (value != null)
  103. {
  104. SelectProduct.FixedDownCamera1ProcedureId = value.Id;
  105. }
  106. }
  107. }
  108. private ProcedureModel _FixedDownCamera2Procedure;
  109. /// <summary>
  110. /// 固定向下相机2取料流程ID
  111. /// </summary>
  112. public ProcedureModel FixedDownCamera2Procedure
  113. {
  114. get { return _FixedDownCamera2Procedure; }
  115. set
  116. {
  117. SetProperty(ref _FixedDownCamera2Procedure, value);
  118. if (value != null)
  119. {
  120. SelectProduct.FixedDownCamera2ProcedureId = value.Id;
  121. }
  122. }
  123. }
  124. private ProcedureModel _MoveDownCamera1LockPhotoProcedure;
  125. /// <summary>
  126. /// 移动向下相机1锁付拍照流程ID
  127. /// </summary>
  128. public ProcedureModel MoveDownCamera1LockPhotoProcedure
  129. {
  130. get { return _MoveDownCamera1LockPhotoProcedure; }
  131. set { SetProperty(ref _MoveDownCamera1LockPhotoProcedure, value);
  132. if (value != null)
  133. {
  134. SelectProduct.MoveDownCamera1LockPhotoProcedureId = value.Id;
  135. }
  136. }
  137. }
  138. private ProcedureModel _MoveDownCamera2LockPhotoProcedure;
  139. /// <summary>
  140. /// 移动向下相机2锁付拍照流程ID
  141. /// </summary>
  142. public ProcedureModel MoveDownCamera2LockPhotoProcedure
  143. {
  144. get { return _MoveDownCamera2LockPhotoProcedure; }
  145. set
  146. {
  147. SetProperty(ref _MoveDownCamera2LockPhotoProcedure, value);
  148. if (value != null)
  149. {
  150. SelectProduct.MoveDownCamera2LockPhotoProcedureId = value.Id;
  151. }
  152. }
  153. }
  154. private ProcedureModel _MoveDownCamer1PickPhotoProcedureId;
  155. /// <summary>
  156. /// 移动向下相机1取料拍照流程ID
  157. /// </summary>
  158. public ProcedureModel MoveDownCamer1PickPhotoProcedureId
  159. {
  160. get { return _MoveDownCamer1PickPhotoProcedureId; }
  161. set
  162. {
  163. SetProperty(ref _MoveDownCamer1PickPhotoProcedureId, value);
  164. if (value != null)
  165. {
  166. SelectProduct.MoveDownCamer1PickPhotoProcedureId = value.Id;
  167. }
  168. }
  169. }
  170. private ProcedureModel _MoveDownCamer2PickPhotoProcedureId;
  171. /// <summary>
  172. /// 移动向下相机2取料拍照流程ID
  173. /// </summary>
  174. public ProcedureModel MoveDownCamer2PickPhotoProcedureId
  175. {
  176. get { return _MoveDownCamer2PickPhotoProcedureId; }
  177. set
  178. {
  179. SetProperty(ref _MoveDownCamer2PickPhotoProcedureId, value);
  180. if (value != null)
  181. {
  182. SelectProduct.MoveDownCamer2PickPhotoProcedureId = value.Id;
  183. }
  184. }
  185. }
  186. private ProcedureModel _MoveDownCamer1CheckProcedureId;
  187. /// <summary>
  188. /// 移动向下相机1检测拍照流程ID
  189. /// </summary>
  190. public ProcedureModel MoveDownCamer1CheckProcedureId
  191. {
  192. get { return _MoveDownCamer1CheckProcedureId; }
  193. set
  194. {
  195. SetProperty(ref _MoveDownCamer1CheckProcedureId, value);
  196. if (value != null)
  197. {
  198. SelectProduct.MoveDownCamer1CheckProcedureId = value.Id;
  199. }
  200. }
  201. }
  202. private ProcedureModel _MoveDownCamer2CheckProcedureId;
  203. /// <summary>
  204. /// 移动向下相机2检测拍照流程ID
  205. /// </summary>
  206. public ProcedureModel MoveDownCamer2CheckProcedureId
  207. {
  208. get { return _MoveDownCamer2CheckProcedureId; }
  209. set
  210. {
  211. SetProperty(ref _MoveDownCamer2CheckProcedureId, value);
  212. if (value != null)
  213. {
  214. SelectProduct.MoveDownCamer2CheckProcedureId = value.Id;
  215. }
  216. }
  217. }
  218. #endregion
  219. #region 命令
  220. private DelegateCommand _NextCommand;
  221. public DelegateCommand NextCommand =>
  222. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand));
  223. private DelegateCommand _BackCommand;
  224. public DelegateCommand BackCommand =>
  225. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
  226. private DelegateCommand _LoadedCommand;
  227. public DelegateCommand LoadedCommand =>
  228. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  229. #endregion
  230. #region 事件
  231. #endregion
  232. public ProductParamsViewModel(IRegionManager regionManager, IEventAggregator ea, IContainerProvider container, IDialogService dialogService,
  233. IConfigService configService, IRobotService robotService, ICameraService cameraService, IFeederService feederService, IPlcService plcService, IProductService productService,
  234. ISystemDatabaseService systemDatabaseService)
  235. {
  236. _regionManager = regionManager;
  237. _eventAggregator = ea;
  238. _container = container;
  239. _dialogService = dialogService;
  240. _configService = configService;
  241. _robotService = robotService;
  242. _cameraService = cameraService;
  243. _feederService = feederService;
  244. _plcService = plcService;
  245. _productService = productService;
  246. _systemDatabaseService = systemDatabaseService;
  247. //订阅权限登录事件
  248. _eventAggregator.GetEvent<UserLoginNotification>().Subscribe(LoginChange);
  249. management = _container.Resolve<Management>();
  250. }
  251. #region 方法
  252. /// <summary>
  253. /// 完成
  254. /// </summary>
  255. async void ExecuteNextCommand()
  256. {
  257. ResetId();
  258. if (MessageBox.Show(Lang.是否保存产品.Replace("[{0}]", ""), Lang.保存产品, MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  259. {
  260. var waiting = new WaitingControl();
  261. //show the dialog
  262. var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
  263. try
  264. {
  265. await _productService.UpdateProductAsync(SelectProduct);
  266. var current = _productService.GetCurrentProduct();
  267. if (current != null && current.IsLoad)
  268. {
  269. await management.WritePositionToPlcAsync();
  270. }
  271. if (DialogHost.IsDialogOpen("RootDialog"))
  272. {
  273. DialogHost.Close("RootDialog");
  274. }
  275. await task;
  276. }
  277. catch (Exception)
  278. {
  279. if (DialogHost.IsDialogOpen("RootDialog"))
  280. {
  281. DialogHost.Close("RootDialog");
  282. }
  283. await task;
  284. }
  285. }
  286. NavigationParameters param = new NavigationParameters();
  287. param.Add("SelectProduct", SelectProduct);
  288. _regionManager.RequestNavigate("ProductRegionContext", "ProductHome", param);
  289. }
  290. private void ResetId()
  291. {
  292. if (FixedUpCamera1Procedure == null)
  293. {
  294. SelectProduct.FixedUpCamera1ProcedureId = Guid.Empty;
  295. }
  296. if (FixedUpCamera2Procedure == null)
  297. {
  298. SelectProduct.FixedUpCamera2ProcedureId = Guid.Empty;
  299. }
  300. if (FixedDownCamera1Procedure == null)
  301. {
  302. SelectProduct.FixedDownCamera1ProcedureId = Guid.Empty;
  303. }
  304. if (FixedDownCamera2Procedure == null)
  305. {
  306. SelectProduct.FixedDownCamera2ProcedureId = Guid.Empty;
  307. }
  308. if (MoveDownCamera1LockPhotoProcedure == null)
  309. {
  310. SelectProduct.MoveDownCamera1LockPhotoProcedureId = Guid.Empty;
  311. }
  312. if (MoveDownCamera2LockPhotoProcedure == null)
  313. {
  314. SelectProduct.MoveDownCamera2LockPhotoProcedureId = Guid.Empty;
  315. }
  316. if (MoveDownCamer1PickPhotoProcedureId == null)
  317. {
  318. SelectProduct.MoveDownCamer1PickPhotoProcedureId = Guid.Empty;
  319. }
  320. if (MoveDownCamer2PickPhotoProcedureId == null)
  321. {
  322. SelectProduct.MoveDownCamer2PickPhotoProcedureId = Guid.Empty;
  323. }
  324. if (MoveDownCamer1CheckProcedureId == null)
  325. {
  326. SelectProduct.MoveDownCamer1CheckProcedureId = Guid.Empty;
  327. }
  328. if (MoveDownCamer2CheckProcedureId == null)
  329. {
  330. SelectProduct.MoveDownCamer2CheckProcedureId = Guid.Empty;
  331. }
  332. }
  333. /// <summary>
  334. /// 上一步
  335. /// </summary>
  336. void ExecuteBackCommand()
  337. {
  338. NavigationParameters param = new NavigationParameters();
  339. param.Add("SelectProduct", SelectProduct);
  340. _regionManager.RequestNavigate("ProductRegionContext", "PlcPointParams", param);
  341. }
  342. void ExecuteLoadedCommand()
  343. {
  344. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  345. {
  346. IsAllowEdit = false;
  347. }
  348. else
  349. {
  350. IsAllowEdit = true;
  351. }
  352. }
  353. #endregion
  354. #region 继承
  355. /// <summary>
  356. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  357. /// </summary>
  358. /// <param name="navigationContext"></param>
  359. /// <param name="continuationCallback"></param>
  360. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  361. {
  362. continuationCallback(true);
  363. }
  364. /// <summary>
  365. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  366. /// </summary>
  367. /// <param name="navigationContext"></param>
  368. /// <exception cref="NotImplementedException"></exception>
  369. public async void OnNavigatedTo(NavigationContext navigationContext)
  370. {
  371. try
  372. {
  373. SelectProduct = navigationContext.Parameters.GetValue<ProductModel>("SelectProduct");
  374. ProcedureModels = new ObservableCollection<ProcedureModel>();
  375. foreach (var item in SelectProduct.CameraProcedures)
  376. {
  377. foreach (var item1 in item.ProcedureModels)
  378. {
  379. ProcedureModels.Add(item1);
  380. }
  381. }
  382. if (SelectProduct.FixedUpCamera1ProcedureId!=Guid.Empty)
  383. {
  384. FixedUpCamera1Procedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.FixedUpCamera1ProcedureId);
  385. }
  386. if (SelectProduct.FixedUpCamera2ProcedureId != Guid.Empty)
  387. {
  388. FixedUpCamera2Procedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.FixedUpCamera2ProcedureId);
  389. }
  390. if (SelectProduct.FixedDownCamera1ProcedureId != Guid.Empty)
  391. {
  392. FixedDownCamera1Procedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.FixedDownCamera1ProcedureId);
  393. }
  394. if (SelectProduct.FixedDownCamera2ProcedureId != Guid.Empty)
  395. {
  396. FixedDownCamera2Procedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.FixedDownCamera2ProcedureId);
  397. }
  398. if (SelectProduct.MoveDownCamera1LockPhotoProcedureId != Guid.Empty)
  399. {
  400. MoveDownCamera1LockPhotoProcedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.MoveDownCamera1LockPhotoProcedureId);
  401. }
  402. if (SelectProduct.MoveDownCamera2LockPhotoProcedureId != Guid.Empty)
  403. {
  404. MoveDownCamera2LockPhotoProcedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.MoveDownCamera2LockPhotoProcedureId);
  405. }
  406. if (SelectProduct.MoveDownCamer1PickPhotoProcedureId != Guid.Empty)
  407. {
  408. MoveDownCamer1PickPhotoProcedureId = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.MoveDownCamer1PickPhotoProcedureId);
  409. }
  410. if (SelectProduct.MoveDownCamer2PickPhotoProcedureId != Guid.Empty)
  411. {
  412. MoveDownCamer2PickPhotoProcedureId = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.MoveDownCamer2PickPhotoProcedureId);
  413. }
  414. if (SelectProduct.MoveDownCamer1CheckProcedureId != Guid.Empty)
  415. {
  416. MoveDownCamer1CheckProcedureId = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.MoveDownCamer1CheckProcedureId);
  417. }
  418. if (SelectProduct.MoveDownCamer2CheckProcedureId != Guid.Empty)
  419. {
  420. MoveDownCamer2CheckProcedureId = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.MoveDownCamer2CheckProcedureId);
  421. }
  422. }
  423. catch (Exception ex)
  424. {
  425. LogHelper.WriteLogError("机器人点位界面进入时出错!", ex);
  426. }
  427. }
  428. /// <summary>
  429. /// 是否允许导航到此视图模型。
  430. /// </summary>
  431. /// <param name="navigationContext"></param>
  432. /// <returns></returns>
  433. public bool IsNavigationTarget(NavigationContext navigationContext)
  434. {
  435. return true;
  436. }
  437. /// <summary>
  438. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  439. /// </summary>
  440. /// <param name="navigationContext"></param>
  441. public void OnNavigatedFrom(NavigationContext navigationContext)
  442. {
  443. }
  444. #endregion
  445. private bool _IsAllowEdit = false;
  446. public bool IsAllowEdit
  447. {
  448. get { return _IsAllowEdit; }
  449. set { SetProperty(ref _IsAllowEdit, value); }
  450. }
  451. private void LoginChange(Models.User user)
  452. {
  453. if (user.userPart == Enums.UserPart.Operator)
  454. {
  455. IsAllowEdit = false;
  456. }
  457. else
  458. {
  459. IsAllowEdit = true;
  460. }
  461. }
  462. }
  463. }