using Avalonia.Collections; using Avalonia.Controls; using Avalonia.Controls.Documents; using Avalonia.Media; using CommunityToolkit.Mvvm.Input; using Newtonsoft.Json.Linq; using OxyPlot; using OxyPlot.Axes; using OxyPlot.Series; using Shaker.Models; using Shaker.Models.Tools; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; namespace ShakerApp.ViewModels { public sealed class ShakerStatusViewModel:ViewModelBase { private float givenDisplacement; private float measuredDisplacement; private float acceleration; private float valveDrive; private float outSignal; public float GivenDisplacement { get => givenDisplacement; set => SetProperty(ref givenDisplacement, value); } public float MeasuredDisplacement { get => measuredDisplacement; set => SetProperty(ref measuredDisplacement, value); } public float Acceleration { get => acceleration; set => SetProperty(ref acceleration, value); } public float ValveDrive { get => valveDrive; set => SetProperty(ref valveDrive, value); } public float OutSignal { get => outSignal; set => SetProperty(ref outSignal, value); } public ViewModels.AnalogSignalPreviewViewModel DisplacementSignal { get; } = new AnalogSignalPreviewViewModel(AnalogType.Displacement) { CanChangedAnalog = false, }; public AnalogSignalPreviewViewModel ValveDriveSignal { get; } = new AnalogSignalPreviewViewModel(AnalogType.Driver) { CanChangedAnalog = false, }; private float workPosition = 0; private bool valvePower = false; [PropertyAssociation(nameof(ShakerStatusModel.RTStatus))] public bool PowerIsEnabled => RTStatus == RTStatus.WaitValvePower; [PropertyAssociation(nameof(ShakerStatusModel.RTStatus))] public bool RiseTableEnabled => RTStatus == RTStatus.WaitRiseTable; [PropertyAssociation(nameof(ShakerStatusModel.RTStatus))] public bool DropTableEnabled => RTStatus == RTStatus.WaitSignalGen; [PropertyAssociation(nameof(ShakerStatusModel.RTStatus))] public bool SignalStartEnabled => RTStatus == RTStatus.WaitSignalGen; [PropertyAssociation(nameof(ShakerStatusModel.RTStatus))] public bool SignalStopEnabled => RTStatus == RTStatus.SignalGen; [PropertyAssociation(nameof(ShakerStatusModel.ErrorCode))] public bool RestEnabeld => ErrorCode!=0; [PropertyAssociation(nameof(ShakerStatusModel.RTStatus))] public RTStatus RTStatus { get => Model.RTStatus; set => SetProperty(ref Model.RTStatus, value); } [PropertyAssociation(nameof(ShakerStatusModel.Start))] public bool Start { get => Model.Start; set => SetProperty(ref Model.Start, value); } [PropertyAssociation(nameof(ShakerStatusModel.IsTableRised))] public bool IsTableRised { get => Model.IsTableRised; set => SetProperty(ref Model.IsTableRised, value); } [PropertyAssociation(nameof(ShakerStatusModel.IsTableDroped))] public bool IsTableDroped { get => Model.IsTableDroped; set => SetProperty(ref Model.IsTableDroped, value); } [PropertyAssociation(nameof(ShakerStatusModel.IsFaultRelease))] public bool IsFaultRelease { get => Model.IsFaultRelease; set => SetProperty(ref Model.IsFaultRelease, value); } [PropertyAssociation(nameof(ShakerStatusModel.RiseCount))] public uint RiseCount { get => Model.RiseCount; set => SetProperty(ref Model.RiseCount, value); } [PropertyAssociation(nameof(ShakerStatusModel.SignalGenStopCount))] public uint SignalGenStopCount { get => Model.SignalGenStopCount; set => SetProperty(ref Model.SignalGenStopCount, value); } [PropertyAssociation(nameof(ShakerStatusModel.EmergencyStopCount))] public uint EmergencyStopCount { get => Model.EmergencyStopCount; set => SetProperty(ref Model.EmergencyStopCount, value); } [PropertyAssociation(nameof(ShakerStatusModel.IsSignalGenStoped))] public bool IsSignalGenStoped { get => Model.IsSignalGenStoped; set => SetProperty(ref Model.IsSignalGenStoped, value); } [PropertyAssociation(nameof(ShakerStatusModel.ZeroChangedCount))] public uint ZeroChangedCount { get => Model.ZeroChangedCount; set => SetProperty(ref Model.ZeroChangedCount, value); } [PropertyAssociation(nameof(ShakerStatusModel.WarnCode))] public ushort WarnCode { get => Model.WarnCode; set => SetProperty(ref Model.WarnCode, value); } [PropertyAssociation(nameof(ShakerStatusModel.ClosedValve))] public bool ClosedValve { get => Model.ClosedValve; set => SetProperty(ref Model.ClosedValve, value); } [PropertyAssociation(nameof(ShakerStatusModel.DropCount))] public uint DropCount { get => Model.DropCount; set => SetProperty(ref Model.DropCount, value); } [PropertyAssociation(nameof(ShakerStatusModel.ErrorCode))] public ushort ErrorCode { get => Model.ErrorCode; set => SetProperty(ref Model.ErrorCode, value); } [PropertyAssociation(nameof(ShakerStatusModel.ErrorCode), nameof(ShakerStatusModel.WarnCode))] public IBrush StatusColor => Status switch { Models.ShakerStatus.Nomal => Brushes.Green, Models.ShakerStatus.Warn => Brushes.Yellow, Models.ShakerStatus.Error => Brushes.Red, _ => Brushes.Green, }; [PropertyAssociation(nameof(ShakerStatusModel.ErrorCode), nameof(ShakerStatusModel.WarnCode))] public Models.ShakerStatus Status => ErrorCode != 0 ? Models.ShakerStatus.Error : (WarnCode != 0 ? Models.ShakerStatus.Warn : Models.ShakerStatus.Nomal); [PropertyAssociation(nameof(ShakerStatusModel.RTStatus))] public string Msg =>RTStatus.ToString(); public float WorkPosition { get => workPosition; set { SetProperty(ref workPosition, value); CommunicationViewModel.Instance.LocalCommunication?.GetEvent(Topic.ZEROCHANGE)?.Publish(this, null, value); } } private ShakerStatusViewModel() { Content = typeof(Views.ShakerStatusControlView); ButtonVisibily = false; GetEvent().Subscrip((sender, args) => { valvePower = false; UpDateModel(args.Data.ShakerStatus); CommunicationViewModel.Instance.LocalCommunication?.GetEvent()?.Subscrip((sender, args) => { UpDateModel(args.Data); CommunicationViewModel.Instance.ServiceCommunication?.GetEvent()?.Publish(this, args.Data); }); }); GetEvent(Topic.DATA).Subscrip((_, _) => { var data = ShakerDataViewModel.Instance.GetAnalogData(AnalogType.Displacement); GivenDisplacement = data[0].Item2.RMS; MeasuredDisplacement = data[1].Item2.RMS; data = ShakerDataViewModel.Instance.GetAnalogData(AnalogType.Driver); ValveDrive = data[0].Item2.RMS; data = ShakerDataViewModel.Instance.GetAnalogData(AnalogType.OutSignal); OutSignal = data[0].Item2.RMS; data = ShakerDataViewModel.Instance.GetAnalogData(AnalogType.Acceleration); Acceleration = data[0].Item2.RMS; }); } protected override void RefreshUI(List changedNames) { base.RefreshUI(changedNames); if(changedNames.Contains(nameof(RTStatus))) { OnPropertyChanged(nameof(PowerIsEnabled)); OnPropertyChanged(nameof(RiseTableEnabled)); OnPropertyChanged(nameof(DropTableEnabled)); OnPropertyChanged(nameof(SignalStartEnabled)); OnPropertyChanged(nameof(SignalStopEnabled)); OnPropertyChanged(nameof(RestEnabeld)); } } public override bool CanCancel => false; static ShakerStatusViewModel() { } public ICommand RiseTableCommand => new RelayCommand(RiseTableCommandExecute); private void RiseTableCommandExecute() { CommunicationViewModel.Instance.LocalCommunication?.GetEvent(Topic.RISETABLE)?.Publish(this, null); } public ICommand DropTableCommand => new RelayCommand(DropTableCommandExecute); private void DropTableCommandExecute() { CommunicationViewModel.Instance.LocalCommunication?.GetEvent(Topic.DROPTABLE)?.Publish(this, null); } public ICommand ResetCommand => new RelayCommand(Reset); private void Reset() { CommunicationViewModel.Instance.LocalCommunication?.GetEvent(Topic.RESETERROR)?.Publish(this, null); } public ICommand EmergencyStopCommand => new RelayCommand(EmergencyStopCommandExecute); private void EmergencyStopCommandExecute() { } public ICommand StartCommand=> new RelayCommand(StartCommandExecute); private void StartCommandExecute() { switch(ShakerControlViewModel.Instance.PageType) { case MainPageType.SinePage: SineMainPageViewModel.Instance.Start(); CommunicationViewModel.Instance.LocalCommunication?.GetEvent()?.Publish(this, MainPageType.SinePage); break; case MainPageType.RandomPage: CommunicationViewModel.Instance.LocalCommunication?.GetEvent()?.Publish(this, RandomTestStep.Start); CommunicationViewModel.Instance.LocalCommunication?.GetEvent()?.Publish(this, MainPageType.RandomPage); RandomMainPageViewModel.Instance.Start(); break; } CommunicationViewModel.Instance.LocalCommunication?.GetEvent(Topic.STARTSIGNALGEN)?.Publish(this, null); } public ICommand StopCommand => new RelayCommand(StopCommandExecute); private void StopCommandExecute() { CommunicationViewModel.Instance.LocalCommunication?.GetEvent(Topic.STOPSIGNALGEN)?.Publish(this, null); } public static ShakerStatusViewModel Instance { get; } = new ShakerStatusViewModel(); public bool ValvePower { get => valvePower; set { //if (valvePower == value) return; SetProperty(ref valvePower, value); CommunicationViewModel.Instance.LocalCommunication?.GetEvent(Topic.VALVEPOWER)?.Publish(this, null, value); } } } }