ProductParamsViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 _FixedDownCameraProcedure;
  95. /// <summary>
  96. /// 固定向下相机取料流程ID
  97. /// </summary>
  98. public ProcedureModel FixedDownCameraProcedure
  99. {
  100. get { return _FixedDownCameraProcedure; }
  101. set { SetProperty(ref _FixedDownCameraProcedure, value);
  102. if (value != null)
  103. {
  104. SelectProduct.FixedDownCameraProcedureId = value.Id;
  105. }
  106. }
  107. }
  108. private ProcedureModel _MoveDownCameraLockPhotoProcedure;
  109. /// <summary>
  110. /// 移动向下相机锁付拍照流程ID
  111. /// </summary>
  112. public ProcedureModel MoveDownCameraLockPhotoProcedure
  113. {
  114. get { return _MoveDownCameraLockPhotoProcedure; }
  115. set { SetProperty(ref _MoveDownCameraLockPhotoProcedure, value);
  116. if (value != null)
  117. {
  118. SelectProduct.MoveDownCameraLockPhotoProcedureId = value.Id;
  119. }
  120. }
  121. }
  122. private ProcedureModel _QrCodePhotoProcedure;
  123. /// <summary>
  124. /// 移动向下相机锁付拍照流程ID
  125. /// </summary>
  126. public ProcedureModel QrCodePhotoProcedure
  127. {
  128. get { return _QrCodePhotoProcedure; }
  129. set
  130. {
  131. SetProperty(ref _QrCodePhotoProcedure, value);
  132. if (value != null)
  133. {
  134. SelectProduct.QrCodeProcedureId = value.Id;
  135. }
  136. }
  137. }
  138. private ProcedureModel _FzCameraProcedure;
  139. /// <summary>
  140. /// 翻转相机视觉流程
  141. /// </summary>
  142. public ProcedureModel FzCameraProcedure
  143. {
  144. get { return _FzCameraProcedure; }
  145. set
  146. {
  147. SetProperty(ref _FzCameraProcedure, value);
  148. if (value != null)
  149. {
  150. SelectProduct.FzCameraProcedureId = value.Id;
  151. }
  152. }
  153. }
  154. #endregion
  155. #region 命令
  156. private DelegateCommand _NextCommand;
  157. public DelegateCommand NextCommand =>
  158. _NextCommand ?? (_NextCommand = new DelegateCommand(ExecuteNextCommand));
  159. private DelegateCommand _BackCommand;
  160. public DelegateCommand BackCommand =>
  161. _BackCommand ?? (_BackCommand = new DelegateCommand(ExecuteBackCommand));
  162. private DelegateCommand _LoadedCommand;
  163. public DelegateCommand LoadedCommand =>
  164. _LoadedCommand ?? (_LoadedCommand = new DelegateCommand(ExecuteLoadedCommand));
  165. #endregion
  166. #region 事件
  167. #endregion
  168. public ProductParamsViewModel(IRegionManager regionManager, IEventAggregator ea, IContainerProvider container, IDialogService dialogService,
  169. IConfigService configService, IRobotService robotService, ICameraService cameraService, IFeederService feederService, IPlcService plcService, IProductService productService,
  170. ISystemDatabaseService systemDatabaseService)
  171. {
  172. _regionManager = regionManager;
  173. _eventAggregator = ea;
  174. _container = container;
  175. _dialogService = dialogService;
  176. _configService = configService;
  177. _robotService = robotService;
  178. _cameraService = cameraService;
  179. _feederService = feederService;
  180. _plcService = plcService;
  181. _productService = productService;
  182. _systemDatabaseService = systemDatabaseService;
  183. //订阅权限登录事件
  184. _eventAggregator.GetEvent<UserLoginNotification>().Subscribe(LoginChange);
  185. management = _container.Resolve<Management>();
  186. }
  187. #region 方法
  188. /// <summary>
  189. /// 完成
  190. /// </summary>
  191. async void ExecuteNextCommand()
  192. {
  193. ResetId();
  194. if (MessageBox.Show(Lang.是否保存产品.Replace("[{0}]", ""), Lang.保存产品, MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  195. {
  196. var waiting = new WaitingControl();
  197. //show the dialog
  198. var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
  199. await _productService.UpdateProductAsync(SelectProduct);
  200. var current = _productService.GetCurrentProduct();
  201. if (current != null && current.IsLoad)
  202. {
  203. await management.WriteParameterToPlcExAsync();
  204. }
  205. if (DialogHost.IsDialogOpen("RootDialog"))
  206. {
  207. DialogHost.Close("RootDialog");
  208. }
  209. await task;
  210. App.Current.Dispatcher.Invoke(() =>
  211. {
  212. _eventAggregator.GetEvent<TaskMessageNotification>().Publish(new Models.MessageStruct() { Message = "保存配方参数成功...", level = Enums.MessageLevel.Info });
  213. });
  214. }
  215. NavigationParameters param = new NavigationParameters();
  216. param.Add("SelectProduct", SelectProduct);
  217. _regionManager.RequestNavigate("ProductRegionContext", "ProductHome", param);
  218. }
  219. private void ResetId()
  220. {
  221. if (FixedUpCamera1Procedure == null)
  222. {
  223. SelectProduct.FixedUpCamera1ProcedureId = Guid.Empty;
  224. }
  225. if (FixedUpCamera2Procedure == null)
  226. {
  227. SelectProduct.FixedUpCamera2ProcedureId = Guid.Empty;
  228. }
  229. if (FixedDownCameraProcedure == null)
  230. {
  231. SelectProduct.FixedDownCameraProcedureId = Guid.Empty;
  232. }
  233. if (MoveDownCameraLockPhotoProcedure == null)
  234. {
  235. SelectProduct.MoveDownCameraLockPhotoProcedureId = Guid.Empty;
  236. }
  237. }
  238. /// <summary>
  239. /// 上一步
  240. /// </summary>
  241. void ExecuteBackCommand()
  242. {
  243. NavigationParameters param = new NavigationParameters();
  244. param.Add("SelectProduct", SelectProduct);
  245. _regionManager.RequestNavigate("ProductRegionContext", "PlcPointParams", param);
  246. }
  247. void ExecuteLoadedCommand()
  248. {
  249. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  250. {
  251. IsAllowEdit = false;
  252. }
  253. else
  254. {
  255. IsAllowEdit = true;
  256. }
  257. }
  258. #endregion
  259. #region 继承
  260. /// <summary>
  261. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  262. /// </summary>
  263. /// <param name="navigationContext"></param>
  264. /// <param name="continuationCallback"></param>
  265. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  266. {
  267. continuationCallback(true);
  268. }
  269. /// <summary>
  270. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  271. /// </summary>
  272. /// <param name="navigationContext"></param>
  273. /// <exception cref="NotImplementedException"></exception>
  274. public async void OnNavigatedTo(NavigationContext navigationContext)
  275. {
  276. try
  277. {
  278. SelectProduct = navigationContext.Parameters.GetValue<ProductModel>("SelectProduct");
  279. ProcedureModels = new ObservableCollection<ProcedureModel>();
  280. foreach (var item in SelectProduct.CameraProcedures)
  281. {
  282. foreach (var item1 in item.ProcedureModels)
  283. {
  284. ProcedureModels.Add(item1);
  285. }
  286. }
  287. if (SelectProduct.FixedUpCamera1ProcedureId!=Guid.Empty)
  288. {
  289. FixedUpCamera1Procedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.FixedUpCamera1ProcedureId);
  290. }
  291. if (SelectProduct.FixedUpCamera2ProcedureId != Guid.Empty)
  292. {
  293. FixedUpCamera2Procedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.FixedUpCamera2ProcedureId);
  294. }
  295. if (SelectProduct.FixedDownCameraProcedureId != Guid.Empty)
  296. {
  297. FixedDownCameraProcedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.FixedDownCameraProcedureId);
  298. }
  299. if (SelectProduct.MoveDownCameraLockPhotoProcedureId != Guid.Empty)
  300. {
  301. MoveDownCameraLockPhotoProcedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.MoveDownCameraLockPhotoProcedureId);
  302. }
  303. if (SelectProduct.QrCodeProcedureId != Guid.Empty)
  304. {
  305. QrCodePhotoProcedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.QrCodeProcedureId);
  306. }
  307. if (SelectProduct.FzCameraProcedureId != Guid.Empty)
  308. {
  309. FzCameraProcedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.FzCameraProcedureId);
  310. }
  311. }
  312. catch (Exception ex)
  313. {
  314. LogHelper.WriteLogError(Lang.机器人点位界面进入时出错, ex);
  315. }
  316. }
  317. /// <summary>
  318. /// 是否允许导航到此视图模型。
  319. /// </summary>
  320. /// <param name="navigationContext"></param>
  321. /// <returns></returns>
  322. public bool IsNavigationTarget(NavigationContext navigationContext)
  323. {
  324. return true;
  325. }
  326. /// <summary>
  327. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  328. /// </summary>
  329. /// <param name="navigationContext"></param>
  330. public void OnNavigatedFrom(NavigationContext navigationContext)
  331. {
  332. }
  333. #endregion
  334. private bool _IsAllowEdit = false;
  335. public bool IsAllowEdit
  336. {
  337. get { return _IsAllowEdit; }
  338. set { SetProperty(ref _IsAllowEdit, value); }
  339. }
  340. private void LoginChange(Models.User user)
  341. {
  342. if (user.userPart == Enums.UserPart.Operator)
  343. {
  344. IsAllowEdit = false;
  345. }
  346. else
  347. {
  348. IsAllowEdit = true;
  349. }
  350. }
  351. }
  352. }