ShakerStatusViewModel.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using Avalonia.Collections;
  2. using Avalonia.Controls;
  3. using Avalonia.Controls.Documents;
  4. using CommunityToolkit.Mvvm.Input;
  5. using Newtonsoft.Json.Linq;
  6. using OxyPlot;
  7. using OxyPlot.Axes;
  8. using OxyPlot.Series;
  9. using Shaker.Models;
  10. using Shaker.Models.Tools;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using System.Windows.Input;
  17. namespace ShakerApp.ViewModels
  18. {
  19. public sealed class ShakerStatusViewModel:ViewModelBase<ShakerStatusModel>
  20. {
  21. private float givenDisplacement;
  22. private float measuredDisplacement;
  23. private float acceleration;
  24. private float valveDrive;
  25. private float outSignal;
  26. public float GivenDisplacement { get => givenDisplacement; set => SetProperty(ref givenDisplacement, value); }
  27. public float MeasuredDisplacement { get => measuredDisplacement; set => SetProperty(ref measuredDisplacement, value); }
  28. public float Acceleration { get => acceleration; set => SetProperty(ref acceleration, value); }
  29. public float ValveDrive { get => valveDrive; set => SetProperty(ref valveDrive, value); }
  30. public float OutSignal { get => outSignal; set => SetProperty(ref outSignal, value); }
  31. public ViewModels.AnalogSignalPreviewViewModel DisplacementSignal { get; } = new AnalogSignalPreviewViewModel(AnalogType.Displacement)
  32. {
  33. CanChangedAnalog = false,
  34. };
  35. public AnalogSignalPreviewViewModel ValveDriveSignal { get; } = new AnalogSignalPreviewViewModel(AnalogType.Driver)
  36. {
  37. CanChangedAnalog = false,
  38. };
  39. private float workPosition = 0;
  40. private bool valvePower = false;
  41. [PropertyAssociation(nameof(ShakerStatusModel.RTStatus))]
  42. public bool PowerIsEnabled => RTStatus == RTStatus.WaitValvePower;
  43. [PropertyAssociation(nameof(ShakerStatusModel.RTStatus))]
  44. public bool RiseTableEnabled => RTStatus == RTStatus.WaitRiseTable;
  45. [PropertyAssociation(nameof(ShakerStatusModel.RTStatus))]
  46. public bool DropTableEnabled => RTStatus == RTStatus.WaitSignalGen;
  47. [PropertyAssociation(nameof(ShakerStatusModel.RTStatus))]
  48. public bool SignalStartEnabled => RTStatus == RTStatus.WaitSignalGen;
  49. [PropertyAssociation(nameof(ShakerStatusModel.RTStatus))]
  50. public bool SignalStopEnabled => RTStatus == RTStatus.SignalGen;
  51. [PropertyAssociation(nameof(ShakerStatusModel.ErrorCode))]
  52. public bool RestEnabeld => ErrorCode!=0;
  53. [PropertyAssociation(nameof(ShakerStatusModel.RTStatus))]
  54. public RTStatus RTStatus { get => Model.RTStatus; set => SetProperty(ref Model.RTStatus, value); }
  55. [PropertyAssociation(nameof(ShakerStatusModel.Start))]
  56. public bool Start { get => Model.Start; set => SetProperty(ref Model.Start, value); }
  57. [PropertyAssociation(nameof(ShakerStatusModel.IsTableRised))]
  58. public bool IsTableRised { get => Model.IsTableRised; set => SetProperty(ref Model.IsTableRised, value); }
  59. [PropertyAssociation(nameof(ShakerStatusModel.IsTableDroped))]
  60. public bool IsTableDroped { get => Model.IsTableDroped; set => SetProperty(ref Model.IsTableDroped, value); }
  61. [PropertyAssociation(nameof(ShakerStatusModel.IsFaultRelease))]
  62. public bool IsFaultRelease { get => Model.IsFaultRelease; set => SetProperty(ref Model.IsFaultRelease, value); }
  63. [PropertyAssociation(nameof(ShakerStatusModel.RiseCount))]
  64. public uint RiseCount { get => Model.RiseCount; set => SetProperty(ref Model.RiseCount, value); }
  65. [PropertyAssociation(nameof(ShakerStatusModel.SignalGenStopCount))]
  66. public uint SignalGenStopCount { get => Model.SignalGenStopCount; set => SetProperty(ref Model.SignalGenStopCount, value); }
  67. [PropertyAssociation(nameof(ShakerStatusModel.EmergencyStopCount))]
  68. public uint EmergencyStopCount { get => Model.EmergencyStopCount; set => SetProperty(ref Model.EmergencyStopCount, value); }
  69. [PropertyAssociation(nameof(ShakerStatusModel.IsSignalGenStoped))]
  70. public bool IsSignalGenStoped { get => Model.IsSignalGenStoped; set => SetProperty(ref Model.IsSignalGenStoped, value); }
  71. [PropertyAssociation(nameof(ShakerStatusModel.ZeroChangedCount))]
  72. public uint ZeroChangedCount { get => Model.ZeroChangedCount; set => SetProperty(ref Model.ZeroChangedCount, value); }
  73. [PropertyAssociation(nameof(ShakerStatusModel.WarnCode))]
  74. public ushort WarnCode { get => Model.WarnCode; set => SetProperty(ref Model.WarnCode, value); }
  75. [PropertyAssociation(nameof(ShakerStatusModel.ClosedValve))]
  76. public bool ClosedValve { get => Model.ClosedValve; set => SetProperty(ref Model.ClosedValve, value); }
  77. [PropertyAssociation(nameof(ShakerStatusModel.DropCount))]
  78. public uint DropCount { get => Model.DropCount; set => SetProperty(ref Model.DropCount, value); }
  79. [PropertyAssociation(nameof(ShakerStatusModel.ErrorCode))]
  80. public ushort ErrorCode { get => Model.ErrorCode; set => SetProperty(ref Model.ErrorCode, value); }
  81. public float WorkPosition
  82. {
  83. get => workPosition;
  84. set
  85. {
  86. SetProperty(ref workPosition, value);
  87. CommunicationViewModel.Intance.LocalCommunication?.GetEvent(Topic.ZEROCHANGE).Publish(this, null, value);
  88. }
  89. }
  90. private ShakerStatusViewModel()
  91. {
  92. Content = typeof(Views.ShakerStatusControlView);
  93. ButtonVisibily = false;
  94. GetEvent<AllConfig>().Subscrip((sender, args) =>
  95. {
  96. valvePower = false;
  97. UpDateModel(args.Data.ShakerStatus);
  98. if(CommunicationViewModel.Intance.ServiceIsStart)
  99. {
  100. CommunicationViewModel.Intance.ServiceCommunication.GetEvent<ShakerStatusModel>().Publish(this, args.Data.ShakerStatus);
  101. }
  102. CommunicationViewModel.Intance.LocalCommunication?.GetEvent<ShakerStatusModel>()?.Subscrip((sender, args) =>
  103. {
  104. UpDateModel(args.Data);
  105. CommunicationViewModel.Intance.ServiceCommunication?.GetEvent<ShakerStatusModel>()?.Publish(this, args.Data);
  106. });
  107. });
  108. GetEvent(Topic.DATA).Subscrip((_, _) =>
  109. {
  110. var data = ShakerDataViewModel.Instance.GetAnalogData(AnalogType.Displacement);
  111. GivenDisplacement = data[0].Item2.RMS;
  112. MeasuredDisplacement = data[1].Item2.RMS;
  113. data = ShakerDataViewModel.Instance.GetAnalogData(AnalogType.Driver);
  114. ValveDrive = data[0].Item2.RMS;
  115. data = ShakerDataViewModel.Instance.GetAnalogData(AnalogType.OutSignal);
  116. OutSignal = data[0].Item2.RMS;
  117. data = ShakerDataViewModel.Instance.GetAnalogData(AnalogType.Acceleration);
  118. Acceleration = data[0].Item2.RMS;
  119. });
  120. }
  121. protected override void RefreshUI(List<string> changedNames)
  122. {
  123. base.RefreshUI(changedNames);
  124. if(changedNames.Contains(nameof(RTStatus)))
  125. {
  126. OnPropertyChanged(nameof(PowerIsEnabled));
  127. OnPropertyChanged(nameof(RiseTableEnabled));
  128. OnPropertyChanged(nameof(DropTableEnabled));
  129. OnPropertyChanged(nameof(SignalStartEnabled));
  130. OnPropertyChanged(nameof(SignalStopEnabled));
  131. OnPropertyChanged(nameof(RestEnabeld));
  132. }
  133. }
  134. public override bool CanCancel => false;
  135. static ShakerStatusViewModel()
  136. {
  137. }
  138. public ICommand RiseTableCommand => new RelayCommand(RiseTableCommandExecute);
  139. private void RiseTableCommandExecute()
  140. {
  141. CommunicationViewModel.Intance.LocalCommunication.GetEvent(Topic.RISETABLE).Publish(this, null);
  142. }
  143. public ICommand DropTableCommand => new RelayCommand(DropTableCommandExecute);
  144. private void DropTableCommandExecute()
  145. {
  146. CommunicationViewModel.Intance.LocalCommunication.GetEvent(Topic.DROPTABLE).Publish(this, null);
  147. }
  148. public ICommand ResetCommand => new RelayCommand(Reset);
  149. private void Reset()
  150. {
  151. CommunicationViewModel.Intance.LocalCommunication.GetEvent(Topic.RESETERROR).Publish(this, null);
  152. }
  153. public ICommand EmergencyStopCommand => new RelayCommand(EmergencyStopCommandExecute);
  154. private void EmergencyStopCommandExecute()
  155. {
  156. }
  157. public ICommand StartCommand=> new RelayCommand(StartCommandExecute);
  158. private void StartCommandExecute()
  159. {
  160. CommunicationViewModel.Intance.LocalCommunication.GetEvent(Topic.STARTSIGNALGEN).Publish(this, null);
  161. }
  162. public ICommand StopCommand => new RelayCommand(StopCommandExecute);
  163. private void StopCommandExecute()
  164. {
  165. CommunicationViewModel.Intance.LocalCommunication.GetEvent(Topic.STOPSIGNALGEN).Publish(this, null);
  166. }
  167. public static ShakerStatusViewModel Instance { get; } = new ShakerStatusViewModel();
  168. public bool ValvePower
  169. {
  170. get => valvePower;
  171. set
  172. {
  173. //if (valvePower == value) return;
  174. SetProperty(ref valvePower, value);
  175. CommunicationViewModel.Intance.LocalCommunication.GetEvent(Topic.VALVEPOWER).Publish(this, null, value);
  176. }
  177. }
  178. }
  179. }