CppGenerate/include/alert_manager.hpp

261 lines
6.5 KiB
C++
Raw Normal View History

2026-04-17 09:17:56 +00:00
#ifndef HSM_ALERT_MANAGER_HPP
#define HSM_ALERT_MANAGER_HPP
#include <cstdint>
#include <string>
#include <vector>
#include <memory>
2026-04-22 06:15:49 +00:00
#include <unordered_map>
#include <utility>
2026-04-17 09:17:56 +00:00
/**
* @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_; }
2026-04-22 06:15:49 +00:00
/**
* @brief
* @return pair<, >
*/
std::pair<float, float> getAltitudeThresholds() const {
return std::make_pair(upper_threshold_, lower_threshold_);
}
2026-04-17 09:17:56 +00:00
/**
* @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);
2026-04-22 06:15:49 +00:00
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);
2026-04-17 09:17:56 +00:00
};
#endif // HSM_ALERT_MANAGER_HPP