Dispenser/DispenserUI/ViewModels/Common/SystemStatusVM.cs

191 lines
4.6 KiB
C#
Raw Normal View History

2024-08-16 07:20:09 +00:00
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;
/// <summary>
/// 系统变量
/// </summary>
[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<BizException>.AddEventHandler(EventType.Exception, ExceptionHandler);
}
/// <summary>
/// 处理接收到的异常信息
/// </summary>
/// <param name="eventType"></param>
/// <param name="exception"></param>
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<Unit, Unit> ToDisableAll { get; }
public ReactiveCommand<Unit, Unit> ToCameraViewer { get; }
public ReactiveCommand<Unit, Unit> 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;
}
/// <summary>
/// 更新状态UI
/// </summary>
private void UpdateStateView(bool normal)
{
if (View == null) return;
Dispatcher.UIThread.InvokeAsync(() =>
{
if (!normal) return;
CameraNormal();
});
}
/// <summary>
/// 待机状态
/// </summary>
private void NormalState()
{
LightState = MotionStatus[1];
LightColor = LightColors[1];
LightShadow = LightShadows[1];
}
/// <summary>
/// 作业中
/// </summary>
private void WorkingState()
{
LightState = MotionStatus[0];
LightColor = LightColors[0];
LightShadow = LightShadows[0];
}
/// <summary>
/// 故障中
/// </summary>
private void FaultState()
{
LightState = MotionStatus[2];
LightColor = LightColors[2];
LightShadow = LightShadows[2];
}
}