SineMainPageViewModel.cs 10 KB

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