SineMainPageViewModel.cs 16 KB

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