SineMainPageViewModel.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using Avalonia.Collections;
  2. using Avalonia.Controls;
  3. using OxyPlot;
  4. using OxyPlot.Series;
  5. using Shaker.Models;
  6. using Shaker.Models.Tools;
  7. using ShakerApp.Tools;
  8. using ShakerApp.Views;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. namespace ShakerApp.ViewModels
  15. {
  16. internal class SineMainPageViewModel : ViewModelBase<SineDataModel>, IMainPageViewModel
  17. {
  18. List<OxyColor> oxyColors = new List<OxyColor>() {OxyColors.Black, OxyColors.Green, OxyColors.Red, OxyColors.Red, OxyColors.Yellow, OxyColors.Yellow };
  19. List<LineStyle> lineStyles = new List<LineStyle>() {LineStyle.Solid, LineStyle.Solid, LineStyle.Solid, LineStyle.Solid, LineStyle.LongDashDotDot, LineStyle.LongDashDotDot };
  20. List<string> properties = new List<string>() {nameof(SweepData.Acceleration), nameof(SweepData.TargetAcceleration), nameof(SweepData.UpStopAcceleration), nameof(SweepData.DownStopAcceleration), nameof(SweepData.UpWarnAcceleration), nameof(SweepData.DownWarnAcceleration) };
  21. List<SweepData> datas = new List<SweepData>();
  22. OxyPlot.Annotations.ArrowAnnotation arrowAnnotation = new OxyPlot.Annotations.ArrowAnnotation();
  23. public AvaloniaList<LineSeries> LineSeries { get; } = new AvaloniaList<LineSeries>();
  24. private SineMainPageViewModel()
  25. {
  26. #region 加速度谱曲线
  27. PlotModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
  28. {
  29. MaximumPadding = 0,
  30. MinimumPadding = 0,
  31. Title = App.Current?.FindResource(ShakerConstant.FrequencyKey) + "",
  32. Unit = "Hz",
  33. MajorGridlineStyle = LineStyle.Solid,
  34. Position = OxyPlot.Axes.AxisPosition.Bottom,
  35. Key = "Freq"
  36. });
  37. PlotModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
  38. {
  39. Key = "Ampt",
  40. Title = App.Current?.FindResource(ShakerConstant.AmptAxisTitleKey) + "",
  41. Unit = "g",
  42. MajorGridlineStyle = LineStyle.Solid,
  43. Position = OxyPlot.Axes.AxisPosition.Left,
  44. });
  45. PlotModel.Title = App.Current?.FindResource(ShakerConstant.AccelerationSpectrumKey) + "";
  46. for (int i = 0; i < oxyColors.Count; i++)
  47. {
  48. LineSeries line = new LineSeries();
  49. line.Title = App.Current?.FindResource(ShakerConstant.ShowSweepAccelerationSpectrumNames[i]) + "";
  50. line.Color = oxyColors[i];
  51. line.StrokeThickness = 1;
  52. line.DataFieldX = nameof(SweepData.Frequency);
  53. line.DataFieldY = properties[i];
  54. line.LineStyle = lineStyles[i];
  55. line.XAxisKey = "Freq";
  56. line.YAxisKey = "Ampt";
  57. LineSeries.Add(line);
  58. PlotModel.Series.Add(line);
  59. }
  60. PlotModel.Annotations.Add(arrowAnnotation);
  61. arrowAnnotation.Selectable = false;
  62. arrowAnnotation.SelectionMode = OxyPlot.SelectionMode.Single;
  63. arrowAnnotation.Text = "Test";
  64. arrowAnnotation.TextColor = OxyColors.Transparent;
  65. arrowAnnotation.StartPoint = new DataPoint(8, 4);
  66. arrowAnnotation.EndPoint = new DataPoint();
  67. arrowAnnotation.Color = OxyColors.Transparent;
  68. PlotModel.Legends.Add(new OxyPlot.Legends.Legend()
  69. {
  70. ShowInvisibleSeries = true,
  71. });
  72. PlotModel.Title = App.Current?.FindResource(MainPageType.SinePage.Description()) + "";
  73. #endregion
  74. GetEvent(ShakerSettingViewModel.LANGUAGECHANGEDEVENT).Subscrip((_, _) =>
  75. {
  76. PlotModel.InvalidatePlot(false);
  77. PlotModel.Title = App.Current?.FindResource(MainPageType.SinePage.Description()) + "";
  78. PlotModel.Axes[0].Title = App.Current?.FindResource("Frequency") + "";
  79. PlotModel.Axes[1].Title = Title = App.Current?.FindResource("Ampt") + "";
  80. for(int i=0;i<LineSeries.Count;i++)
  81. {
  82. LineSeries[i].Title = App.Current?.FindResource(ShakerConstant.ShowSweepAccelerationSpectrumNames[i]) + "";
  83. }
  84. PlotModel.InvalidatePlot(true);
  85. });
  86. GetEvent<AllConfig>().Subscrip((_, _) =>
  87. {
  88. CommunicationViewModel.Intance.LocalCommunication?.GetEvent<SineDataModel>().Subscrip((_, args) =>
  89. {
  90. UpdateData(args.Data);
  91. });
  92. });
  93. }
  94. static SineMainPageViewModel()
  95. {
  96. }
  97. public void SetRefSpectrum(List<SweepData> data)
  98. {
  99. datas.Clear();
  100. datas.AddRange(data);
  101. PlotModel.InvalidatePlot(false);
  102. //LineSeries[0].IsVisible = false;
  103. for(int i=0;i<LineSeries.Count;i++)
  104. {
  105. LineSeries[i].ItemsSource = datas;
  106. }
  107. PlotModel.InvalidatePlot(true);
  108. }
  109. public void Start()
  110. {
  111. SetRefSpectrum(SweepConfigViewModel.Instance.RefSpectrum);
  112. }
  113. public void Stop()
  114. {
  115. }
  116. public void UpdateData(IResultDataModel model)
  117. {
  118. if (model is SineDataModel sine)
  119. {
  120. if(sine.SweepDirection != Model.SweepDirection)
  121. {
  122. SetRefSpectrum(SweepConfigViewModel.Instance.RefSpectrum);
  123. }
  124. UpDateModel(sine);
  125. uint targetacc = (uint)sine.CurrentAcceleration * 1000_1000;
  126. int index = -1;
  127. for (int i = 0; i < datas.Count; i++)
  128. {
  129. uint tempacc = (uint)(datas[i].Acceleration * 1000_000);
  130. if (tempacc == targetacc)
  131. {
  132. index = i;
  133. datas[i].Acceleration = sine.CurrentAcceleration;
  134. UpPlot();
  135. return;
  136. }
  137. else if (tempacc > targetacc)
  138. {
  139. if (i > 0)
  140. {
  141. index = i - 1;
  142. }
  143. else index = 0;
  144. break;
  145. }
  146. }
  147. if (index == -1) index = datas.Count - 1;
  148. double value = 0; double upstop = 0; double upwarn = 0; double downstop = 0; double downwarn = 0;
  149. SweepConfigViewModel.Instance.Model.CalcAmpt(sine.CurrentFrequency, ref value, ref upstop, ref upwarn, ref downstop, ref downwarn);
  150. datas.Insert(index + 1, new SweepData()
  151. {
  152. Acceleration = sine.CurrentAcceleration,
  153. Frequency = sine.CurrentFrequency,
  154. TargetAcceleration = value,
  155. UpStopAcceleration = upstop,
  156. UpWarnAcceleration = upwarn,
  157. DownStopAcceleration = downstop,
  158. DownWarnAcceleration = downwarn,
  159. });
  160. UpPlot();
  161. }
  162. }
  163. private void UpPlot()
  164. {
  165. PlotModel.InvalidatePlot(false);
  166. arrowAnnotation.TextColor = OxyColors.Black;
  167. arrowAnnotation.Color = OxyColors.Green;
  168. arrowAnnotation.EndPoint = new DataPoint(CurrentFrequency, CurrentAcceleration);
  169. arrowAnnotation.StartPoint = new DataPoint(CurrentFrequency, 1E-30);
  170. arrowAnnotation.Text = $"{App.Current?.FindResource("Frequency")}:{CurrentFrequency:F2}Hz\r\n{App.Current?.FindResource("Acceleration")}:{CurrentAcceleration:F3}g";
  171. arrowAnnotation.TextPosition = arrowAnnotation.EndPoint;
  172. arrowAnnotation.TextHorizontalAlignment = HorizontalAlignment.Left;
  173. arrowAnnotation.TextVerticalAlignment = VerticalAlignment.Top;
  174. for(int i=0;i<LineSeries.Count;i++)
  175. {
  176. LineSeries[i].ItemsSource = datas;
  177. }
  178. PlotModel.InvalidatePlot(true);
  179. }
  180. [PropertyAssociation(nameof(SineDataModel.TotalTime))]
  181. public double TotalTime { get => Model.TotalTime; set => SetProperty(ref Model.TotalTime, value); }
  182. [PropertyAssociation(nameof(SineDataModel.RunTime))]
  183. public double RunTime { get => Model.RunTime; set => SetProperty(ref Model.RunTime, value); }
  184. [PropertyAssociation(nameof(SineDataModel.CurrentFrequency))]
  185. public double CurrentFrequency { get => Model.CurrentFrequency; set => SetProperty(ref Model.CurrentFrequency, value); }
  186. [PropertyAssociation(nameof(SineDataModel.CurrentAcceleration))]
  187. public double CurrentAcceleration { get => Model.CurrentAcceleration; set => SetProperty(ref Model.CurrentAcceleration, value); }
  188. [PropertyAssociation(nameof(SineDataModel.SweepDirection))]
  189. public SweepDirection SweepDirection { get => Model.SweepDirection; set => SetProperty(ref Model.SweepDirection, value); }
  190. [PropertyAssociation(nameof(SineDataModel.SweepStep))]
  191. public SweepStep SweepStep { get => Model.SweepStep; set => SetProperty(ref Model.SweepStep, value); }
  192. [PropertyAssociation(nameof(SineDataModel.SweepIndex))]
  193. public uint SweepIndex { get => Model.SweepIndex; set => SetProperty(ref Model.SweepIndex, value); }
  194. [PropertyAssociation(nameof(SineDataModel.MainPageType))]
  195. public MainPageType PageType => Model.MainPageType;
  196. public OxyPlot.PlotModel PlotModel { get; private set; } = new OxyPlot.PlotModel();
  197. public static SineMainPageViewModel Instance { get; } = new SineMainPageViewModel();
  198. public OxyPlot.PlotModel DriverModel { get; private set; } = new PlotModel();
  199. }
  200. }