using System.Reactive; using Avalonia.Media; using Avalonia.Threading; using DispenserCommon.Aop; using DispenserCommon.Enums; using DispenserCommon.Events; using DispenserCommon.Exceptions; using DispenserCommon.Ioc; using DispenserHal.Camera.Factory; using DispenserUI.Exceptions; using DispenserUI.ViewModels.DTO; using DispenserUI.Views.Common; using DispenserUI.Views.Windows; using ReactiveUI; namespace DispenserUI.ViewModels.Common; /// /// 系统变量 /// [Component, GlobalTry] public class SystemStatusVM : ViewModelBase { private static readonly string[] MotionStatus = ["作业中", "待机中", "故障"]; private static readonly IBrush[] LightColors = [ new SolidColorBrush(Color.Parse("#31D05E")), new SolidColorBrush(Colors.DodgerBlue), new SolidColorBrush(Colors.Red) ]; private static readonly BoxShadows[] LightShadows = [ BoxShadows.Parse("0 0 20 5 LightGreen"), BoxShadows.Parse("0 0 20 5 DodgerBlue"), BoxShadows.Parse("0 0 20 5 IndianRed") ]; private SystemStatusView View { get; set; } private SystemStatus _camera = new("视觉相机"); private string _lightState = MotionStatus[1]; private IBrush _lightColor = LightColors[1]; private BoxShadows _lightShadow = LightShadows[1]; private EmergencyWindow? _emergencyWindow; private CameraViewerWindow? _cameraViewerWindow; private int _lastErrorId; public void BindView(SystemStatusView view) { View = view; } public SystemStatus Camera { get => _camera; set => this.RaiseAndSetIfChanged(ref _camera, value); } public string LightState { get => _lightState; set => this.RaiseAndSetIfChanged(ref _lightState, value); } public IBrush LightColor { get => _lightColor; set => this.RaiseAndSetIfChanged(ref _lightColor, value); } public BoxShadows LightShadow { get => _lightShadow; set => this.RaiseAndSetIfChanged(ref _lightShadow, value); } public SystemStatusVM() { ToCameraViewer = ReactiveCommand.Create(OpenCameraViewer); EventBus.AddEventHandler(EventType.Exception, ExceptionHandler); } /// /// 处理接收到的异常信息 /// /// /// private void ExceptionHandler(EventType eventType, BizException exception) { switch (exception) { case CameraException when exception.Level == ExceptionLevel.ERROR: CameraFault(); break; } // 只记录告警以上级别的异常 if (exception.Level <= ExceptionLevel.NORMAL) return; } public ReactiveCommand ToDisableAll { get; } public ReactiveCommand ToCameraViewer { get; } public ReactiveCommand ToTest { get; } [Operation("开启相机预览")] private void OpenCameraViewer() { if (CameraManager.GetCamera() == null) return; if (_cameraViewerWindow == null) { _cameraViewerWindow = new CameraViewerWindow(); _cameraViewerWindow.Show(); _cameraViewerWindow.Closed += (_, _) => { _cameraViewerWindow = null; }; } else { _cameraViewerWindow.Activate(); } } private void CameraFault() { Camera.Background = "#EAD2D7"; Camera.IsNormal = false; Camera.Tip = "连接异常"; } private void CameraNormal() { Camera.Background = "Transparent"; Camera.IsNormal = true; } /// /// 更新状态UI /// private void UpdateStateView(bool normal) { if (View == null) return; Dispatcher.UIThread.InvokeAsync(() => { if (!normal) return; CameraNormal(); }); } /// /// 待机状态 /// private void NormalState() { LightState = MotionStatus[1]; LightColor = LightColors[1]; LightShadow = LightShadows[1]; } /// /// 作业中 /// private void WorkingState() { LightState = MotionStatus[0]; LightColor = LightColors[0]; LightShadow = LightShadows[0]; } /// /// 故障中 /// private void FaultState() { LightState = MotionStatus[2]; LightColor = LightColors[2]; LightShadow = LightShadows[2]; } }