RandomMainPageViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using Avalonia;
  2. using Avalonia.Collections;
  3. using Avalonia.Controls;
  4. using Avalonia.Controls.ApplicationLifetimes;
  5. using OxyPlot;
  6. using OxyPlot.Series;
  7. using Shaker.Models;
  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.Text;
  15. using System.Threading.Tasks;
  16. namespace ShakerApp.ViewModels
  17. {
  18. internal class RandomMainPageViewModel:BaseMainPageViewModel<RandomDataModel>
  19. {
  20. public const string SpectrumData = "SpectrumData";
  21. [PropertyAssociation(nameof(RandomDataModel.CurrentIdentifyDisplacement))]
  22. public double CurrentIdentifyDisplacement => Model.CurrentIdentifyDisplacement;
  23. [PropertyAssociation(nameof(RandomDataModel.CurrentIdentifyRms))]
  24. public double CurrentIdentifyRms => Model.CurrentIdentifyRms;
  25. [PropertyAssociation(nameof(RandomDataModel.IdentifyIndex))]
  26. public double IdentifyIndex => Model.IdentifyIndex;
  27. [PropertyAssociation(nameof(RandomDataModel.RandomStatus))]
  28. public RandomStatus RandomStatus => Model.RandomStatus;
  29. [PropertyAssociation(nameof(RandomDataModel.TimeDomainRMS))]
  30. public double TimeDomainRMS => Model.TimeDomainRMS;
  31. [PropertyAssociation(nameof(RandomDataModel.RandomTestStep))]
  32. public RandomTestStep RandomTestStep => Model.RandomTestStep;
  33. [PropertyAssociation(nameof(RandomDataModel.RandomTestStep))]
  34. public string RandomTestStepKey => RandomTestStep.Description();
  35. [PropertyAssociation(nameof(RandomDataModel.CurrentTestLevel))]
  36. public double CurrentTestLevel => Model.CurrentTestLevel;
  37. [PropertyAssociation(nameof(RandomDataModel.CurrentTestTime))]
  38. public uint CurrentTestTime => Model.CurrentTestTime;
  39. [PropertyAssociation(nameof(RandomDataModel.CurrentLevelTestMaxTime))]
  40. public uint CurrentLevelTestMaxTime => Model.CurrentLevelTestMaxTime;
  41. public AvaloniaList<LineSeries> LineSeries { get; } = new AvaloniaList<LineSeries>();
  42. List<OxyColor> oxyColors = new List<OxyColor>() {OxyColors.Blue, OxyColors.Black, OxyColors.Green, OxyColors.Red, OxyColors.Red, OxyColors.Yellow, OxyColors.Yellow };
  43. List<LineStyle> lineStyles = new List<LineStyle>() { LineStyle.Solid, LineStyle.Solid, LineStyle.Solid, LineStyle.Solid, LineStyle.Solid, LineStyle.LongDashDotDot, LineStyle.LongDashDotDot };
  44. List<string> properties = new List<string>() {nameof(RandomData.Driver), nameof(RandomData.Acceleration), nameof(RandomData.TargetAcceleration), nameof(RandomData.UpStopAcceleration), nameof(RandomData.DownStopAcceleration), nameof(RandomData.UpWarnAcceleration), nameof(RandomData.DownWarnAcceleration) };
  45. List<RandomData> datas = new List<RandomData>();
  46. private RandomMainPageViewModel()
  47. {
  48. Content = typeof(Views.RandomMainPage);
  49. #region 加速度谱曲线
  50. PlotModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
  51. {
  52. MaximumPadding = 0,
  53. MinimumPadding = 0,
  54. Title = App.Current?.FindResource(ShakerConstant.FrequencyKey) + "",
  55. Unit = "Hz",
  56. MajorGridlineStyle = LineStyle.Solid,
  57. Position = OxyPlot.Axes.AxisPosition.Bottom,
  58. Key = "Freq"
  59. });
  60. PlotModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
  61. {
  62. Key = "Ampt",
  63. Title = App.Current?.FindResource(ShakerConstant.AmptAxisTitleKey) + "",
  64. Unit = App.Current?.FindResource("RandomValueUnit")?.ToString() ?? string.Empty,
  65. MajorGridlineStyle = LineStyle.Solid,
  66. Position = OxyPlot.Axes.AxisPosition.Left,
  67. });
  68. PlotModel.Title = App.Current?.FindResource(MainPageType.RandomPage.Description()) + "";
  69. for (int i = 0; i < oxyColors.Count; i++)
  70. {
  71. LineSeries line = new LineSeries();
  72. line.Title = App.Current?.FindResource(ShakerConstant.ShowRandomAccelerationSpectrumNames[i]) + "";
  73. line.Color = oxyColors[i];
  74. line.StrokeThickness = 1;
  75. line.DataFieldX = nameof(SweepData.Frequency);
  76. line.DataFieldY = properties[i];
  77. line.LineStyle = lineStyles[i];
  78. line.XAxisKey = "Freq";
  79. line.YAxisKey = "Ampt";
  80. LineSeries.Add(line);
  81. PlotModel.Series.Add(line);
  82. }
  83. PlotModel.Legends.Add(new OxyPlot.Legends.Legend()
  84. {
  85. ShowInvisibleSeries = true,
  86. });
  87. PlotModel.Title = App.Current?.FindResource(MainPageType.RandomPage.Description()) + "";
  88. #endregion
  89. GetEvent(ShakerSettingViewModel.LANGUAGECHANGEDEVENT).Subscrip((_, _) =>
  90. {
  91. PlotModel.InvalidatePlot(false);
  92. PlotModel.Title = App.Current?.FindResource(MainPageType.RandomPage.Description()) + "";
  93. PlotModel.Axes[0].Title = App.Current?.FindResource("Frequency") + "";
  94. PlotModel.Axes[1].Title = Title = App.Current?.FindResource("Ampt") + "";
  95. PlotModel.InvalidatePlot(true);
  96. });
  97. GetEvent<AllConfig>().Subscrip((_, _) =>
  98. {
  99. CommunicationViewModel.Instance.LocalCommunication?.GetEvent<RandomDataModel>()?.Subscrip((_, args) =>
  100. {
  101. UpdateData(new List<IResultDataModel>() { args.Data });
  102. CommunicationViewModel.Instance.ServiceCommunication?.GetEvent<RandomDataModel>()?.Publish(this, args.Data);
  103. });
  104. CommunicationViewModel.Instance.LocalCommunication?.GetEvent(nameof(RandomDataModel.TransferFunction))?.Subscrip((sender, args) =>
  105. {
  106. if (args.Data.Length > 0 && args.Data[0] is double[] func)
  107. {
  108. Tools.DispatherInovke.Inovke(()=> ShowTransferFunction(func));
  109. CommunicationViewModel.Instance.ServiceCommunication?.GetEvent(nameof(RandomDataModel.TransferFunction))?.Publish(this, null, func);
  110. }
  111. });
  112. });
  113. }
  114. private async void ShowTransferFunction(double[] data)
  115. {
  116. if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
  117. {
  118. RandomTransferFunctionViewModel.Instance.InitData();
  119. RandomTransferFunctionViewModel.Instance.InitTransferFunctionData(data);
  120. RandomTransferFunctionViewModel.Instance.Title = "TransferFunction";
  121. BaseDialogWindow window = new BaseDialogWindow();
  122. window.DataContext = RandomTransferFunctionViewModel.Instance;
  123. bool isclose = false;
  124. RandomTransferFunctionViewModel.Instance.CloseWindowAction = () =>
  125. {
  126. if (isclose) return;
  127. isclose = true;
  128. window?.Close();
  129. };
  130. await window.ShowDialog(desktop.MainWindow!);
  131. }
  132. }
  133. static RandomMainPageViewModel()
  134. {
  135. }
  136. public override void InitUI()
  137. {
  138. }
  139. public override void SaveTdmsConfig(TDMS.ITDMSFile? config)
  140. {
  141. if (config == null) return;
  142. base.SaveTdmsConfig(config);
  143. var group = config.Contains(nameof(RandomConfigModel)) ? config[nameof(RandomConfigModel)]:config.AddGroup(nameof(RandomConfigModel));
  144. if (group == null) return;
  145. typeof(RandomConfigModel).GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
  146. .Select(x => (x, x.GetValue(RandomConfigViewModel.Instance.Model)))
  147. .ToList()
  148. .ForEach(x =>
  149. {
  150. if (x.Item2 == null)
  151. {
  152. return;
  153. }
  154. if (x.x.FieldType.IsAssignableTo(typeof(IList)) || x.x.FieldType.IsArray)
  155. {
  156. group.CreateOrUpdateProperty($"{x.x.Name}", string.Join("", Tools.Tools.Serialize(x.x.FieldType, x.Item2).Select(x => $"{x:X2}")));
  157. }
  158. else
  159. {
  160. group.CreateOrUpdateProperty($"{x.x.Name}", x.Item2?.ToString() ?? string.Empty);
  161. }
  162. });
  163. }
  164. public override void SaveTestData(TDMS.ITDMSFile? file)
  165. {
  166. if (file == null) return;
  167. base.SaveTestData(file);
  168. var group = file.Contains(SpectrumData) ? file[SpectrumData] : file.AddGroup(SpectrumData);
  169. if (group == null) return;
  170. var acc = ShakerConfigViewModel.Instance.Model.AnalogSignalConfigs.Where(x => x.AnalogType == AnalogType.DivideAcceleration).ToArray();
  171. if(acc.Length>0)
  172. {
  173. for(int i=0;i<acc.Length;i++)
  174. {
  175. var ch = group.Contains(acc[i].Name) ? group[acc[i].Name] : group.AddChannel(TDMS.Common.TDMSDataType.Double, acc[i].Name, App.Current?.FindResource("RandomValueUnit") + "");
  176. ch?.AppendData(Model.CurrentAccelerationPSD[i]);
  177. }
  178. }
  179. {
  180. var ch = group.Contains("AccelerationSpectrum") ? group["AccelerationSpectrum"] : group.AddChannel(TDMS.Common.TDMSDataType.Double, "AccelerationSpectrum", App.Current?.FindResource("RandomValueUnit") + "");
  181. ch?.AppendData(Model.CurrentAccelerationSynthesisPSD);
  182. }
  183. {
  184. var dr = ShakerConfigViewModel.Instance.Model.AnalogSignalConfigs.FirstOrDefault(x => x.AnalogType == AnalogType.GivenDriver);
  185. if (dr != null)
  186. {
  187. var ch = group.Contains(dr.Name) ? group[dr.Name] : group.AddChannel(TDMS.Common.TDMSDataType.Double, dr.Name, App.Current?.FindResource("RandomValueUnit") + "");
  188. ch?.AppendData(Model.DriverPSD);
  189. }
  190. }
  191. }
  192. public override bool Start()
  193. {
  194. SetRefSpectrum(RandomConfigViewModel.Instance.RefSpectrum);
  195. return true;
  196. }
  197. public override void Stop()
  198. {
  199. }
  200. public override void UpDateModel(RandomDataModel model)
  201. {
  202. base.UpDateModel(model);
  203. int startindex = (int)(RandomConfigViewModel.Instance.MinFrequency / RandomConfigViewModel.Instance.FrequencyResolution);
  204. for(int i=0;i<datas.Count;i++)
  205. {
  206. datas[i].SetAcceleration(model.CurrentAccelerationSynthesisPSD[i + startindex]);
  207. datas[i].SetDriver(model.DriverPSD[i + startindex]);
  208. }
  209. PlotModel.InvalidatePlot(false);
  210. for(int i=0;i<LineSeries.Count;i++)
  211. {
  212. LineSeries[i].ItemsSource = datas;
  213. }
  214. PlotModel.InvalidatePlot(true);
  215. SaveSpectrumData();
  216. }
  217. private void SaveSpectrumData()
  218. {
  219. var file = ViewModels.ShakerDataViewModel.Instance.File;
  220. if (file == null) return;
  221. var group= file.Contains(SpectrumData) ? file[SpectrumData]:file.AddGroup(SpectrumData);
  222. if (group == null) return;
  223. }
  224. public override void UpdateData(List<IResultDataModel> model)
  225. {
  226. if(model !=null &&model.Count >0 && model.First() is RandomDataModel randomdata)
  227. {
  228. UpDateModel(randomdata);
  229. }
  230. }
  231. public void SetRefSpectrum(List<RandomData> data)
  232. {
  233. datas.Clear();
  234. datas.AddRange(data);
  235. PlotModel.InvalidatePlot(false);
  236. for (int i = 0; i < LineSeries.Count; i++)
  237. {
  238. LineSeries[i].ItemsSource = datas;
  239. }
  240. PlotModel.InvalidatePlot(true);
  241. }
  242. public override MainPageType PageType => MainPageType.RandomPage;
  243. public OxyPlot.PlotModel PlotModel { get; private set; } = new OxyPlot.PlotModel();
  244. public static RandomMainPageViewModel Instance { get; } = new RandomMainPageViewModel();
  245. }
  246. }