ProductParamsViewModel.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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 _IndependentCamer1ProductProcedureId;
  187. /// <summary>
  188. /// 独立移动模组相机1产品定位拍照流程ID
  189. /// </summary>
  190. public ProcedureModel IndependentCamer1ProductProcedureId
  191. {
  192. get { return _IndependentCamer1ProductProcedureId; }
  193. set { SetProperty(ref _IndependentCamer1ProductProcedureId, value);
  194. if (value != null)
  195. {
  196. SelectProduct.IndependentCamer1ProductProcedureId = value.Id;
  197. }
  198. }
  199. }
  200. private ProcedureModel _IndependentCamer2ProductProcedureId;
  201. /// <summary>
  202. /// 独立移动模组相机2产品定位拍照流程ID
  203. /// </summary>
  204. public ProcedureModel IndependentCamer2ProductProcedureId
  205. {
  206. get { return _IndependentCamer2ProductProcedureId; }
  207. set { SetProperty(ref _IndependentCamer2ProductProcedureId, value);
  208. if (value != null)
  209. {
  210. SelectProduct.IndependentCamer2ProductProcedureId = value.Id;
  211. }
  212. }
  213. }
  214. private ProcedureModel _IndependentCamer3ProductProcedureId;
  215. /// <summary>
  216. /// 独立移动模组相机3产品定位拍照流程ID
  217. /// </summary>
  218. public ProcedureModel IndependentCamer3ProductProcedureId
  219. {
  220. get { return _IndependentCamer3ProductProcedureId; }
  221. set { SetProperty(ref _IndependentCamer3ProductProcedureId, value);
  222. if (value != null)
  223. {
  224. SelectProduct.IndependentCamer3ProductProcedureId = value.Id;
  225. }
  226. }
  227. }
  228. private ProcedureModel _IndependentCamer4ProductProcedureId;
  229. /// <summary>
  230. /// 独立移动模组相机4产品定位拍照流程ID
  231. /// </summary>
  232. public ProcedureModel IndependentCamer4ProductProcedureId
  233. {
  234. get { return _IndependentCamer4ProductProcedureId; }
  235. set { SetProperty(ref _IndependentCamer4ProductProcedureId, value);
  236. if (value != null)
  237. {
  238. SelectProduct.IndependentCamer4ProductProcedureId = value.Id;
  239. }
  240. }
  241. }
  242. private ProcedureModel _IndependentCamer1AuxProcedureId;
  243. /// <summary>
  244. /// 独立移动模组相机1辅料定位拍照流程ID
  245. /// </summary>
  246. public ProcedureModel IndependentCamer1AuxProcedureId
  247. {
  248. get { return _IndependentCamer1AuxProcedureId; }
  249. set { SetProperty(ref _IndependentCamer1AuxProcedureId, value);
  250. if (value != null)
  251. {
  252. SelectProduct.IndependentCamer1AuxProcedureId = value.Id;
  253. }
  254. }
  255. }
  256. private ProcedureModel _IndependentCamer2AuxProcedureId;
  257. /// <summary>
  258. /// 独立移动模组相机2辅料定位拍照流程ID
  259. /// </summary>
  260. public ProcedureModel IndependentCamer2AuxProcedureId
  261. {
  262. get { return _IndependentCamer2AuxProcedureId; }
  263. set { SetProperty(ref _IndependentCamer2AuxProcedureId, value);
  264. if (value != null)
  265. {
  266. SelectProduct.IndependentCamer2AuxProcedureId = value.Id;
  267. }
  268. }
  269. }
  270. private ProcedureModel _IndependentCamer3AuxProcedureId;
  271. /// <summary>
  272. /// 独立移动模组相机3辅料定位拍照流程ID
  273. /// </summary>
  274. public ProcedureModel IndependentCamer3AuxProcedureId
  275. {
  276. get { return _IndependentCamer3AuxProcedureId; }
  277. set { SetProperty(ref _IndependentCamer3AuxProcedureId, value);
  278. if (value != null)
  279. {
  280. SelectProduct.IndependentCamer3AuxProcedureId = value.Id;
  281. }
  282. }
  283. }
  284. private ProcedureModel _IndependentCamer4AuxProcedureId;
  285. /// <summary>
  286. /// 独立移动模组相机4辅料定位拍照流程ID
  287. /// </summary>
  288. public ProcedureModel IndependentCamer4AuxProcedureId
  289. {
  290. get { return _IndependentCamer4AuxProcedureId; }
  291. set { SetProperty(ref _IndependentCamer4AuxProcedureId, value);
  292. if (value != null)
  293. {
  294. SelectProduct.IndependentCamer4AuxProcedureId = value.Id;
  295. }
  296. }
  297. }
  298. #endregion
  299. #region 命令
  300. private DelegateCommand _NextCommand;
  301. public DelegateCommand NextCommand =>
  302. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand));
  303. private DelegateCommand _BackCommand;
  304. public DelegateCommand BackCommand =>
  305. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
  306. private DelegateCommand _LoadedCommand;
  307. public DelegateCommand LoadedCommand =>
  308. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  309. #endregion
  310. #region 事件
  311. #endregion
  312. public ProductParamsViewModel(IRegionManager regionManager, IEventAggregator ea, IContainerProvider container, IDialogService dialogService,
  313. IConfigService configService, IRobotService robotService, ICameraService cameraService, IFeederService feederService, IPlcService plcService, IProductService productService,
  314. ISystemDatabaseService systemDatabaseService)
  315. {
  316. _regionManager = regionManager;
  317. _eventAggregator = ea;
  318. _container = container;
  319. _dialogService = dialogService;
  320. _configService = configService;
  321. _robotService = robotService;
  322. _cameraService = cameraService;
  323. _feederService = feederService;
  324. _plcService = plcService;
  325. _productService = productService;
  326. _systemDatabaseService = systemDatabaseService;
  327. //订阅权限登录事件
  328. _eventAggregator.GetEvent<UserLoginNotification>().Subscribe(LoginChange);
  329. management = _container.Resolve<Management>();
  330. }
  331. #region 方法
  332. /// <summary>
  333. /// 完成
  334. /// </summary>
  335. async void ExecuteNextCommand()
  336. {
  337. ResetId();
  338. if (MessageBox.Show(Lang.是否保存产品.Replace("[{0}]", ""), Lang.保存产品, MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  339. {
  340. var waiting = new WaitingControl();
  341. //show the dialog
  342. var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
  343. try
  344. {
  345. await _productService.UpdateProductAsync(SelectProduct);
  346. var current = _productService.GetCurrentProduct();
  347. if (current != null && current.IsLoad)
  348. {
  349. await management.WritePositionToPlcAsync();
  350. }
  351. if (DialogHost.IsDialogOpen("RootDialog"))
  352. {
  353. DialogHost.Close("RootDialog");
  354. }
  355. await task;
  356. }
  357. catch (Exception)
  358. {
  359. if (DialogHost.IsDialogOpen("RootDialog"))
  360. {
  361. DialogHost.Close("RootDialog");
  362. }
  363. await task;
  364. }
  365. }
  366. NavigationParameters param = new NavigationParameters();
  367. param.Add("SelectProduct", SelectProduct);
  368. _regionManager.RequestNavigate("ProductRegionContext", "ProductHome", param);
  369. }
  370. private void ResetId()
  371. {
  372. if(FixedUpCamera1Procedure == null)
  373. {
  374. SelectProduct.FixedUpCamera1ProcedureId = Guid.Empty;
  375. }
  376. if (FixedUpCamera2Procedure == null)
  377. {
  378. SelectProduct.FixedUpCamera2ProcedureId = Guid.Empty;
  379. }
  380. if (FixedDownCamera1Procedure == null)
  381. {
  382. SelectProduct.FixedDownCamera1ProcedureId = Guid.Empty;
  383. }
  384. if (FixedDownCamera2Procedure == null)
  385. {
  386. SelectProduct.FixedDownCamera2ProcedureId = Guid.Empty;
  387. }
  388. if (MoveDownCamera1LockPhotoProcedure == null)
  389. {
  390. SelectProduct.MoveDownCamera1LockPhotoProcedureId = Guid.Empty;
  391. }
  392. if (MoveDownCamera2LockPhotoProcedure == null)
  393. {
  394. SelectProduct.MoveDownCamera2LockPhotoProcedureId = Guid.Empty;
  395. }
  396. if (MoveDownCamer1PickPhotoProcedureId == null)
  397. {
  398. SelectProduct.MoveDownCamer1PickPhotoProcedureId = Guid.Empty;
  399. }
  400. if (MoveDownCamer2PickPhotoProcedureId == null)
  401. {
  402. SelectProduct.MoveDownCamer2PickPhotoProcedureId = Guid.Empty;
  403. }
  404. if (IndependentCamer1ProductProcedureId == null)
  405. {
  406. SelectProduct.IndependentCamer1ProductProcedureId = Guid.Empty;
  407. }
  408. if (IndependentCamer2ProductProcedureId == null)
  409. {
  410. SelectProduct.IndependentCamer2ProductProcedureId = Guid.Empty;
  411. }
  412. if (IndependentCamer3ProductProcedureId == null)
  413. {
  414. SelectProduct.IndependentCamer3ProductProcedureId = Guid.Empty;
  415. }
  416. if (IndependentCamer4ProductProcedureId == null)
  417. {
  418. SelectProduct.IndependentCamer4ProductProcedureId = Guid.Empty;
  419. }
  420. if (IndependentCamer1AuxProcedureId == null)
  421. {
  422. SelectProduct.IndependentCamer1AuxProcedureId = Guid.Empty;
  423. }
  424. if (IndependentCamer2AuxProcedureId == null)
  425. {
  426. SelectProduct.IndependentCamer2AuxProcedureId = Guid.Empty;
  427. }
  428. if (IndependentCamer3AuxProcedureId == null)
  429. {
  430. SelectProduct.IndependentCamer3AuxProcedureId = Guid.Empty;
  431. }
  432. if (IndependentCamer4AuxProcedureId == null)
  433. {
  434. SelectProduct.IndependentCamer4AuxProcedureId = Guid.Empty;
  435. }
  436. }
  437. /// <summary>
  438. /// 上一步
  439. /// </summary>
  440. void ExecuteBackCommand()
  441. {
  442. NavigationParameters param = new NavigationParameters();
  443. param.Add("SelectProduct", SelectProduct);
  444. _regionManager.RequestNavigate("ProductRegionContext", "PlcPointParams", param);
  445. }
  446. void ExecuteLoadedCommand()
  447. {
  448. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  449. {
  450. IsAllowEdit = false;
  451. }
  452. else
  453. {
  454. IsAllowEdit = true;
  455. }
  456. }
  457. #endregion
  458. #region 继承
  459. /// <summary>
  460. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  461. /// </summary>
  462. /// <param name="navigationContext"></param>
  463. /// <param name="continuationCallback"></param>
  464. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  465. {
  466. continuationCallback(true);
  467. }
  468. /// <summary>
  469. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  470. /// </summary>
  471. /// <param name="navigationContext"></param>
  472. /// <exception cref="NotImplementedException"></exception>
  473. public async void OnNavigatedTo(NavigationContext navigationContext)
  474. {
  475. try
  476. {
  477. SelectProduct = navigationContext.Parameters.GetValue<ProductModel>("SelectProduct");
  478. ProcedureModels = new ObservableCollection<ProcedureModel>();
  479. foreach (var item in SelectProduct.CameraProcedures)
  480. {
  481. foreach (var item1 in item.ProcedureModels)
  482. {
  483. ProcedureModels.Add(item1);
  484. }
  485. }
  486. if (SelectProduct.FixedUpCamera1ProcedureId!=Guid.Empty)
  487. {
  488. FixedUpCamera1Procedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.FixedUpCamera1ProcedureId);
  489. }
  490. if (SelectProduct.FixedUpCamera2ProcedureId != Guid.Empty)
  491. {
  492. FixedUpCamera2Procedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.FixedUpCamera2ProcedureId);
  493. }
  494. if (SelectProduct.FixedDownCamera1ProcedureId != Guid.Empty)
  495. {
  496. FixedDownCamera1Procedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.FixedDownCamera1ProcedureId);
  497. }
  498. if (SelectProduct.FixedDownCamera2ProcedureId != Guid.Empty)
  499. {
  500. FixedDownCamera2Procedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.FixedDownCamera2ProcedureId);
  501. }
  502. if (SelectProduct.MoveDownCamera1LockPhotoProcedureId != Guid.Empty)
  503. {
  504. MoveDownCamera1LockPhotoProcedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.MoveDownCamera1LockPhotoProcedureId);
  505. }
  506. if (SelectProduct.MoveDownCamera2LockPhotoProcedureId != Guid.Empty)
  507. {
  508. MoveDownCamera2LockPhotoProcedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.MoveDownCamera2LockPhotoProcedureId);
  509. }
  510. if (SelectProduct.MoveDownCamer1PickPhotoProcedureId != Guid.Empty)
  511. {
  512. MoveDownCamer1PickPhotoProcedureId = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.MoveDownCamer1PickPhotoProcedureId);
  513. }
  514. if (SelectProduct.MoveDownCamer2PickPhotoProcedureId != Guid.Empty)
  515. {
  516. MoveDownCamer2PickPhotoProcedureId = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.MoveDownCamer2PickPhotoProcedureId);
  517. }
  518. }
  519. catch (Exception ex)
  520. {
  521. LogHelper.WriteLogError("机器人点位界面进入时出错!", ex);
  522. }
  523. }
  524. /// <summary>
  525. /// 是否允许导航到此视图模型。
  526. /// </summary>
  527. /// <param name="navigationContext"></param>
  528. /// <returns></returns>
  529. public bool IsNavigationTarget(NavigationContext navigationContext)
  530. {
  531. return true;
  532. }
  533. /// <summary>
  534. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  535. /// </summary>
  536. /// <param name="navigationContext"></param>
  537. public void OnNavigatedFrom(NavigationContext navigationContext)
  538. {
  539. }
  540. #endregion
  541. private bool _IsAllowEdit = false;
  542. public bool IsAllowEdit
  543. {
  544. get { return _IsAllowEdit; }
  545. set { SetProperty(ref _IsAllowEdit, value); }
  546. }
  547. private void LoginChange(Models.User user)
  548. {
  549. if (user.userPart == Enums.UserPart.Operator)
  550. {
  551. IsAllowEdit = false;
  552. }
  553. else
  554. {
  555. IsAllowEdit = true;
  556. }
  557. }
  558. }
  559. }