CppGenerate/include/alert_manager.hpp

261 lines
6.5 KiB
C++

#ifndef HSM_ALERT_MANAGER_HPP
#define HSM_ALERT_MANAGER_HPP
#include <cstdint>
#include <string>
#include <vector>
#include <memory>
#include <unordered_map>
#include <utility>
/**
* @brief 预警类型枚举
*/
enum class AlertType {
NONE = 0,
ALTITUDE_UPPER, // 高度上限预警
ALTITUDE_LOWER, // 高度下限预警
SENSOR_FAILURE, // 传感器故障
LOW_BATTERY, // 低电量
COMMUNICATION_ERROR, // 通信错误
CALIBRATION_ERROR // 校准错误
};
/**
* @brief 预警状态枚举
*/
enum class AlertStatus {
INACTIVE, // 未激活
ACTIVE, // 已激活(预警中)
ACKNOWLEDGED,// 已确认(等待恢复正常)
RESOLVED // 已解决
};
/**
* @brief 预警信息结构体
*/
struct AlertInfo {
AlertType type; // 预警类型
AlertStatus status; // 预警状态
uint32_t trigger_time; // 触发时间戳(秒)
uint32_t acknowledge_time;// 确认时间戳(秒)
uint32_t resolve_time; // 解决时间戳(秒)
std::string description; // 预警描述
AlertInfo() : type(AlertType::NONE), status(AlertStatus::INACTIVE),
trigger_time(0), acknowledge_time(0), resolve_time(0) {}
AlertInfo(AlertType t, const std::string& desc = "")
: type(t), status(AlertStatus::INACTIVE), description(desc),
trigger_time(0), acknowledge_time(0), resolve_time(0) {}
/**
* @brief 检查预警是否处于活动状态
* @return 是否活动
*/
bool isActive() const {
return status == AlertStatus::ACTIVE || status == AlertStatus::ACKNOWLEDGED;
}
};
/**
* @brief 预警管理器类
*
* 负责管理系统的所有预警功能,包括:
* 1. 高度阈值预警(上下限)
* 2. 传感器状态预警
* 3. 系统状态预警
* 4. 预警确认和重置
* 5. 预警历史记录
*/
class AlertManager {
public:
/**
* @brief 构造函数
*/
AlertManager();
/**
* @brief 析构函数
*/
~AlertManager();
/**
* @brief 初始化预警管理器
*/
void initialize();
/**
* @brief 检查高度是否越限
* @param altitude 当前高度(米)
* @param timestamp 当前时间戳
*/
void checkAltitudeAlert(float altitude, uint32_t timestamp);
/**
* @brief 设置高度预警阈值
* @param upper 上限阈值(米)
* @param lower 下限阈值(米)
*/
void setAltitudeThresholds(float upper, float lower);
/**
* @brief 触发传感器故障预警
* @param timestamp 时间戳
*/
void triggerSensorFailure(uint32_t timestamp);
/**
* @brief 触发低电量预警
* @param timestamp 时间戳
* @param battery_level 电量百分比
*/
void triggerLowBattery(uint32_t timestamp, float battery_level = 20.0f);
/**
* @brief 触发通信错误预警
* @param timestamp 时间戳
*/
void triggerCommunicationError(uint32_t timestamp);
/**
* @brief 确认当前所有预警
* @param timestamp 确认时间戳
*/
void acknowledgeAllAlerts(uint32_t timestamp);
/**
* @brief 确认指定类型的预警
* @param type 预警类型
* @param timestamp 确认时间戳
*/
void acknowledgeAlert(AlertType type, uint32_t timestamp);
/**
* @brief 重置指定预警
* @param type 预警类型
*/
void resetAlert(AlertType type);
/**
* @brief 重置所有预警
*/
void resetAllAlerts();
/**
* @brief 检查是否有活动预警
* @return 是否有活动预警
*/
bool hasActiveAlerts() const;
/**
* @brief 检查是否有未确认的活动预警
* @return 是否有未确认预警
*/
bool hasUnacknowledgedAlerts() const;
/**
* @brief 获取所有活动预警
* @return 活动预警列表
*/
std::vector<AlertInfo> getActiveAlerts() const;
/**
* @brief 获取预警历史记录
* @param max_count 最大返回数量
* @return 预警历史
*/
std::vector<AlertInfo> getAlertHistory(size_t max_count = 100) const;
/**
* @brief 获取预警状态描述
* @return 状态描述字符串
*/
std::string getStatusDescription() const;
/**
* @brief 清除所有预警历史
*/
void clearHistory();
/**
* @brief 获取高度上限阈值
* @return 上限阈值
*/
float getUpperThreshold() const { return upper_threshold_; }
/**
* @brief 获取高度下限阈值
* @return 下限阈值
*/
float getLowerThreshold() const { return lower_threshold_; }
/**
* @brief 获取高度阈值(上下限)
* @return pair<上限阈值, 下限阈值>
*/
std::pair<float, float> getAltitudeThresholds() const {
return std::make_pair(upper_threshold_, lower_threshold_);
}
/**
* @brief 检查传感器是否正常
* @return 传感器状态
*/
bool isSensorNormal() const {
auto it = alerts_.find(AlertType::SENSOR_FAILURE);
return it == alerts_.end() || it->second.status != AlertStatus::ACTIVE;
}
/**
* @brief 触发预警
* @param type 预警类型
* @param timestamp 时间戳
* @param description 预警描述
*/
void triggerAlert(AlertType type, uint32_t timestamp, const std::string& description = "");
/**
* @brief 更新预警状态
* @param type 预警类型
* @param new_status 新状态
* @param timestamp 时间戳
*/
void updateAlertStatus(AlertType type, AlertStatus new_status, uint32_t timestamp);
/**
* @brief 获取预警类型名称
* @param type 预警类型
* @return 类型名称
*/
static std::string getAlertTypeName(AlertType type);
/**
* @brief 获取预警状态名称
* @param status 预警状态
* @return 状态名称
*/
static std::string getAlertStatusName(AlertStatus status);
private:
// 预警阈值
float upper_threshold_;
float lower_threshold_;
// 当前预警状态
std::unordered_map<AlertType, AlertInfo> alerts_;
// 预警历史记录
std::vector<AlertInfo> alert_history_;
// 历史记录最大数量
static const size_t MAX_HISTORY = 1000;
/**
* @brief 添加预警到历史记录
* @param alert 预警信息
*/
void addToHistory(const AlertInfo& alert);
};
#endif // HSM_ALERT_MANAGER_HPP