ShakerConfigViewModel.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using Avalonia.Collections;
  2. using Avalonia.Controls;
  3. using Shaker.Models;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace ShakerApp.ViewModels
  10. {
  11. internal class ShakerConfigViewModel : ViewModelBase<Shaker.Models.ShakerConfigModel>
  12. {
  13. [PropertyAssociation(nameof(ShakerConfigModel.DisplacementBias))]
  14. public float DisplacementBias { get => Model.DisplacementBias; set => SetProperty(ref Model.DisplacementBias, value); }
  15. [PropertyAssociation(nameof(ShakerConfigModel.SynthesisType))]
  16. public AccelerationSynthesisType SynthesisType { get => Model.SynthesisType; set => SetProperty(ref Model.SynthesisType, value); }
  17. [PropertyAssociation(nameof(ShakerConfigModel.OutSignalGain))]
  18. public double OutSignalGain { get => Model.OutSignalGain; set => SetProperty(ref Model.OutSignalGain, value); }
  19. public override bool CanResize => false;
  20. public override double Width => 960;
  21. public override double Height => 560;
  22. [PropertyAssociation(nameof(ShakerConfigModel.BitstreamMD5))]
  23. public string BitstreamMD5 => Model.BitstreamMD5;
  24. [PropertyAssociation(nameof(ShakerConfigModel.BitstreamVersion))]
  25. public string BitfileVersion =>Model.BitstreamVersion;
  26. [PropertyAssociation(nameof(ShakerConfigModel.SignatureRegister))]
  27. public string SignatureRegister =>Model.SignatureRegister;
  28. [PropertyAssociation(nameof(ShakerConfigModel.BitstreamVersion))]
  29. public string BitstreamVersion =>Model.BitstreamVersion;
  30. [PropertyAssociation(nameof(ShakerConfigModel.BitstreamVersion))]
  31. public string SignatureNames => Model.BitstreamVersion;
  32. [PropertyAssociation(nameof(ShakerConfigModel.CreateTime))]
  33. public DateTime CreateTime =>Model.CreateTime;
  34. [PropertyAssociation(nameof(ShakerConfigModel.MaxRiseCount))]
  35. public uint MaxRiseCount => Model.MaxRiseCount;
  36. [PropertyAssociation(nameof(ShakerConfigModel.MaxZeroChangedCount))]
  37. public uint MaxZeroChangedCount => Model.MaxZeroChangedCount;
  38. [PropertyAssociation(nameof(ShakerConfigModel.MaxSignalCount))]
  39. public uint MaxSignalCount => Model.MaxSignalCount;
  40. [PropertyAssociation(nameof(ShakerConfigModel.MaxEmergencyStopCount))]
  41. public uint MaxEmergencyStopCount => Model.MaxEmergencyStopCount;
  42. [PropertyAssociation(nameof(ShakerConfigModel.MaxStopWindowCount))]
  43. public uint MaxStopWindowCount => MaxStopWindowCount;
  44. [PropertyAssociation(nameof(ShakerConfigModel.MaxAdjustCount))]
  45. public uint MaxAdjustCount => Model.MaxAdjustCount;
  46. [PropertyAssociation(nameof(ShakerConfigModel.MaxFallCount))]
  47. public uint MaxFallCount => Model.MaxFallCount;
  48. [PropertyAssociation(nameof(ShakerConfigModel.StartCount))]
  49. public uint StartCount => Model.StartCount;
  50. [PropertyAssociation(nameof(ShakerConfigModel.InitialLocation))]
  51. public double InitialLocation { get => Model.InitialLocation; set => SetProperty(ref Model.InitialLocation, value); }
  52. [PropertyAssociation(nameof(ShakerConfigModel.SampleRate))]
  53. public uint SampleRate => Model.SampleRate;
  54. [PropertyAssociation(nameof(ShakerConfigModel.FPGAClock))]
  55. public uint FPGAClock => Model.FPGAClock;
  56. [PropertyAssociation(nameof(ShakerConfigModel.AnalogSignalConfigs))]
  57. public int ChannelCount => Model.AnalogSignalConfigs.Count;
  58. [PropertyAssociation(nameof(ShakerConfigModel.MaxFrequency))]
  59. public double MaxFrequency { get => Model.MaxFrequency; set => SetProperty(ref Model.MaxFrequency, value); }
  60. [PropertyAssociation(nameof(ShakerConfigModel.MinFrequency))]
  61. public double MinFrequency { get => Model.MinFrequency; set => SetProperty(ref Model.MinFrequency, value); }
  62. [PropertyAssociation(nameof(ShakerConfigModel.MaxOutInput))]
  63. public double MaxOutInput { get => Model.MaxOutInput; set => SetProperty(ref Model.MaxOutInput, value); }
  64. [PropertyAssociation(nameof(ShakerConfigModel.MaxAcceleration))]
  65. public double MaxAcceleration { get => Model.MaxAcceleration; set => SetProperty(ref Model.MaxAcceleration, value); }
  66. [PropertyAssociation(nameof(ShakerConfigModel.MaxDisplacement))]
  67. public double MaxDisplacement { get => Model.MaxDisplacement; set => SetProperty(ref Model.MaxDisplacement, value); }
  68. [PropertyAssociation(nameof(ShakerConfigModel.MaxDriver))]
  69. public double MaxDriver { get => Model.MaxDriver; set => SetProperty(ref Model.MaxDriver, value); }
  70. [PropertyAssociation(nameof(ShakerConfigModel.MaxJitterAcceleration))]
  71. public double MaxJitterAcceleration { get => Model.MaxJitterAcceleration; set => SetProperty(ref Model.MaxJitterAcceleration, value); }
  72. [PropertyAssociation(nameof(ShakerConfigModel.MaxJitterDisplacement))]
  73. public double MaxJitterDisplacement { get => Model.MaxJitterDisplacement; set => SetProperty(ref Model.MaxJitterDisplacement, value); }
  74. [PropertyAssociation(nameof(ShakerConfigModel.MaxVelocity))]
  75. public double MaxVelocity { get => Model.MaxVelocity; set => SetProperty(ref Model.MaxVelocity, value); }
  76. [PropertyAssociation(nameof(ShakerConfigModel.AccelerationConfigs))]
  77. public int AccelerationSensorCount => Model.AccelerationConfigs.Count;
  78. public AvaloniaList<IndexValueItemViewModel<AnalogSignalConfigViewModel>> AnalogSignals { get; } = new AvaloniaList<IndexValueItemViewModel<AnalogSignalConfigViewModel>>();
  79. public AvaloniaList<IndexValueItemViewModel<AccelerationConfigViewModel>> Accelerations { get; } = new AvaloniaList<IndexValueItemViewModel<AccelerationConfigViewModel>>();
  80. [PropertyAssociation(nameof(ShakerConfigModel.DisplacementSensitivity))]
  81. public double DisplacementSensitivity { get => Model.DisplacementSensitivity; set => SetProperty(ref Model.DisplacementSensitivity, value); }
  82. public double AccelerationSensitivity => Accelerations.First().Value.Model.Sensitivity;
  83. private ShakerConfigViewModel()
  84. {
  85. Content = typeof(Views.ShakerConfigView);
  86. for(int i=0;i<Model.AccelerationConfigs.Count;i++)
  87. {
  88. Accelerations.Add(new IndexValueItemViewModel<AccelerationConfigViewModel>(i + 1,new AccelerationConfigViewModel(Model.AccelerationConfigs[i])));
  89. Accelerations[^1].Value.PropertyChanged += (sender, args) =>
  90. {
  91. SaveIsEnabled = true;
  92. switch(args.PropertyName)
  93. {
  94. case nameof(AccelerationConfigViewModel.Weight):
  95. if (AccelerationSensorCount <= 1) return;
  96. Accelerations[^1].Value.Weight = 1 - Accelerations.Take(AccelerationSensorCount - 1).Sum(x => x.Value.Weight);
  97. break;
  98. }
  99. };
  100. }
  101. for(int i=0;i<Model.AnalogSignalConfigs.Count;i++)
  102. {
  103. AnalogSignals.Add(new IndexValueItemViewModel<AnalogSignalConfigViewModel>(i + 1, new AnalogSignalConfigViewModel(Model.AnalogSignalConfigs[i])));
  104. }
  105. GetEvent<AllConfig>().Subscrip((sender, args) =>
  106. {
  107. UpDateModel(args.Data.ShakerConfig);
  108. OnPropertyChanged(nameof(AccelerationSensorCount));
  109. CommunicationViewModel.Instance.LocalCommunication?.GetEvent<Shaker.Models.ShakerConfigModel>()?.Subscrip((sender, args) =>
  110. {
  111. if (args.Data == null) return;
  112. UpDateModel(args.Data);
  113. OnPropertyChanged(nameof(AccelerationSensorCount));
  114. CommunicationViewModel.Instance.ServiceCommunication?.GetEvent<Shaker.Models.ShakerConfigModel>()?.Publish(this, args.Data);
  115. });
  116. GetEvent(Models.Topic.InitSeries).Publish(this, null);
  117. });
  118. }
  119. static ShakerConfigViewModel()
  120. {
  121. }
  122. public override void UpDateModel(Shaker.Models.ShakerConfigModel model)
  123. {
  124. base.UpDateModel(model);
  125. Accelerations.Clear();
  126. for(int i=0;i<model.AccelerationConfigs.Count;i++)
  127. {
  128. Accelerations.Add(new IndexValueItemViewModel<AccelerationConfigViewModel>(i + 1, new AccelerationConfigViewModel(model.AccelerationConfigs[i])));
  129. Accelerations[^1].Value.PropertyChanged += (sender, args) =>
  130. {
  131. SaveIsEnabled = true;
  132. };
  133. }
  134. AnalogSignals.Clear();
  135. for(int i=0;i<model.AnalogSignalConfigs.Count;i++)
  136. {
  137. AnalogSignals.Add(new IndexValueItemViewModel<AnalogSignalConfigViewModel>(i + 1, new AnalogSignalConfigViewModel(model.AnalogSignalConfigs[i])));
  138. }
  139. }
  140. public static ShakerConfigViewModel Instance { get; } = new ShakerConfigViewModel();
  141. protected override void Save()
  142. {
  143. if(SynthesisType == AccelerationSynthesisType.Synthesis && MathF.Abs(Accelerations.Sum(x=>x.Value.Weight) -1f)>0.0001f)
  144. {
  145. ShowError(string.Format(App.Current?.FindResource("AccelerationWeightSumError") + "", 1));
  146. return;
  147. }
  148. base.Save();
  149. CommunicationViewModel.Instance.LocalCommunication?.GetEvent<Shaker.Models.ShakerConfigModel>()?.Publish(this, Model);
  150. CommunicationViewModel.Instance.ServiceCommunication?.GetEvent<Shaker.Models.ShakerConfigModel>()?.Publish(this, Model);
  151. }
  152. public override void ReceiveServiceModel(ShakerConfigModel model)
  153. {
  154. base.ReceiveServiceModel(model);
  155. CommunicationViewModel.Instance.LocalCommunication?.GetEvent<Shaker.Models.ShakerConfigModel>()?.Publish(this, Model);
  156. }
  157. }
  158. }