SineMainPageViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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.Net.Http.Headers;
  13. using System.Runtime.CompilerServices;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. namespace ShakerApp.ViewModels
  17. {
  18. internal class SineMainPageViewModel : ViewModelBase<SineDataModel>, IMainPageViewModel
  19. {
  20. List<OxyColor> oxyColors = new List<OxyColor>() {OxyColors.Black, OxyColors.Green, OxyColors.Red, OxyColors.Red, OxyColors.Yellow, OxyColors.Yellow };
  21. List<LineStyle> lineStyles = new List<LineStyle>() {LineStyle.Solid, LineStyle.Solid, LineStyle.Solid, LineStyle.Solid, LineStyle.LongDashDotDot, LineStyle.LongDashDotDot };
  22. List<string> properties = new List<string>() {nameof(SweepData.Acceleration), nameof(SweepData.TargetAcceleration), nameof(SweepData.UpStopAcceleration), nameof(SweepData.DownStopAcceleration), nameof(SweepData.UpWarnAcceleration), nameof(SweepData.DownWarnAcceleration) };
  23. List<SweepData> datas = new List<SweepData>();
  24. DateTime dateTime = DateTime.Now;
  25. OxyPlot.Annotations.ArrowAnnotation arrowAnnotation = new OxyPlot.Annotations.ArrowAnnotation();
  26. public AvaloniaList<LineSeries> LineSeries { get; } = new AvaloniaList<LineSeries>();
  27. private SineMainPageViewModel()
  28. {
  29. #region 加速度谱曲线
  30. PlotModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
  31. {
  32. MaximumPadding = 0,
  33. MinimumPadding = 0,
  34. Title = App.Current?.FindResource(ShakerConstant.FrequencyKey) + "",
  35. Unit = "Hz",
  36. MajorGridlineStyle = LineStyle.Solid,
  37. Position = OxyPlot.Axes.AxisPosition.Bottom,
  38. Key = "Freq"
  39. });
  40. PlotModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
  41. {
  42. Key = "Ampt",
  43. Title = App.Current?.FindResource(ShakerConstant.AmptAxisTitleKey) + "",
  44. Unit = "g",
  45. MajorGridlineStyle = LineStyle.Solid,
  46. Position = OxyPlot.Axes.AxisPosition.Left,
  47. });
  48. PlotModel.Title = App.Current?.FindResource(ShakerConstant.AccelerationSpectrumKey) + "";
  49. for (int i = 0; i < oxyColors.Count; i++)
  50. {
  51. LineSeries line = new LineSeries();
  52. line.Title = App.Current?.FindResource(ShakerConstant.ShowSweepAccelerationSpectrumNames[i]) + "";
  53. line.Color = oxyColors[i];
  54. line.StrokeThickness = 1;
  55. line.DataFieldX = nameof(SweepData.Frequency);
  56. line.DataFieldY = properties[i];
  57. line.LineStyle = lineStyles[i];
  58. line.XAxisKey = "Freq";
  59. line.YAxisKey = "Ampt";
  60. LineSeries.Add(line);
  61. PlotModel.Series.Add(line);
  62. }
  63. PlotModel.Annotations.Add(arrowAnnotation);
  64. arrowAnnotation.Selectable = false;
  65. arrowAnnotation.SelectionMode = OxyPlot.SelectionMode.Single;
  66. arrowAnnotation.Text = "Test";
  67. arrowAnnotation.TextColor = OxyColors.Transparent;
  68. arrowAnnotation.StartPoint = new DataPoint(8, 4);
  69. arrowAnnotation.EndPoint = new DataPoint();
  70. arrowAnnotation.Color = OxyColors.Transparent;
  71. PlotModel.Legends.Add(new OxyPlot.Legends.Legend()
  72. {
  73. ShowInvisibleSeries = true,
  74. });
  75. PlotModel.Title = App.Current?.FindResource(MainPageType.SinePage.Description()) + "";
  76. #endregion
  77. GetEvent(ShakerSettingViewModel.LANGUAGECHANGEDEVENT).Subscrip((_, _) =>
  78. {
  79. PlotModel.InvalidatePlot(false);
  80. PlotModel.Title = App.Current?.FindResource(MainPageType.SinePage.Description()) + "";
  81. PlotModel.Axes[0].Title = App.Current?.FindResource("Frequency") + "";
  82. PlotModel.Axes[1].Title = Title = App.Current?.FindResource("Ampt") + "";
  83. for(int i=0;i<LineSeries.Count;i++)
  84. {
  85. LineSeries[i].Title = App.Current?.FindResource(ShakerConstant.ShowSweepAccelerationSpectrumNames[i]) + "";
  86. }
  87. PlotModel.InvalidatePlot(true);
  88. });
  89. GetEvent<AllConfig>().Subscrip((_, _) =>
  90. {
  91. CommunicationViewModel.Instance.LocalCommunication?.GetEvent<List<SineDataModel>>()?.Subscrip((_, args) =>
  92. {
  93. UpdateData(args.Data.Cast<IResultDataModel>().ToList());
  94. CommunicationViewModel.Instance.ServiceCommunication?.GetEvent<List<SineDataModel>>()?.Publish(this, args.Data);
  95. });
  96. });
  97. }
  98. static SineMainPageViewModel()
  99. {
  100. }
  101. public void SetRefSpectrum(List<SweepData> data)
  102. {
  103. datas.Clear();
  104. datas.AddRange(data);
  105. PlotModel.InvalidatePlot(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(List<IResultDataModel> model)
  120. {
  121. if (model.Last() is SineDataModel sine)
  122. {
  123. if (sine.SweepDirection != Model.SweepDirection && sine.SweepStep != Shaker.Models.SweepStep.Stop)
  124. {
  125. SetRefSpectrum(SweepConfigViewModel.Instance.RefSpectrum);
  126. }
  127. UpDataModels(model.Cast<SineDataModel>().ToList());
  128. UpDateModel(sine);
  129. UpPlot();
  130. }
  131. }
  132. private void UpDataModels(List<SineDataModel> models)
  133. {
  134. models.ForEach(sine =>
  135. {
  136. double currentacc = Shaker.Models.Tools.Tools.VoltageToQuantities(sine.CurrentAcceleration, ShakerConfigViewModel.Instance.AccelerationSensitivity);
  137. uint currentfreq = (uint)(sine.CurrentFrequency * 1000_000);
  138. int index = -1;
  139. for (int i = 0; i < datas.Count; i++)
  140. {
  141. uint tempfreq = (uint)(datas[i].Frequency * 1000_000);
  142. if (tempfreq == currentfreq)
  143. {
  144. index = i;
  145. datas[i].SetAcceleration(currentacc);
  146. if (sine.SweepDirection == Shaker.Models.SweepDirection.Up)
  147. {
  148. for (int j = i - 1; j >= 0; j--)
  149. {
  150. if (datas[j].Frequency > SweepConfigViewModel.Instance.EndFrequency) break;
  151. if (double.IsNaN(datas[j].Acceleration))
  152. {
  153. datas[j].SetAcceleration(currentacc);
  154. }
  155. else break;
  156. }
  157. }
  158. else
  159. {
  160. for (int j = i + 1; j < datas.Count; j++)
  161. {
  162. if (datas[j].Frequency < SweepConfigViewModel.Instance.StartFrequency) break;
  163. if (double.IsNaN(datas[j].Acceleration))
  164. {
  165. datas[j].SetAcceleration(currentacc);
  166. }
  167. else break;
  168. }
  169. }
  170. return;
  171. }
  172. else if (tempfreq > currentfreq)
  173. {
  174. if (i > 0)
  175. {
  176. index = i - 1;
  177. }
  178. else index = 0;
  179. break;
  180. }
  181. }
  182. if (index == -1) index = datas.Count - 1;
  183. if (sine.SweepDirection == Shaker.Models.SweepDirection.Up)
  184. {
  185. for (int j = index; j >= 0; j--)
  186. {
  187. if (datas[j].Frequency > SweepConfigViewModel.Instance.EndFrequency) break;
  188. if (double.IsNaN(datas[j].Acceleration))
  189. {
  190. datas[j].SetAcceleration(currentacc);
  191. }
  192. else break;
  193. }
  194. }
  195. else
  196. {
  197. for (int j = index + 1; j < datas.Count; j++)
  198. {
  199. if (datas[j].Frequency < SweepConfigViewModel.Instance.StartFrequency) break;
  200. if (double.IsNaN(datas[j].Acceleration))
  201. {
  202. datas[j].SetAcceleration(currentacc);
  203. }
  204. else break;
  205. }
  206. }
  207. double value = 0; double upstop = 0; double upwarn = 0; double downstop = 0; double downwarn = 0;
  208. SweepConfigViewModel.Instance.Model.CalcAmpt(sine.CurrentFrequency, ref value, ref upstop, ref upwarn, ref downstop, ref downwarn);
  209. datas.Insert(index + 1, new SweepData(sine.CurrentFrequency,currentacc, value, upstop, downstop, upwarn, downwarn));
  210. });
  211. }
  212. private void UpPlot()
  213. {
  214. PlotModel.InvalidatePlot(false);
  215. if (Model.SweepStep == Shaker.Models.SweepStep.Stop)
  216. {
  217. arrowAnnotation.TextColor = OxyColors.Transparent;
  218. arrowAnnotation.Color = OxyColors.Transparent;
  219. }
  220. else
  221. {
  222. arrowAnnotation.TextColor = OxyColors.Black;
  223. arrowAnnotation.Color = OxyColors.Green;
  224. arrowAnnotation.EndPoint = new DataPoint(CurrentFrequency, CurrentAcceleration);
  225. arrowAnnotation.StartPoint = new DataPoint(CurrentFrequency, 1E-30);
  226. arrowAnnotation.Text = $"{App.Current?.FindResource("Frequency")}:{CurrentFrequency:F2}Hz\r\n{App.Current?.FindResource("Acceleration")}:{CurrentAcceleration:F3}g";
  227. arrowAnnotation.TextPosition = arrowAnnotation.EndPoint;
  228. arrowAnnotation.TextHorizontalAlignment = HorizontalAlignment.Left;
  229. arrowAnnotation.TextVerticalAlignment = VerticalAlignment.Top;
  230. }
  231. for(int i=0;i<LineSeries.Count;i++)
  232. {
  233. LineSeries[i].ItemsSource = datas;
  234. }
  235. PlotModel.InvalidatePlot(true);
  236. }
  237. [PropertyAssociation(nameof(SineDataModel.TotalTime))]
  238. public double TotalTime { get => Model.TotalTime; set => SetProperty(ref Model.TotalTime, value); }
  239. [PropertyAssociation(nameof(SineDataModel.RunTime))]
  240. public double RunTime { get => Model.RunTime; set => SetProperty(ref Model.RunTime, value); }
  241. [PropertyAssociation(nameof(SineDataModel.CurrentFrequency))]
  242. public double CurrentFrequency { get => Model.CurrentFrequency; set => SetProperty(ref Model.CurrentFrequency, value); }
  243. [PropertyAssociation(nameof(SineDataModel.CurrentAcceleration))]
  244. public double CurrentAcceleration { get => Shaker.Models.Tools.Tools.VoltageToQuantities(Model.CurrentAcceleration, ShakerConfigViewModel.Instance.AccelerationSensitivity); set => SetProperty(ref Model.CurrentAcceleration, value); }
  245. [PropertyAssociation(nameof(SineDataModel.SweepDirection))]
  246. public string SweepDirection { get => Model.SweepDirection.Description(); }
  247. [PropertyAssociation(nameof(SineDataModel.SweepStep))]
  248. public string SweepStep { get => Model.SweepStep.Description(); }
  249. [PropertyAssociation(nameof(SineDataModel.SweepIndex))]
  250. public uint SweepIndex { get => Model.SweepIndex; set => SetProperty(ref Model.SweepIndex, value); }
  251. [PropertyAssociation(nameof(SineDataModel.MainPageType))]
  252. public MainPageType PageType => Model.MainPageType;
  253. public OxyPlot.PlotModel PlotModel { get; private set; } = new OxyPlot.PlotModel();
  254. public static SineMainPageViewModel Instance { get; } = new SineMainPageViewModel();
  255. public OxyPlot.PlotModel DriverModel { get; private set; } = new PlotModel();
  256. }
  257. }