ShakerConfigViewModel.cs 11 KB

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