SineMainPageViewModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. using Avalonia.Collections;
  2. using Avalonia.Controls;
  3. using OxyPlot;
  4. using OxyPlot.Series;
  5. using ParquetSharp;
  6. using Shaker.Models;
  7. using Shaker.Models.Tools;
  8. using ShakerApp.Tools;
  9. using ShakerApp.Views;
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Net.Http.Headers;
  15. using System.Runtime.CompilerServices;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. namespace ShakerApp.ViewModels
  19. {
  20. internal class SineMainPageViewModel : BaseMainPageViewModel<SineDataModel>
  21. {
  22. public const string TDMSConfigName = "SweepConfig";
  23. public const string TDMSDataName = "SweepData";
  24. List<OxyColor> oxyColors = new List<OxyColor>() {OxyColors.Black, OxyColors.Green, OxyColors.Red, OxyColors.Red, OxyColors.Yellow, OxyColors.Yellow };
  25. List<LineStyle> lineStyles = new List<LineStyle>() {LineStyle.Solid, LineStyle.Solid, LineStyle.Solid, LineStyle.Solid, LineStyle.LongDashDotDot, LineStyle.LongDashDotDot };
  26. List<string> properties = new List<string>() {nameof(SweepData.Acceleration), nameof(SweepData.TargetAcceleration), nameof(SweepData.UpStopAcceleration), nameof(SweepData.DownStopAcceleration), nameof(SweepData.UpWarnAcceleration), nameof(SweepData.DownWarnAcceleration) };
  27. List<SweepData> datas = new List<SweepData>();
  28. DateTime dateTime = DateTime.Now;
  29. OxyPlot.Annotations.ArrowAnnotation arrowAnnotation = new OxyPlot.Annotations.ArrowAnnotation();
  30. public AvaloniaList<LineSeries> LineSeries { get; } = new AvaloniaList<LineSeries>();
  31. private SineMainPageViewModel()
  32. {
  33. #region 加速度谱曲线
  34. PlotModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
  35. {
  36. MaximumPadding = 0,
  37. MinimumPadding = 0,
  38. Title = App.Current?.FindResource(ShakerConstant.FrequencyKey) + "",
  39. Unit = "Hz",
  40. MajorGridlineStyle = LineStyle.Solid,
  41. Position = OxyPlot.Axes.AxisPosition.Bottom,
  42. Key = "Freq"
  43. });
  44. PlotModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
  45. {
  46. Key = "Ampt",
  47. Title = App.Current?.FindResource(ShakerConstant.AmptAxisTitleKey) + "",
  48. Unit = "g",
  49. MajorGridlineStyle = LineStyle.Solid,
  50. Position = OxyPlot.Axes.AxisPosition.Left,
  51. });
  52. PlotModel.Title = App.Current?.FindResource(ShakerConstant.AccelerationSpectrumKey) + "";
  53. for (int i = 0; i < oxyColors.Count; i++)
  54. {
  55. LineSeries line = new LineSeries();
  56. line.Title = App.Current?.FindResource(ShakerConstant.ShowSweepAccelerationSpectrumNames[i]) + "";
  57. line.Color = oxyColors[i];
  58. line.StrokeThickness = 1;
  59. line.DataFieldX = nameof(SweepData.Frequency);
  60. line.DataFieldY = properties[i];
  61. line.LineStyle = lineStyles[i];
  62. line.XAxisKey = "Freq";
  63. line.YAxisKey = "Ampt";
  64. LineSeries.Add(line);
  65. PlotModel.Series.Add(line);
  66. }
  67. PlotModel.Annotations.Add(arrowAnnotation);
  68. arrowAnnotation.Selectable = false;
  69. arrowAnnotation.SelectionMode = OxyPlot.SelectionMode.Single;
  70. arrowAnnotation.Text = "Test";
  71. arrowAnnotation.TextColor = OxyColors.Transparent;
  72. arrowAnnotation.StartPoint = new DataPoint(8, 4);
  73. arrowAnnotation.EndPoint = new DataPoint();
  74. arrowAnnotation.Color = OxyColors.Transparent;
  75. PlotModel.Legends.Add(new OxyPlot.Legends.Legend()
  76. {
  77. ShowInvisibleSeries = true,
  78. });
  79. PlotModel.Title = App.Current?.FindResource(MainPageType.SinePage.Description()) + "";
  80. #endregion
  81. GetEvent(ShakerSettingViewModel.LANGUAGECHANGEDEVENT).Subscrip((_, _) =>
  82. {
  83. PlotModel.InvalidatePlot(false);
  84. PlotModel.Title = App.Current?.FindResource(MainPageType.SinePage.Description()) + "";
  85. PlotModel.Axes[0].Title = App.Current?.FindResource("Frequency") + "";
  86. PlotModel.Axes[1].Title = Title = App.Current?.FindResource("Ampt") + "";
  87. for(int i=0;i<LineSeries.Count;i++)
  88. {
  89. LineSeries[i].Title = App.Current?.FindResource(ShakerConstant.ShowSweepAccelerationSpectrumNames[i]) + "";
  90. }
  91. PlotModel.InvalidatePlot(true);
  92. });
  93. GetEvent<AllConfig>().Subscrip((_, _) =>
  94. {
  95. CommunicationViewModel.Instance.LocalCommunication?.GetEvent<List<SineDataModel>>()?.Subscrip((_, args) =>
  96. {
  97. UpdateData(args.Data.Cast<IResultDataModel>().ToList());
  98. CommunicationViewModel.Instance.ServiceCommunication?.GetEvent<List<SineDataModel>>()?.Publish(this, args.Data);
  99. });
  100. });
  101. }
  102. static SineMainPageViewModel()
  103. {
  104. }
  105. public override void UpdataColnumInfo()
  106. {
  107. columns = new Column[ViewModels.ShakerConfigViewModel.Instance.Model.AnalogSignalConfigs.Count+3];
  108. for (int i = 0; i < ShakerConfigViewModel.Instance.Model.AnalogSignalConfigs.Count; i++)
  109. {
  110. columns[i] = new Column<double>($"{ShakerConfigViewModel.Instance.Model.AnalogSignalConfigs[i].Name}_{ShakerConfigViewModel.Instance.Model.AnalogSignalConfigs[i].Unit}_{ShakerConfigViewModel.Instance.Model.AnalogSignalConfigs[i].AnalogType}_{Models.DataAxisType.Linear}");
  111. }
  112. columns[^3] = new Column<double>($"Acceleration_g_{" "}_{Models.DataAxisType.Logarithm}");
  113. columns[^2] = new Column<double>($"Frequency_g_{" "}_{Models.DataAxisType.Logarithm}");
  114. columns[^1] = new Column<uint>($"SweepIndex_ _{" "}_{Models.DataAxisType.Logarithm}");
  115. }
  116. public override void SaveTdmsConfig(Dictionary<string, string> config)
  117. {
  118. base.SaveTdmsConfig(config);
  119. typeof(SweepConfigModel) .GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
  120. .Select(x => (x, x.GetValue(SweepConfigViewModel.Instance.Model)))
  121. .ToList()
  122. .ForEach(x =>
  123. {
  124. if (x.Item2 == null)
  125. {
  126. return;
  127. }
  128. if (x.x.FieldType.IsAssignableTo(typeof(IList)) || x.x.FieldType.IsArray)
  129. {
  130. config[$"{nameof(SweepConfigModel)}_{x.x.Name}"] = string.Join("", x.Item2.GetBytes().Select(x => $"{x:X2}"));
  131. }
  132. else
  133. {
  134. config[$"{nameof(SweepConfigModel)}_{x.x.Name}"] = x.Item2?.ToString() ?? string.Empty;
  135. }
  136. });
  137. }
  138. public void SetRefSpectrum(List<SweepData> data)
  139. {
  140. datas.Clear();
  141. datas.AddRange(data);
  142. PlotModel.InvalidatePlot(false);
  143. for(int i=0;i<LineSeries.Count;i++)
  144. {
  145. LineSeries[i].ItemsSource = datas;
  146. }
  147. PlotModel.InvalidatePlot(true);
  148. }
  149. public override void Start()
  150. {
  151. SetRefSpectrum(SweepConfigViewModel.Instance.RefSpectrum);
  152. lastwritefreq = 0;
  153. }
  154. public override void Stop()
  155. {
  156. }
  157. public override void UpdateData(List<IResultDataModel> model)
  158. {
  159. if (model.Last() is SineDataModel sine)
  160. {
  161. if (sine.SweepDirection != Model.SweepDirection && sine.SweepStep != Shaker.Models.SweepStep.Stop)
  162. {
  163. SetRefSpectrum(SweepConfigViewModel.Instance.RefSpectrum);
  164. }
  165. UpDataModels(model.Cast<SineDataModel>().ToList());
  166. UpDateModel(sine);
  167. UpPlot();
  168. }
  169. }
  170. private double lastwritefreq = 0;
  171. private void UpDataModels(List<SineDataModel> models)
  172. {
  173. models.ForEach(sine =>
  174. {
  175. double currentacc = Shaker.Models.Tools.Tools.VoltageToQuantities(sine.CurrentAcceleration, ShakerConfigViewModel.Instance.AccelerationSensitivity);
  176. uint currentfreq = (uint)(sine.CurrentFrequency * 1000_000);
  177. int index = -1;
  178. for (int i = 0; i < datas.Count; i++)
  179. {
  180. uint tempfreq = (uint)(datas[i].Frequency * 1000_000);
  181. if (tempfreq == currentfreq)
  182. {
  183. index = i;
  184. datas[i].SetAcceleration(currentacc);
  185. if (sine.SweepDirection == Shaker.Models.SweepDirection.Up)
  186. {
  187. for (int j = i - 1; j >= 0; j--)
  188. {
  189. if (datas[j].Frequency > SweepConfigViewModel.Instance.EndFrequency) break;
  190. if (double.IsNaN(datas[j].Acceleration))
  191. {
  192. datas[j].SetAcceleration(currentacc);
  193. }
  194. else break;
  195. }
  196. }
  197. else
  198. {
  199. for (int j = i + 1; j < datas.Count; j++)
  200. {
  201. if (datas[j].Frequency < SweepConfigViewModel.Instance.StartFrequency) break;
  202. if (double.IsNaN(datas[j].Acceleration))
  203. {
  204. datas[j].SetAcceleration(currentacc);
  205. }
  206. else break;
  207. }
  208. }
  209. return;
  210. }
  211. else if (tempfreq > currentfreq)
  212. {
  213. if (i > 0)
  214. {
  215. index = i - 1;
  216. }
  217. else index = 0;
  218. break;
  219. }
  220. }
  221. if (index == -1) index = datas.Count - 1;
  222. if (sine.SweepDirection == Shaker.Models.SweepDirection.Up)
  223. {
  224. for (int j = index; j >= 0; j--)
  225. {
  226. if (datas[j].Frequency > SweepConfigViewModel.Instance.EndFrequency) break;
  227. if (double.IsNaN(datas[j].Acceleration))
  228. {
  229. datas[j].SetAcceleration(currentacc);
  230. }
  231. else break;
  232. }
  233. }
  234. else
  235. {
  236. for (int j = index + 1; j < datas.Count; j++)
  237. {
  238. if (datas[j].Frequency < SweepConfigViewModel.Instance.StartFrequency) break;
  239. if (double.IsNaN(datas[j].Acceleration))
  240. {
  241. datas[j].SetAcceleration(currentacc);
  242. }
  243. else break;
  244. }
  245. }
  246. double value = 0; double upstop = 0; double upwarn = 0; double downstop = 0; double downwarn = 0;
  247. SweepConfigViewModel.Instance.Model.CalcAmpt(sine.CurrentFrequency, ref value, ref upstop, ref upwarn, ref downstop, ref downwarn);
  248. datas.Insert(index + 1, new SweepData(sine.CurrentFrequency,currentacc, value, upstop, downstop, upwarn, downwarn));
  249. });
  250. var file = ShakerDataViewModel.Instance.Writer;
  251. if (file == null) return;
  252. var savemodels = models.DistinctBy(x => x.CurrentFrequency).Where(x=>x.CurrentFrequency!=lastwritefreq).ToList();
  253. if (savemodels.Count == 0) return;
  254. lastwritefreq = savemodels[^1].CurrentFrequency;
  255. try
  256. {
  257. var writer = file.AppendBufferedRowGroup();
  258. writer.Column(ShakerConfigViewModel.Instance.AccelerationSensorCount).LogicalWriter<double>().WriteBatch(savemodels.Select(x => x.CurrentAcceleration).ToArray());
  259. writer.Column(ShakerConfigViewModel.Instance.AccelerationSensorCount + 1).LogicalWriter<double>().WriteBatch(savemodels.Select(x => x.CurrentFrequency).ToArray());
  260. writer.Column(ShakerConfigViewModel.Instance.AccelerationSensorCount + 2).LogicalWriter<uint>().WriteBatch(savemodels.Select(x => x.SweepIndex).ToArray());
  261. }
  262. catch
  263. {
  264. }
  265. }
  266. private void UpPlot()
  267. {
  268. PlotModel.InvalidatePlot(false);
  269. if (Model.SweepStep == Shaker.Models.SweepStep.Stop)
  270. {
  271. arrowAnnotation.TextColor = OxyColors.Transparent;
  272. arrowAnnotation.Color = OxyColors.Transparent;
  273. }
  274. else
  275. {
  276. arrowAnnotation.TextColor = OxyColors.Black;
  277. arrowAnnotation.Color = OxyColors.Green;
  278. arrowAnnotation.EndPoint = new DataPoint(CurrentFrequency, CurrentAcceleration);
  279. arrowAnnotation.StartPoint = new DataPoint(CurrentFrequency, 1E-30);
  280. arrowAnnotation.Text = $"{App.Current?.FindResource("Frequency")}:{CurrentFrequency:F2}Hz\r\n{App.Current?.FindResource("Acceleration")}:{CurrentAcceleration:F3}g";
  281. arrowAnnotation.TextPosition = arrowAnnotation.EndPoint;
  282. arrowAnnotation.TextHorizontalAlignment = HorizontalAlignment.Left;
  283. arrowAnnotation.TextVerticalAlignment = VerticalAlignment.Top;
  284. }
  285. for(int i=0;i<LineSeries.Count;i++)
  286. {
  287. LineSeries[i].ItemsSource = datas;
  288. }
  289. PlotModel.InvalidatePlot(true);
  290. }
  291. [PropertyAssociation(nameof(SineDataModel.TotalTime))]
  292. public double TotalTime { get => Model.TotalTime; set => SetProperty(ref Model.TotalTime, value); }
  293. [PropertyAssociation(nameof(SineDataModel.RunTime))]
  294. public double RunTime { get => Model.RunTime; set => SetProperty(ref Model.RunTime, value); }
  295. [PropertyAssociation(nameof(SineDataModel.CurrentFrequency))]
  296. public double CurrentFrequency { get => Model.CurrentFrequency; set => SetProperty(ref Model.CurrentFrequency, value); }
  297. [PropertyAssociation(nameof(SineDataModel.CurrentAcceleration))]
  298. public double CurrentAcceleration { get => Shaker.Models.Tools.Tools.VoltageToQuantities(Model.CurrentAcceleration, ShakerConfigViewModel.Instance.AccelerationSensitivity); set => SetProperty(ref Model.CurrentAcceleration, value); }
  299. [PropertyAssociation(nameof(SineDataModel.SweepDirection))]
  300. public string SweepDirection { get => Model.SweepDirection.Description(); }
  301. [PropertyAssociation(nameof(SineDataModel.SweepStep))]
  302. public string SweepStep { get => Model.SweepStep.Description(); }
  303. [PropertyAssociation(nameof(SineDataModel.SweepIndex))]
  304. public uint SweepIndex { get => Model.SweepIndex; set => SetProperty(ref Model.SweepIndex, value); }
  305. [PropertyAssociation(nameof(SineDataModel.MainPageType))]
  306. public override MainPageType PageType => Model.MainPageType;
  307. public OxyPlot.PlotModel PlotModel { get; private set; } = new OxyPlot.PlotModel();
  308. public static SineMainPageViewModel Instance { get; } = new SineMainPageViewModel();
  309. public OxyPlot.PlotModel DriverModel { get; private set; } = new PlotModel();
  310. }
  311. }