ProductParamsViewModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. #endregion
  187. #region 命令
  188. private DelegateCommand _NextCommand;
  189. public DelegateCommand NextCommand =>
  190. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand));
  191. private DelegateCommand _BackCommand;
  192. public DelegateCommand BackCommand =>
  193. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
  194. private DelegateCommand _LoadedCommand;
  195. public DelegateCommand LoadedCommand =>
  196. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  197. #endregion
  198. #region 事件
  199. #endregion
  200. public ProductParamsViewModel(IRegionManager regionManager, IEventAggregator ea, IContainerProvider container, IDialogService dialogService,
  201. IConfigService configService, IRobotService robotService, ICameraService cameraService, IFeederService feederService, IPlcService plcService, IProductService productService,
  202. ISystemDatabaseService systemDatabaseService)
  203. {
  204. _regionManager = regionManager;
  205. _eventAggregator = ea;
  206. _container = container;
  207. _dialogService = dialogService;
  208. _configService = configService;
  209. _robotService = robotService;
  210. _cameraService = cameraService;
  211. _feederService = feederService;
  212. _plcService = plcService;
  213. _productService = productService;
  214. _systemDatabaseService = systemDatabaseService;
  215. //订阅权限登录事件
  216. _eventAggregator.GetEvent<UserLoginNotification>().Subscribe(LoginChange);
  217. management = _container.Resolve<Management>();
  218. }
  219. #region 方法
  220. /// <summary>
  221. /// 完成
  222. /// </summary>
  223. async void ExecuteNextCommand()
  224. {
  225. ResetId();
  226. if (MessageBox.Show(Lang.是否保存产品.Replace("[{0}]", ""), Lang.保存产品, MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  227. {
  228. var waiting = new WaitingControl();
  229. //show the dialog
  230. var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
  231. try
  232. {
  233. await _productService.UpdateProductAsync(SelectProduct);
  234. var current = _productService.GetCurrentProduct();
  235. if (current != null && current.IsLoad)
  236. {
  237. await management.WritePositionToPlcAsync();
  238. }
  239. if (DialogHost.IsDialogOpen("RootDialog"))
  240. {
  241. DialogHost.Close("RootDialog");
  242. }
  243. await task;
  244. }
  245. catch (Exception)
  246. {
  247. if (DialogHost.IsDialogOpen("RootDialog"))
  248. {
  249. DialogHost.Close("RootDialog");
  250. }
  251. await task;
  252. }
  253. }
  254. NavigationParameters param = new NavigationParameters();
  255. param.Add("SelectProduct", SelectProduct);
  256. _regionManager.RequestNavigate("ProductRegionContext", "ProductHome", param);
  257. }
  258. private void ResetId()
  259. {
  260. if (FixedUpCamera1Procedure == null)
  261. {
  262. SelectProduct.FixedUpCamera1ProcedureId = Guid.Empty;
  263. }
  264. if (FixedUpCamera2Procedure == null)
  265. {
  266. SelectProduct.FixedUpCamera2ProcedureId = Guid.Empty;
  267. }
  268. if (FixedDownCamera1Procedure == null)
  269. {
  270. SelectProduct.FixedDownCamera1ProcedureId = Guid.Empty;
  271. }
  272. if (FixedDownCamera2Procedure == null)
  273. {
  274. SelectProduct.FixedDownCamera2ProcedureId = Guid.Empty;
  275. }
  276. if (MoveDownCamera1LockPhotoProcedure == null)
  277. {
  278. SelectProduct.MoveDownCamera1LockPhotoProcedureId = Guid.Empty;
  279. }
  280. if (MoveDownCamera2LockPhotoProcedure == null)
  281. {
  282. SelectProduct.MoveDownCamera2LockPhotoProcedureId = Guid.Empty;
  283. }
  284. if (MoveDownCamer1PickPhotoProcedureId == null)
  285. {
  286. SelectProduct.MoveDownCamer1PickPhotoProcedureId = Guid.Empty;
  287. }
  288. if (MoveDownCamer2PickPhotoProcedureId == null)
  289. {
  290. SelectProduct.MoveDownCamer2PickPhotoProcedureId = Guid.Empty;
  291. }
  292. }
  293. /// <summary>
  294. /// 上一步
  295. /// </summary>
  296. void ExecuteBackCommand()
  297. {
  298. NavigationParameters param = new NavigationParameters();
  299. param.Add("SelectProduct", SelectProduct);
  300. _regionManager.RequestNavigate("ProductRegionContext", "PlcPointParams", param);
  301. }
  302. void ExecuteLoadedCommand()
  303. {
  304. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  305. {
  306. IsAllowEdit = false;
  307. }
  308. else
  309. {
  310. IsAllowEdit = true;
  311. }
  312. }
  313. #endregion
  314. #region 继承
  315. /// <summary>
  316. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  317. /// </summary>
  318. /// <param name="navigationContext"></param>
  319. /// <param name="continuationCallback"></param>
  320. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  321. {
  322. continuationCallback(true);
  323. }
  324. /// <summary>
  325. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  326. /// </summary>
  327. /// <param name="navigationContext"></param>
  328. /// <exception cref="NotImplementedException"></exception>
  329. public async void OnNavigatedTo(NavigationContext navigationContext)
  330. {
  331. try
  332. {
  333. SelectProduct = navigationContext.Parameters.GetValue<ProductModel>("SelectProduct");
  334. ProcedureModels = new ObservableCollection<ProcedureModel>();
  335. foreach (var item in SelectProduct.CameraProcedures)
  336. {
  337. foreach (var item1 in item.ProcedureModels)
  338. {
  339. ProcedureModels.Add(item1);
  340. }
  341. }
  342. if (SelectProduct.FixedUpCamera1ProcedureId!=Guid.Empty)
  343. {
  344. FixedUpCamera1Procedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.FixedUpCamera1ProcedureId);
  345. }
  346. if (SelectProduct.FixedUpCamera2ProcedureId != Guid.Empty)
  347. {
  348. FixedUpCamera2Procedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.FixedUpCamera2ProcedureId);
  349. }
  350. if (SelectProduct.FixedDownCamera1ProcedureId != Guid.Empty)
  351. {
  352. FixedDownCamera1Procedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.FixedDownCamera1ProcedureId);
  353. }
  354. if (SelectProduct.FixedDownCamera2ProcedureId != Guid.Empty)
  355. {
  356. FixedDownCamera2Procedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.FixedDownCamera2ProcedureId);
  357. }
  358. if (SelectProduct.MoveDownCamera1LockPhotoProcedureId != Guid.Empty)
  359. {
  360. MoveDownCamera1LockPhotoProcedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.MoveDownCamera1LockPhotoProcedureId);
  361. }
  362. if (SelectProduct.MoveDownCamera2LockPhotoProcedureId != Guid.Empty)
  363. {
  364. MoveDownCamera2LockPhotoProcedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.MoveDownCamera2LockPhotoProcedureId);
  365. }
  366. if (SelectProduct.MoveDownCamer1PickPhotoProcedureId != Guid.Empty)
  367. {
  368. MoveDownCamer1PickPhotoProcedureId = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.MoveDownCamer1PickPhotoProcedureId);
  369. }
  370. if (SelectProduct.MoveDownCamer2PickPhotoProcedureId != Guid.Empty)
  371. {
  372. MoveDownCamer2PickPhotoProcedureId = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.MoveDownCamer2PickPhotoProcedureId);
  373. }
  374. }
  375. catch (Exception ex)
  376. {
  377. LogHelper.WriteLogError("机器人点位界面进入时出错!", ex);
  378. }
  379. }
  380. /// <summary>
  381. /// 是否允许导航到此视图模型。
  382. /// </summary>
  383. /// <param name="navigationContext"></param>
  384. /// <returns></returns>
  385. public bool IsNavigationTarget(NavigationContext navigationContext)
  386. {
  387. return true;
  388. }
  389. /// <summary>
  390. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  391. /// </summary>
  392. /// <param name="navigationContext"></param>
  393. public void OnNavigatedFrom(NavigationContext navigationContext)
  394. {
  395. }
  396. #endregion
  397. private bool _IsAllowEdit = false;
  398. public bool IsAllowEdit
  399. {
  400. get { return _IsAllowEdit; }
  401. set { SetProperty(ref _IsAllowEdit, value); }
  402. }
  403. private void LoginChange(Models.User user)
  404. {
  405. if (user.userPart == Enums.UserPart.Operator)
  406. {
  407. IsAllowEdit = false;
  408. }
  409. else
  410. {
  411. IsAllowEdit = true;
  412. }
  413. }
  414. }
  415. }