RandomMainPageViewModel.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using Avalonia;
  2. using Avalonia.Collections;
  3. using Avalonia.Controls;
  4. using Avalonia.Controls.ApplicationLifetimes;
  5. using OxyPlot;
  6. using OxyPlot.Series;
  7. using Shaker.Models;
  8. using ShakerApp.Tools;
  9. using ShakerApp.Views;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. namespace ShakerApp.ViewModels
  16. {
  17. internal class RandomMainPageViewModel:ViewModelBase<RandomDataModel>,IMainPageViewModel
  18. {
  19. [PropertyAssociation(nameof(RandomDataModel.CurrentIdentifyDisplacement))]
  20. public double CurrentIdentifyDisplacement => Model.CurrentIdentifyDisplacement;
  21. [PropertyAssociation(nameof(RandomDataModel.CurrentIdentifyRms))]
  22. public double CurrentIdentifyRms => Model.CurrentIdentifyRms;
  23. [PropertyAssociation(nameof(RandomDataModel.IdentifyIndex))]
  24. public double IdentifyIndex => Model.IdentifyIndex;
  25. [PropertyAssociation(nameof(RandomDataModel.RandomStatus))]
  26. public RandomStatus RandomStatus => Model.RandomStatus;
  27. [PropertyAssociation(nameof(RandomDataModel.TimeDomainRMS))]
  28. public double TimeDomainRMS => Model.TimeDomainRMS;
  29. [PropertyAssociation(nameof(RandomDataModel.RandomTestStep))]
  30. public RandomTestStep RandomTestStep => Model.RandomTestStep;
  31. [PropertyAssociation(nameof(RandomDataModel.RandomTestStep))]
  32. public string RandomTestStepKey => RandomTestStep.Description();
  33. [PropertyAssociation(nameof(RandomDataModel.CurrentTestLevel))]
  34. public double CurrentTestLevel => Model.CurrentTestLevel;
  35. [PropertyAssociation(nameof(RandomDataModel.CurrentTestTime))]
  36. public uint CurrentTestTime => Model.CurrentTestTime;
  37. [PropertyAssociation(nameof(RandomDataModel.CurrentLevelTestMaxTime))]
  38. public uint CurrentLevelTestMaxTime => Model.CurrentLevelTestMaxTime;
  39. public AvaloniaList<LineSeries> LineSeries { get; } = new AvaloniaList<LineSeries>();
  40. List<OxyColor> oxyColors = new List<OxyColor>() {OxyColors.Blue, OxyColors.Black, OxyColors.Green, OxyColors.Red, OxyColors.Red, OxyColors.Yellow, OxyColors.Yellow };
  41. List<LineStyle> lineStyles = new List<LineStyle>() { LineStyle.Solid, LineStyle.Solid, LineStyle.Solid, LineStyle.Solid, LineStyle.Solid, LineStyle.LongDashDotDot, LineStyle.LongDashDotDot };
  42. List<string> properties = new List<string>() {nameof(RandomData.Driver), nameof(RandomData.Acceleration), nameof(RandomData.TargetAcceleration), nameof(RandomData.UpStopAcceleration), nameof(RandomData.DownStopAcceleration), nameof(RandomData.UpWarnAcceleration), nameof(RandomData.DownWarnAcceleration) };
  43. List<RandomData> datas = new List<RandomData>();
  44. private RandomMainPageViewModel()
  45. {
  46. Content = typeof(Views.RandomMainPage);
  47. #region 加速度谱曲线
  48. PlotModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
  49. {
  50. MaximumPadding = 0,
  51. MinimumPadding = 0,
  52. Title = App.Current?.FindResource(ShakerConstant.FrequencyKey) + "",
  53. Unit = "Hz",
  54. MajorGridlineStyle = LineStyle.Solid,
  55. Position = OxyPlot.Axes.AxisPosition.Bottom,
  56. Key = "Freq"
  57. });
  58. PlotModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
  59. {
  60. Key = "Ampt",
  61. Title = App.Current?.FindResource(ShakerConstant.AmptAxisTitleKey) + "",
  62. Unit = "g",
  63. MajorGridlineStyle = LineStyle.Solid,
  64. Position = OxyPlot.Axes.AxisPosition.Left,
  65. });
  66. PlotModel.Title = App.Current?.FindResource(MainPageType.RandomPage.Description()) + "";
  67. for (int i = 0; i < oxyColors.Count; i++)
  68. {
  69. LineSeries line = new LineSeries();
  70. line.Title = App.Current?.FindResource(ShakerConstant.ShowRandomAccelerationSpectrumNames[i]) + "";
  71. line.Color = oxyColors[i];
  72. line.StrokeThickness = 1;
  73. line.DataFieldX = nameof(SweepData.Frequency);
  74. line.DataFieldY = properties[i];
  75. line.LineStyle = lineStyles[i];
  76. line.XAxisKey = "Freq";
  77. line.YAxisKey = "Ampt";
  78. LineSeries.Add(line);
  79. PlotModel.Series.Add(line);
  80. }
  81. PlotModel.Legends.Add(new OxyPlot.Legends.Legend()
  82. {
  83. ShowInvisibleSeries = true,
  84. });
  85. PlotModel.Title = App.Current?.FindResource(MainPageType.RandomPage.Description()) + "";
  86. #endregion
  87. GetEvent(ShakerSettingViewModel.LANGUAGECHANGEDEVENT).Subscrip((_, _) =>
  88. {
  89. PlotModel.InvalidatePlot(false);
  90. PlotModel.Title = App.Current?.FindResource(MainPageType.RandomPage.Description()) + "";
  91. PlotModel.Axes[0].Title = App.Current?.FindResource("Frequency") + "";
  92. PlotModel.Axes[1].Title = Title = App.Current?.FindResource("Ampt") + "";
  93. PlotModel.InvalidatePlot(true);
  94. });
  95. GetEvent<AllConfig>().Subscrip((_, _) =>
  96. {
  97. CommunicationViewModel.Instance.LocalCommunication?.GetEvent<RandomDataModel>()?.Subscrip((_, args) =>
  98. {
  99. UpdateData(new List<IResultDataModel>() { args.Data });
  100. CommunicationViewModel.Instance.ServiceCommunication?.GetEvent<RandomDataModel>()?.Publish(this, args.Data);
  101. });
  102. CommunicationViewModel.Instance.LocalCommunication?.GetEvent(nameof(RandomDataModel.TransferFunction))?.Subscrip((sender, args) =>
  103. {
  104. if (args.Data.Length > 0 && args.Data[0] is double[] func)
  105. {
  106. Tools.DispatherInovke.Inovke(()=> ShowTransferFunction(func));
  107. CommunicationViewModel.Instance.ServiceCommunication?.GetEvent(nameof(RandomDataModel.TransferFunction))?.Publish(this, null, func);
  108. }
  109. });
  110. });
  111. }
  112. private async void ShowTransferFunction(double[] data)
  113. {
  114. if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
  115. {
  116. RandomTransferFunctionViewModel.Instance.InitData();
  117. RandomTransferFunctionViewModel.Instance.InitTransferFunctionData(data);
  118. RandomTransferFunctionViewModel.Instance.Title = "TransferFunction";
  119. BaseDialogWindow window = new BaseDialogWindow();
  120. window.DataContext = RandomTransferFunctionViewModel.Instance;
  121. RandomTransferFunctionViewModel.Instance.CloseWindowAction = () => window?.Close();
  122. await window.ShowDialog(desktop.MainWindow!);
  123. }
  124. }
  125. static RandomMainPageViewModel()
  126. {
  127. }
  128. public void Start()
  129. {
  130. SetRefSpectrum(RandomConfigViewModel.Instance.RefSpectrum);
  131. }
  132. public void Stop()
  133. {
  134. }
  135. public override void UpDateModel(RandomDataModel model)
  136. {
  137. base.UpDateModel(model);
  138. int startindex = (int)(RandomConfigViewModel.Instance.MinFrequency / RandomConfigViewModel.Instance.FrequencyResolution);
  139. for(int i=0;i<datas.Count;i++)
  140. {
  141. datas[i].SetAcceleration(model.CurrentAccelerationSynthesisPSD[i + startindex]);
  142. datas[i].SetDriver(model.DriverPSD[i + startindex]);
  143. }
  144. PlotModel.InvalidatePlot(false);
  145. for(int i=0;i<LineSeries.Count;i++)
  146. {
  147. LineSeries[i].ItemsSource = datas;
  148. }
  149. PlotModel.InvalidatePlot(true);
  150. }
  151. public void UpdateData(List<IResultDataModel> model)
  152. {
  153. if(model !=null &&model.Count >0 && model.First() is RandomDataModel randomdata)
  154. {
  155. UpDateModel(randomdata);
  156. }
  157. }
  158. public void SetRefSpectrum(List<RandomData> data)
  159. {
  160. datas.Clear();
  161. datas.AddRange(data);
  162. PlotModel.InvalidatePlot(false);
  163. for (int i = 0; i < LineSeries.Count; i++)
  164. {
  165. LineSeries[i].ItemsSource = datas;
  166. }
  167. PlotModel.InvalidatePlot(true);
  168. }
  169. public MainPageType PageType => MainPageType.RandomPage;
  170. public OxyPlot.PlotModel PlotModel { get; private set; } = new OxyPlot.PlotModel();
  171. public static RandomMainPageViewModel Instance { get; } = new RandomMainPageViewModel();
  172. }
  173. }