ProductParamsViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. }
  195. NavigationParameters param = new NavigationParameters();
  196. param.Add("SelectProduct", SelectProduct);
  197. _regionManager.RequestNavigate("ProductRegionContext", "ProductHome", param);
  198. }
  199. private void ResetId()
  200. {
  201. if (FixedUpCamera1Procedure == null)
  202. {
  203. SelectProduct.FixedUpCamera1ProcedureId = Guid.Empty;
  204. }
  205. if (FixedUpCamera2Procedure == null)
  206. {
  207. SelectProduct.FixedUpCamera2ProcedureId = Guid.Empty;
  208. }
  209. if (FixedDownCameraProcedure == null)
  210. {
  211. SelectProduct.FixedDownCameraProcedureId = Guid.Empty;
  212. }
  213. if (MoveDownCameraLockPhotoProcedure == null)
  214. {
  215. SelectProduct.MoveDownCameraLockPhotoProcedureId = Guid.Empty;
  216. }
  217. }
  218. /// <summary>
  219. /// 上一步
  220. /// </summary>
  221. void ExecuteBackCommand()
  222. {
  223. NavigationParameters param = new NavigationParameters();
  224. param.Add("SelectProduct", SelectProduct);
  225. _regionManager.RequestNavigate("ProductRegionContext", "PlcPointParams", param);
  226. }
  227. void ExecuteLoadedCommand()
  228. {
  229. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  230. {
  231. IsAllowEdit = false;
  232. }
  233. else
  234. {
  235. IsAllowEdit = true;
  236. }
  237. }
  238. #endregion
  239. #region 继承
  240. /// <summary>
  241. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  242. /// </summary>
  243. /// <param name="navigationContext"></param>
  244. /// <param name="continuationCallback"></param>
  245. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  246. {
  247. continuationCallback(true);
  248. }
  249. /// <summary>
  250. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  251. /// </summary>
  252. /// <param name="navigationContext"></param>
  253. /// <exception cref="NotImplementedException"></exception>
  254. public async void OnNavigatedTo(NavigationContext navigationContext)
  255. {
  256. try
  257. {
  258. SelectProduct = navigationContext.Parameters.GetValue<ProductModel>("SelectProduct");
  259. ProcedureModels = new ObservableCollection<ProcedureModel>();
  260. foreach (var item in SelectProduct.CameraProcedures)
  261. {
  262. foreach (var item1 in item.ProcedureModels)
  263. {
  264. ProcedureModels.Add(item1);
  265. }
  266. }
  267. if (SelectProduct.FixedUpCamera1ProcedureId!=Guid.Empty)
  268. {
  269. FixedUpCamera1Procedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.FixedUpCamera1ProcedureId);
  270. }
  271. if (SelectProduct.FixedUpCamera2ProcedureId != Guid.Empty)
  272. {
  273. FixedUpCamera2Procedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.FixedUpCamera2ProcedureId);
  274. }
  275. if (SelectProduct.FixedDownCameraProcedureId != Guid.Empty)
  276. {
  277. FixedDownCameraProcedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.FixedDownCameraProcedureId);
  278. }
  279. if (SelectProduct.MoveDownCameraLockPhotoProcedureId != Guid.Empty)
  280. {
  281. MoveDownCameraLockPhotoProcedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.MoveDownCameraLockPhotoProcedureId);
  282. }
  283. if (SelectProduct.QrCodeProcedureId != Guid.Empty)
  284. {
  285. QrCodePhotoProcedure = ProcedureModels.FirstOrDefault(p => p.Id == SelectProduct.QrCodeProcedureId);
  286. }
  287. }
  288. catch (Exception ex)
  289. {
  290. LogHelper.WriteLogError(Lang.机器人点位界面进入时出错, ex);
  291. }
  292. }
  293. /// <summary>
  294. /// 是否允许导航到此视图模型。
  295. /// </summary>
  296. /// <param name="navigationContext"></param>
  297. /// <returns></returns>
  298. public bool IsNavigationTarget(NavigationContext navigationContext)
  299. {
  300. return true;
  301. }
  302. /// <summary>
  303. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  304. /// </summary>
  305. /// <param name="navigationContext"></param>
  306. public void OnNavigatedFrom(NavigationContext navigationContext)
  307. {
  308. }
  309. #endregion
  310. private bool _IsAllowEdit = false;
  311. public bool IsAllowEdit
  312. {
  313. get { return _IsAllowEdit; }
  314. set { SetProperty(ref _IsAllowEdit, value); }
  315. }
  316. private void LoginChange(Models.User user)
  317. {
  318. if (user.userPart == Enums.UserPart.Operator)
  319. {
  320. IsAllowEdit = false;
  321. }
  322. else
  323. {
  324. IsAllowEdit = true;
  325. }
  326. }
  327. }
  328. }