HomeViewModel.cs 28 KB

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