#ifndef ALARM_MANAGER_H
|
#define ALARM_MANAGER_H
|
|
#include <string>
|
#include <vector>
|
#include <mutex>
|
#include <unordered_map>
|
#include "Database.h"
|
|
struct AlarmInfo {
|
std::string strUnitID;
|
std::string strUnitNo;
|
int nAlarmLevel;
|
int nAlarmCode;
|
int nAlarmID;
|
std::string strAlarmText;
|
std::string strDescription;
|
};
|
|
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();
|
|
/**
|
* ³õʼ»¯±¨¾¯±í
|
* @return ³É¹¦·µ»Øtrue£¬Ê§°Ü·µ»Øfalse
|
*/
|
bool initAlarmTable();
|
|
/**
|
* Ïú»Ù±¨¾¯±í
|
*/
|
void termAlarmTable();
|
|
/**
|
* Ïú»Ù±¨¾¯±í
|
* @return ³É¹¦·µ»Øtrue£¬Ê§°Ü·µ»Øfalse
|
*/
|
bool destroyAlarmTable();
|
|
/**
|
* ²åÈëÄ£ÄâÊý¾Ý
|
*/
|
void insertMockData();
|
|
/**
|
* Ìí¼Ó±¨¾¯
|
* @param alarmData ±¨¾¯Êý¾ÝµÄ½á¹¹Ìå
|
* @param alarmEventId ×î½ü²åÈëµÄ alarm_event_id
|
* @return ³É¹¦·µ»Øtrue£¬Ê§°Ü·µ»Øfalse
|
*/
|
bool addAlarm(const AlarmData& alarmData, int& alarmEventId);
|
|
/**
|
* ²éѯËùÓб¨¾¯Êý¾Ý
|
* @return °üº¬ËùÓб¨¾¯Êý¾ÝµÄ½á¹¹Ìå
|
*/
|
std::vector<AlarmData> getAllAlarms();
|
|
/**
|
* ¸ù¾Ý±¨¾¯ID²éѯ±¨¾¯
|
* @param id ±¨¾¯ID
|
* @return °üº¬É¸Ñ¡ºó±¨¾¯Êý¾ÝµÄ½á¹¹Ìå
|
*/
|
std::vector<AlarmData> getAlarmsById(const std::string& id);
|
|
/**
|
* ¸ù¾ÝÃèÊö²éѯ±¨¾¯
|
* @param description ±¨¾¯ÃèÊöµÄɸѡÌõ¼þ
|
* @return °üº¬É¸Ñ¡ºó±¨¾¯Êý¾ÝµÄ½á¹¹Ìå
|
*/
|
std::vector<AlarmData> getAlarmsByDescription(const std::string& description);
|
|
/**
|
* ¸ù¾Ýʱ¼ä·¶Î§²éѯ±¨¾¯
|
* @param startTime Æðʼʱ¼ä
|
* @param endTime ½áÊøÊ±¼ä
|
* @return °üº¬²éѯ½á¹ûµÄ±¨¾¯Êý¾Ý
|
*/
|
std::vector<AlarmData> getAlarmsByTimeRange(const std::string& startTime, const std::string& endTime);
|
|
/**
|
* ¸ù¾ÝIDºÍʱ¼ä·¶Î§²éѯ±¨¾¯
|
* @param id ±¨¾¯ID
|
* @param startTime Æðʼʱ¼ä
|
* @param endTime ½áÊøÊ±¼ä
|
* @return °üº¬²éѯ½á¹ûµÄ±¨¾¯Êý¾Ý
|
*/
|
std::vector<AlarmData> getAlarmsByIdAndTimeRange(const std::string& id, const std::string& startTime, const std::string& endTime);
|
|
/**
|
* »ñÈ¡±¨¾¯Êý¾Ý
|
* @param startPosition ÆðʼλÖÃ
|
* @param count »ñÈ¡µÄ¼Ç¼ÊýÁ¿
|
* @return °üº¬²éѯ½á¹ûµÄ±¨¾¯Êý¾Ý
|
*/
|
std::vector<AlarmData> getAlarms(int startPosition, int count);
|
|
/**
|
* ɸѡ±¨¾¯Êý¾Ý
|
* @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 keyword ¹Ø¼ü×ÖɸѡÌõ¼þ
|
* @param startTime Æðʼʱ¼äɸѡÌõ¼þ
|
* @param endTime ½áÊøÊ±¼äɸѡÌõ¼þ
|
* @return ·ûºÏÌõ¼þµÄ±¨¾¯×ÜÊý
|
*/
|
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& severityLevel,
|
const std::string& deviceId,
|
const std::string& unitId,
|
const std::string& description,
|
const std::string& startTime,
|
const std::string& newEndTime);
|
|
/**
|
* ÇåÀí¾É±¨¾¯
|
* @param daysToKeep ±£ÁôµÄÌìÊý
|
* @param deviceId É豸ID
|
* @param unitId µ¥ÔªID
|
*/
|
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);
|
|
/**
|
* ¶ÁÈ¡±¨¾¯Îļþ
|
* @param filename ÎļþÃû
|
* @return ³É¹¦·µ»Øtrue£¬Ê§°Ü·µ»Øfalse
|
*/
|
bool readAlarmFile(const std::string& filename);
|
|
/**
|
* ±£´æ±¨¾¯Îļþ
|
* @param filename ÎļþÃû
|
* @return ³É¹¦·µ»Øtrue£¬Ê§°Ü·µ»Øfalse
|
*/
|
bool saveAlarmFile(const std::string& filename);
|
|
/**
|
* ͨ¹ý±¨¾¯ID²éѯ±¨¾¯ÐÅÏ¢
|
* @param nAlarmID ±¨¾¯ID
|
* @return ±¨¾¯ÐÅÏ¢µÄÖ¸Õë
|
*/
|
const AlarmInfo* getAlarmInfoByID(int nAlarmID) const;
|
|
/**
|
* ͨ¹ý¶à¸ö±¨¾¯ID²éѯ¶ÔÓ¦µÄ±¨¾¯ÐÅÏ¢
|
* @param alarmIDs ¶à¸ö±¨¾¯ID
|
* @return ·µ»Ø¶à¸ö±¨¾¯ÐÅÏ¢
|
*/
|
std::vector<AlarmInfo> getAlarmsInfoByIDs(const std::vector<int>& alarmIDs) const;
|
|
private:
|
AlarmManager();
|
~AlarmManager();
|
|
// ½ûÖ¹¿½±´ºÍ¸³Öµ
|
AlarmManager(const AlarmManager&) = delete;
|
AlarmManager& operator=(const AlarmManager&) = delete;
|
|
BL::Database* m_pDB;
|
AlarmInfoMap m_mapAlarm;
|
AlarmDataMap m_mapCache;
|
static std::mutex m_mutex;
|
};
|
|
#endif // ALARM_MANAGER_H
|