ShakerDataViewModel.cs 10 KB

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