ShakerConfigViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.OSVersion))]
  37. public string OSVersion { get => Model.OSVersion; }
  38. [PropertyAssociation(nameof(ShakerConfigModel.FrameworkVersion))]
  39. public string FrameworkVersion { get => Model.FrameworkVersion; }
  40. [PropertyAssociation(nameof(ShakerConfigModel.MachineName))]
  41. public string MachineName { get => Model.MachineName; }
  42. [PropertyAssociation(nameof(ShakerConfigModel.Version))]
  43. public string Version { get => Model.Version; }
  44. [PropertyAssociation(nameof(ShakerConfigModel.CreateTime))]
  45. public DateTime CreateTime =>Model.CreateTime;
  46. [PropertyAssociation(nameof(ShakerConfigModel.MaxRiseCount))]
  47. public uint MaxRiseCount => Model.MaxRiseCount;
  48. [PropertyAssociation(nameof(ShakerConfigModel.MaxZeroChangedCount))]
  49. public uint MaxZeroChangedCount => Model.MaxZeroChangedCount;
  50. [PropertyAssociation(nameof(ShakerConfigModel.MaxSignalCount))]
  51. public uint MaxSignalCount => Model.MaxSignalCount;
  52. [PropertyAssociation(nameof(ShakerConfigModel.MaxEmergencyStopCount))]
  53. public uint MaxEmergencyStopCount => Model.MaxEmergencyStopCount;
  54. [PropertyAssociation(nameof(ShakerConfigModel.MaxStopWindowCount))]
  55. public uint MaxStopWindowCount => MaxStopWindowCount;
  56. [PropertyAssociation(nameof(ShakerConfigModel.MaxAdjustCount))]
  57. public uint MaxAdjustCount => Model.MaxAdjustCount;
  58. [PropertyAssociation(nameof(ShakerConfigModel.MaxFallCount))]
  59. public uint MaxFallCount => Model.MaxFallCount;
  60. [PropertyAssociation(nameof(ShakerConfigModel.StartCount))]
  61. public uint StartCount => Model.StartCount;
  62. [PropertyAssociation(nameof(ShakerConfigModel.InitialLocation))]
  63. public double InitialLocation { get => Model.InitialLocation; set => SetProperty(ref Model.InitialLocation, value); }
  64. [PropertyAssociation(nameof(ShakerConfigModel.SampleRate))]
  65. public uint SampleRate => Model.SampleRate;
  66. [PropertyAssociation(nameof(ShakerConfigModel.FPGAClock))]
  67. public uint FPGAClock => Model.FPGAClock;
  68. [PropertyAssociation(nameof(ShakerConfigModel.AnalogSignalConfigs))]
  69. public int ChannelCount => Model.AnalogSignalConfigs.Count;
  70. [PropertyAssociation(nameof(ShakerConfigModel.MaxFrequency))]
  71. public double MaxFrequency { get => Model.MaxFrequency; set => SetProperty(ref Model.MaxFrequency, value); }
  72. [PropertyAssociation(nameof(ShakerConfigModel.MinFrequency))]
  73. public double MinFrequency { get => Model.MinFrequency; set => SetProperty(ref Model.MinFrequency, value); }
  74. [PropertyAssociation(nameof(ShakerConfigModel.MaxOutInput))]
  75. public double MaxOutInput { get => Model.MaxOutInput; set => SetProperty(ref Model.MaxOutInput, value); }
  76. [PropertyAssociation(nameof(ShakerConfigModel.MaxAcceleration))]
  77. public double MaxAcceleration { get => Model.MaxAcceleration; set => SetProperty(ref Model.MaxAcceleration, value); }
  78. [PropertyAssociation(nameof(ShakerConfigModel.MaxDisplacement))]
  79. public double MaxDisplacement { get => Model.MaxDisplacement; set => SetProperty(ref Model.MaxDisplacement, value); }
  80. [PropertyAssociation(nameof(ShakerConfigModel.MaxDriver))]
  81. public double MaxDriver { get => Model.MaxDriver; set => SetProperty(ref Model.MaxDriver, value); }
  82. [PropertyAssociation(nameof(ShakerConfigModel.MaxJitterAcceleration))]
  83. public double MaxJitterAcceleration { get => Model.MaxJitterAcceleration; set => SetProperty(ref Model.MaxJitterAcceleration, value); }
  84. [PropertyAssociation(nameof(ShakerConfigModel.MaxJitterDisplacement))]
  85. public double MaxJitterDisplacement { get => Model.MaxJitterDisplacement; set => SetProperty(ref Model.MaxJitterDisplacement, value); }
  86. [PropertyAssociation(nameof(ShakerConfigModel.MaxVelocity))]
  87. public double MaxVelocity { get => Model.MaxVelocity; set => SetProperty(ref Model.MaxVelocity, value); }
  88. [PropertyAssociation(nameof(ShakerConfigModel.AccelerationConfigs))]
  89. public int AccelerationSensorCount => Model.AccelerationConfigs.Count;
  90. public AvaloniaList<IndexValueItemViewModel<AnalogSignalConfigViewModel>> AnalogSignals { get; } = new AvaloniaList<IndexValueItemViewModel<AnalogSignalConfigViewModel>>();
  91. public AvaloniaList<IndexValueItemViewModel<AccelerationConfigViewModel>> Accelerations { get; } = new AvaloniaList<IndexValueItemViewModel<AccelerationConfigViewModel>>();
  92. [PropertyAssociation(nameof(ShakerConfigModel.DisplacementSensitivity))]
  93. public double DisplacementSensitivity { get => Model.DisplacementSensitivity; set => SetProperty(ref Model.DisplacementSensitivity, value); }
  94. public double AccelerationSensitivity => Accelerations.First().Value.Model.Sensitivity;
  95. private ShakerConfigViewModel()
  96. {
  97. Content = typeof(Views.ShakerConfigView);
  98. for(int i=0;i<Model.AccelerationConfigs.Count;i++)
  99. {
  100. Accelerations.Add(new IndexValueItemViewModel<AccelerationConfigViewModel>(i + 1,new AccelerationConfigViewModel(Model.AccelerationConfigs[i])));
  101. Accelerations[^1].Value.PropertyChanged += (sender, args) =>
  102. {
  103. SaveIsEnabled = true;
  104. switch(args.PropertyName)
  105. {
  106. case nameof(AccelerationConfigViewModel.Weight):
  107. if (AccelerationSensorCount <= 1) return;
  108. Accelerations[^1].Value.Weight = 1 - Accelerations.Take(AccelerationSensorCount - 1).Sum(x => x.Value.Weight);
  109. break;
  110. }
  111. };
  112. }
  113. for(int i=0;i<Model.AnalogSignalConfigs.Count;i++)
  114. {
  115. AnalogSignals.Add(new IndexValueItemViewModel<AnalogSignalConfigViewModel>(i + 1, new AnalogSignalConfigViewModel(Model.AnalogSignalConfigs[i])));
  116. }
  117. GetEvent<AllConfig>().Subscrip((sender, args) =>
  118. {
  119. UpDateModel(args.Data.ShakerConfig);
  120. OnPropertyChanged(nameof(AccelerationSensorCount));
  121. CommunicationViewModel.Instance.LocalCommunication?.GetEvent<Shaker.Models.ShakerConfigModel>()?.Subscrip((sender, args) =>
  122. {
  123. if (args.Data == null) return;
  124. UpDateModel(args.Data);
  125. OnPropertyChanged(nameof(AccelerationSensorCount));
  126. CommunicationViewModel.Instance.ServiceCommunication?.GetEvent<Shaker.Models.ShakerConfigModel>()?.Publish(this, args.Data);
  127. });
  128. GetEvent(Models.Topic.InitSeries).Publish(this, null);
  129. });
  130. }
  131. static ShakerConfigViewModel()
  132. {
  133. }
  134. public void SaveTdmsConfig(TDMS.ITDMSFile? config)
  135. {
  136. if (config == null) return;
  137. var group = config.Contains(nameof(ShakerConfigModel)) ? config[nameof(ShakerConfigModel)]:config.AddGroup(nameof(ShakerConfigModel));
  138. if (group == null) return;
  139. typeof(ShakerConfigModel).GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
  140. .Select(x =>(x, x.GetValue(Model)))
  141. .ToList()
  142. .ForEach(x =>
  143. {
  144. if(x.Item2 ==null)
  145. {
  146. return;
  147. }
  148. if(x.x.FieldType.IsAssignableTo(typeof(IList)) || x.x.FieldType.IsArray)
  149. {
  150. group.CreateOrUpdateProperty($"{x.x.Name}", string.Join("", Tools.Tools.Serialize(x.x.FieldType, x.Item2).Select(x => $"{x:X2}")));
  151. }
  152. else
  153. {
  154. group.CreateOrUpdateProperty($"{x.x.Name}", x.Item2?.ToString() ?? string.Empty);
  155. }
  156. });
  157. }
  158. public override void UpDateModel(Shaker.Models.ShakerConfigModel model)
  159. {
  160. base.UpDateModel(model);
  161. Accelerations.Clear();
  162. for(int i=0;i<model.AccelerationConfigs.Count;i++)
  163. {
  164. Accelerations.Add(new IndexValueItemViewModel<AccelerationConfigViewModel>(i + 1, new AccelerationConfigViewModel(model.AccelerationConfigs[i])));
  165. Accelerations[^1].Value.PropertyChanged += (sender, args) =>
  166. {
  167. SaveIsEnabled = true;
  168. };
  169. }
  170. AnalogSignals.Clear();
  171. for(int i=0;i<model.AnalogSignalConfigs.Count;i++)
  172. {
  173. AnalogSignals.Add(new IndexValueItemViewModel<AnalogSignalConfigViewModel>(i + 1, new AnalogSignalConfigViewModel(model.AnalogSignalConfigs[i])));
  174. }
  175. }
  176. public static ShakerConfigViewModel Instance { get; } = new ShakerConfigViewModel();
  177. protected override void Save()
  178. {
  179. if(SynthesisType == AccelerationSynthesisType.Synthesis && MathF.Abs(Accelerations.Sum(x=>x.Value.Weight) -1f)>0.0001f)
  180. {
  181. ShowError(string.Format(App.Current?.FindResource("AccelerationWeightSumError") + "", 1));
  182. return;
  183. }
  184. base.Save();
  185. CommunicationViewModel.Instance.LocalCommunication?.GetEvent<Shaker.Models.ShakerConfigModel>()?.Publish(this, Model);
  186. CommunicationViewModel.Instance.ServiceCommunication?.GetEvent<Shaker.Models.ShakerConfigModel>()?.Publish(this, Model);
  187. }
  188. public override void ReceiveServiceModel(ShakerConfigModel model)
  189. {
  190. base.ReceiveServiceModel(model);
  191. CommunicationViewModel.Instance.LocalCommunication?.GetEvent<Shaker.Models.ShakerConfigModel>()?.Publish(this, Model);
  192. }
  193. }
  194. }