| | |
| | | std::string strDescription; |
| | | }; |
| | | |
| | | using AlarmMap = std::unordered_map<int, AlarmInfo>; |
| | | struct AlarmData { |
| | | int nId; // 错误码 |
| | | int nSeverityLevel; // 报警等级 |
| | | int nDeviceId; // 设备ID |
| | | int nUnitId; // 单元ID |
| | | std::string strDeviceName; // 设备名称 |
| | | std::string strUnitName; // 单元名称 |
| | | std::string strDescription; // 描述 |
| | | std::string strStartTime; // 开始时间 |
| | | std::string strEndTime; // 结束时间 |
| | | }; |
| | | |
| | | using AlarmInfoMap = std::unordered_map<int, AlarmInfo>; |
| | | using AlarmDataMap = std::unordered_map<int, AlarmData>; |
| | | |
| | | class AlarmManager { |
| | | public: |
| | |
| | | * @return AlarmManager实例的引用 |
| | | */ |
| | | static AlarmManager& getInstance(); |
| | | |
| | | /** |
| | | * 设置数据库连接 |
| | | * @param db 数据库连接的指针 |
| | | */ |
| | | void setDatabase(BL::Database* db); |
| | | |
| | | /** |
| | | * 初始化报警表 |
| | |
| | | bool destroyAlarmTable(); |
| | | |
| | | /** |
| | | * 插入模拟数据 |
| | | */ |
| | | void insertMockData(); |
| | | |
| | | /** |
| | | * 添加报警 |
| | | * @param id 报警ID |
| | | * @param deviceName 设备名称 |
| | | * @param description 报警描述 |
| | | * @param startTime 报警开始时间 |
| | | * @param endTime 报警结束时间 |
| | | * @param alarmData 报警数据的结构体 |
| | | * @param alarmEventId 最近插入的 alarm_event_id |
| | | * @return 成功返回true,失败返回false |
| | | */ |
| | | bool addAlarm(const std::string& id, const std::string& deviceName, const std::string& description, const std::string& startTime, const std::string& endTime); |
| | | bool addAlarm(const AlarmData& alarmData, int& alarmEventId); |
| | | |
| | | /** |
| | | * 查询所有报警数据 |
| | | * @return 包含所有报警数据的二维字符串向量 |
| | | * @return 包含所有报警数据的结构体 |
| | | */ |
| | | std::vector<std::vector<std::string>> getAllAlarms(); |
| | | std::vector<AlarmData> getAllAlarms(); |
| | | |
| | | /** |
| | | * 根据报警ID查询报警 |
| | | * @param id 报警ID |
| | | * @return 包含筛选后报警数据的二维字符串向量 |
| | | * @return 包含筛选后报警数据的结构体 |
| | | */ |
| | | std::vector<std::vector<std::string>> getAlarmsById(const std::string& id); |
| | | std::vector<AlarmData> getAlarmsById(const std::string& id); |
| | | |
| | | /** |
| | | * 根据描述查询报警 |
| | | * @param description 报警描述的筛选条件 |
| | | * @return 包含筛选后报警数据的二维字符串向量 |
| | | * @return 包含筛选后报警数据的结构体 |
| | | */ |
| | | std::vector<std::vector<std::string>> getAlarmsByDescription(const std::string& description); |
| | | std::vector<AlarmData> getAlarmsByDescription(const std::string& description); |
| | | |
| | | /** |
| | | * 根据时间范围查询报警 |
| | | * @param startTime 起始时间 |
| | | * @param endTime 结束时间 |
| | | * @return 包含查询结果的二维字符串向量 |
| | | * @return 包含查询结果的报警数据 |
| | | */ |
| | | std::vector<std::vector<std::string>> getAlarmsByTimeRange( |
| | | const std::string& startTime, const std::string& endTime); |
| | | std::vector<AlarmData> getAlarmsByTimeRange(const std::string& startTime, const std::string& endTime); |
| | | |
| | | /** |
| | | * 根据ID和时间范围查询报警 |
| | | * @param id 报警ID |
| | | * @param startTime 起始时间 |
| | | * @param endTime 结束时间 |
| | | * @return 包含查询结果的二维字符串向量 |
| | | * @return 包含查询结果的报警数据 |
| | | */ |
| | | std::vector<std::vector<std::string>> getAlarmsByIdAndTimeRange( |
| | | const std::string& id, const std::string& startTime, const std::string& endTime); |
| | | std::vector<AlarmData> getAlarmsByIdAndTimeRange(const std::string& id, const std::string& startTime, const std::string& endTime); |
| | | |
| | | /** |
| | | * 获取报警数据 |
| | | * @param startPosition 起始位置 |
| | | * @param count 获取的记录数量 |
| | | * @return 包含报警数据的二维字符串向量 |
| | | * @return 包含查询结果的报警数据 |
| | | */ |
| | | std::vector<std::vector<std::string>> getAlarms(int startPosition, int count); |
| | | std::vector<AlarmData> getAlarms(int startPosition, int count); |
| | | |
| | | /** |
| | | * 获取筛选后的报警数据 |
| | | * @param id 报警ID的筛选条件 |
| | | * @param deviceName 设备名称的筛选条件 |
| | | * @param description 报警描述的筛选条件 |
| | | * @param startTime 起始时间筛选条件 |
| | | * @param endTime 结束时间筛选条件 |
| | | * @param pageNumber 页码 |
| | | * @param pageSize 每页的记录数 |
| | | * @return 包含筛选后报警数据的二维字符串向量 |
| | | */ |
| | | std::vector<std::vector<std::string>> getFilteredAlarms( |
| | | const std::string& id, |
| | | const std::string& deviceName, |
| | | const std::string& description, |
| | | const std::string& startTime, |
| | | const std::string& endTime, |
| | | int pageNumber, |
| | | int pageSize); |
| | | /** |
| | | * 筛选报警数据 |
| | | * @param keyword 关键字筛选条件 |
| | | * @param startTime 起始时间筛选条件 |
| | | * @param endTime 结束时间筛选条件 |
| | | * @param pageNumber 页码 |
| | | * @param pageSize 每页记录数 |
| | | * @return 包含筛选后报警数据的结构体 |
| | | */ |
| | | std::vector<AlarmData> getFilteredAlarms(const std::string& keyword, const std::string& startTime, const std::string& endTime, int pageNumber, int pageSize); |
| | | |
| | | /** |
| | | * 获取符合条件的报警总数 |
| | | * @param id 报警ID的筛选条件 |
| | | * @param deviceName 设备名称的筛选条件 |
| | | * @param description 报警描述的筛选条件 |
| | | * @param keyword 关键字筛选条件 |
| | | * @param startTime 起始时间筛选条件 |
| | | * @param endTime 结束时间筛选条件 |
| | | * @return 符合条件的报警总数 |
| | | */ |
| | | int getTotalAlarmCount( |
| | | const std::string& id, |
| | | const std::string& deviceName, |
| | | const std::string& description, |
| | | const std::string& startTime, |
| | | const std::string& endTime); |
| | | int getTotalAlarmCount(const std::string& keyword, const std::string& startTime, const std::string& endTime); |
| | | |
| | | /** |
| | | * 更新报警结束时间 |
| | | * @param id 报警ID |
| | | * @param severityLevel 报警等级筛选条件 |
| | | * @param deviceId 设备ID |
| | | * @param unitId 单元ID |
| | | * @param description 报警描述 |
| | | * @param startTime 报警开始时间 |
| | | * @param newEndTime 新的报警结束时间 |
| | | * @return 成功返回true,失败返回false |
| | | */ |
| | | bool updateAlarmEndTime(const std::string& id, const std::string& deviceName, const std::string& description, const std::string& startTime, const std::string& newEndTime); |
| | | bool updateAlarmEndTime( |
| | | const std::string& id, |
| | | const std::string& severityLevel, |
| | | const std::string& deviceId, |
| | | const std::string& unitId, |
| | | const std::string& description, |
| | | const std::string& startTime, |
| | | const std::string& newEndTime); |
| | | |
| | | /** |
| | | * 清理旧报警 |
| | | * @param daysToKeep 保留的天数 |
| | | * @param deviceName 设备名称 |
| | | * @param deviceId 设备ID |
| | | * @param unitId 单元ID |
| | | */ |
| | | void cleanOldAlarms(int daysToKeep = 30, const std::string& deviceName = ""); |
| | | void cleanOldAlarms(int daysToKeep = 30, const std::string& deviceId = "", const std::string& unitId = ""); |
| | | |
| | | /** |
| | | * 通过设备ID获取设备名称 |
| | | * @param deviceId 设备ID |
| | | * @return 成功返回设备名称,失败返回空 |
| | | */ |
| | | std::string getDeviceNameById(int deviceId); |
| | | |
| | | /** |
| | | * 通过设备ID和单元ID获取单元名称 |
| | | * @param deviceId 设备ID |
| | | * @param unitId 单元ID |
| | | * @return 成功返回单元名称,失败返回空 |
| | | */ |
| | | std::string getUnitNameById(int deviceId, int unitId); |
| | | |
| | | /** |
| | | * 获取最近插入的 alarm_event_id |
| | | * @return 失败返回-1,成功返回最近插入的 alarm_event_id |
| | | */ |
| | | int getLastInsertId(); |
| | | |
| | | /** |
| | | * 通过事件id解除报警(更新结束时间) |
| | | * @param alarmEventId 事件ID |
| | | * @param endTime 结束时间 |
| | | * @return 成功返回true,失败返回false |
| | | */ |
| | | bool clearAlarmByEventId(int alarmEventId, const std::string& endTime); |
| | | |
| | | /** |
| | | * 通过多个属性查找并解除报警(更新结束时间) |
| | | * @param nId 报警ID |
| | | * @param nSeverityLevel 报警等级 |
| | | * @param nDeviceId 设备ID |
| | | * @param nUnitId 单元ID |
| | | * @param strDescription 描述 |
| | | * @param endTime 结束时间 |
| | | * @return 成功返回true,失败返回false |
| | | */ |
| | | bool clearAlarmByAttributes(int nId, int nDeviceId, int nUnitId, const std::string& endTime); |
| | | |
| | | /** |
| | | * 读取报警文件 |
| | |
| | | AlarmManager& operator=(const AlarmManager&) = delete; |
| | | |
| | | BL::Database* m_pDB; |
| | | AlarmMap m_mapAlarm; |
| | | AlarmInfoMap m_mapAlarm; |
| | | AlarmDataMap m_mapCache; |
| | | static std::mutex m_mutex; |
| | | }; |
| | | |