123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- 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<OxyColor> oxyColors = new List<OxyColor>() { OxyColors.Black, OxyColors.Green, OxyColors.Red, OxyColors.Red, OxyColors.Yellow, OxyColors.Yellow };
- List<LineStyle> lineStyles = new List<LineStyle>() { LineStyle.Solid, LineStyle.Solid, LineStyle.Solid, LineStyle.Solid, LineStyle.LongDashDotDot, LineStyle.LongDashDotDot };
- List<string> properties = new List<string>() { 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> LineSeries { get; } = new AvaloniaList<LineSeries>();
- 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<int> Frames { get; } = new AvaloniaList<int>();
- 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<SweepConfigModel>(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<int>((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<int>((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<MaxFrame;i++)
- {
- Frames.Add(i + 1);
- }
- CurrentFrame = 1;
- IsEnabled = true;
- base.InitConfig();
- }
- public override void InitData()
- {
- base.InitData();
- GetSweepData();
- }
- private void GetSweepData()
- {
- if (!IsEnabled) return;
- List<SweepData> data = new List<SweepData>();
- 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<double>((uint)startindex, (uint)count);
- double[] acc = accachannel.GetDataValues<double>((uint)startindex, (uint)count);
- double value = 0; double upstop = 0; double upwarn = 0; double downstop = 0; double downwarn = 0;
- SweepDirection = ((SweepDirection)dirchannel.GetDataValues<int>((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<LineSeries.Count;i++)
- {
- LineSeries[i].ItemsSource = data;
- }
- PlotModel.InvalidatePlot(true);
- }
- public static SineDataReviewViewModel Instance { get; } = new SineDataReviewViewModel();
- }
- }
|