SineDataReviewViewModel.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. line.Tag = "g";
  65. LineSeries.Add(line);
  66. PlotModel.Series.Add(line);
  67. }
  68. PlotModel.Title = App.Current?.FindResource(MainPageType.SinePage.Description()) + "";
  69. PlotModel.Legends.Add(new OxyPlot.Legends.Legend()
  70. {
  71. IsLegendVisible = true,
  72. });
  73. }
  74. static SineDataReviewViewModel()
  75. {
  76. }
  77. public string SweepDirection { get => sweepDirection; set =>SetProperty(ref sweepDirection , value); }
  78. public AvaloniaList<int> Frames { get; } = new AvaloniaList<int>();
  79. public int MaxFrame { get => maxFrame; set =>SetProperty(ref maxFrame , value); }
  80. public int CurrentFrame
  81. {
  82. get =>currentFrame;
  83. set
  84. {
  85. SetProperty(ref currentFrame, value);
  86. GetSweepData();
  87. }
  88. }
  89. public int MinFrame => 1;
  90. public bool IsEnabled { get => isEnabled; set =>SetProperty(ref isEnabled , value); }
  91. private string sweepDirection = string.Empty;
  92. private protected override void InitConfig()
  93. {
  94. IsEnabled = false;
  95. var group = tdmsfile.Contains(nameof(SweepConfigModel)) ? tdmsfile[nameof(SweepConfigModel)] : null;
  96. if (group == null) return;
  97. GetConfig<SweepConfigModel>(sweepConfig, group);
  98. group = tdmsfile.Contains(SineMainPageViewModel.TDMSDataName) ? tdmsfile[SineMainPageViewModel.TDMSDataName] : null;
  99. if (group == null) return;
  100. var channeld = group.Contains(nameof(SineDataModel.SweepIndex)) ? group[nameof(SineDataModel.SweepIndex)] : null;
  101. if (channeld == null) return;
  102. if (channeld.ChildCount == 0) return;
  103. var max = channeld.GetDataValues<int>((uint)channeld.ChildCount - 1, 1)[0];
  104. indexLenght = new int[max + 1];
  105. int index = 0;
  106. for (int i = 0; i < max + 1; i++)
  107. {
  108. while (true)
  109. {
  110. uint readlen = Math.Min(10000, (uint)(channeld.ChildCount - (uint)index));
  111. if (readlen == 0) break;
  112. var temp = channeld.GetDataValues<int>((uint)index, readlen);
  113. int c = temp.Count(x => x == i);
  114. indexLenght[i] += c;
  115. if (c == readlen)
  116. {
  117. index += (int)readlen;
  118. continue;
  119. }
  120. else
  121. {
  122. if (temp[^1] != i)
  123. {
  124. index += c;
  125. break;
  126. }
  127. }
  128. }
  129. }
  130. Frames.Clear();
  131. MaxFrame = max+1;
  132. for(int i=0;i<MaxFrame;i++)
  133. {
  134. Frames.Add(i + 1);
  135. }
  136. CurrentFrame = 1;
  137. IsEnabled = true;
  138. base.InitConfig();
  139. }
  140. public override void InitData()
  141. {
  142. base.InitData();
  143. GetSweepData();
  144. }
  145. private void GetSweepData()
  146. {
  147. if (!IsEnabled) return;
  148. List<SweepData> data = new List<SweepData>();
  149. var index = Frames.ToList().FindIndex(x => CurrentFrame == x);
  150. if(index ==-1)
  151. {
  152. DataReviewViewModel.Instance.ShowError("");
  153. return;
  154. }
  155. int startindex =0;
  156. if(index>0)
  157. {
  158. startindex = indexLenght.Take(1).Sum();
  159. }
  160. int count = indexLenght[index];
  161. if(count ==0)
  162. {
  163. }
  164. else
  165. {
  166. var group = tdmsfile.Contains(SineMainPageViewModel.TDMSDataName) ? tdmsfile[SineMainPageViewModel.TDMSDataName] : null;
  167. if (group != null)
  168. {
  169. var freqchanneld = group.Contains(nameof(SineDataModel.CurrentFrequency)) ? group[nameof(SineDataModel.CurrentFrequency)] : null;
  170. var accachannel = group.Contains(nameof(SineDataModel.CurrentAcceleration)) ? group[nameof(SineDataModel.CurrentAcceleration)] : null;
  171. var dirchannel = group.Contains(nameof(SineDataModel.SweepDirection)) ? group[nameof(SineDataModel.SweepDirection)] : null;
  172. if (freqchanneld != null && freqchanneld.ChildCount != 0 && accachannel != null && accachannel.ChildCount != 0 && dirchannel!=null)
  173. {
  174. double[] freq = freqchanneld.GetDataValues<double>((uint)startindex, (uint)count);
  175. double[] acc = accachannel.GetDataValues<double>((uint)startindex, (uint)count);
  176. double value = 0; double upstop = 0; double upwarn = 0; double downstop = 0; double downwarn = 0;
  177. SweepDirection = ((SweepDirection)dirchannel.GetDataValues<int>((uint)startindex, 1)[0]).Description();
  178. for (int i = 0; i < count; i++)
  179. {
  180. if (freq[i] > sweepConfig.EndFrequency || freq[i] < sweepConfig.StartFrequency) continue;
  181. sweepConfig.CalcAmpt(freq[i], ref value, ref upstop, ref upwarn, ref downstop, ref downwarn);
  182. SweepData sweep = new SweepData(freq[i], Shaker.Models.Tools.Tools.VoltageToQuantities(acc[i], ShakerConfig.AccelerationConfigs[0].Sensitivity), value, upstop, downstop, upwarn, downwarn);
  183. data.Add(sweep);
  184. }
  185. }
  186. }
  187. }
  188. PlotModel.InvalidatePlot(false);
  189. for(int i=0;i<LineSeries.Count;i++)
  190. {
  191. LineSeries[i].ItemsSource = data;
  192. }
  193. PlotModel.InvalidatePlot(true);
  194. }
  195. public static SineDataReviewViewModel Instance { get; } = new SineDataReviewViewModel();
  196. }
  197. }