123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- using IViewModel.Models;
- using IViewModel.ViewModels;
- using OxyPlot;
- using Shaker.Model;
- using Shaker.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.CompilerServices;
- using System.Text;
- using System.Threading.Tasks;
- namespace Dynamicloadsimulationdevice.ViewModels
- {
- public sealed class ShakerDataViewModel:ViewModelBase
- {
- private object dataLock = new object();
- 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)>>();
- private ShakerDataViewModel():base()
- {
- GetEvent<AllConfig>().Subscrip((_, args) =>
- {
- CommunicationViewModel.Instance.LocalCommunication?.GetEvent(Shaker.Model.Topic.DATA)?.Subscrip((sender, args) =>
- {
- if (args.Data.Length >= 3 && args.Data[0] is byte[] data && args.Data[1] is uint row && args.Data[2] is uint col)
- {
- ReceiveData(data, row, col);
- }
- });
- });
- }
- private void ReceiveData(byte[] data,uint row,uint col)
- {
- int bytecount = Unsafe.SizeOf<float>();
- if (row == 0 || col == 0 || data.Length != row * col * bytecount) return;
- lock (dataLock)
- {
- var tempdata = Shaker.Tools.Tools.DecompressionBytes(data);
- AnalogDataCache.Clear();
- for(int i=0;i<ShakerConfigViewModel.Instance.MaxResultChannels.Count;i++)
- {
- if (i >= row) break;
- List<DataPoint> dataPoints = new List<DataPoint>();
- float max = float.MinValue;
- float min = float.MaxValue;
- float v = 0;
- for (int j = 0; j < col; j++)
- {
- float tempv = Unsafe.As<byte, float>(ref tempdata[j * bytecount + i * col * bytecount]);
- dataPoints.Add(new DataPoint(1.0 / ShakerConfigViewModel.Instance.SampleRate * j, tempv));
- max = tempv > max ? tempv : max;
- min = tempv < min ? tempv : min;
- v += tempv;
- }
- StatisticsModel model = new StatisticsModel()
- {
- Name = ShakerChannelViewModel.Instance.ResultChannels[i].Value.Name,
- Max = max,
- Min = min,
- RMS = SIMDFxpConvert.SIMDCalc.Instance.Sum.Rms(ref Unsafe.As<byte, float>(ref tempdata[i * bytecount * col]), col),
- Average = v / col,
- Unit = ShakerChannelViewModel.Instance.ResultChannels[i].Value.Unit,
- };
- if (AnalogDataCache.TryGetValue(ShakerChannelViewModel.Instance.ResultChannels[i].Value.ChannelType, out var list))
- {
- if (list == null) list = new List<(List<DataPoint>, StatisticsModel)>();
- list.Add((dataPoints, model));
- }
- else
- {
- AnalogDataCache[ShakerChannelViewModel.Instance.ResultChannels[i].Value.ChannelType] = new List<(List<DataPoint>, StatisticsModel)> { (dataPoints, model) };
- }
- }
- }
- }
- static ShakerDataViewModel()
- {
- }
- public List<(List<DataPoint>, StatisticsModel)> GetAnalogData(ResultChannelType analogType)
- {
- lock (dataLock)
- {
- if (AnalogDataCache.TryGetValue(analogType, out var list))
- {
- if (list == null) return new List<(List<DataPoint>, StatisticsModel)>();
- return list;
- }
- return new List<(List<DataPoint>, StatisticsModel)>();
- }
- }
- public static ShakerDataViewModel Instance { get; } = new ShakerDataViewModel();
- }
- }
|