ProductParamsViewModel.cs 13 KB

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