HomeViewModel.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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 OxyPlot.Wpf;
  9. using Prism.Commands;
  10. using Prism.Events;
  11. using Prism.Ioc;
  12. using Prism.Mvvm;
  13. using Prism.Regions;
  14. using Prism.Services.Dialogs;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Collections.ObjectModel;
  18. using System.IO;
  19. using System.Linq;
  20. using System.Threading.Tasks;
  21. using System.Windows;
  22. using System.Windows.Media;
  23. using TeamAAS_VP;
  24. using TeamAAS_VP.Controls;
  25. using TeamAAS_VP.Core;
  26. using TeamAAS_VP.Data;
  27. using TeamAAS_VP.Enums;
  28. using TeamAAS_VP.Events;
  29. using TeamAAS_VP.Interfaces;
  30. using TeamAAS_VP.Models;
  31. using TeamAAS_VP.Resources.Languages;
  32. using TeamAAS_VP.Services;
  33. using TeamAAS_VP.ViewModels.DebugMod;
  34. using TeamAAS_VP.Views;
  35. using TeamAAS_VP.Views.DebugMod;
  36. using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
  37. namespace TeamAAS_VP.ViewModels
  38. {
  39. public class HomeViewModel : BindableBase, IConfirmNavigationRequest
  40. {
  41. IEventAggregator _eventAggregator;
  42. IContainerProvider _container;
  43. IProductService _productService;
  44. IConfigService _configService;
  45. IRemoteCommandService _remoteCommandService;
  46. ISystemDatabaseService _systemDatabaseService;
  47. IDialogService _dialogService;
  48. #region 属性
  49. private PlotModel _PressurePlotModel = new PlotModel();
  50. /// <summary>
  51. /// 每个元素是一个 PlotModel,对应一个压力传感器
  52. /// </summary>
  53. public PlotModel PressurePlotModel
  54. {
  55. get { return _PressurePlotModel; }
  56. set { SetProperty(ref _PressurePlotModel, value); }
  57. }
  58. private ObservableCollection<TaskMessage> messageQueue = new ObservableCollection<TaskMessage>();
  59. /// <summary>
  60. /// 运行信息队列
  61. /// </summary>
  62. public ObservableCollection<TaskMessage> MessageQueue
  63. {
  64. get { return messageQueue; }
  65. set { SetProperty(ref messageQueue, value); }
  66. }
  67. private ObservableCollection<ProductModel> _ProductList;
  68. public ObservableCollection<ProductModel> ProductList
  69. {
  70. get { return _ProductList; }
  71. set { SetProperty(ref _ProductList, value); }
  72. }
  73. private Management _management;
  74. public Management management
  75. {
  76. get { return _management; }
  77. set { SetProperty(ref _management, value); }
  78. }
  79. private double _CTTotalSeconds;
  80. public double CTTotalSeconds
  81. {
  82. get { return _CTTotalSeconds; }
  83. set { SetProperty(ref _CTTotalSeconds, value); }
  84. }
  85. private ProductModel _SelectProduct;
  86. public ProductModel SelectProduct
  87. {
  88. get { return _SelectProduct; }
  89. set { SetProperty(ref _SelectProduct, value); }
  90. }
  91. private bool CanLoad
  92. {
  93. get
  94. {
  95. if (SelectProduct != null && IsLoadProduct)
  96. {
  97. return true;
  98. }
  99. else
  100. {
  101. return false;
  102. }
  103. }
  104. }
  105. private bool _IsLoadProduct = true;
  106. public bool IsLoadProduct
  107. {
  108. get { return _IsLoadProduct; }
  109. set { SetProperty(ref _IsLoadProduct, value); }
  110. }
  111. private bool _isCanExecute = true;
  112. public bool isCanExecute
  113. {
  114. get { return _isCanExecute; }
  115. set { SetProperty(ref _isCanExecute, value); }
  116. }
  117. private ProductModel _CurrentProduct;
  118. public ProductModel CurrentProduct
  119. {
  120. get { return _CurrentProduct; }
  121. set { SetProperty(ref _CurrentProduct, value); }
  122. }
  123. private PlotModel _LockCurvePlotModel;
  124. /// <summary>
  125. /// 锁付曲线
  126. /// </summary>
  127. public PlotModel LockCurvePlotModel
  128. {
  129. get { return _LockCurvePlotModel; }
  130. set { SetProperty(ref _LockCurvePlotModel, value); }
  131. }
  132. private ObservableCollection<LockResult> _LockResults = new ObservableCollection<LockResult>();
  133. /// <summary>
  134. /// 锁付结果列表
  135. /// </summary>
  136. public ObservableCollection<LockResult> LockResults
  137. {
  138. get { return _LockResults; }
  139. set { SetProperty(ref _LockResults, value); }
  140. }
  141. private bool _IsAllowEdit = false;
  142. public bool IsAllowEdit
  143. {
  144. get { return _IsAllowEdit; }
  145. set { SetProperty(ref _IsAllowEdit, value); }
  146. }
  147. // 可选:记录传感器 Id -> 索引 的映射,便于外部(如 Management)按 Id 更新
  148. private readonly Dictionary<string, int> _sensorIdToIndex = new Dictionary<string, int>();
  149. #endregion
  150. #region 命令
  151. public DelegateCommand LoadedCommand { get; set; }
  152. private DelegateCommand<ProductModel> _LoadProductCommand;
  153. public DelegateCommand<ProductModel> LoadProductCommand =>
  154. _LoadProductCommand ?? (_LoadProductCommand = new DelegateCommand<ProductModel>(ExecuteLoadProductCommand).ObservesCanExecute(() => CanLoad).ObservesProperty(() => SelectProduct).ObservesProperty(() => IsLoadProduct));
  155. private DelegateCommand _ResetCounterCommand;
  156. public DelegateCommand ResetCounterCommand =>
  157. _ResetCounterCommand ?? (_ResetCounterCommand = new DelegateCommand(ExecuteResetCounterCommand).ObservesCanExecute(() => DoCondition).ObservesProperty(() => isCanExecute));
  158. private bool DoCondition
  159. {
  160. get { return isCanExecute; }
  161. }
  162. private DelegateCommand<string> _CopyCommand;
  163. public DelegateCommand<string> CopyCommand =>
  164. _CopyCommand ?? (_CopyCommand = new DelegateCommand<string>(ExecuteCopyCommand));
  165. private DelegateCommand _TorqueInspectionCommand;
  166. public DelegateCommand TorqueInspectionCommand =>
  167. _TorqueInspectionCommand ?? (_TorqueInspectionCommand = new DelegateCommand(ExecuteTorqueInspectionCommand));
  168. //更换吸嘴
  169. private DelegateCommand _ChangeNozzleCommand;
  170. public DelegateCommand ChangeNozzleCommand =>
  171. _ChangeNozzleCommand ?? (_ChangeNozzleCommand = new DelegateCommand(ExecuteChangeNozzleCommand));
  172. //更换披头
  173. private DelegateCommand _ChangeHeaderCommand;
  174. public DelegateCommand ChangeHeaderCommand =>
  175. _ChangeHeaderCommand ?? (_ChangeHeaderCommand = new DelegateCommand(ExecuteChangeHeaderCommand));
  176. //添加螺丝
  177. private DelegateCommand _AddScrewCommand;
  178. public DelegateCommand AddScrewCommand =>
  179. _AddScrewCommand ?? (_AddScrewCommand = new DelegateCommand(ExecuteAddScrewCommand));
  180. private DelegateCommand _SetAlarmValueCommand;
  181. public DelegateCommand SetAlarmValueCommand =>
  182. _SetAlarmValueCommand ?? (_SetAlarmValueCommand = new DelegateCommand(ExecuteSetAlarmValueCommand));
  183. #endregion
  184. #region 事件
  185. #endregion
  186. public HomeViewModel(IEventAggregator ea, IContainerProvider container, IProductService productService, IConfigService configService, IRemoteCommandService remoteCommandService
  187. , ISystemDatabaseService systemDatabaseService, IDialogService dialogService)
  188. {
  189. _eventAggregator = ea;
  190. _container = container;
  191. _dialogService = dialogService;
  192. _productService = productService;
  193. LoadedCommand = new DelegateCommand(OnLoad);
  194. _eventAggregator.GetEvent<TaskMessageNotification>().Subscribe(AddMessageInvoke);
  195. //订阅权限登录事件
  196. _eventAggregator.GetEvent<UserLoginNotification>().Subscribe(LoginChange);
  197. management = _container.Resolve<Management>();
  198. _configService = configService;
  199. _productService.OnProductChanged += _productService_OnProductChanged;
  200. _remoteCommandService = remoteCommandService;
  201. _remoteCommandService.CycleTimingTicked += _remoteCommandService_CycleTimingTicked;
  202. _systemDatabaseService = systemDatabaseService;
  203. _eventAggregator.GetEvent<LockFinishNotification>().Subscribe(OnLockFinish);
  204. _eventAggregator.GetEvent<PressureFinishNotification>().Subscribe(OnPressureFinish);
  205. InitPressurePlots();
  206. }
  207. private void _remoteCommandService_CycleTimingTicked(object sender, TimeSpan e)
  208. {
  209. try
  210. {
  211. if (App.Current != null)
  212. {
  213. App.Current.Invoke(() =>
  214. {
  215. CTTotalSeconds = e.TotalSeconds;
  216. });
  217. }
  218. }
  219. catch (Exception)
  220. {
  221. }
  222. }
  223. #region 方法
  224. bool isFirst = true;
  225. private void OnLoad()
  226. {
  227. if (!isFirst)
  228. {
  229. return;
  230. }
  231. InitLockCurvePlotModel();
  232. isFirst = false;
  233. if (_systemDatabaseService.GetCurrentUser().userPart == Enums.UserPart.Operator)
  234. {
  235. IsAllowEdit = false;
  236. }
  237. else
  238. {
  239. IsAllowEdit = true;
  240. }
  241. }
  242. async void ExecuteLoadProductCommand(ProductModel product)
  243. {
  244. if (product == null) return;
  245. var view = new ShowMessage(Lang.是否加载产品, $"{Lang.加载产品}:{product.Name}?");
  246. //show the dialog
  247. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  248. if (result != null && result is bool)
  249. {
  250. if (((bool)result))
  251. {
  252. IsLoadProduct = false;
  253. var waiting = new WaitingControl();
  254. //show the dialog
  255. var task = DialogHost.Show(waiting, "RootDialog", null, null, null);
  256. await _productService.LoadProductAsync(product.ID);
  257. _productService.SetCurrentProduct(product.ID);
  258. await management.WriteParameterToPlcExAsync();
  259. if (DialogHost.IsDialogOpen("RootDialog"))
  260. {
  261. DialogHost.Close("RootDialog");
  262. }
  263. await task;
  264. //DatabaseHelper.AddLoadProductRecord(product.ID, product.Name, false);
  265. IsLoadProduct = true;
  266. }
  267. }
  268. }
  269. void ExecuteCopyCommand(string textToCopy)
  270. {
  271. Clipboard.SetText(textToCopy);
  272. }
  273. /// <summary>
  274. /// 添加消息至消息队列
  275. /// </summary>
  276. /// <param name="msg"></param>
  277. /// <param name="color"></param>
  278. public void AddMessage(string msg, Color color)
  279. {
  280. App.Current.Dispatcher.BeginInvoke(new Action(() =>
  281. {
  282. try
  283. {
  284. MessageQueue.Add(new TaskMessage()
  285. {
  286. DT = DateTime.Now,
  287. Message = msg,
  288. FontColol = new SolidColorBrush(color)
  289. });
  290. LogHelper.WriteLogInfo(msg);
  291. }
  292. catch (Exception)
  293. {
  294. }
  295. }));
  296. }
  297. /// <summary>
  298. /// 添加消息至消息队列
  299. /// </summary>
  300. /// <param name="msg"></param>
  301. /// <param name="color"></param>
  302. public void AddMessage(string msg, MessageLevel level)
  303. {
  304. MessageStruct messageStruct = new MessageStruct() { Message = msg, level = level };
  305. App.Current.Dispatcher.BeginInvoke(new Action(() =>
  306. {
  307. AddMessage(messageStruct);
  308. }));
  309. }
  310. /// <summary>
  311. /// 添加消息至消息队列
  312. /// </summary>
  313. /// <param name="messageStruct"></param>
  314. public void AddMessage(MessageStruct messageStruct)
  315. {
  316. try
  317. {
  318. if (MessageQueue.Count >= 100)
  319. {
  320. MessageQueue.RemoveAt(0);// 从队列头部移除最早的消息
  321. }
  322. Color color = Colors.Black;
  323. switch (messageStruct.level)
  324. {
  325. case MessageLevel.Info:
  326. color = Colors.Black;
  327. break;
  328. case MessageLevel.Debug:
  329. color = Colors.Green;
  330. break;
  331. case MessageLevel.Alarm:
  332. color = Colors.Orange;
  333. break;
  334. case MessageLevel.Error:
  335. color = Colors.Red;
  336. break;
  337. }
  338. MessageQueue.Add(new TaskMessage()
  339. {
  340. DT = DateTime.Now,
  341. Message = messageStruct.Message,
  342. FontColol = new SolidColorBrush(color)
  343. });
  344. LogHelper.WriteLogInfo(messageStruct.Message);
  345. }
  346. catch (Exception)
  347. {
  348. }
  349. }
  350. public void AddMessageInvoke(MessageStruct messageStruct)
  351. {
  352. App.Current.Dispatcher.BeginInvoke(new Action(() =>
  353. {
  354. AddMessage(messageStruct);
  355. }));
  356. }
  357. private void LoginChange(Models.User user)
  358. {
  359. if (user.userPart == Enums.UserPart.Operator)
  360. {
  361. IsAllowEdit = false;
  362. }
  363. else
  364. {
  365. IsAllowEdit = true;
  366. }
  367. }
  368. /// <summary>
  369. /// 重置计数
  370. /// </summary>
  371. async void ExecuteResetCounterCommand()
  372. {
  373. var currentProduct = _productService.GetCurrentProduct();
  374. if (currentProduct == null) return;
  375. var view = new ShowMessage(Lang.计数清零, Lang.是否重置当前产品的计数);
  376. //show the dialog
  377. var result = await DialogHost.Show(view, "RootDialog", null, null, null);
  378. if (result != null && result is bool)
  379. {
  380. if (((bool)result))
  381. {
  382. isCanExecute = false;
  383. await _systemDatabaseService.ResetProductionAsync(currentProduct.Name);
  384. await _systemDatabaseService.ResetThrowNumberAsync();
  385. isCanExecute = true;
  386. }
  387. }
  388. }
  389. /// <summary>
  390. /// 初始化曲线图
  391. /// </summary>
  392. private void InitLockCurvePlotModel()
  393. {
  394. var model = new PlotModel
  395. {
  396. Title = Lang.锁付曲线,
  397. TitleFontSize = 16
  398. };
  399. // 图例
  400. var legend = new Legend
  401. {
  402. LegendPosition = LegendPosition.TopRight,
  403. LegendPlacement = LegendPlacement.Outside,
  404. LegendOrientation = LegendOrientation.Vertical,
  405. LegendBorderThickness = 0
  406. };
  407. model.Legends.Add(legend);
  408. // X轴(圈数)
  409. var xAxis = new LinearAxis
  410. {
  411. Key = "X",
  412. Position = AxisPosition.Bottom,
  413. Title = Lang.锁付圈数,
  414. Minimum = 0,
  415. MajorGridlineStyle = LineStyle.Solid,
  416. MinorGridlineStyle = LineStyle.Dot
  417. };
  418. model.Axes.Add(xAxis);
  419. // Y轴(力矩)
  420. var yAxis = new LinearAxis
  421. {
  422. Key = "Y",
  423. Position = AxisPosition.Left,
  424. Title = Lang.力矩kgf,
  425. MajorGridlineStyle = LineStyle.Solid,
  426. MinorGridlineStyle = LineStyle.Dot
  427. };
  428. model.Axes.Add(yAxis);
  429. // 实际锁付曲线
  430. var torqueSeries = new LineSeries
  431. {
  432. Title = Lang.锁付力矩曲线,
  433. StrokeThickness = 2,
  434. MarkerSize = 3,
  435. MarkerType = MarkerType.Circle,
  436. CanTrackerInterpolatePoints = false,
  437. Color = OxyColors.Blue
  438. };
  439. model.Series.Add(torqueSeries);
  440. // 上限线
  441. var upperLimitSeries = new LineSeries
  442. {
  443. Title = "Torque Upper Limit",
  444. StrokeThickness = 2,
  445. LineStyle = LineStyle.Dash,
  446. Color = OxyColors.Red
  447. };
  448. model.Series.Add(upperLimitSeries);
  449. // 下限线
  450. var lowerLimitSeries = new LineSeries
  451. {
  452. Title = "Torque Lower Limit",
  453. StrokeThickness = 2,
  454. LineStyle = LineStyle.Dash,
  455. Color = OxyColors.Green
  456. };
  457. model.Series.Add(lowerLimitSeries);
  458. LockCurvePlotModel = model;
  459. }
  460. /// <summary>
  461. /// 螺丝锁付完成时调用
  462. /// </summary>
  463. /// <param name="result"></param>
  464. private void OnLockFinish(LockResult result)
  465. {
  466. //如果当前螺丝编号为1,则清除之前的结果
  467. if (result.Number == 1)
  468. {
  469. LockResults.Clear();
  470. }
  471. LockResults.Add(result);
  472. App.Current.Dispatcher.BeginInvoke(new Action(() =>
  473. {
  474. try
  475. {
  476. //获取三条曲线
  477. var torqueSeries = LockCurvePlotModel.Series[0] as LineSeries; //实际扭矩
  478. var upperSeries = LockCurvePlotModel.Series[1] as LineSeries; //上限
  479. var lowerSeries = LockCurvePlotModel.Series[2] as LineSeries; //下限
  480. torqueSeries.Points.Clear();
  481. upperSeries.Points.Clear();
  482. lowerSeries.Points.Clear();
  483. //上下限值(根据你的工艺设置)
  484. double upperLimit = result.TargetTorqueUpperLimit; //上限扭矩
  485. double lowerLimit = result.TargetTorqueLowerLimit; //下限扭矩
  486. foreach (var waveData in result.WaveDatas)
  487. {
  488. //实际曲线
  489. torqueSeries.Points.Add(new OxyPlot.DataPoint(waveData.Turns, waveData.Torque));
  490. //上限曲线(固定扭矩)
  491. upperSeries.Points.Add(new OxyPlot.DataPoint(waveData.Turns, upperLimit));
  492. //下限曲线
  493. lowerSeries.Points.Add(new OxyPlot.DataPoint(waveData.Turns, lowerLimit));
  494. }
  495. //刷新
  496. LockCurvePlotModel.ResetAllAxes();
  497. LockCurvePlotModel.InvalidatePlot(true);
  498. //保存图片
  499. if (!string.IsNullOrEmpty(result.LockCurveImagePath))
  500. {
  501. PngExporter.Export(
  502. LockCurvePlotModel,
  503. result.LockCurveImagePath,
  504. (int)LockCurvePlotModel.Width,
  505. (int)LockCurvePlotModel.Height);
  506. }
  507. }
  508. catch (Exception ex)
  509. {
  510. LogHelper.WriteLogError("更新曲线图和保存曲线图时出错!", ex);
  511. }
  512. }));
  513. }
  514. /// <summary>
  515. /// 添加螺丝
  516. /// </summary>
  517. private void ExecuteAddScrewCommand()
  518. {
  519. //_dialogService.ShowDialog("AddScrew", rst =>
  520. //{
  521. // //对话框关闭之后的回调函数,可以在这解析结果。
  522. // ButtonResult result1 = rst.Result;
  523. // if (result1 == ButtonResult.OK)
  524. // {
  525. // _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  526. // }
  527. //});
  528. var dialog = new AddScrewView();
  529. dialog.Owner = Application.Current.MainWindow;
  530. // 显示弹窗,扫码枪结果会自动显示在输入框中
  531. if (dialog.ShowDialog() == true)
  532. {
  533. string scanResult = dialog.ScannedBarcode;
  534. _eventAggregator.GetEvent<SnackbarMessageNotification>()
  535. ?.Publish(new MessageParameter()
  536. {
  537. Msg = $"已扫描:{scanResult}",
  538. Duration = 1
  539. });
  540. // 这里可以继续处理你的添加螺丝逻辑
  541. // AddScrew(scanResult);
  542. }
  543. }
  544. /// <summary>
  545. /// 更换披头
  546. /// </summary>
  547. void ExecuteChangeHeaderCommand()
  548. {
  549. _dialogService.ShowDialog("ChangeHeader", rst =>
  550. {
  551. //对话框关闭之后的回调函数,可以在这解析结果。
  552. ButtonResult result1 = rst.Result;
  553. if (result1 == ButtonResult.OK)
  554. {
  555. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  556. }
  557. });
  558. }
  559. /// <summary>
  560. /// 更换吸嘴
  561. /// </summary>
  562. void ExecuteChangeNozzleCommand()
  563. {
  564. _dialogService.ShowDialog("ChangeNozzle", rst =>
  565. {
  566. //对话框关闭之后的回调函数,可以在这解析结果。
  567. ButtonResult result1 = rst.Result;
  568. if (result1 == ButtonResult.OK)
  569. {
  570. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  571. }
  572. });
  573. }
  574. void ExecuteTorqueInspectionCommand()
  575. {
  576. _dialogService.ShowDialog("TorqueCheck", rst =>
  577. {
  578. //对话框关闭之后的回调函数,可以在这解析结果。
  579. ButtonResult result1 = rst.Result;
  580. if (result1 == ButtonResult.OK)
  581. {
  582. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  583. }
  584. });
  585. }
  586. void ExecuteSetAlarmValueCommand()
  587. {
  588. _dialogService.ShowDialog("SetAlarmValue", rst =>
  589. {
  590. //对话框关闭之后的回调函数,可以在这解析结果。
  591. ButtonResult result1 = rst.Result;
  592. if (result1 == ButtonResult.OK)
  593. {
  594. _eventAggregator.GetEvent<SnackbarMessageNotification>().Publish(new MessageParameter() { Msg = Lang.完成, Duration = 1 });
  595. }
  596. });
  597. }
  598. /// <summary>
  599. /// 保压完成时调用
  600. /// </summary>
  601. /// <param name="tuple"></param>
  602. private void OnPressureFinish((bool IsSuccess, (DateTime Timestamp, float Value)[] values, string SavePath) tuple)
  603. {
  604. //更新对应传感器的曲线图
  605. //App.Current.Dispatcher.BeginInvoke(new Action(() =>
  606. //{
  607. // var model = PressurePlotModel;
  608. // var series = model.Series[0] as LineSeries;
  609. // series.Points.Clear();
  610. // //第一个点的时间作为X轴起点
  611. // DateTime xAxisStart = tuple.values[0].Timestamp;
  612. // foreach (var dataPoint in tuple.values)
  613. // {
  614. // // 计算相对于起点的秒数作为X轴坐标
  615. // double xValue = (dataPoint.Timestamp - xAxisStart).TotalSeconds;
  616. // series.Points.Add(new OxyPlot.DataPoint(xValue, dataPoint.Value));
  617. // }
  618. // // 重置所有轴的范围以适应新数据
  619. // model.ResetAllAxes();
  620. // model.InvalidatePlot(true);
  621. // if (!string.IsNullOrEmpty(tuple.SavePath))
  622. // {
  623. // if (!Directory.Exists(Path.GetDirectoryName(tuple.SavePath)))
  624. // {
  625. // Directory.CreateDirectory(Path.GetDirectoryName(tuple.SavePath));
  626. // }
  627. // PngExporter.Export(model, tuple.SavePath, (int)model.Width, (int)model.Height);
  628. // }
  629. //}));
  630. }
  631. // 初始化由配置决定的曲线数量(在读取 SystemConfiguration 后调用)
  632. public void InitPressurePlots()
  633. {
  634. var model = CreatePlotModelForSensor();
  635. PressurePlotModel = model;
  636. }
  637. private PlotModel CreatePlotModelForSensor()
  638. {
  639. var model = new PlotModel { Title = $"压力曲线", TitleFontSize = 16 };
  640. // 设置图例
  641. var legend = new Legend
  642. {
  643. LegendPosition = LegendPosition.TopRight,
  644. LegendPlacement = LegendPlacement.Outside,
  645. LegendOrientation = LegendOrientation.Vertical,
  646. LegendBorderThickness = 0
  647. };
  648. model.Legends.Add(legend);
  649. // 设置X轴
  650. var xAxis = new LinearAxis
  651. {
  652. Key = "X",
  653. Position = AxisPosition.Bottom,
  654. Title = "Time (s)",
  655. Minimum = 0,
  656. MajorGridlineStyle = LineStyle.Solid,
  657. MinorGridlineStyle = LineStyle.Dot
  658. };
  659. model.Axes.Add(xAxis);
  660. // 设置Y轴
  661. var yAxis = new LinearAxis
  662. {
  663. Key = "Y",
  664. Position = AxisPosition.Left,
  665. Title = "Pressure",
  666. MajorGridlineStyle = LineStyle.Solid,
  667. MinorGridlineStyle = LineStyle.Dot
  668. };
  669. model.Axes.Add(yAxis);
  670. // 添加示例数据系列
  671. var series = new LineSeries
  672. {
  673. Title = $"Sensor 1",
  674. StrokeThickness = 2,
  675. MarkerSize = 3,
  676. MarkerType = MarkerType.Circle,
  677. CanTrackerInterpolatePoints = false,
  678. };
  679. model.Series.Add(series);
  680. return model;
  681. }
  682. #endregion
  683. #region 继承
  684. /// <summary>
  685. /// 确认导航请求时调用。此方法允许您在导航之前执行一些操作。
  686. /// </summary>
  687. /// <param name="navigationContext"></param>
  688. /// <param name="continuationCallback"></param>
  689. public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
  690. {
  691. continuationCallback(true);
  692. }
  693. /// <summary>
  694. /// 接收导航请求时调用。传入导航参数,包含有关导航目标的信息。
  695. /// </summary>
  696. /// <param name="navigationContext"></param>
  697. /// <exception cref="NotImplementedException"></exception>
  698. public async void OnNavigatedTo(NavigationContext navigationContext)
  699. {
  700. try
  701. {
  702. ProductList = new ObservableCollection<ProductModel>(_productService.GetAllProducts());
  703. }
  704. catch (Exception ex)
  705. {
  706. LogHelper.WriteLogError(Lang.主界面界面进入时出错, ex);
  707. }
  708. }
  709. /// <summary>
  710. /// 是否允许导航到此视图模型。
  711. /// </summary>
  712. /// <param name="navigationContext"></param>
  713. /// <returns></returns>
  714. public bool IsNavigationTarget(NavigationContext navigationContext)
  715. {
  716. return true;
  717. }
  718. /// <summary>
  719. /// 导航离开此视图模型时调用。您可以在此处执行清理操作或保存状态。
  720. /// </summary>
  721. /// <param name="navigationContext"></param>
  722. public void OnNavigatedFrom(NavigationContext navigationContext)
  723. {
  724. }
  725. #endregion
  726. private void _productService_OnProductChanged(ProductModel obj)
  727. {
  728. App.Current.Dispatcher.BeginInvoke(new Action(() =>
  729. {
  730. CurrentProduct = obj;
  731. }));
  732. }
  733. }
  734. }