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,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 { get; } = new AvaloniaList(); List oxyColors = new List() {OxyColors.Blue, 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.Solid, LineStyle.LongDashDotDot, LineStyle.LongDashDotDot }; List properties = new List() {nameof(RandomData.Driver), nameof(RandomData.Acceleration), nameof(RandomData.TargetAcceleration), nameof(RandomData.UpStopAcceleration), nameof(RandomData.DownStopAcceleration), nameof(RandomData.UpWarnAcceleration), nameof(RandomData.DownWarnAcceleration) }; List datas = new List(); 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().Subscrip((_, _) => { CommunicationViewModel.Instance.LocalCommunication?.GetEvent()?.Subscrip((_, args) => { UpdateData(new List() { args.Data }); CommunicationViewModel.Instance.ServiceCommunication?.GetEvent()?.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 model) { if(model !=null &&model.Count >0 && model.First() is RandomDataModel randomdata) { UpDateModel(randomdata); } } public void SetRefSpectrum(List 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(); } }