darker
2025-02-21 debee9b5cae51f744937fc4f0ef4c25272886676
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#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;
};
 
using AlarmMap = std::unordered_map<int, AlarmInfo>;
 
class AlarmManager {
public:
    /**
     * »ñÈ¡µ¥ÀýʵÀý
     * @return AlarmManagerʵÀýµÄÒýÓÃ
     */
    static AlarmManager& getInstance();
 
    /**
     * ÉèÖÃÊý¾Ý¿âÁ¬½Ó
     * @param db Êý¾Ý¿âÁ¬½ÓµÄÖ¸Õë
     */
    void setDatabase(BL::Database* db);
 
    /**
     * ³õʼ»¯±¨¾¯±í
     * @return ³É¹¦·µ»Øtrue£¬Ê§°Ü·µ»Øfalse
     */
    bool initAlarmTable();
 
    /**
     * Ïú»Ù±¨¾¯±í
     */
    void termAlarmTable();
 
    /**
     * Ïú»Ù±¨¾¯±í
     * @return ³É¹¦·µ»Øtrue£¬Ê§°Ü·µ»Øfalse
     */
    bool destroyAlarmTable();
 
    /**
     * Ìí¼Ó±¨¾¯
     * @param id ±¨¾¯ID
     * @param deviceName É豸Ãû³Æ
     * @param description ±¨¾¯ÃèÊö
     * @param startTime ±¨¾¯¿ªÊ¼Ê±¼ä
     * @param endTime ±¨¾¯½áÊøÊ±¼ä
     * @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);
 
    /**
     * ²éѯËùÓб¨¾¯Êý¾Ý
     * @return °üº¬ËùÓб¨¾¯Êý¾ÝµÄ¶þά×Ö·û´®ÏòÁ¿
     */
    std::vector<std::vector<std::string>> getAllAlarms();
 
    /**
     * ¸ù¾Ý±¨¾¯ID²éѯ±¨¾¯
     * @param id ±¨¾¯ID
     * @return °üº¬É¸Ñ¡ºó±¨¾¯Êý¾ÝµÄ¶þά×Ö·û´®ÏòÁ¿
     */
    std::vector<std::vector<std::string>> getAlarmsById(const std::string& id);
 
    /**
     * ¸ù¾ÝÃèÊö²éѯ±¨¾¯
     * @param description ±¨¾¯ÃèÊöµÄɸѡÌõ¼þ
     * @return °üº¬É¸Ñ¡ºó±¨¾¯Êý¾ÝµÄ¶þά×Ö·û´®ÏòÁ¿
     */
    std::vector<std::vector<std::string>> getAlarmsByDescription(const std::string& description);
 
    /**
     * ¸ù¾Ýʱ¼ä·¶Î§²éѯ±¨¾¯
     * @param startTime Æðʼʱ¼ä
     * @param endTime ½áÊøÊ±¼ä
     * @return °üº¬²éѯ½á¹ûµÄ¶þά×Ö·û´®ÏòÁ¿
     */
    std::vector<std::vector<std::string>> getAlarmsByTimeRange(
        const std::string& startTime, const std::string& endTime);
 
    /**
    * ¸ù¾ÝIDºÍʱ¼ä·¶Î§²éѯ±¨¾¯
     * @param id ±¨¾¯ID
     * @param startTime Æðʼʱ¼ä
     * @param endTime ½áÊøÊ±¼ä
     * @return °üº¬²éѯ½á¹ûµÄ¶þά×Ö·û´®ÏòÁ¿
     */
    std::vector<std::vector<std::string>> getAlarmsByIdAndTimeRange(
        const std::string& id, const std::string& startTime, const std::string& endTime);
 
    /**
     * »ñÈ¡±¨¾¯Êý¾Ý
     * @param startPosition ÆðʼλÖÃ
     * @param count »ñÈ¡µÄ¼Ç¼ÊýÁ¿
     * @return °üº¬±¨¾¯Êý¾ÝµÄ¶þά×Ö·û´®ÏòÁ¿
     */
    std::vector<std::vector<std::string>> 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 id ±¨¾¯IDµÄɸѡÌõ¼þ
     * @param deviceName É豸Ãû³ÆµÄɸѡÌõ¼þ
     * @param description ±¨¾¯ÃèÊöµÄɸѡÌõ¼þ
     * @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);
 
    /**
     * ¸üб¨¾¯½áÊøÊ±¼ä
     * @param id ±¨¾¯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);
 
    /**
     * ÇåÀí¾É±¨¾¯
     * @param daysToKeep ±£ÁôµÄÌìÊý
     * @param deviceName É豸Ãû³Æ
     */
    void cleanOldAlarms(int daysToKeep = 30, const std::string& deviceName = "");
 
    /**
     * ¶ÁÈ¡±¨¾¯Îļþ
     * @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;
    AlarmMap m_mapAlarm;
    static std::mutex m_mutex;
};
 
#endif // ALARM_MANAGER_H