ShakerStatusViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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.MemoryUsage))]
  43. public long MemoryUsage => Model.MemoryUsage;
  44. [PropertyAssociation(nameof(ShakerStatusModel.CPUUsage))]
  45. public double CPUUsage =>Model.CPUUsage;
  46. [PropertyAssociation(nameof(ShakerStatusModel.RTStatus))]
  47. public bool PowerIsEnabled => RTStatus == RTStatus.WaitValvePower || (RTStatus == RTStatus.WaitRiseTable && (OilSourceStatusViewModel.Instance.IsEnabled ? (OilSourceStatusViewModel.Instance.Circuit.Any(x=>!x.Value.IsStart && (x.Value.IsEnablePressure ? !x.Value.IsLoadPressure:true))):true));
  48. [PropertyAssociation(nameof(ShakerStatusModel.RTStatus))]
  49. public bool RiseTableEnabled => RTStatus == RTStatus.WaitRiseTable;
  50. [PropertyAssociation(nameof(ShakerStatusModel.RTStatus))]
  51. public bool DropTableEnabled => RTStatus == RTStatus.WaitSignalGen;
  52. [PropertyAssociation(nameof(ShakerStatusModel.RTStatus))]
  53. public bool SignalStartEnabled => RTStatus == RTStatus.WaitSignalGen;
  54. [PropertyAssociation(nameof(ShakerStatusModel.RTStatus))]
  55. public bool SignalStopEnabled => RTStatus == RTStatus.SignalGen;
  56. [PropertyAssociation(nameof(ShakerStatusModel.ErrorCode))]
  57. public bool RestEnabeld => ErrorCode!=0;
  58. [PropertyAssociation(nameof(ShakerStatusModel.RTStatus))]
  59. public bool IsSignalGenStart => RTStatus == RTStatus.SignalGen;
  60. [PropertyAssociation(nameof(ShakerStatusModel.RTStatus))]
  61. public RTStatus RTStatus { get => Model.RTStatus; set => SetProperty(ref Model.RTStatus, value); }
  62. [PropertyAssociation(nameof(ShakerStatusModel.Start))]
  63. public bool Start { get => Model.Start; set => SetProperty(ref Model.Start, value); }
  64. [PropertyAssociation(nameof(ShakerStatusModel.IsTableRised))]
  65. public bool IsTableRised { get => Model.IsTableRised; set => SetProperty(ref Model.IsTableRised, value); }
  66. [PropertyAssociation(nameof(ShakerStatusModel.IsTableDroped))]
  67. public bool IsTableDroped { get => Model.IsTableDroped; set => SetProperty(ref Model.IsTableDroped, value); }
  68. [PropertyAssociation(nameof(ShakerStatusModel.IsFaultRelease))]
  69. public bool IsFaultRelease { get => Model.IsFaultRelease; set => SetProperty(ref Model.IsFaultRelease, value); }
  70. [PropertyAssociation(nameof(ShakerStatusModel.RiseCount))]
  71. public uint RiseCount { get => Model.RiseCount; set => SetProperty(ref Model.RiseCount, value); }
  72. [PropertyAssociation(nameof(ShakerStatusModel.SignalGenStopCount))]
  73. public uint SignalGenStopCount { get => Model.SignalGenStopCount; set => SetProperty(ref Model.SignalGenStopCount, value); }
  74. [PropertyAssociation(nameof(ShakerStatusModel.EmergencyStopCount))]
  75. public uint EmergencyStopCount { get => Model.EmergencyStopCount; set => SetProperty(ref Model.EmergencyStopCount, value); }
  76. [PropertyAssociation(nameof(ShakerStatusModel.IsSignalGenStoped))]
  77. public bool IsSignalGenStoped { get => Model.IsSignalGenStoped; set => SetProperty(ref Model.IsSignalGenStoped, value); }
  78. [PropertyAssociation(nameof(ShakerStatusModel.ZeroChangedCount))]
  79. public uint ZeroChangedCount { get => Model.ZeroChangedCount; set => SetProperty(ref Model.ZeroChangedCount, value); }
  80. [PropertyAssociation(nameof(ShakerStatusModel.WarnCode))]
  81. public ushort WarnCode { get => Model.WarnCode; set => SetProperty(ref Model.WarnCode, value); }
  82. [PropertyAssociation(nameof(ShakerStatusModel.ClosedValve))]
  83. public bool ClosedValve { get => Model.ClosedValve; set => SetProperty(ref Model.ClosedValve, value); }
  84. [PropertyAssociation(nameof(ShakerStatusModel.DropCount))]
  85. public uint DropCount { get => Model.DropCount; set => SetProperty(ref Model.DropCount, value); }
  86. [PropertyAssociation(nameof(ShakerStatusModel.ErrorCode))]
  87. public ushort ErrorCode { get => Model.ErrorCode; set => SetProperty(ref Model.ErrorCode, value); }
  88. [PropertyAssociation(nameof(ShakerStatusModel.ErrorCode), nameof(ShakerStatusModel.WarnCode))]
  89. public IBrush StatusColor => Status switch
  90. {
  91. Models.ShakerStatus.Nomal => Brushes.Green,
  92. Models.ShakerStatus.Warn => Brushes.Yellow,
  93. Models.ShakerStatus.Error => Brushes.Red,
  94. _ => Brushes.Green,
  95. };
  96. [PropertyAssociation(nameof(ShakerStatusModel.ErrorCode), nameof(ShakerStatusModel.WarnCode))]
  97. public Models.ShakerStatus Status => ErrorCode != 0 ? Models.ShakerStatus.Error : (WarnCode != 0 ? Models.ShakerStatus.Warn : Models.ShakerStatus.Nomal);
  98. [PropertyAssociation(nameof(ShakerStatusModel.RTStatus))]
  99. public string Msg =>RTStatus.ToString();
  100. public float WorkPosition
  101. {
  102. get => workPosition;
  103. set
  104. {
  105. SetProperty(ref workPosition, value);
  106. CommunicationViewModel.Instance.LocalCommunication?.GetEvent(Topic.ZEROCHANGE)?.Publish(this, null, value);
  107. }
  108. }
  109. private ShakerStatusViewModel()
  110. {
  111. Content = typeof(Views.ShakerStatusControlView);
  112. ButtonVisibily = false;
  113. GetEvent<AllConfig>().Subscrip((sender, args) =>
  114. {
  115. valvePower = false;
  116. UpDateModel(args.Data.ShakerStatus);
  117. CommunicationViewModel.Instance.LocalCommunication?.GetEvent<ShakerStatusModel>()?.Subscrip((sender, args) =>
  118. {
  119. UpDateModel(args.Data);
  120. CommunicationViewModel.Instance.ServiceCommunication?.GetEvent<ShakerStatusModel>()?.Publish(this, args.Data);
  121. });
  122. });
  123. GetEvent(Topic.DATA).Subscrip((_, _) =>
  124. {
  125. var data = ShakerDataViewModel.Instance.GetAnalogData(AnalogType.Displacement);
  126. GivenDisplacement = data[0].Item2.RMS;
  127. MeasuredDisplacement = data[1].Item2.RMS;
  128. data = ShakerDataViewModel.Instance.GetAnalogData(AnalogType.Driver);
  129. ValveDrive = data[0].Item2.RMS;
  130. data = ShakerDataViewModel.Instance.GetAnalogData(AnalogType.OutSignal);
  131. OutSignal = data[0].Item2.RMS;
  132. data = ShakerDataViewModel.Instance.GetAnalogData(AnalogType.Acceleration);
  133. Acceleration = data[0].Item2.RMS;
  134. });
  135. }
  136. protected override void RefreshUI(List<string> changedNames)
  137. {
  138. base.RefreshUI(changedNames);
  139. if(changedNames.Contains(nameof(RTStatus)))
  140. {
  141. OnPropertyChanged(nameof(PowerIsEnabled));
  142. OnPropertyChanged(nameof(RiseTableEnabled));
  143. OnPropertyChanged(nameof(DropTableEnabled));
  144. OnPropertyChanged(nameof(SignalStartEnabled));
  145. OnPropertyChanged(nameof(SignalStopEnabled));
  146. OnPropertyChanged(nameof(RestEnabeld));
  147. }
  148. }
  149. public override bool CanCancel => false;
  150. static ShakerStatusViewModel()
  151. {
  152. }
  153. public ICommand RiseTableCommand => new RelayCommand(RiseTableCommandExecute);
  154. private void RiseTableCommandExecute()
  155. {
  156. CommunicationViewModel.Instance.LocalCommunication?.GetEvent(Topic.RISETABLE)?.Publish(this, null);
  157. }
  158. public ICommand DropTableCommand => new RelayCommand(DropTableCommandExecute);
  159. private void DropTableCommandExecute()
  160. {
  161. CommunicationViewModel.Instance.LocalCommunication?.GetEvent(Topic.DROPTABLE)?.Publish(this, null);
  162. }
  163. public ICommand ResetCommand => new RelayCommand(Reset);
  164. private void Reset()
  165. {
  166. CommunicationViewModel.Instance.LocalCommunication?.GetEvent(Topic.RESETERROR)?.Publish(this, null);
  167. }
  168. public ICommand EmergencyStopCommand => new RelayCommand(EmergencyStopCommandExecute);
  169. private void EmergencyStopCommandExecute()
  170. {
  171. }
  172. public ICommand StartCommand=> new RelayCommand(StartCommandExecute);
  173. private void StartCommandExecute()
  174. {
  175. switch(ShakerControlViewModel.Instance.PageType)
  176. {
  177. case MainPageType.SinePage:
  178. if (!SineMainPageViewModel.Instance.Start()) return;
  179. CommunicationViewModel.Instance.LocalCommunication?.GetEvent<MainPageType>()?.Publish(this, MainPageType.SinePage);
  180. break;
  181. case MainPageType.RandomPage:
  182. CommunicationViewModel.Instance.LocalCommunication?.GetEvent<RandomTestStep>()?.Publish(this, RandomTestStep.Start);
  183. CommunicationViewModel.Instance.LocalCommunication?.GetEvent<MainPageType>()?.Publish(this, MainPageType.RandomPage);
  184. RandomMainPageViewModel.Instance.Start();
  185. break;
  186. }
  187. CommunicationViewModel.Instance.LocalCommunication?.GetEvent(Topic.STARTSIGNALGEN)?.Publish(this, null);
  188. }
  189. public ICommand StopCommand => new RelayCommand(StopCommandExecute);
  190. private void StopCommandExecute()
  191. {
  192. CommunicationViewModel.Instance.LocalCommunication?.GetEvent(Topic.STOPSIGNALGEN)?.Publish(this, null);
  193. }
  194. public static ShakerStatusViewModel Instance { get; } = new ShakerStatusViewModel();
  195. public bool ValvePower
  196. {
  197. get => valvePower;
  198. set
  199. {
  200. //if (valvePower == value) return;
  201. SetProperty(ref valvePower, value);
  202. CommunicationViewModel.Instance.LocalCommunication?.GetEvent(Topic.VALVEPOWER)?.Publish(this, null, value);
  203. }
  204. }
  205. }
  206. }