using OxyPlot; using Avalonia.Controls; using Shaker.Models; using Shaker.Models.Tools; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using OxyPlot.Series; using Avalonia.Collections; using ShakerApp.Tools; using TDMS; namespace ShakerApp.ViewModels { internal class SineDataReviewViewModel : BaseDataReviewItemViewModel { private int[] indexLenght = new int[0]; private protected SweepConfigModel sweepConfig = new SweepConfigModel(); public SweepConfigModel SweepConfig => sweepConfig; List oxyColors = new List() { OxyColors.Black, OxyColors.Green, OxyColors.Red, OxyColors.Red, OxyColors.Yellow, OxyColors.Yellow }; List lineStyles = new List() { LineStyle.Solid, LineStyle.Solid, LineStyle.Solid, LineStyle.Solid, LineStyle.LongDashDotDot, LineStyle.LongDashDotDot }; List properties = new List() { nameof(SweepData.Acceleration), nameof(SweepData.TargetAcceleration), nameof(SweepData.UpStopAcceleration), nameof(SweepData.DownStopAcceleration), nameof(SweepData.UpWarnAcceleration), nameof(SweepData.DownWarnAcceleration) }; private int maxFrame; private int currentFrame; private bool isEnabled = false; public AvaloniaList LineSeries { get; } = new AvaloniaList(); public PlotModel PlotModel { get; } = new PlotModel(); public override MainPageType PageType => MainPageType.SinePage; protected override void Cancel() { base.Cancel(); } private SineDataReviewViewModel():base() { Content = typeof(Views.SineDataReviewView); PlotModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis() { MajorGridlineStyle = LineStyle.Solid, Position = OxyPlot.Axes.AxisPosition.Bottom, Title = App.Current?.FindResource("Frequency")+"", Key = "X", Unit = "Hz" }); PlotModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis() { MajorGridlineStyle = LineStyle.Solid, Position = OxyPlot.Axes.AxisPosition.Left, Title = App.Current?.FindResource("AccelerationSignal")+"", Key = "Y", Unit = "g" }); for (int i = 0; i < oxyColors.Count; i++) { LineSeries line = new LineSeries(); line.Title = App.Current?.FindResource(ShakerConstant.ShowSweepAccelerationSpectrumNames[i]) + ""; line.Color = oxyColors[i]; line.StrokeThickness = 1; line.DataFieldX = nameof(SweepData.Frequency); line.DataFieldY = properties[i]; line.LineStyle = lineStyles[i]; line.XAxisKey = "X"; line.YAxisKey = "Y"; LineSeries.Add(line); PlotModel.Series.Add(line); } PlotModel.Title = App.Current?.FindResource(MainPageType.SinePage.Description()) + ""; PlotModel.Legends.Add(new OxyPlot.Legends.Legend() { IsLegendVisible = true, }); } static SineDataReviewViewModel() { } public string SweepDirection { get => sweepDirection; set =>SetProperty(ref sweepDirection , value); } public AvaloniaList Frames { get; } = new AvaloniaList(); public int MaxFrame { get => maxFrame; set =>SetProperty(ref maxFrame , value); } public int CurrentFrame { get =>currentFrame; set { SetProperty(ref currentFrame, value); GetSweepData(); } } public int MinFrame => 1; public bool IsEnabled { get => isEnabled; set =>SetProperty(ref isEnabled , value); } private string sweepDirection = string.Empty; private protected override void InitConfig() { IsEnabled = false; var group = tdmsfile.Contains(nameof(SweepConfigModel)) ? tdmsfile[nameof(SweepConfigModel)] : null; if (group == null) return; GetConfig(sweepConfig, group); group = tdmsfile.Contains(SineMainPageViewModel.TDMSDataName) ? tdmsfile[SineMainPageViewModel.TDMSDataName] : null; if (group == null) return; var channeld = group.Contains(nameof(SineDataModel.SweepIndex)) ? group[nameof(SineDataModel.SweepIndex)] : null; if (channeld == null) return; if (channeld.ChildCount == 0) return; var max = channeld.GetDataValues((uint)channeld.ChildCount - 1, 1)[0]; indexLenght = new int[max + 1]; int index = 0; for (int i = 0; i < max + 1; i++) { while (true) { uint readlen = Math.Min(10000, (uint)(channeld.ChildCount - (uint)index)); if (readlen == 0) break; var temp = channeld.GetDataValues((uint)index, readlen); int c = temp.Count(x => x == i); indexLenght[i] += c; if (c == readlen) { index += (int)readlen; continue; } else { if (temp[^1] != i) { index += c; break; } } } } Frames.Clear(); MaxFrame = max+1; for(int i=0;i data = new List(); var index = Frames.ToList().FindIndex(x => CurrentFrame == x); if(index ==-1) { DataReviewViewModel.Instance.ShowError(""); return; } int startindex =0; if(index>0) { startindex = indexLenght.Take(1).Sum(); } int count = indexLenght[index]; if(count ==0) { } else { var group = tdmsfile.Contains(SineMainPageViewModel.TDMSDataName) ? tdmsfile[SineMainPageViewModel.TDMSDataName] : null; if (group != null) { var freqchanneld = group.Contains(nameof(SineDataModel.CurrentFrequency)) ? group[nameof(SineDataModel.CurrentFrequency)] : null; var accachannel = group.Contains(nameof(SineDataModel.CurrentAcceleration)) ? group[nameof(SineDataModel.CurrentAcceleration)] : null; var dirchannel = group.Contains(nameof(SineDataModel.SweepDirection)) ? group[nameof(SineDataModel.SweepDirection)] : null; if (freqchanneld != null && freqchanneld.ChildCount != 0 && accachannel != null && accachannel.ChildCount != 0 && dirchannel!=null) { double[] freq = freqchanneld.GetDataValues((uint)startindex, (uint)count); double[] acc = accachannel.GetDataValues((uint)startindex, (uint)count); double value = 0; double upstop = 0; double upwarn = 0; double downstop = 0; double downwarn = 0; SweepDirection = ((SweepDirection)dirchannel.GetDataValues((uint)startindex, 1)[0]).Description(); for (int i = 0; i < count; i++) { if (freq[i] > sweepConfig.EndFrequency || freq[i] < sweepConfig.StartFrequency) continue; sweepConfig.CalcAmpt(freq[i], ref value, ref upstop, ref upwarn, ref downstop, ref downwarn); SweepData sweep = new SweepData(freq[i], Shaker.Models.Tools.Tools.VoltageToQuantities(acc[i], ShakerConfig.AccelerationConfigs[0].Sensitivity), value, upstop, downstop, upwarn, downwarn); data.Add(sweep); } } } } PlotModel.InvalidatePlot(false); for(int i=0;i