ProductParamsViewModel.cs 11 KB

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