123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- using Avalonia;
- using Avalonia.Collections;
- using Avalonia.Controls;
- using Avalonia.Controls.ApplicationLifetimes;
- using OxyPlot;
- using OxyPlot.Series;
- using Shaker.Models;
- using ShakerApp.Tools;
- using ShakerApp.Views;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ShakerApp.ViewModels
- {
- internal class RandomMainPageViewModel:ViewModelBase<RandomDataModel>,IMainPageViewModel
- {
- [PropertyAssociation(nameof(RandomDataModel.CurrentIdentifyDisplacement))]
- public double CurrentIdentifyDisplacement => Model.CurrentIdentifyDisplacement;
- [PropertyAssociation(nameof(RandomDataModel.CurrentIdentifyRms))]
- public double CurrentIdentifyRms => Model.CurrentIdentifyRms;
- [PropertyAssociation(nameof(RandomDataModel.IdentifyIndex))]
- public double IdentifyIndex => Model.IdentifyIndex;
- [PropertyAssociation(nameof(RandomDataModel.RandomStatus))]
- public RandomStatus RandomStatus => Model.RandomStatus;
- [PropertyAssociation(nameof(RandomDataModel.TimeDomainRMS))]
- public double TimeDomainRMS => Model.TimeDomainRMS;
- [PropertyAssociation(nameof(RandomDataModel.RandomTestStep))]
- public RandomTestStep RandomTestStep => Model.RandomTestStep;
- [PropertyAssociation(nameof(RandomDataModel.RandomTestStep))]
- public string RandomTestStepKey => RandomTestStep.Description();
- [PropertyAssociation(nameof(RandomDataModel.CurrentTestLevel))]
- public double CurrentTestLevel => Model.CurrentTestLevel;
- [PropertyAssociation(nameof(RandomDataModel.CurrentTestTime))]
- public uint CurrentTestTime => Model.CurrentTestTime;
- [PropertyAssociation(nameof(RandomDataModel.CurrentLevelTestMaxTime))]
- public uint CurrentLevelTestMaxTime => Model.CurrentLevelTestMaxTime;
- public AvaloniaList<LineSeries> LineSeries { get; } = new AvaloniaList<LineSeries>();
- List<OxyColor> oxyColors = new List<OxyColor>() {OxyColors.Blue, 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.Solid, LineStyle.LongDashDotDot, LineStyle.LongDashDotDot };
- List<string> properties = new List<string>() {nameof(RandomData.Driver), nameof(RandomData.Acceleration), nameof(RandomData.TargetAcceleration), nameof(RandomData.UpStopAcceleration), nameof(RandomData.DownStopAcceleration), nameof(RandomData.UpWarnAcceleration), nameof(RandomData.DownWarnAcceleration) };
- List<RandomData> datas = new List<RandomData>();
- private RandomMainPageViewModel()
- {
- Content = typeof(Views.RandomMainPage);
- #region 加速度谱曲线
- PlotModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
- {
- MaximumPadding = 0,
- MinimumPadding = 0,
- Title = App.Current?.FindResource(ShakerConstant.FrequencyKey) + "",
- Unit = "Hz",
- MajorGridlineStyle = LineStyle.Solid,
- Position = OxyPlot.Axes.AxisPosition.Bottom,
- Key = "Freq"
- });
- PlotModel.Axes.Add(new OxyPlot.Axes.LogarithmicAxis()
- {
- Key = "Ampt",
- Title = App.Current?.FindResource(ShakerConstant.AmptAxisTitleKey) + "",
- Unit = "g",
- MajorGridlineStyle = LineStyle.Solid,
- Position = OxyPlot.Axes.AxisPosition.Left,
- });
- PlotModel.Title = App.Current?.FindResource(MainPageType.RandomPage.Description()) + "";
- for (int i = 0; i < oxyColors.Count; i++)
- {
- LineSeries line = new LineSeries();
- line.Title = App.Current?.FindResource(ShakerConstant.ShowRandomAccelerationSpectrumNames[i]) + "";
- line.Color = oxyColors[i];
- line.StrokeThickness = 1;
- line.DataFieldX = nameof(SweepData.Frequency);
- line.DataFieldY = properties[i];
- line.LineStyle = lineStyles[i];
- line.XAxisKey = "Freq";
- line.YAxisKey = "Ampt";
- LineSeries.Add(line);
- PlotModel.Series.Add(line);
- }
- PlotModel.Legends.Add(new OxyPlot.Legends.Legend()
- {
- ShowInvisibleSeries = true,
- });
- PlotModel.Title = App.Current?.FindResource(MainPageType.RandomPage.Description()) + "";
- #endregion
- GetEvent(ShakerSettingViewModel.LANGUAGECHANGEDEVENT).Subscrip((_, _) =>
- {
- PlotModel.InvalidatePlot(false);
- PlotModel.Title = App.Current?.FindResource(MainPageType.RandomPage.Description()) + "";
- PlotModel.Axes[0].Title = App.Current?.FindResource("Frequency") + "";
- PlotModel.Axes[1].Title = Title = App.Current?.FindResource("Ampt") + "";
- PlotModel.InvalidatePlot(true);
- });
- GetEvent<AllConfig>().Subscrip((_, _) =>
- {
- CommunicationViewModel.Instance.LocalCommunication?.GetEvent<RandomDataModel>()?.Subscrip((_, args) =>
- {
- UpdateData(new List<IResultDataModel>() { args.Data });
- CommunicationViewModel.Instance.ServiceCommunication?.GetEvent<RandomDataModel>()?.Publish(this, args.Data);
- });
- CommunicationViewModel.Instance.LocalCommunication?.GetEvent(nameof(RandomDataModel.TransferFunction))?.Subscrip((sender, args) =>
- {
- if (args.Data.Length > 0 && args.Data[0] is double[] func)
- {
- Tools.DispatherInovke.Inovke(()=> ShowTransferFunction(func));
- CommunicationViewModel.Instance.ServiceCommunication?.GetEvent(nameof(RandomDataModel.TransferFunction))?.Publish(this, null, func);
- }
- });
- });
- }
- private async void ShowTransferFunction(double[] data)
- {
- if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
- {
- RandomTransferFunctionViewModel.Instance.InitData();
- RandomTransferFunctionViewModel.Instance.InitTransferFunctionData(data);
- RandomTransferFunctionViewModel.Instance.Title = "TransferFunction";
- BaseDialogWindow window = new BaseDialogWindow();
- window.DataContext = RandomTransferFunctionViewModel.Instance;
- RandomTransferFunctionViewModel.Instance.CloseWindowAction = () => window?.Close();
- await window.ShowDialog(desktop.MainWindow!);
- }
- }
- static RandomMainPageViewModel()
- {
- }
- public void Start()
- {
- SetRefSpectrum(RandomConfigViewModel.Instance.RefSpectrum);
- }
- public void Stop()
- {
- }
- public override void UpDateModel(RandomDataModel model)
- {
- base.UpDateModel(model);
- int startindex = (int)(RandomConfigViewModel.Instance.MinFrequency / RandomConfigViewModel.Instance.FrequencyResolution);
- for(int i=0;i<datas.Count;i++)
- {
- datas[i].SetAcceleration(model.CurrentAccelerationSynthesisPSD[i + startindex]);
- datas[i].SetDriver(model.DriverPSD[i + startindex]);
- }
- PlotModel.InvalidatePlot(false);
- for(int i=0;i<LineSeries.Count;i++)
- {
- LineSeries[i].ItemsSource = datas;
- }
- PlotModel.InvalidatePlot(true);
- }
- public void UpdateData(List<IResultDataModel> model)
- {
- if(model !=null &&model.Count >0 && model.First() is RandomDataModel randomdata)
- {
- UpDateModel(randomdata);
- }
- }
- public void SetRefSpectrum(List<RandomData> data)
- {
- datas.Clear();
- datas.AddRange(data);
- PlotModel.InvalidatePlot(false);
- for (int i = 0; i < LineSeries.Count; i++)
- {
- LineSeries[i].ItemsSource = datas;
- }
- PlotModel.InvalidatePlot(true);
- }
- public MainPageType PageType => MainPageType.RandomPage;
- public OxyPlot.PlotModel PlotModel { get; private set; } = new OxyPlot.PlotModel();
- public static RandomMainPageViewModel Instance { get; } = new RandomMainPageViewModel();
- }
- }
|