ShakerStatusViewModel.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using Avalonia.Controls;
  2. using CommunityToolkit.Mvvm.Input;
  3. using Shaker.Model;
  4. using Shaker.Model.Tools;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Input;
  11. namespace ShakerApp.ViewModels
  12. {
  13. public sealed class ShakerStatusViewModel:ViewModelBase<Shaker.Model.ShakerStatusModel>
  14. {
  15. private float givenDisplacement;
  16. private float measuredDisplacement;
  17. private float acceleration;
  18. private float valveDrive;
  19. private float outSignal;
  20. public float GivenDisplacement { get => givenDisplacement; set => SetProperty(ref givenDisplacement, value); }
  21. public float MeasuredDisplacement { get => measuredDisplacement; set => SetProperty(ref measuredDisplacement, value); }
  22. public float Acceleration { get => acceleration; set => SetProperty(ref acceleration, value); }
  23. public float ValveDrive { get => valveDrive; set => SetProperty(ref valveDrive, value); }
  24. public float OutSignal { get => outSignal; set => SetProperty(ref outSignal, value); }
  25. private float workPosition = 0;
  26. public RTStatus RTStatus { get => Model.RTStatus; set => SetProperty(ref Model.RTStatus, value); }
  27. public bool Start { get => Model.Start; set => SetProperty(ref Model.Start, value); }
  28. public bool IsTableRised { get => Model.IsTableRised; set => SetProperty(ref Model.IsTableRised, value); }
  29. public bool IsTableDroped { get => Model.IsTableDroped; set => SetProperty(ref Model.IsTableDroped, value); }
  30. public bool IsFaultRelease { get => Model.IsFaultRelease; set => SetProperty(ref Model.IsFaultRelease, value); }
  31. public uint RiseCount { get => Model.RiseCount; set => SetProperty(ref Model.RiseCount, value); }
  32. public uint SignalGenStopCount { get => Model.SignalGenStopCount; set => SetProperty(ref Model.SignalGenStopCount, value); }
  33. public uint EmergencyStopCount { get => Model.EmergencyStopCount; set => SetProperty(ref Model.EmergencyStopCount, value); }
  34. public bool IsSignalGenStoped { get => Model.IsSignalGenStoped; set => SetProperty(ref Model.IsSignalGenStoped, value); }
  35. public uint ZeroChangedCount { get => Model.ZeroChangedCount; set => SetProperty(ref Model.ZeroChangedCount, value); }
  36. public ushort WarnCode { get => Model.WarnCode; set => SetProperty(ref Model.WarnCode, value); }
  37. public float CurrentFrequency { get => Model.CurrentFrequency; set => SetProperty(ref Model.CurrentFrequency, value); }
  38. public bool ClosedValve { get => Model.ClosedValve; set => SetProperty(ref Model.ClosedValve, value); }
  39. public uint DropCount { get => Model.DropCount; set => SetProperty(ref Model.DropCount, value); }
  40. public ushort ErrorCode { get => Model.ErrorCode; set => SetProperty(ref Model.ErrorCode, value); }
  41. public float WorkPosition { get => workPosition; set =>SetProperty(ref workPosition , value); }
  42. private ShakerStatusViewModel()
  43. {
  44. Content = typeof(Views.ShakerStatusControlView);
  45. ButtonVisibily = false;
  46. InitDisplacementModel();
  47. InitServoValveModel();
  48. GetEvent(ShakerSettingViewModel.LANGUAGECHANGEDEVENT).Subscrip((_, _) =>
  49. {
  50. DisplacementModel.Axes[0].Title = App.Current?.FindResource("Time") + "";
  51. DisplacementModel.Axes[1].Title = App.Current?.FindResource("Ampt") + "";
  52. DisplacementModel.Title = App.Current?.FindResource("Displacement") + "";
  53. ServoValveModel.Axes[0].Title = App.Current?.FindResource("Time") + "";
  54. ServoValveModel.Axes[1].Title = App.Current?.FindResource("Ampt") + "";
  55. ServoValveModel.Title = App.Current?.FindResource("ValveDriveSignal") + "";
  56. });
  57. GetEvent<AllConfig>().Subscrip((sender, args) =>
  58. {
  59. UpDateModel(args.Data.ShakerStatus);
  60. if(CommunicationViewModel.Intance.ServiceIsStart)
  61. {
  62. CommunicationViewModel.Intance.ServiceCommunication.GetEvent<ShakerStatusModel>().Publish(this, args.Data.ShakerStatus);
  63. }
  64. CommunicationViewModel.Intance.LocalCommunication?.GetEvent<Shaker.Model.ShakerStatusModel>()?.Subscrip((sender, args) =>
  65. {
  66. UpDateModel(args.Data);
  67. CommunicationViewModel.Intance.ServiceCommunication?.GetEvent<Shaker.Model.ShakerStatusModel>()?.Publish(this, args.Data);
  68. });
  69. });
  70. }
  71. private void InitDisplacementModel()
  72. {
  73. DisplacementModel.Axes.Add(new OxyPlot.Axes.TimeSpanAxis()
  74. {
  75. MaximumPadding = 0,
  76. MinimumPadding = 0,
  77. Title = App.Current?.FindResource("Time") + "",
  78. Unit="s",
  79. MajorGridlineStyle = OxyPlot.LineStyle.Solid,
  80. Position = OxyPlot.Axes.AxisPosition.Bottom
  81. });
  82. DisplacementModel.Axes.Add(new OxyPlot.Axes.LinearAxis()
  83. {
  84. Title = App.Current?.FindResource("Ampt") + "",
  85. Unit = "mm",
  86. Position = OxyPlot.Axes.AxisPosition.Left,
  87. MajorGridlineStyle = OxyPlot.LineStyle.Solid,
  88. });
  89. DisplacementModel.Title = App.Current?.FindResource("Displacement") + "";
  90. }
  91. private void InitServoValveModel()
  92. {
  93. ServoValveModel.Axes.Add(new OxyPlot.Axes.TimeSpanAxis()
  94. {
  95. MaximumPadding = 0,
  96. MinimumPadding = 0,
  97. Title = App.Current?.FindResource("Time") + "",
  98. Unit = "s",
  99. MajorGridlineStyle = OxyPlot.LineStyle.Solid,
  100. Position= OxyPlot.Axes.AxisPosition.Bottom
  101. });
  102. ServoValveModel.Axes.Add(new OxyPlot.Axes.LinearAxis()
  103. {
  104. Title = App.Current?.FindResource("Ampt") + "",
  105. Unit = "V",
  106. MajorGridlineStyle = OxyPlot.LineStyle.Solid,
  107. Position = OxyPlot.Axes.AxisPosition.Left,
  108. });
  109. ServoValveModel.Title = App.Current?.FindResource("ValveDriveSignal") + "";
  110. }
  111. public override bool CanCancel => false;
  112. static ShakerStatusViewModel()
  113. {
  114. }
  115. public ICommand RiseTableCommand => new RelayCommand(RiseTableCommandExecute);
  116. private void RiseTableCommandExecute()
  117. {
  118. }
  119. public ICommand DropTableCommand => new RelayCommand(DropTableCommandExecute);
  120. private void DropTableCommandExecute()
  121. {
  122. }
  123. public ICommand EmergencyStopCommand => new RelayCommand(EmergencyStopCommandExecute);
  124. private void EmergencyStopCommandExecute()
  125. {
  126. }
  127. public ICommand StartCommand=> new RelayCommand(StartCommandExecute);
  128. private void StartCommandExecute()
  129. {
  130. if (MainPageViewModel.Instance.MainPage is SineMainPageViewModel sine)
  131. {
  132. SinePlotTest();
  133. }
  134. }
  135. private async void SinePlotTest()
  136. {
  137. float invtert=0.01f;
  138. float start = SweepConfigViewModel.Instance.SweepDirection == SweepDirection.Up ? SweepConfigViewModel.Instance.StartFrequency : SweepConfigViewModel.Instance.EndFrequency;
  139. float end = SweepConfigViewModel.Instance.SweepDirection != SweepDirection.Up ? SweepConfigViewModel.Instance.StartFrequency : SweepConfigViewModel.Instance.EndFrequency;
  140. while (true)
  141. {
  142. var val = SweepConfigViewModel.Instance.Model.CalcAmpt(start);
  143. SineDataModel data = new SineDataModel()
  144. {
  145. SweepDirection = SweepConfigViewModel.Instance.SweepDirection,
  146. CurrentAcceleration =val,
  147. CurrentFrequency = start,
  148. TotalTime = SweepConfigViewModel.Instance.TotalSweepTime,
  149. RunTime =0f,
  150. };
  151. SineMainPageViewModel.Instance.UpdateData(data);
  152. start += invtert;
  153. await Task.Delay(100);
  154. if (start > end) break;
  155. }
  156. }
  157. public ICommand StopCommand => new RelayCommand(StopCommandExecute);
  158. private void StopCommandExecute()
  159. {
  160. }
  161. public ICommand ResetCommand => new RelayCommand(ResetCommandExecute);
  162. private void ResetCommandExecute()
  163. {
  164. }
  165. public static ShakerStatusViewModel Instance { get; } = new ShakerStatusViewModel();
  166. public OxyPlot.PlotModel DisplacementModel { get; } = new OxyPlot.PlotModel();
  167. public OxyPlot.PlotModel ServoValveModel { get; } = new OxyPlot.PlotModel();
  168. }
  169. }