ShakerControlViewModel.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using Shaker.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Input;
  9. namespace ShakerApp.ViewModels
  10. {
  11. public class ShakerControlViewModel : ViewModelBase<ShakerControlModel>
  12. {
  13. public override bool CanResize => false;
  14. public override double Width => 960;
  15. public override double Height => 560;
  16. public static ShakerControlViewModel Instance { get; } = new ShakerControlViewModel();
  17. static ShakerControlViewModel()
  18. {
  19. }
  20. private ShakerControlViewModel()
  21. {
  22. for (int i = 0; i < Model.ValveConfig.Count; i++)
  23. {
  24. ValveConfig.Add(new IndexValueItemViewModel<ValveConfigItemViewModel>(i + 1, new ValveConfigItemViewModel(Model.ValveConfig[i])));
  25. }
  26. GetEvent<AllConfig>().Subscrip((sender, args) =>
  27. {
  28. UpDateModel(args.Data.ShakerControl);
  29. if(CommunicationViewModel.Intance.ServiceIsStart)
  30. {
  31. CommunicationViewModel.Intance.ServiceCommunication.GetEvent<ShakerControlModel>().Publish(this, args.Data.ShakerControl);
  32. }
  33. CommunicationViewModel.Intance.LocalCommunication?.GetEvent<ShakerControlModel>()?.Subscrip((sender, args) =>
  34. {
  35. UpDateModel(args.Data);
  36. CommunicationViewModel.Intance.ServiceCommunication?.GetEvent<ShakerControlModel>()?.Publish(this, args.Data);
  37. });
  38. });
  39. }
  40. public override void UpDateModel(ShakerControlModel model)
  41. {
  42. ValveConfig.Clear();
  43. base.UpDateModel(model);
  44. for(int i=0;i<model.ValveConfig.Count;i++)
  45. {
  46. ValveConfig.Add(new IndexValueItemViewModel<ValveConfigItemViewModel>(i + 1, new ValveConfigItemViewModel(model.ValveConfig[i])));
  47. }
  48. if (model.ControlItems == null)
  49. {
  50. model.ControlItems = new List<SweepControlItemModel>();
  51. }
  52. var items = model.ControlItems.ToArray();
  53. ControlItems.Clear();
  54. if (items.Length > 0)
  55. {
  56. for (int i = 0; i < model.ControlItems.Count; i++)
  57. {
  58. ControlItems.Add(new IndexValueItemViewModel<SweepControlItemViewModel>(i + 1, new SweepControlItemViewModel(items[i])));
  59. }
  60. }
  61. }
  62. [PropertyAssociation(nameof(ShakerControlModel.PageType))]
  63. public MainPageType PageType
  64. {
  65. get => Model.PageType;
  66. set
  67. {
  68. SetProperty(ref Model.PageType, value);
  69. CommunicationViewModel.Intance.LocalCommunication?.GetEvent<ShakerControlModel>()?.Publish(this, Model);
  70. }
  71. }
  72. [PropertyAssociation(nameof(ShakerControlModel.MaxControlItemCount))]
  73. public int MaxControlItemCount => Model.MaxControlItemCount;
  74. [PropertyAssociation(nameof(ShakerControlModel.MaxDisplacementVoltage))]
  75. public float MaxDisplacementVoltage => Model.MaxDisplacementVoltage;
  76. [PropertyAssociation(nameof(ShakerControlModel.MinDisplacementVoltage))]
  77. public float MinDisplacementVoltage => Model.MinDisplacementVoltage;
  78. [PropertyAssociation(nameof(ShakerControlModel.MaxValveVoltage))]
  79. public float MaxValveVoltage => Model.MaxValveVoltage;
  80. [PropertyAssociation(nameof(ShakerControlModel.MinValveVoltage))]
  81. public float MinValveVoltage => Model.MinValveVoltage;
  82. [PropertyAssociation(nameof(ShakerControlModel.ValvePolarity))]
  83. public Polarity ValvePolarity { get => Model.ValvePolarity; set => SetProperty(ref Model.ValvePolarity, value); }
  84. [PropertyAssociation(nameof(ShakerControlModel.SelfLoop))]
  85. public bool SelfLoop { get => Model.SelfLoop; set => SetProperty(ref Model.SelfLoop, value); }
  86. [PropertyAssociation(nameof(ShakerControlModel.Debug))]
  87. public bool Debug { get => Model.Debug; set => SetProperty(ref Model.Debug, value); }
  88. [PropertyAssociation(nameof(ShakerControlModel.DisplacementP))]
  89. public float DisplacementP { get => Model.DisplacementP; set => SetProperty(ref Model.DisplacementP, value); }
  90. [PropertyAssociation(nameof(ShakerControlModel.DisplacementI))]
  91. public float DisplacementI { get => Model.DisplacementI; set => SetProperty(ref Model.DisplacementI, value); }
  92. [PropertyAssociation(nameof(ShakerControlModel.FlutterFrequency))]
  93. public float FlutterFrequency { get => Model.FlutterFrequency; set => SetProperty(ref Model.FlutterFrequency, value); }
  94. [PropertyAssociation(nameof(ShakerControlModel.FlutterAmpt))]
  95. public float FlutterAmpt { get => Model.FlutterAmpt; set => SetProperty(ref Model.FlutterAmpt, value); }
  96. [PropertyAssociation(nameof(ShakerControlModel.ValveConfig))]
  97. public int ServoValveCount => Model.ValveConfig.Count;
  98. [PropertyAssociation(nameof(ShakerControlModel.MaxDisplacementIntegral))]
  99. public float MaxDisplacementIntegral { get => Model.MaxDisplacementIntegral; set => SetProperty(ref Model.MaxDisplacementIntegral, value); }
  100. [PropertyAssociation(nameof(ShakerControlModel.DisplacementBias))]
  101. public float DisplacementBias { get => Model.DisplacementBias; set => SetProperty(ref Model.DisplacementBias, value); }
  102. [PropertyAssociation(nameof(ShakerControlModel.ValveConfig))]
  103. public ObservableCollection<IndexValueItemViewModel<ValveConfigItemViewModel>> ValveConfig { get; } = new ObservableCollection<IndexValueItemViewModel<ValveConfigItemViewModel>>();
  104. [PropertyAssociation(nameof(ShakerControlModel.OilStopped))]
  105. public bool OilStopped { get => Model.OilStopped; set => SetProperty(ref Model.OilStopped, value); }
  106. [PropertyAssociation(nameof(ShakerControlModel.DisplacementOpenLoop))]
  107. public bool DisplacementOpenLoop { get => Model.DisplacementOpenLoop; set => SetProperty(ref Model.DisplacementOpenLoop, value); }
  108. [PropertyAssociation(nameof(ShakerControlModel.ControlItems))]
  109. public ObservableCollection<IndexValueItemViewModel<SweepControlItemViewModel>> ControlItems { get; } = new ObservableCollection<IndexValueItemViewModel<SweepControlItemViewModel>>();
  110. [PropertyAssociation(nameof(ShakerControlModel.OutSignal))]
  111. public bool OutSignal { get => Model.OutSignal; set => SetProperty(ref Model.OutSignal, value); }
  112. [PropertyAssociation(nameof(ShakerControlModel.ControlItems))]
  113. public bool AddEnabled => ControlItems.Count < SweepFreqConfigModel.MAXITEM;
  114. [PropertyAssociation(nameof(ShakerControlModel.ControlItems))]
  115. public bool RemoveEnabled => ControlItems.Count > 0;
  116. public ICommand AddCommand => new CommunityToolkit.Mvvm.Input.RelayCommand(Add);
  117. private void Add()
  118. {
  119. if (ControlItems.Count >= MaxControlItemCount) return;
  120. SaveIsEnabled = true;
  121. Model.ControlItems.Add(new SweepControlItemModel());
  122. ControlItems.Add(new IndexValueItemViewModel<SweepControlItemViewModel>(ControlItems.Count + 1,new SweepControlItemViewModel( Model.ControlItems[^1])));
  123. }
  124. public ICommand DeleteLastCommand => new CommunityToolkit.Mvvm.Input.RelayCommand(Delete);
  125. private void Delete()
  126. {
  127. if (ControlItems.Count == 0) return;
  128. Model.ControlItems.RemoveAt(Model.ControlItems.Count - 1);
  129. ControlItems.RemoveAt(ControlItems.Count - 1);
  130. }
  131. public ICommand RefreshCommand => new CommunityToolkit.Mvvm.Input.RelayCommand(Refresh);
  132. private void Refresh()
  133. {
  134. if (ControlItems.Count == 0) return;
  135. var items = ControlItems.OrderBy(x => x.Value.Frequency).DistinctBy(x => x.Value.Frequency).Select(x => x.Value).ToList();
  136. if (items.Count == 0)
  137. {
  138. ControlItems.Clear();
  139. return;
  140. }
  141. for (int i = 0; i < items.Count; i++)
  142. {
  143. ControlItems[i].Value = items[i];
  144. }
  145. if (ControlItems.Count > items.Count)
  146. {
  147. int removecount = ControlItems.Count - items.Count;
  148. for (int i = 0; i < removecount; i++)
  149. {
  150. ControlItems.RemoveAt(items.Count);
  151. }
  152. }
  153. }
  154. protected override void Save()
  155. {
  156. base.Save();
  157. CommunicationViewModel.Intance.LocalCommunication?.GetEvent<ShakerControlModel>()?.Publish(this, Model);
  158. CommunicationViewModel.Intance.ServiceCommunication?.GetEvent<ShakerControlModel>()?.Publish(this, Model);
  159. }
  160. }
  161. }