ShakerConfigViewModel.cs 11 KB

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