160 lines
5.3 KiB
C#
160 lines
5.3 KiB
C#
using ActiveProtect.SimulationEnvironment;
|
|
using System;
|
|
|
|
namespace ActiveProtect.Models
|
|
{
|
|
/// <summary>
|
|
/// 激光告警器类,用于检测激光照射并发出警报
|
|
/// </summary>
|
|
public class LaserWarner : SimulationElement
|
|
{
|
|
/// <summary>
|
|
/// 是否正在发出警报
|
|
/// </summary>
|
|
public bool IsAlarming { get; private set; }
|
|
|
|
/// <summary>
|
|
/// 警报持续时间(秒)
|
|
/// </summary>
|
|
private readonly double alarmDuration;
|
|
|
|
/// <summary>
|
|
/// 当前警报计时器
|
|
/// </summary>
|
|
private double alarmTimer = 0;
|
|
|
|
/// <summary>
|
|
/// 被监视实体的ID
|
|
/// </summary>
|
|
public string MonitoredEntityId { get; private set; }
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
/// <param name="id">激光告警器ID</param>
|
|
/// <param name="position">初始位置</param>
|
|
/// <param name="orientation">初始朝向</param>
|
|
/// <param name="simulationManager">仿真管理器</param>
|
|
/// <param name="monitoredEntityId">被监视实体的ID</param>
|
|
/// <param name="config">激光告警器配置</param>
|
|
public LaserWarner(string id, Vector3D position, Orientation orientation, ISimulationManager simulationManager, string monitoredEntityId, LaserWarnerConfig config)
|
|
: base(id, position, orientation, 0, simulationManager)
|
|
{
|
|
MonitoredEntityId = monitoredEntityId;
|
|
IsAlarming = false;
|
|
alarmDuration = config.AlarmDuration;
|
|
base.SimulationManager.SubscribeToEvent<LaserIlluminationStartEvent>(OnLaserIlluminationStart);
|
|
base.SimulationManager.SubscribeToEvent<LaserIlluminationStopEvent>(OnLaserIlluminationStop);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新激光告警器状态
|
|
/// </summary>
|
|
/// <param name="deltaTime">时间步长</param>
|
|
public override void Update(double deltaTime)
|
|
{
|
|
if (!IsActive) return;
|
|
|
|
var tank = SimulationManager.GetEntityById(MonitoredEntityId) as Tank;
|
|
if (tank != null)
|
|
{
|
|
Position = tank.Position;
|
|
Orientation = tank.Orientation;
|
|
}
|
|
|
|
if (IsAlarming)
|
|
{
|
|
alarmTimer += deltaTime;
|
|
if (alarmTimer >= alarmDuration)
|
|
{
|
|
StopAlarm();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 处理激光照射事件
|
|
/// </summary>
|
|
/// <param name="evt">激光照射事件</param>
|
|
private void OnLaserIlluminationStart(LaserIlluminationStartEvent evt)
|
|
{
|
|
if (evt?.Target != null && evt.Target.Id == MonitoredEntityId)
|
|
{
|
|
TriggerAlarm();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 处理激光照射停止事件
|
|
/// </summary>
|
|
/// <param name="evt">激光照射停止事件</param>
|
|
private void OnLaserIlluminationStop(LaserIlluminationStopEvent evt)
|
|
{
|
|
if (evt?.Target != null && evt.Target.Id == MonitoredEntityId)
|
|
{
|
|
StopAlarm();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 触发警报
|
|
/// </summary>
|
|
public void TriggerAlarm()
|
|
{
|
|
if (!IsAlarming)
|
|
{
|
|
IsAlarming = true;
|
|
alarmTimer = 0;
|
|
PublishEvent(new LaserWarnerAlarmEvent() { TargetId = MonitoredEntityId });
|
|
Console.WriteLine($"激光告警器 {Id} 发出警报!");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 停止警报
|
|
/// </summary>
|
|
public void StopAlarm()
|
|
{
|
|
if (IsAlarming)
|
|
{
|
|
IsAlarming = false;
|
|
alarmTimer = 0;
|
|
PublishEvent(new LaserWarnerAlarmStopEvent() { TargetId = MonitoredEntityId });
|
|
Console.WriteLine($"激光告警器 {Id} 停止警报");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取激光告警器状态信息
|
|
/// </summary>
|
|
/// <returns>状态信息字符串</returns>
|
|
public override string GetStatus()
|
|
{
|
|
return $"激光告警器 {Id}:\n" +
|
|
$" 监视实体: {MonitoredEntityId}\n" +
|
|
$" 警报状态: {(IsAlarming ? "警报中" : "正常")}\n" +
|
|
$" 警报持续时间: {(IsAlarming ? alarmTimer : 0):F2}/{alarmDuration:F2}";
|
|
}
|
|
|
|
/// <summary>
|
|
/// 激活激光告警器
|
|
/// </summary>
|
|
public override void Activate()
|
|
{
|
|
base.Activate();
|
|
SimulationManager.SubscribeToEvent<LaserIlluminationStartEvent>(OnLaserIlluminationStart);
|
|
SimulationManager.SubscribeToEvent<LaserIlluminationStopEvent>(OnLaserIlluminationStop);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 停用激光告警器
|
|
/// </summary>
|
|
public override void Deactivate()
|
|
{
|
|
base.Deactivate();
|
|
SimulationManager.UnsubscribeFromEvent<LaserIlluminationStartEvent>(OnLaserIlluminationStart);
|
|
SimulationManager.UnsubscribeFromEvent<LaserIlluminationStopEvent>(OnLaserIlluminationStop);
|
|
}
|
|
}
|
|
}
|