ShakerDataViewModel.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using IViewModel.Models;
  2. using IViewModel.ViewModels;
  3. using OxyPlot;
  4. using Shaker.Model;
  5. using Shaker.Models;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Runtime.CompilerServices;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace Dynamicloadsimulationdevice.ViewModels
  13. {
  14. public sealed class ShakerDataViewModel:ViewModelBase
  15. {
  16. private object dataLock = new object();
  17. private Dictionary<Shaker.Model.ResultChannelType, List<(List<DataPoint> PlotDatas, IViewModel.Models.StatisticsModel Statistics)>> AnalogDataCache = new Dictionary<Shaker.Model.ResultChannelType, List<(List<DataPoint> PlotDatas, IViewModel.Models.StatisticsModel Statistics)>>();
  18. private ShakerDataViewModel():base()
  19. {
  20. GetEvent<AllConfig>().Subscrip((_, args) =>
  21. {
  22. CommunicationViewModel.Instance.LocalCommunication?.GetEvent(Shaker.Model.Topic.DATA)?.Subscrip((sender, args) =>
  23. {
  24. if (args.Data.Length >= 3 && args.Data[0] is byte[] data && args.Data[1] is uint row && args.Data[2] is uint col)
  25. {
  26. ReceiveData(data, row, col);
  27. }
  28. });
  29. });
  30. }
  31. private void ReceiveData(byte[] data,uint row,uint col)
  32. {
  33. int bytecount = Unsafe.SizeOf<float>();
  34. if (row == 0 || col == 0 || data.Length != row * col * bytecount) return;
  35. lock (dataLock)
  36. {
  37. var tempdata = Shaker.Tools.Tools.DecompressionBytes(data);
  38. AnalogDataCache.Clear();
  39. for(int i=0;i<ShakerConfigViewModel.Instance.MaxResultChannels.Count;i++)
  40. {
  41. if (i >= row) break;
  42. List<DataPoint> dataPoints = new List<DataPoint>();
  43. float max = float.MinValue;
  44. float min = float.MaxValue;
  45. float v = 0;
  46. for (int j = 0; j < col; j++)
  47. {
  48. float tempv = Unsafe.As<byte, float>(ref tempdata[j * bytecount + i * col * bytecount]);
  49. dataPoints.Add(new DataPoint(1.0 / ShakerConfigViewModel.Instance.SampleRate * j, tempv));
  50. max = tempv > max ? tempv : max;
  51. min = tempv < min ? tempv : min;
  52. v += tempv;
  53. }
  54. StatisticsModel model = new StatisticsModel()
  55. {
  56. Name = ShakerChannelViewModel.Instance.ResultChannels[i].Value.Name,
  57. Max = max,
  58. Min = min,
  59. RMS = SIMDFxpConvert.SIMDCalc.Instance.Sum.Rms(ref Unsafe.As<byte, float>(ref tempdata[i * bytecount * col]), col),
  60. Average = v / col,
  61. Unit = ShakerChannelViewModel.Instance.ResultChannels[i].Value.Unit,
  62. };
  63. if (AnalogDataCache.TryGetValue(ShakerChannelViewModel.Instance.ResultChannels[i].Value.ChannelType, out var list))
  64. {
  65. if (list == null) list = new List<(List<DataPoint>, StatisticsModel)>();
  66. list.Add((dataPoints, model));
  67. }
  68. else
  69. {
  70. AnalogDataCache[ShakerChannelViewModel.Instance.ResultChannels[i].Value.ChannelType] = new List<(List<DataPoint>, StatisticsModel)> { (dataPoints, model) };
  71. }
  72. }
  73. }
  74. }
  75. static ShakerDataViewModel()
  76. {
  77. }
  78. public List<(List<DataPoint>, StatisticsModel)> GetAnalogData(ResultChannelType analogType)
  79. {
  80. lock (dataLock)
  81. {
  82. if (AnalogDataCache.TryGetValue(analogType, out var list))
  83. {
  84. if (list == null) return new List<(List<DataPoint>, StatisticsModel)>();
  85. return list;
  86. }
  87. return new List<(List<DataPoint>, StatisticsModel)>();
  88. }
  89. }
  90. public static ShakerDataViewModel Instance { get; } = new ShakerDataViewModel();
  91. }
  92. }