SineDataReviewViewModel.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using OxyPlot;
  2. using Avalonia.Controls;
  3. using Shaker.Models;
  4. using Shaker.Models.Tools;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using OxyPlot.Series;
  11. using Avalonia.Collections;
  12. using ShakerApp.Tools;
  13. using TDMS;
  14. namespace ShakerApp.ViewModels
  15. {
  16. internal class SineDataReviewViewModel : BaseDataReviewItemViewModel
  17. {
  18. private int[] indexLenght = new int[0];
  19. private protected SweepConfigModel sweepConfig = new SweepConfigModel();
  20. public SweepConfigModel SweepConfig => sweepConfig;
  21. List<OxyColor> oxyColors = new List<OxyColor>() { OxyColors.Black, OxyColors.Green, OxyColors.Red, OxyColors.Red, OxyColors.Yellow, OxyColors.Yellow };
  22. List<LineStyle> lineStyles = new List<LineStyle>() { LineStyle.Solid, LineStyle.Solid, LineStyle.Solid, LineStyle.Solid, LineStyle.LongDashDotDot, LineStyle.LongDashDotDot };
  23. List<string> properties = new List<string>() { nameof(SweepData.Acceleration), nameof(SweepData.TargetAcceleration), nameof(SweepData.UpStopAcceleration), nameof(SweepData.DownStopAcceleration), nameof(SweepData.UpWarnAcceleration), nameof(SweepData.DownWarnAcceleration) };
  24. private int maxFrame;
  25. private int currentFrame;
  26. private bool isEnabled = false;
  27. public AvaloniaList<LineSeries> LineSeries { get; } = new AvaloniaList<LineSeries>();
  28. public PlotModel PlotModel { get; } = new PlotModel();
  29. public override MainPageType PageType => MainPageType.SinePage;
  30. protected override void Cancel()
  31. {
  32. base.Cancel();
  33. }
  34. private SineDataReviewViewModel():base()
  35. {
  36. Content = typeof(Views.SineDataReviewView);
  37. PlotModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
  38. {
  39. MajorGridlineStyle = LineStyle.Solid,
  40. Position = OxyPlot.Axes.AxisPosition.Bottom,
  41. Title = App.Current?.FindResource("Frequency")+"",
  42. Key = "X",
  43. Unit = "Hz"
  44. });
  45. PlotModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
  46. {
  47. MajorGridlineStyle = LineStyle.Solid,
  48. Position = OxyPlot.Axes.AxisPosition.Left,
  49. Title = App.Current?.FindResource("AccelerationSignal")+"",
  50. Key = "Y",
  51. Unit = "g"
  52. });
  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 = "X";
  63. line.YAxisKey = "Y";
  64. LineSeries.Add(line);
  65. PlotModel.Series.Add(line);
  66. }
  67. PlotModel.Title = App.Current?.FindResource(MainPageType.SinePage.Description()) + "";
  68. PlotModel.Legends.Add(new OxyPlot.Legends.Legend()
  69. {
  70. IsLegendVisible = true,
  71. });
  72. }
  73. static SineDataReviewViewModel()
  74. {
  75. }
  76. public string SweepDirection { get => sweepDirection; set =>SetProperty(ref sweepDirection , value); }
  77. public AvaloniaList<int> Frames { get; } = new AvaloniaList<int>();
  78. public int MaxFrame { get => maxFrame; set =>SetProperty(ref maxFrame , value); }
  79. public int CurrentFrame
  80. {
  81. get =>currentFrame;
  82. set
  83. {
  84. SetProperty(ref currentFrame, value);
  85. GetSweepData();
  86. }
  87. }
  88. public int MinFrame => 1;
  89. public bool IsEnabled { get => isEnabled; set =>SetProperty(ref isEnabled , value); }
  90. private string sweepDirection = string.Empty;
  91. private protected override void InitConfig()
  92. {
  93. IsEnabled = false;
  94. var group = tdmsfile.Contains(nameof(SweepConfigModel)) ? tdmsfile[nameof(SweepConfigModel)] : null;
  95. if (group == null) return;
  96. GetConfig<SweepConfigModel>(sweepConfig, group);
  97. group = tdmsfile.Contains(SineMainPageViewModel.TDMSDataName) ? tdmsfile[SineMainPageViewModel.TDMSDataName] : null;
  98. if (group == null) return;
  99. var channeld = group.Contains(nameof(SineDataModel.SweepIndex)) ? group[nameof(SineDataModel.SweepIndex)] : null;
  100. if (channeld == null) return;
  101. if (channeld.ChildCount == 0) return;
  102. var max = channeld.GetDataValues<int>((uint)channeld.ChildCount - 1, 1)[0];
  103. indexLenght = new int[max + 1];
  104. int index = 0;
  105. for (int i = 0; i < max + 1; i++)
  106. {
  107. while (true)
  108. {
  109. uint readlen = Math.Min(10000, (uint)(channeld.ChildCount - (uint)index));
  110. if (readlen == 0) break;
  111. var temp = channeld.GetDataValues<int>((uint)index, readlen);
  112. int c = temp.Count(x => x == i);
  113. indexLenght[i] += c;
  114. if (c == readlen)
  115. {
  116. index += (int)readlen;
  117. continue;
  118. }
  119. else
  120. {
  121. if (temp[^1] != i)
  122. {
  123. index += c;
  124. break;
  125. }
  126. }
  127. }
  128. }
  129. Frames.Clear();
  130. MaxFrame = max+1;
  131. for(int i=0;i<MaxFrame;i++)
  132. {
  133. Frames.Add(i + 1);
  134. }
  135. CurrentFrame = 1;
  136. IsEnabled = true;
  137. base.InitConfig();
  138. }
  139. public override void InitData()
  140. {
  141. base.InitData();
  142. GetSweepData();
  143. }
  144. private void GetSweepData()
  145. {
  146. if (!IsEnabled) return;
  147. List<SweepData> data = new List<SweepData>();
  148. var index = Frames.ToList().FindIndex(x => CurrentFrame == x);
  149. if(index ==-1)
  150. {
  151. DataReviewViewModel.Instance.ShowError("");
  152. return;
  153. }
  154. int startindex =0;
  155. if(index>0)
  156. {
  157. startindex = indexLenght.Take(1).Sum();
  158. }
  159. int count = indexLenght[index];
  160. if(count ==0)
  161. {
  162. }
  163. else
  164. {
  165. var group = tdmsfile.Contains(SineMainPageViewModel.TDMSDataName) ? tdmsfile[SineMainPageViewModel.TDMSDataName] : null;
  166. if (group != null)
  167. {
  168. var freqchanneld = group.Contains(nameof(SineDataModel.CurrentFrequency)) ? group[nameof(SineDataModel.CurrentFrequency)] : null;
  169. var accachannel = group.Contains(nameof(SineDataModel.CurrentAcceleration)) ? group[nameof(SineDataModel.CurrentAcceleration)] : null;
  170. var dirchannel = group.Contains(nameof(SineDataModel.SweepDirection)) ? group[nameof(SineDataModel.SweepDirection)] : null;
  171. if (freqchanneld != null && freqchanneld.ChildCount != 0 && accachannel != null && accachannel.ChildCount != 0 && dirchannel!=null)
  172. {
  173. double[] freq = freqchanneld.GetDataValues<double>((uint)startindex, (uint)count);
  174. double[] acc = accachannel.GetDataValues<double>((uint)startindex, (uint)count);
  175. double value = 0; double upstop = 0; double upwarn = 0; double downstop = 0; double downwarn = 0;
  176. SweepDirection = ((SweepDirection)dirchannel.GetDataValues<int>((uint)startindex, 1)[0]).Description();
  177. for (int i = 0; i < count; i++)
  178. {
  179. if (freq[i] > sweepConfig.EndFrequency || freq[i] < sweepConfig.StartFrequency) continue;
  180. sweepConfig.CalcAmpt(freq[i], ref value, ref upstop, ref upwarn, ref downstop, ref downwarn);
  181. SweepData sweep = new SweepData(freq[i], Shaker.Models.Tools.Tools.VoltageToQuantities(acc[i], ShakerConfig.AccelerationConfigs[0].Sensitivity), value, upstop, downstop, upwarn, downwarn);
  182. data.Add(sweep);
  183. }
  184. }
  185. }
  186. }
  187. PlotModel.InvalidatePlot(false);
  188. for(int i=0;i<LineSeries.Count;i++)
  189. {
  190. LineSeries[i].ItemsSource = data;
  191. }
  192. PlotModel.InvalidatePlot(true);
  193. }
  194. public static SineDataReviewViewModel Instance { get; } = new SineDataReviewViewModel();
  195. }
  196. }