284 lines
6.1 KiB
C#
284 lines
6.1 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
public class TopBarComponent : MonoBehaviour
|
||
{
|
||
[Header("信息显示")]
|
||
public TextMeshProUGUI PatientInfoText;
|
||
public TextMeshProUGUI AlarmInfoText;
|
||
public TextMeshProUGUI BatteryText;
|
||
// public TextMeshProUGUI ACText;
|
||
|
||
[Header("报警灯")]
|
||
public AlarmLightComponent AlarmLight;
|
||
|
||
[Header("电量显示")]
|
||
public Slider BatterySlider;
|
||
public Image BatteryFillImage;
|
||
public Sprite[] BatteryFillSprites; // 不同电量段的图片
|
||
|
||
|
||
private IPatientInfoService _patientInfoService;
|
||
private ISerialCommunicationService _serialService;
|
||
private IAuthenticationService _authService;
|
||
private bool _lastLoginState;
|
||
private float _lastLoginStateCheckTime;
|
||
private const float LOGIN_STATE_CHECK_INTERVAL = 0.5f;
|
||
|
||
void Start()
|
||
{
|
||
_patientInfoService = ServiceLocator.Get<IPatientInfoService>();
|
||
_serialService = ServiceLocator.Get<ISerialCommunicationService>();
|
||
_authService = ServiceLocator.Get<IAuthenticationService>();
|
||
|
||
// 订阅DCSAlarmManager的报警事件
|
||
DCSAlarmManager.OnAlarmRaised += OnDCSAlarmRaised;
|
||
DCSAlarmManager.OnAlarmCleared += OnAllAlarmsCleared;
|
||
// DCSAlarmManager.OnMuteStateChanged += UpdateMuteButtonText;
|
||
|
||
if (_patientInfoService != null)
|
||
{
|
||
_patientInfoService.OnPatientInfoChanged += LoadPatientInfo;
|
||
}
|
||
|
||
if (_serialService != null)
|
||
{
|
||
_serialService.OnDeviceStatusReceived += OnSerialDeviceStatusReceived;
|
||
}
|
||
|
||
if (BatteryText != null && string.IsNullOrEmpty(BatteryText.text))
|
||
{
|
||
BatteryText.text = "电量: --";
|
||
}
|
||
|
||
_lastLoginState = IsUserLoggedIn();
|
||
if (!_lastLoginState)
|
||
{
|
||
ClearAlarmDisplay();
|
||
}
|
||
}
|
||
|
||
|
||
void Update()
|
||
{
|
||
if (Time.unscaledTime - _lastLoginStateCheckTime < LOGIN_STATE_CHECK_INTERVAL)
|
||
{
|
||
return;
|
||
}
|
||
|
||
_lastLoginStateCheckTime = Time.unscaledTime;
|
||
bool isLoggedIn = IsUserLoggedIn();
|
||
if (isLoggedIn == _lastLoginState)
|
||
{
|
||
return;
|
||
}
|
||
|
||
_lastLoginState = isLoggedIn;
|
||
if (isLoggedIn)
|
||
{
|
||
RestoreActiveAlarmAfterLogin();
|
||
}
|
||
else
|
||
{
|
||
ClearAlarmDisplay();
|
||
}
|
||
}
|
||
|
||
|
||
private bool IsUserLoggedIn()
|
||
{
|
||
return _authService != null && _authService.IsLoggedIn;
|
||
}
|
||
|
||
|
||
private void RestoreActiveAlarmAfterLogin()
|
||
{
|
||
if (DCSAlarmManager.Instance == null)
|
||
{
|
||
OnAllAlarmsCleared();
|
||
return;
|
||
}
|
||
|
||
var activeAlarms = DCSAlarmManager.Instance.GetActiveAlarms();
|
||
if (activeAlarms == null || activeAlarms.Count == 0)
|
||
{
|
||
OnAllAlarmsCleared();
|
||
return;
|
||
}
|
||
|
||
AlarmPriority highestPriority = AlarmPriority.None;
|
||
string message = string.Empty;
|
||
for (int i = 0; i < activeAlarms.Count; i++)
|
||
{
|
||
var current = activeAlarms[i];
|
||
if (current.priority >= highestPriority)
|
||
{
|
||
highestPriority = current.priority;
|
||
message = current.message;
|
||
}
|
||
}
|
||
|
||
if (highestPriority != AlarmPriority.None)
|
||
{
|
||
OnDCSAlarmRaised(highestPriority, message);
|
||
}
|
||
}
|
||
|
||
|
||
private void ClearAlarmDisplay()
|
||
{
|
||
if (AlarmInfoText != null)
|
||
{
|
||
AlarmInfoText.text = string.Empty;
|
||
}
|
||
|
||
if (AlarmLight != null)
|
||
{
|
||
AlarmLight.SetAlarmState(AlarmPriority.None, false);
|
||
}
|
||
}
|
||
|
||
|
||
private void LoadPatientInfo(PatientInfo patient)
|
||
{
|
||
if (PatientInfoText == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
if (patient != null)
|
||
{
|
||
string ageText = patient.Age > 0 ? $"{patient.Age}岁" : "-";
|
||
string heightText = patient.Height > 0f ? $"{patient.Height:F1}cm" : "-";
|
||
string weightText = patient.Weight > 0f ? $"{patient.Weight:F1}kg" : "-";
|
||
string genderText = string.IsNullOrEmpty(patient.Gender) ? "-" : patient.Gender;
|
||
|
||
PatientInfoText.text =
|
||
$"姓名: {patient.Name} 住院号: {patient.HospitalId}\n";
|
||
}
|
||
else
|
||
{
|
||
PatientInfoText.text = "未设置病人信息";
|
||
}
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 所有报警清除后显示健康状态
|
||
/// </summary>
|
||
private void OnAllAlarmsCleared()
|
||
{
|
||
ClearAlarmDisplay();
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 处理DCSAlarmManager的报警事件
|
||
/// </summary>
|
||
private void OnDCSAlarmRaised(AlarmPriority priority, string reason)
|
||
{
|
||
if (!IsUserLoggedIn())
|
||
{
|
||
return;
|
||
}
|
||
|
||
if (AlarmInfoText != null)
|
||
{
|
||
var color = priority == AlarmPriority.High ? "#FF0000" : (priority == AlarmPriority.Medium ? "#FFFF00" : "#FFFFAA");
|
||
// AlarmInfoText.text = $"<color={color}>{reason}</color>";
|
||
AlarmInfoText.text = $"{reason}";
|
||
}
|
||
|
||
// 更新报警灯
|
||
if (AlarmLight != null)
|
||
{
|
||
AlarmLight.SetAlarmState(priority, true);
|
||
}
|
||
|
||
|
||
// Debug.Log($"收到DCS报警 - [{priority}] {reason}");
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 处理串口设备状态数据(来自SerialCommunicationService)
|
||
/// </summary>
|
||
private void OnSerialDeviceStatusReceived(DeviceStatusData status)
|
||
{
|
||
|
||
// 更新电池显示
|
||
if (BatteryText != null)
|
||
{
|
||
BatteryText.text = $"电量: {status.BatteryLevel}%";
|
||
// if (status.PowerType == 1)
|
||
// {
|
||
// if (ACText != null)
|
||
// {
|
||
// ACText.gameObject.SetActive(true);
|
||
// ACText.text = "未连接交流电";
|
||
// ACText.color = Color.yellow;
|
||
// }
|
||
// }
|
||
// else
|
||
// {
|
||
// if (ACText != null)
|
||
// {
|
||
// ACText.gameObject.SetActive(false);
|
||
// }
|
||
// }
|
||
}
|
||
|
||
if (BatteryFillImage != null && BatteryFillSprites != null && BatteryFillSprites.Length >= 4)
|
||
{
|
||
if (status.BatteryLevel > 90)
|
||
{
|
||
BatteryFillImage.sprite = BatteryFillSprites[0];
|
||
}
|
||
else if (status.BatteryLevel > 60)
|
||
{
|
||
BatteryFillImage.sprite = BatteryFillSprites[1];
|
||
}
|
||
else if (status.BatteryLevel > 20)
|
||
{
|
||
BatteryFillImage.sprite = BatteryFillSprites[2];
|
||
}
|
||
else
|
||
{
|
||
BatteryFillImage.sprite = BatteryFillSprites[3];
|
||
}
|
||
}
|
||
|
||
|
||
if (BatterySlider != null)
|
||
{
|
||
BatterySlider.value = Mathf.Clamp01(status.BatteryLevel / 100f);
|
||
}
|
||
|
||
|
||
}
|
||
|
||
|
||
void OnDestroy()
|
||
{
|
||
|
||
// 取消订阅DCSAlarmManager事件
|
||
DCSAlarmManager.OnAlarmRaised -= OnDCSAlarmRaised;
|
||
DCSAlarmManager.OnAlarmCleared -= OnAllAlarmsCleared;
|
||
// DCSAlarmManager.OnMuteStateChanged -= UpdateMuteButtonText;
|
||
|
||
if (_patientInfoService != null)
|
||
{
|
||
_patientInfoService.OnPatientInfoChanged -= LoadPatientInfo;
|
||
}
|
||
|
||
if (_serialService != null)
|
||
{
|
||
_serialService.OnDeviceStatusReceived -= OnSerialDeviceStatusReceived;
|
||
}
|
||
|
||
}
|
||
}
|