using Avalonia.Collections; using Avalonia.Controls; using Avalonia.Markup.Xaml.MarkupExtensions; using Shaker.Models; using ShakerApp.Tools; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ShakerApp.ViewModels { internal class ShakerConfigViewModel : DisplayViewModelBase { public const string TDMSConfigName = "ShakerConfig"; [PropertyAssociation(nameof(ShakerConfigModel.DisplacementBias))] public float DisplacementBias { get => Model.DisplacementBias; set => SetProperty(ref Model.DisplacementBias, value); } [PropertyAssociation(nameof(ShakerConfigModel.SynthesisType))] public AccelerationSynthesisType SynthesisType { get => Model.SynthesisType; set => SetProperty(ref Model.SynthesisType, value); } [PropertyAssociation(nameof(ShakerConfigModel.OutSignalGain))] public double OutSignalGain { get => Model.OutSignalGain; set => SetProperty(ref Model.OutSignalGain, value); } public override bool CanResize => false; public override double Width => 960; public override double Height => 560; [PropertyAssociation(nameof(ShakerConfigModel.BitstreamMD5))] public string BitstreamMD5 => Model.BitstreamMD5; [PropertyAssociation(nameof(ShakerConfigModel.BitstreamVersion))] public string BitfileVersion =>Model.BitstreamVersion; [PropertyAssociation(nameof(ShakerConfigModel.SignatureRegister))] public string SignatureRegister =>Model.SignatureRegister; [PropertyAssociation(nameof(ShakerConfigModel.BitstreamVersion))] public string BitstreamVersion =>Model.BitstreamVersion; [PropertyAssociation(nameof(ShakerConfigModel.BitstreamVersion))] public string SignatureNames => Model.BitstreamVersion; [PropertyAssociation(nameof(ShakerConfigModel.CreateTime))] public DateTime CreateTime =>Model.CreateTime; [PropertyAssociation(nameof(ShakerConfigModel.MaxRiseCount))] public uint MaxRiseCount => Model.MaxRiseCount; [PropertyAssociation(nameof(ShakerConfigModel.MaxZeroChangedCount))] public uint MaxZeroChangedCount => Model.MaxZeroChangedCount; [PropertyAssociation(nameof(ShakerConfigModel.MaxSignalCount))] public uint MaxSignalCount => Model.MaxSignalCount; [PropertyAssociation(nameof(ShakerConfigModel.MaxEmergencyStopCount))] public uint MaxEmergencyStopCount => Model.MaxEmergencyStopCount; [PropertyAssociation(nameof(ShakerConfigModel.MaxStopWindowCount))] public uint MaxStopWindowCount => MaxStopWindowCount; [PropertyAssociation(nameof(ShakerConfigModel.MaxAdjustCount))] public uint MaxAdjustCount => Model.MaxAdjustCount; [PropertyAssociation(nameof(ShakerConfigModel.MaxFallCount))] public uint MaxFallCount => Model.MaxFallCount; [PropertyAssociation(nameof(ShakerConfigModel.StartCount))] public uint StartCount => Model.StartCount; [PropertyAssociation(nameof(ShakerConfigModel.InitialLocation))] public double InitialLocation { get => Model.InitialLocation; set => SetProperty(ref Model.InitialLocation, value); } [PropertyAssociation(nameof(ShakerConfigModel.SampleRate))] public uint SampleRate => Model.SampleRate; [PropertyAssociation(nameof(ShakerConfigModel.FPGAClock))] public uint FPGAClock => Model.FPGAClock; [PropertyAssociation(nameof(ShakerConfigModel.AnalogSignalConfigs))] public int ChannelCount => Model.AnalogSignalConfigs.Count; [PropertyAssociation(nameof(ShakerConfigModel.MaxFrequency))] public double MaxFrequency { get => Model.MaxFrequency; set => SetProperty(ref Model.MaxFrequency, value); } [PropertyAssociation(nameof(ShakerConfigModel.MinFrequency))] public double MinFrequency { get => Model.MinFrequency; set => SetProperty(ref Model.MinFrequency, value); } [PropertyAssociation(nameof(ShakerConfigModel.MaxOutInput))] public double MaxOutInput { get => Model.MaxOutInput; set => SetProperty(ref Model.MaxOutInput, value); } [PropertyAssociation(nameof(ShakerConfigModel.MaxAcceleration))] public double MaxAcceleration { get => Model.MaxAcceleration; set => SetProperty(ref Model.MaxAcceleration, value); } [PropertyAssociation(nameof(ShakerConfigModel.MaxDisplacement))] public double MaxDisplacement { get => Model.MaxDisplacement; set => SetProperty(ref Model.MaxDisplacement, value); } [PropertyAssociation(nameof(ShakerConfigModel.MaxDriver))] public double MaxDriver { get => Model.MaxDriver; set => SetProperty(ref Model.MaxDriver, value); } [PropertyAssociation(nameof(ShakerConfigModel.MaxJitterAcceleration))] public double MaxJitterAcceleration { get => Model.MaxJitterAcceleration; set => SetProperty(ref Model.MaxJitterAcceleration, value); } [PropertyAssociation(nameof(ShakerConfigModel.MaxJitterDisplacement))] public double MaxJitterDisplacement { get => Model.MaxJitterDisplacement; set => SetProperty(ref Model.MaxJitterDisplacement, value); } [PropertyAssociation(nameof(ShakerConfigModel.MaxVelocity))] public double MaxVelocity { get => Model.MaxVelocity; set => SetProperty(ref Model.MaxVelocity, value); } [PropertyAssociation(nameof(ShakerConfigModel.AccelerationConfigs))] public int AccelerationSensorCount => Model.AccelerationConfigs.Count; public AvaloniaList> AnalogSignals { get; } = new AvaloniaList>(); public AvaloniaList> Accelerations { get; } = new AvaloniaList>(); [PropertyAssociation(nameof(ShakerConfigModel.DisplacementSensitivity))] public double DisplacementSensitivity { get => Model.DisplacementSensitivity; set => SetProperty(ref Model.DisplacementSensitivity, value); } public double AccelerationSensitivity => Accelerations.First().Value.Model.Sensitivity; private ShakerConfigViewModel() { Content = typeof(Views.ShakerConfigView); for(int i=0;i(i + 1,new AccelerationConfigViewModel(Model.AccelerationConfigs[i]))); Accelerations[^1].Value.PropertyChanged += (sender, args) => { SaveIsEnabled = true; switch(args.PropertyName) { case nameof(AccelerationConfigViewModel.Weight): if (AccelerationSensorCount <= 1) return; Accelerations[^1].Value.Weight = 1 - Accelerations.Take(AccelerationSensorCount - 1).Sum(x => x.Value.Weight); break; } }; } for(int i=0;i(i + 1, new AnalogSignalConfigViewModel(Model.AnalogSignalConfigs[i]))); } GetEvent().Subscrip((sender, args) => { UpDateModel(args.Data.ShakerConfig); OnPropertyChanged(nameof(AccelerationSensorCount)); CommunicationViewModel.Instance.LocalCommunication?.GetEvent()?.Subscrip((sender, args) => { if (args.Data == null) return; UpDateModel(args.Data); OnPropertyChanged(nameof(AccelerationSensorCount)); CommunicationViewModel.Instance.ServiceCommunication?.GetEvent()?.Publish(this, args.Data); }); GetEvent(Models.Topic.InitSeries).Publish(this, null); }); } static ShakerConfigViewModel() { } public void SaveTdmsConfig(TDMS.ITDMSFile? config) { if (config == null) return; var group = config.Contains(nameof(ShakerConfigModel)) ? config[nameof(ShakerConfigModel)]:config.AddGroup(nameof(ShakerConfigModel)); if (group == null) return; typeof(ShakerConfigModel).GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance) .Select(x =>(x, x.GetValue(Model))) .ToList() .ForEach(x => { if(x.Item2 ==null) { return; } if(x.x.FieldType.IsAssignableTo(typeof(IList)) || x.x.FieldType.IsArray) { group.CreateOrUpdateProperty($"{x.x.Name}", string.Join("", Tools.Tools.Serialize(x.x.FieldType, x.Item2).Select(x => $"{x:X2}"))); } else { group.CreateOrUpdateProperty($"{x.x.Name}", x.Item2?.ToString() ?? string.Empty); } }); } public override void UpDateModel(Shaker.Models.ShakerConfigModel model) { base.UpDateModel(model); Accelerations.Clear(); for(int i=0;i(i + 1, new AccelerationConfigViewModel(model.AccelerationConfigs[i]))); Accelerations[^1].Value.PropertyChanged += (sender, args) => { SaveIsEnabled = true; }; } AnalogSignals.Clear(); for(int i=0;i(i + 1, new AnalogSignalConfigViewModel(model.AnalogSignalConfigs[i]))); } } public static ShakerConfigViewModel Instance { get; } = new ShakerConfigViewModel(); protected override void Save() { if(SynthesisType == AccelerationSynthesisType.Synthesis && MathF.Abs(Accelerations.Sum(x=>x.Value.Weight) -1f)>0.0001f) { ShowError(string.Format(App.Current?.FindResource("AccelerationWeightSumError") + "", 1)); return; } base.Save(); CommunicationViewModel.Instance.LocalCommunication?.GetEvent()?.Publish(this, Model); CommunicationViewModel.Instance.ServiceCommunication?.GetEvent()?.Publish(this, Model); } public override void ReceiveServiceModel(ShakerConfigModel model) { base.ReceiveServiceModel(model); CommunicationViewModel.Instance.LocalCommunication?.GetEvent()?.Publish(this, Model); } } }