using Shaker.Models; using ShakerApp.Tools; using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; namespace ShakerApp.ViewModels { public class ShakerControlViewModel : DisplayViewModelBase { public override bool CanResize => false; public override double Width => 960; public override double Height => 560; public static ShakerControlViewModel Instance { get; } = new ShakerControlViewModel(); static ShakerControlViewModel() { } public void SaveTdmsConfig(TDMS.ITDMSFile? config) { if (config == null) return; var group=config.Contains(nameof(ShakerControlModel)) ? config[nameof(ShakerControlModel)]:config.AddGroup(nameof(ShakerControlModel)); if (group == null) return; typeof(ShakerControlModel).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); } }); } private ShakerControlViewModel() { for (int i = 0; i < Model.ValveConfig.Count; i++) { ValveConfig.Add(new IndexValueItemViewModel(i + 1, new ValveConfigItemViewModel(Model.ValveConfig[i]))); } GetEvent().Subscrip((sender, args) => { UpDateModel(args.Data.ShakerControl); CommunicationViewModel.Instance.LocalCommunication?.GetEvent()?.Subscrip((sender, args) => { UpDateModel(args.Data); CommunicationViewModel.Instance.ServiceCommunication?.GetEvent()?.Publish(this, args.Data); }); }); } public override void UpDateModel(ShakerControlModel model) { ValveConfig.Clear(); base.UpDateModel(model); for(int i=0;i(i + 1, new ValveConfigItemViewModel(model.ValveConfig[i]))); } if (model.ControlItems == null) { model.ControlItems = new List(); } var items = model.ControlItems.ToArray(); ControlItems.Clear(); if (items.Length > 0) { for (int i = 0; i < model.ControlItems.Count; i++) { ControlItems.Add(new IndexValueItemViewModel(i + 1, new SweepControlItemViewModel(items[i]))); } } } [PropertyAssociation(nameof(ShakerControlModel.PageType))] public MainPageType PageType { get => Model.PageType; set { bool changed = Model.PageType != value; SetProperty(ref Model.PageType, value); if (!changed) return; CommunicationViewModel.Instance.LocalCommunication?.GetEvent()?.Publish(this, value); CommunicationViewModel.Instance.ServiceCommunication?.GetEvent()?.Publish(this, value); } } [PropertyAssociation(nameof(ShakerControlModel.MaxControlItemCount))] public int MaxControlItemCount => Model.MaxControlItemCount; [PropertyAssociation(nameof(ShakerControlModel.MaxDisplacementVoltage))] public float MaxDisplacementVoltage => Model.MaxDisplacementVoltage; [PropertyAssociation(nameof(ShakerControlModel.MinDisplacementVoltage))] public float MinDisplacementVoltage => Model.MinDisplacementVoltage; [PropertyAssociation(nameof(ShakerControlModel.MaxValveVoltage))] public float MaxValveVoltage => Model.MaxValveVoltage; [PropertyAssociation(nameof(ShakerControlModel.MinValveVoltage))] public float MinValveVoltage => Model.MinValveVoltage; [PropertyAssociation(nameof(ShakerControlModel.ValvePolarity))] public Polarity ValvePolarity { get => Model.ValvePolarity; set => SetProperty(ref Model.ValvePolarity, value); } [PropertyAssociation(nameof(ShakerControlModel.SelfLoop))] public bool SelfLoop { get => Model.SelfLoop; set => SetProperty(ref Model.SelfLoop, value); } [PropertyAssociation(nameof(ShakerControlModel.Debug))] public bool Debug { get => Model.Debug; set => SetProperty(ref Model.Debug, value); } [PropertyAssociation(nameof(ShakerControlModel.DisplacementP))] public float DisplacementP { get => Model.DisplacementP; set => SetProperty(ref Model.DisplacementP, value); } [PropertyAssociation(nameof(ShakerControlModel.DisplacementI))] public float DisplacementI { get => Model.DisplacementI; set => SetProperty(ref Model.DisplacementI, value); } [PropertyAssociation(nameof(ShakerControlModel.FlutterFrequency))] public float FlutterFrequency { get => Model.FlutterFrequency; set => SetProperty(ref Model.FlutterFrequency, value); } [PropertyAssociation(nameof(ShakerControlModel.FlutterAmpt))] public float FlutterAmpt { get => Model.FlutterAmpt; set => SetProperty(ref Model.FlutterAmpt, value); } [PropertyAssociation(nameof(ShakerControlModel.ValveConfig))] public int ServoValveCount => Model.ValveConfig.Count; [PropertyAssociation(nameof(ShakerControlModel.MaxDisplacementIntegral))] public float MaxDisplacementIntegral { get => Model.MaxDisplacementIntegral; set => SetProperty(ref Model.MaxDisplacementIntegral, value); } [PropertyAssociation(nameof(ShakerControlModel.ValveConfig))] public ObservableCollection> ValveConfig { get; } = new ObservableCollection>(); [PropertyAssociation(nameof(ShakerControlModel.OilStopped))] public bool OilStopped { get => Model.OilStopped; set => SetProperty(ref Model.OilStopped, value); } [PropertyAssociation(nameof(ShakerControlModel.DisplacementOpenLoop))] public bool DisplacementOpenLoop { get => Model.DisplacementOpenLoop; set => SetProperty(ref Model.DisplacementOpenLoop, value); } [PropertyAssociation(nameof(ShakerControlModel.ControlItems))] public ObservableCollection> ControlItems { get; } = new ObservableCollection>(); [PropertyAssociation(nameof(ShakerControlModel.OutSignal))] public bool OutSignal { get => Model.OutSignal; set => SetProperty(ref Model.OutSignal, value); } [PropertyAssociation(nameof(ShakerControlModel.ControlItems))] public bool AddEnabled => ControlItems.Count < SweepFreqConfigModel.MAXITEM; [PropertyAssociation(nameof(ShakerControlModel.ControlItems))] public bool RemoveEnabled => ControlItems.Count > 0; public ICommand AddCommand => new CommunityToolkit.Mvvm.Input.RelayCommand(Add); private void Add() { if (ControlItems.Count >= MaxControlItemCount) return; SaveIsEnabled = true; Model.ControlItems.Add(new SweepControlItemModel()); ControlItems.Add(new IndexValueItemViewModel(ControlItems.Count + 1,new SweepControlItemViewModel( Model.ControlItems[^1]))); } public ICommand DeleteLastCommand => new CommunityToolkit.Mvvm.Input.RelayCommand(Delete); private void Delete() { if (ControlItems.Count == 0) return; Model.ControlItems.RemoveAt(Model.ControlItems.Count - 1); ControlItems.RemoveAt(ControlItems.Count - 1); } public ICommand RefreshCommand => new CommunityToolkit.Mvvm.Input.RelayCommand(Refresh); private void Refresh() { if (ControlItems.Count == 0) return; var items = ControlItems.OrderBy(x => x.Value.Frequency).DistinctBy(x => x.Value.Frequency).Select(x => x.Value).ToList(); if (items.Count == 0) { ControlItems.Clear(); return; } for (int i = 0; i < items.Count; i++) { ControlItems[i].Value = items[i]; } if (ControlItems.Count > items.Count) { int removecount = ControlItems.Count - items.Count; for (int i = 0; i < removecount; i++) { ControlItems.RemoveAt(items.Count); } } } protected override void Save() { base.Save(); CommunicationViewModel.Instance.LocalCommunication?.GetEvent()?.Publish(this, Model); CommunicationViewModel.Instance.ServiceCommunication?.GetEvent()?.Publish(this, Model); } } }