ShakerDataViewModel.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using Avalonia.Media;
  2. using OxyPlot;
  3. using Shaker.Models;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics.CodeAnalysis;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Runtime.CompilerServices;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace ShakerApp.ViewModels
  13. {
  14. internal class ShakerDataViewModel:DisplayViewModelBase<IModel>
  15. {
  16. private Dictionary<string, Queue<byte[]>> THDDataCache = new Dictionary<string, Queue<byte[]>>();
  17. public IReadOnlyList<string> AllowTHDChannels = new List<string>() { "MeasuredDisplacementSignal", "AccelerationSignal" };
  18. public TDMS.ITDMSFile File => tdmsfile;
  19. List<Models.StatisticsModel> statistics = new List<Models.StatisticsModel>();
  20. [AllowNull]
  21. private TDMS.ITDMSFile tdmsfile;
  22. private object _datalocker = new object();
  23. private Dictionary<Shaker.Models.AnalogType, List<(List<DataPoint>,Models.StatisticsModel)>> AnalogDataCache = new Dictionary<AnalogType, List<(List<DataPoint>, Models.StatisticsModel)>>();
  24. private ShakerDataViewModel()
  25. {
  26. foreach (var item in AllowTHDChannels)
  27. {
  28. THDDataCache[item] = new Queue<byte[]>();
  29. }
  30. GetEvent<AllConfig>().Subscrip((sender, args) =>
  31. {
  32. CommunicationViewModel.Instance.LocalCommunication.GetEvent(Topic.DATA)?.Subscrip((sender, args) =>
  33. {
  34. if(args.Data.Length>=3 && args.Data[0] is byte[] v && args.Data[1] is uint row && args.Data[2] is uint col)
  35. {
  36. ReceiveData(v, row, col);
  37. }
  38. });
  39. });
  40. }
  41. private void ReceiveData(byte[] data, uint row, uint col)
  42. {
  43. if (row == 0 || col == 0) return;
  44. data = Shaker.Models.Tools.Tools.DecompressionBytes(data);
  45. switch (ViewModels.ShakerStatusViewModel.Instance.RTStatus)
  46. {
  47. case RTStatus.SignalGen:
  48. {
  49. if (tdmsfile == null)
  50. {
  51. tdmsfile = TDMS.TDMSDataBuilder.BuildNew(System.IO.Path.Combine(ViewModels.ShakerSettingViewModel.Instance.DataDirectory, $"{DateTime.Now:yyyy-MM-dd HH-mm-ss}.tdms"),"Data");
  52. Dictionary<string, string> config = new Dictionary<string, string>();
  53. ShakerControlViewModel.Instance.SaveTdmsConfig(tdmsfile);
  54. MainPageViewModel.Instance.SaveTdmsConfig(tdmsfile);
  55. DeviceMangerViewModel.Instance.CurrentDevice?.SaveTdmsConfig(tdmsfile);
  56. }
  57. }
  58. break;
  59. default:
  60. if (tdmsfile != null)
  61. {
  62. tdmsfile.Save();
  63. tdmsfile.Close();
  64. tdmsfile.Dispose();
  65. tdmsfile = null;
  66. }
  67. break;
  68. }
  69. CalcAnalog(data, row, col);
  70. if (ShakerStatusViewModel.Instance.RTStatus == RTStatus.SignalGen && tdmsfile != null)
  71. {
  72. MainPageViewModel.Instance.SaveTestData(tdmsfile);
  73. tdmsfile.Save();
  74. }
  75. CommunicationViewModel.Instance.ServiceCommunication?.GetEvent(Topic.DATA)?.Publish(this, null);
  76. }
  77. static ShakerDataViewModel()
  78. {
  79. }
  80. public List<(List<DataPoint>, Models.StatisticsModel)> GetAnalogData(AnalogType analogType)
  81. {
  82. lock(_datalocker)
  83. {
  84. if(AnalogDataCache.TryGetValue(analogType,out var list))
  85. {
  86. if (list == null) return new List<(List<DataPoint>, Models.StatisticsModel)>();
  87. return list;
  88. }
  89. return new List<(List<DataPoint>, Models.StatisticsModel)>();
  90. }
  91. }
  92. public List<double[]> GetAnalogRawData(AnalogType analogType)
  93. {
  94. lock (_datalocker)
  95. {
  96. if (AnalogDataCache.TryGetValue(analogType, out var list))
  97. {
  98. if (list == null) return new List<double[]>();
  99. return list.Select(x=>x.Item1.Select(x=>x.Y).ToArray()).ToList();
  100. }
  101. return new List<double[]>();
  102. }
  103. }
  104. public Models.StatisticsModel? GetStatistics(string name)
  105. {
  106. lock(_datalocker)
  107. {
  108. return statistics.FirstOrDefault(x => x.Name == name);
  109. }
  110. }
  111. public double[] GetAnalogRawData(int index)
  112. {
  113. lock (_datalocker)
  114. {
  115. if (index >= ShakerConfigViewModel.Instance.Model.AnalogSignalConfigs.Count) return new double[0];
  116. var type = ShakerConfigViewModel.Instance.Model.AnalogSignalConfigs[index].AnalogType;
  117. int seindex = 0;
  118. if (index > 0)
  119. {
  120. seindex = ShakerConfigViewModel.Instance.Model.AnalogSignalConfigs.Take(index).Count(x => x.AnalogType == type);
  121. }
  122. return AnalogDataCache[type][seindex].Item1.Select(x => x.Y).ToArray();
  123. }
  124. }
  125. private void CalcAnalog(byte[] data,uint row,uint col)
  126. {
  127. if (data.Length == 0 || row == 0 || col == 0) return;
  128. int bytecount = Unsafe.SizeOf<float>();
  129. lock (_datalocker)
  130. {
  131. AnalogDataCache.Clear();
  132. statistics.Clear();
  133. for (int i = 0; i < ShakerConfigViewModel.Instance.AnalogSignals.Count; i++)
  134. {
  135. if (i >= row) break;
  136. List<DataPoint> dataPoints = new List<DataPoint>();
  137. float max = float.MinValue;
  138. float min = float.MaxValue;
  139. float v = 0;
  140. for (int j= 0; j < col; j++)
  141. {
  142. float tempv = Unsafe.As<byte, float>(ref data[j * bytecount + i * col * bytecount]);
  143. dataPoints.Add(new DataPoint(1.0 / ShakerConfigViewModel.Instance.SampleRate * j, tempv));
  144. max = tempv > max ? tempv : max;
  145. min = tempv < min ? tempv : min;
  146. v += tempv;
  147. }
  148. float thd = 0;
  149. if (AllowTHDChannels.Contains(ShakerConfigViewModel.Instance.AnalogSignals[i].Value.Name))
  150. {
  151. var queue = THDDataCache[ShakerConfigViewModel.Instance.AnalogSignals[i].Value.Name];
  152. if (ShakerStatusViewModel.Instance.RTStatus == RTStatus.SignalGen && MainPageViewModel.Instance.MainPage is SineMainPageViewModel sine)
  153. {
  154. int framecount = (int)Math.Ceiling(1 / sine.CurrentFrequency);
  155. if(framecount>1)
  156. {
  157. byte[] currentchanneldata = new byte[col * Unsafe.SizeOf<float>()];
  158. Unsafe.CopyBlock(ref currentchanneldata[0], ref data[i * col * bytecount], (uint)currentchanneldata.Length);
  159. queue.Enqueue(currentchanneldata);
  160. if (queue.Count > framecount) queue.Dequeue();
  161. currentchanneldata = queue.SelectMany(x => x).ToArray();
  162. thd = Tools.Tools.CalcTHD(ref Unsafe.As<byte, float>(ref currentchanneldata[0]), (uint)queue.Count *col);
  163. }
  164. else
  165. {
  166. if(queue.Count>0)
  167. {
  168. queue.Clear();
  169. }
  170. thd = Tools.Tools.CalcTHD(ref Unsafe.As<byte, float>(ref data[i * col * bytecount]), col);
  171. }
  172. }
  173. else
  174. {
  175. if(queue.Count>0)
  176. {
  177. queue.Clear();
  178. }
  179. thd = Tools.Tools.CalcTHD(ref Unsafe.As<byte, float>(ref data[i * col * bytecount]), col);
  180. }
  181. }
  182. Models.StatisticsModel model = new Models.StatisticsModel()
  183. {
  184. Name = ShakerConfigViewModel.Instance.Model.AnalogSignalConfigs[i].Name,
  185. Max = max,
  186. Min = min,
  187. RMS = MainViewModel.Default.Calc.Sum.Rms(ref Unsafe.As<byte,float>(ref data[i*bytecount*col]), col),
  188. Average = v/ col,
  189. THD = thd,
  190. Unit = ShakerConfigViewModel.Instance.Model.AnalogSignalConfigs[i].Unit,
  191. };
  192. if (AnalogDataCache.TryGetValue(ShakerConfigViewModel.Instance.Model.AnalogSignalConfigs[i].AnalogType,out var list))
  193. {
  194. if (list == null) list = new List<(List<DataPoint>, Models.StatisticsModel)>();
  195. list.Add((dataPoints,model));
  196. }
  197. else
  198. {
  199. AnalogDataCache[ShakerConfigViewModel.Instance.Model.AnalogSignalConfigs[i].AnalogType] = new List<(List<DataPoint>, Models.StatisticsModel)> { (dataPoints,model) };
  200. }
  201. statistics.Add(model);
  202. }
  203. }
  204. GetEvent(Topic.DATA).Publish(this,null);
  205. }
  206. public static ShakerDataViewModel Instance { get; } = new ShakerDataViewModel();
  207. }
  208. }