123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- 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<ShakerControlModel>
- {
- 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<ValveConfigItemViewModel>(i + 1, new ValveConfigItemViewModel(Model.ValveConfig[i])));
- }
- GetEvent<AllConfig>().Subscrip((sender, args) =>
- {
- UpDateModel(args.Data.ShakerControl);
- CommunicationViewModel.Instance.LocalCommunication?.GetEvent<ShakerControlModel>()?.Subscrip((sender, args) =>
- {
- UpDateModel(args.Data);
- CommunicationViewModel.Instance.ServiceCommunication?.GetEvent<ShakerControlModel>()?.Publish(this, args.Data);
- });
- });
- }
- public override void UpDateModel(ShakerControlModel model)
- {
- ValveConfig.Clear();
- base.UpDateModel(model);
- for(int i=0;i<model.ValveConfig.Count;i++)
- {
- ValveConfig.Add(new IndexValueItemViewModel<ValveConfigItemViewModel>(i + 1, new ValveConfigItemViewModel(model.ValveConfig[i])));
- }
- if (model.ControlItems == null)
- {
- model.ControlItems = new List<SweepControlItemModel>();
- }
- var items = model.ControlItems.ToArray();
- ControlItems.Clear();
- if (items.Length > 0)
- {
- for (int i = 0; i < model.ControlItems.Count; i++)
- {
- ControlItems.Add(new IndexValueItemViewModel<SweepControlItemViewModel>(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<MainPageType>()?.Publish(this, value);
- CommunicationViewModel.Instance.ServiceCommunication?.GetEvent<MainPageType>()?.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<IndexValueItemViewModel<ValveConfigItemViewModel>> ValveConfig { get; } = new ObservableCollection<IndexValueItemViewModel<ValveConfigItemViewModel>>();
- [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<IndexValueItemViewModel<SweepControlItemViewModel>> ControlItems { get; } = new ObservableCollection<IndexValueItemViewModel<SweepControlItemViewModel>>();
- [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<SweepControlItemViewModel>(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<Shaker.Models.ShakerControlModel>()?.Publish(this, Model);
- CommunicationViewModel.Instance.ServiceCommunication?.GetEvent<Shaker.Models.ShakerControlModel>()?.Publish(this, Model);
- }
- }
- }
|