123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- using OxyPlot;
- using Shaker.Models;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics.CodeAnalysis;
- using System.IO;
- using System.Linq;
- using System.Runtime.CompilerServices;
- using System.Text;
- using System.Threading.Tasks;
- namespace ShakerApp.ViewModels
- {
- internal class ShakerDataViewModel:DisplayViewModelBase<IModel>
- {
- public TDMS.ITDMSFile File => tdmsfile;
- [AllowNull]
- private TDMS.ITDMSFile tdmsfile;
- private object _datalocker = new object();
- private Dictionary<Shaker.Models.AnalogType, List<(List<DataPoint>,Models.StatisticsModel)>> AnalogDataCache = new Dictionary<AnalogType, List<(List<DataPoint>, Models.StatisticsModel)>>();
- private ShakerDataViewModel()
- {
- GetEvent<AllConfig>().Subscrip((sender, args) =>
- {
- CommunicationViewModel.Instance.LocalCommunication.GetEvent(Topic.DATA)?.Subscrip((sender, args) =>
- {
- if(args.Data.Length>=3 && args.Data[0] is byte[] v && args.Data[1] is uint row && args.Data[2] is uint col)
- {
- ReceiveData(v, row, col);
- }
- });
- });
- }
- private void ReceiveData(byte[] data, uint row, uint col)
- {
- if (row == 0 || col == 0) return;
- data = Shaker.Models.Tools.Tools.DecompressionBytes(data);
- switch (ViewModels.ShakerStatusViewModel.Instance.RTStatus)
- {
- case RTStatus.SignalGen:
- {
- if (tdmsfile == null)
- {
- tdmsfile = TDMS.TDMSDataBuilder.BuildNew(System.IO.Path.Combine(ViewModels.ShakerSettingViewModel.Instance.DataDirectory, $"{DateTime.Now:yyyy-MM-dd HH-mm-ss}.tdms"),"Data");
- Dictionary<string, string> config = new Dictionary<string, string>();
- ShakerControlViewModel.Instance.SaveTdmsConfig(tdmsfile);
- MainPageViewModel.Instance.SaveTdmsConfig(tdmsfile);
- DeviceMangerViewModel.Instance.CurrentDevice?.SaveTdmsConfig(tdmsfile);
- }
- }
- break;
- default:
- if (tdmsfile != null)
- {
- tdmsfile.Save();
- tdmsfile.Close();
- tdmsfile.Dispose();
- tdmsfile = null;
- }
- break;
- }
- CalcAnalog(data, row, col);
- if (ShakerStatusViewModel.Instance.RTStatus == RTStatus.SignalGen && tdmsfile != null)
- {
- MainPageViewModel.Instance.SaveTestData(tdmsfile);
- tdmsfile.Save();
- }
- CommunicationViewModel.Instance.ServiceCommunication?.GetEvent(Topic.DATA)?.Publish(this, null);
- }
- static ShakerDataViewModel()
- {
- }
- public List<(List<DataPoint>, Models.StatisticsModel)> GetAnalogData(AnalogType analogType)
- {
- lock(_datalocker)
- {
- if(AnalogDataCache.TryGetValue(analogType,out var list))
- {
- if (list == null) return new List<(List<DataPoint>, Models.StatisticsModel)>();
- return list;
- }
- return new List<(List<DataPoint>, Models.StatisticsModel)>();
- }
- }
- public List<double[]> GetAnalogRawData(AnalogType analogType)
- {
- lock (_datalocker)
- {
- if (AnalogDataCache.TryGetValue(analogType, out var list))
- {
- if (list == null) return new List<double[]>();
- return list.Select(x=>x.Item1.Select(x=>x.Y).ToArray()).ToList();
- }
- return new List<double[]>();
- }
- }
- public double[] GetAnalogRawData(int index)
- {
- lock (_datalocker)
- {
- if (index >= ShakerConfigViewModel.Instance.Model.AnalogSignalConfigs.Count) return new double[0];
- var type = ShakerConfigViewModel.Instance.Model.AnalogSignalConfigs[index].AnalogType;
- int seindex = 0;
- if (index > 0)
- {
- seindex = ShakerConfigViewModel.Instance.Model.AnalogSignalConfigs.Take(index).Count(x => x.AnalogType == type);
- }
- return AnalogDataCache[type][seindex].Item1.Select(x => x.Y).ToArray();
- }
- }
- private void CalcAnalog(byte[] data,uint row,uint col)
- {
- if (data.Length == 0 || row == 0 || col == 0) return;
- int bytecount = Unsafe.SizeOf<float>();
- lock (_datalocker)
- {
- AnalogDataCache.Clear();
- for (int i = 0; i < ShakerConfigViewModel.Instance.AnalogSignals.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 data[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;
- }
- Models.StatisticsModel model = new Models.StatisticsModel()
- {
- Name = ShakerConfigViewModel.Instance.Model.AnalogSignalConfigs[i].Name,
- Max = max,
- Min = min,
- RMS = MainViewModel.Default.Calc.Sum.Rms(ref Unsafe.As<byte,float>(ref data[i*bytecount*col]), col),
- Average = v/ col,
- Unit = ShakerConfigViewModel.Instance.Model.AnalogSignalConfigs[i].Unit,
- };
- if (AnalogDataCache.TryGetValue(ShakerConfigViewModel.Instance.Model.AnalogSignalConfigs[i].AnalogType,out var list))
- {
- if (list == null) list = new List<(List<DataPoint>, Models.StatisticsModel)>();
- list.Add((dataPoints,model));
- }
- else
- {
- AnalogDataCache[ShakerConfigViewModel.Instance.Model.AnalogSignalConfigs[i].AnalogType] = new List<(List<DataPoint>, Models.StatisticsModel)> { (dataPoints,model) };
- }
- }
- }
- GetEvent(Topic.DATA).Publish(this,null);
- }
- public static ShakerDataViewModel Instance { get; } = new ShakerDataViewModel();
- }
- }
|