ShakerStatusViewModel.cs 11 KB

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