HomeViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. using MahApps.Metro.Controls;
  2. using MaterialDesignThemes.Wpf;
  3. using NPOI.SS.Formula.Functions;
  4. using OxyPlot;
  5. using OxyPlot.Axes;
  6. using OxyPlot.Legends;
  7. using OxyPlot.Series;
  8. using Prism.Commands;
  9. using Prism.Events;
  10. using Prism.Ioc;
  11. using Prism.Mvvm;
  12. using Prism.Regions;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Collections.ObjectModel;
  16. using System.Linq;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Media;
  20. using TeamAAS_VP;
  21. using TeamAAS_VP.Controls;
  22. using TeamAAS_VP.Core;
  23. using TeamAAS_VP.Data;
  24. using TeamAAS_VP.Enums;
  25. using TeamAAS_VP.Events;
  26. using TeamAAS_VP.Interfaces;
  27. using TeamAAS_VP.Models;
  28. using TeamAAS_VP.Resources.Languages;
  29. using TeamAAS_VP.Services;
  30. using TeamAAS_VP.ViewModels.DebugMod;
  31. using TeamAAS_VP.Views.DebugMod;
  32. namespace TeamAAS_VP.ViewModels
  33. {
  34. public class HomeViewModel : BindableBase, IConfirmNavigationRequest
  35. {
  36. IEventAggregator _eventAggregator;
  37. IContainerProvider _container;
  38. IProductService _productService;
  39. IConfigService _configService;
  40. IRemoteCommandService _remoteCommandService;
  41. ISystemDatabaseService _systemDatabaseService;
  42. #region 属性
  43. private ObservableCollection<TaskMessage> messageQueue=new ObservableCollection<TaskMessage>();
  44. /// <summary>
  45. /// 运行信息队列
  46. /// </summary>
  47. public ObservableCollection<TaskMessage> MessageQueue
  48. {
  49. get { return messageQueue; }
  50. set { SetProperty(ref messageQueue,value); }
  51. }
  52. private ObservableCollection<ProductModel> _ProductList;
  53. public ObservableCollection<ProductModel> ProductList
  54. {
  55. get { return _ProductList; }
  56. set { SetProperty(ref _ProductList, value); }
  57. }
  58. private Management _management;
  59. public Management management
  60. {
  61. get { return _management; }
  62. set { SetProperty(ref _management, value); }
  63. }
  64. private double _CTTotalSeconds;
  65. public double CTTotalSeconds
  66. {
  67. get { return _CTTotalSeconds; }
  68. set { SetProperty(ref _CTTotalSeconds, value); }
  69. }
  70. private ProductModel _SelectProduct;
  71. public ProductModel SelectProduct
  72. {
  73. get { return _SelectProduct; }
  74. set { SetProperty(ref _SelectProduct,value); }
  75. }
  76. private bool CanLoad
  77. {
  78. get
  79. {
  80. if (SelectProduct!=null && IsLoadProduct)
  81. {
  82. return true;
  83. }
  84. else
  85. {
  86. return false;
  87. }
  88. }
  89. }
  90. private bool _IsLoadProduct = true;
  91. public bool IsLoadProduct
  92. {
  93. get { return _IsLoadProduct; }
  94. set { SetProperty(ref _IsLoadProduct, value); }
  95. }
  96. private bool _isCanExecute = true;
  97. public bool isCanExecute
  98. {
  99. get { return _isCanExecute; }
  100. set { SetProperty(ref _isCanExecute, value); }
  101. }
  102. private ProductModel _CurrentProduct;
  103. public ProductModel CurrentProduct
  104. {
  105. get { return _CurrentProduct; }
  106. set { SetProperty(ref _CurrentProduct, value); }
  107. }
  108. private PlotModel _LockCurvePlotModel;
  109. /// <summary>
  110. /// 锁付曲线
  111. /// </summary>
  112. public PlotModel LockCurvePlotModel
  113. {
  114. get { return _LockCurvePlotModel; }
  115. set { SetProperty(ref _LockCurvePlotModel, value); }
  116. }
  117. #endregion
  118. #region 命令
  119. public DelegateCommand LoadedCommand { get; set; }
  120. private DelegateCommand<ProductModel> _LoadProductCommand;
  121. public DelegateCommand<ProductModel> LoadProductCommand =>
  122. _LoadProductCommand ?? (_LoadProductCommand = new DelegateCommand<ProductModel>(ExecuteLoadProductCommand).ObservesCanExecute(()=> CanLoad).ObservesProperty(()=> SelectProduct).ObservesProperty(()=> IsLoadProduct));
  123. private DelegateCommand _ResetCounterCommand;
  124. public DelegateCommand ResetCounterCommand =>
  125. _ResetCounterCommand ?? (_ResetCounterCommand = new DelegateCommand(ExecuteResetCounterCommand).ObservesCanExecute(() => DoCondition).ObservesProperty(()=>isCanExecute));
  126. private bool DoCondition
  127. {
  128. get { return isCanExecute; }
  129. }
  130. private DelegateCommand<string> _CopyCommand;
  131. public DelegateCommand<string> CopyCommand =>
  132. _CopyCommand ?? (_CopyCommand = new DelegateCommand<string>(ExecuteCopyCommand));
  133. #endregion
  134. #region 事件
  135. #endregion
  136. public HomeViewModel(IEventAggregator ea, IContainerProvider container, IProductService productService, IConfigService configService, IRemoteCommandService remoteCommandService
  137. , ISystemDatabaseService systemDatabaseService)
  138. {
  139. _eventAggregator = ea;
  140. _container = container;
  141. _productService = productService;
  142. LoadedCommand = new DelegateCommand(OnLoad);
  143. _eventAggregator.GetEvent<TaskMessageNotification>().Subscribe(AddMessageInvoke);
  144. //订阅权限登录事件
  145. _eventAggregator.GetEvent<UserLoginNotification>().Subscribe(LoginChange);
  146. management = _container.Resolve<Management>();
  147. _configService = configService;
  148. _productService.OnProductChanged += _productService_OnProductChanged;
  149. _remoteCommandService = remoteCommandService;
  150. _remoteCommandService.CycleTimingTicked += _remoteCommandService_CycleTimingTicked;
  151. _systemDatabaseService = systemDatabaseService;
  152. }
  153. private void _remoteCommandService_CycleTimingTicked(object sender, TimeSpan e)
  154. {
  155. try
  156. {
  157. if (App.Current!=null)
  158. {
  159. App.Current.Invoke(() =>
  160. {
  161. CTTotalSeconds = e.TotalSeconds;
  162. });
  163. }
  164. }
  165. catch (Exception)
  166. {
  167. }
  168. }
  169. #region 方法
  170. bool isFirst=true;
  171. private void OnLoad()
  172. {
  173. if (!isFirst)
  174. {
  175. return;
  176. }
  177. InitLockCurvePlotModel();
  178. isFirst = false;
  179. }
  180. async void ExecuteLoadProductCommand(ProductModel product)
  181. {
  182. if (product == null) return;
  183. var view = new ShowMessage(Lang.是否加载产品, $"{Lang.加载产品}:{product.Name}?");
  184. //show the dialog
  185. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  186. if (result != null && result is bool)
  187. {
  188. if (((bool)result))
  189. {
  190. IsLoadProduct = false;
  191. var waiting = new WaitingControl();
  192. //show the dialog
  193. var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
  194. await _productService.LoadProductAsync(product.ID);
  195. _productService.SetCurrentProduct(product.ID);
  196. await management.WritePositionToPlcAsync();
  197. if (DialogHost.IsDialogOpen("RootDialog"))
  198. {
  199. DialogHost.Close("RootDialog");
  200. }
  201. await task;
  202. //DatabaseHelper.AddLoadProductRecord(product.ID, product.Name, false);
  203. IsLoadProduct = true;
  204. }
  205. }
  206. }
  207. void ExecuteCopyCommand(string textToCopy)
  208. {
  209. Clipboard.SetText(textToCopy);
  210. }
  211. /// <summary>
  212. /// 添加消息至消息队列
  213. /// </summary>
  214. /// <param name="msg"></param>
  215. /// <param name="color"></param>
  216. public void AddMessage(string msg, Color color)
  217. {
  218. App.Current.Dispatcher.BeginInvoke(new Action(() => {
  219. try
  220. {
  221. MessageQueue.Add(new TaskMessage()
  222. {
  223. DT = DateTime.Now,
  224. Message = msg,
  225. FontColol = new SolidColorBrush(color)
  226. });
  227. LogHelper.WriteLogInfo(msg);
  228. }
  229. catch (Exception)
  230. {
  231. }
  232. }));
  233. }
  234. /// <summary>
  235. /// 添加消息至消息队列
  236. /// </summary>
  237. /// <param name="msg"></param>
  238. /// <param name="color"></param>
  239. public void AddMessage(string msg, MessageLevel level)
  240. {
  241. MessageStruct messageStruct = new MessageStruct() { Message = msg, level = level };
  242. App.Current.Dispatcher.BeginInvoke(new Action(() => {
  243. AddMessage(messageStruct);
  244. }));
  245. }
  246. /// <summary>
  247. /// 添加消息至消息队列
  248. /// </summary>
  249. /// <param name="messageStruct"></param>
  250. public void AddMessage(MessageStruct messageStruct)
  251. {
  252. try
  253. {
  254. if (MessageQueue.Count >= 20)
  255. {
  256. MessageQueue.RemoveAt(0);// 从队列头部移除最早的消息
  257. }
  258. Color color = Colors.Black;
  259. switch (messageStruct.level)
  260. {
  261. case MessageLevel.Info:
  262. color = Colors.Black;
  263. break;
  264. case MessageLevel.Debug:
  265. color = Colors.Green;
  266. break;
  267. case MessageLevel.Alarm:
  268. color = Colors.Orange;
  269. break;
  270. case MessageLevel.Error:
  271. color = Colors.Red;
  272. break;
  273. }
  274. MessageQueue.Add(new TaskMessage()
  275. {
  276. DT = DateTime.Now,
  277. Message = messageStruct.Message,
  278. FontColol = new SolidColorBrush(color)
  279. });
  280. LogHelper.WriteLogInfo(messageStruct.Message);
  281. }
  282. catch (Exception)
  283. {
  284. }
  285. }
  286. public void AddMessageInvoke(MessageStruct messageStruct)
  287. {
  288. App.Current.Dispatcher.BeginInvoke(new Action(() => {
  289. AddMessage(messageStruct);
  290. }));
  291. }
  292. private void LoginChange(Models.User user)
  293. {
  294. }
  295. /// <summary>
  296. /// 重置计数
  297. /// </summary>
  298. async void ExecuteResetCounterCommand()
  299. {
  300. if (management.CurrentProduct == null) return;
  301. var view = new ShowMessage(Lang.计数清零, Lang.是否重置当前产品的计数);
  302. //show the dialog
  303. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  304. if (result != null && result is bool)
  305. {
  306. if (((bool)result))
  307. {
  308. isCanExecute = false;
  309. await _systemDatabaseService.ResetProductionAsync(management.CurrentProduct.Name);
  310. isCanExecute = true;
  311. }
  312. }
  313. }
  314. //初始化曲线图
  315. private void InitLockCurvePlotModel()
  316. {
  317. var model = new PlotModel { Title = "锁付曲线", TitleFontSize = 16};
  318. // 设置图例
  319. var legend = new Legend
  320. {
  321. LegendPosition = LegendPosition.TopRight,
  322. LegendPlacement = LegendPlacement.Outside,
  323. LegendOrientation = LegendOrientation.Vertical,
  324. LegendBorderThickness = 0
  325. };
  326. model.Legends.Add(legend);
  327. // 设置X轴
  328. var xAxis = new LinearAxis
  329. {
  330. Position = AxisPosition.Bottom,
  331. Title = "角度 (°)",
  332. Minimum = 0,
  333. MajorGridlineStyle = LineStyle.Solid,
  334. MinorGridlineStyle = LineStyle.Dot
  335. };
  336. model.Axes.Add(xAxis);
  337. // 设置Y轴
  338. var yAxis = new LinearAxis
  339. {
  340. Position = AxisPosition.Left,
  341. Title = "力矩 (kgf)",
  342. MajorGridlineStyle = LineStyle.Solid,
  343. MinorGridlineStyle = LineStyle.Dot
  344. };
  345. model.Axes.Add(yAxis);
  346. // 添加示例数据系列
  347. var series = new LineSeries
  348. {
  349. Title = "锁付力矩曲线",
  350. StrokeThickness = 2,
  351. MarkerSize = 3,
  352. MarkerType = MarkerType.Circle,
  353. CanTrackerInterpolatePoints = false,
  354. };
  355. model.Series.Add(series);
  356. LockCurvePlotModel = model;
  357. }
  358. #endregion
  359. #region 继承
  360. /// <summary>
  361. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  362. /// </summary>
  363. /// <param name="navigationContext"></param>
  364. /// <param name="continuationCallback"></param>
  365. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  366. {
  367. continuationCallback(true);
  368. }
  369. /// <summary>
  370. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  371. /// </summary>
  372. /// <param name="navigationContext"></param>
  373. /// <exception cref="NotImplementedException"></exception>
  374. public async void OnNavigatedTo(NavigationContext navigationContext)
  375. {
  376. try
  377. {
  378. ProductList = new ObservableCollection<ProductModel>(_productService.GetAllProducts());
  379. }
  380. catch (Exception ex)
  381. {
  382. LogHelper.WriteLogError("主界面界面进入时出错!", ex);
  383. }
  384. }
  385. /// <summary>
  386. /// 是否允许导航到此视图模型。
  387. /// </summary>
  388. /// <param name="navigationContext"></param>
  389. /// <returns></returns>
  390. public bool IsNavigationTarget(NavigationContext navigationContext)
  391. {
  392. return true;
  393. }
  394. /// <summary>
  395. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  396. /// </summary>
  397. /// <param name="navigationContext"></param>
  398. public void OnNavigatedFrom(NavigationContext navigationContext)
  399. {
  400. }
  401. #endregion
  402. private void _productService_OnProductChanged(ProductModel obj)
  403. {
  404. App.Current.Dispatcher.BeginInvoke(new Action(() => {
  405. CurrentProduct = obj;
  406. }));
  407. }
  408. }
  409. }