25-1023工业控制-火力发电厂辅助控制系统
hk
2025-03-12 cfa43bdeea0b1fd7ea175fe72e609a76f1ba2747
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "stdafx.h"
#include "NetworkConfig.h"
 
bool NetworkConfig::startupModeSelected = false;
 
NetworkConfig::NetworkConfig(const std::string& xmlFilePath)
{
    tinyxml2::XMLError eResult = doc.LoadFile(xmlFilePath.c_str());
    loaded = (eResult == tinyxml2::XML_SUCCESS);
 
    if (!startupModeSelected) {
        std::string selectedMode;
        std::time_t lastModifiedTime = 0;
        std::cout << "Please select the startup mode (cold_start/hot_start): " << std::endl;
        std::cin >> selectedMode;
 
        if (this->setStartupMode(selectedMode)) {
            std::cout << "Selected " << selectedMode << std::endl;
            if (this->saveXML("config.xml")) {
                std::cout << "( XML file saved successfully )" << std::endl;
            }
            else {
                std::cout << "( Failed to save XML file )" << std::endl;
            }
 
            if (selectedMode == "cold_start") {
                StartupMode currentMode = StartupMode::ColdStart;
                this->restartServices(currentMode, "config.xml", lastModifiedTime);
            }
            else if (selectedMode == "hot_start") {
                startupModeSelected = true;
                NetworkConfig oldParser("Info.xml");
                StartupMode currentMode = StartupMode::HotStart;
                this->restartServices(currentMode, "config.xml", lastModifiedTime);
            }
        }
        else {
            std::cout << "Invalid startup mode. Please enter cold_start or hot_start." << std::endl;
        }
        startupModeSelected = true;
    }
}
 
NetworkConfig::~NetworkConfig()
{
 
}
 
bool NetworkConfig::isLoaded() const
{
    return loaded;
}
 
bool NetworkConfig::getTCPConfig(std::string & ipAddress, int & portNumber)
{
    if (!loaded) return false;
    tinyxml2::XMLElement* tcpElement = doc.FirstChildElement("NetworkConfigurationInfo")
        ->FirstChildElement("Socket")->FirstChildElement("TCP");
 
    if (tcpElement) {
        const char* ip = tcpElement->FirstChildElement("IPAddress")->GetText();// Get IP address text
        if (ip)  ipAddress = ip;
 
        portNumber = std::stoi(tcpElement->FirstChildElement("PortNumber")->GetText());// Get port number
        return true;
    }
    return false;
}
 
bool NetworkConfig::getRTSPConfig(std::string & ipAddress, int & portNumber)
{
    if (!loaded) return false;
    tinyxml2::XMLElement* rtspElement = doc.FirstChildElement("NetworkConfigurationInfo")
        ->FirstChildElement("Socket")->FirstChildElement("RTSP");
    if (rtspElement) {
        const char* ip = rtspElement->FirstChildElement("IPAddress")->GetText();  // Get IP address text
        if (ip) ipAddress = ip;
        rtspElement->FirstChildElement("PortNumber")->QueryIntText(&portNumber);// Get port number
        return true;
    }
    return false;
}
 
bool NetworkConfig::getLogConfig(std::string & path, int & size)
{
    if (!loaded) return false;
    tinyxml2::XMLElement* logElement = doc.FirstChildElement("NetworkConfigurationInfo")
        ->FirstChildElement("Log");
    if (logElement) {
        const char* logPath = logElement->FirstChildElement("Path")->GetText();  // Get log path text
        if (logPath) path = logPath;
        logElement->FirstChildElement("Size")->QueryIntText(&size);// Get log size
        return true;
    }
    return false;
}
 
bool NetworkConfig::getMySQLConfig(std::string & ip, int & port, std::string & db, std::string & user, std::string & password)
{
    if (!loaded) return false;
    tinyxml2::XMLElement* mysqlElement = doc.FirstChildElement("NetworkConfigurationInfo")
        ->FirstChildElement("MySQL");
    if (mysqlElement) {
        const char* mysqlIp = mysqlElement->FirstChildElement("IP")->GetText();  // Get IP address text
        if (mysqlIp) ip = mysqlIp;
        mysqlElement->FirstChildElement("port")->QueryIntText(&port);
 
        const char* mysqlDb = mysqlElement->FirstChildElement("db")->GetText();  // Get database name text
        if (mysqlDb) db = mysqlDb;
 
        const char* mysqlUser = mysqlElement->FirstChildElement("user")->GetText();  // Get username text
        if (mysqlUser) user = mysqlUser;
 
        const char* mysqlPassword = mysqlElement->FirstChildElement("password")->GetText();  // Get password text
        if (mysqlPassword) password = mysqlPassword;
        return true;
    }
    return false;
}
 
bool NetworkConfig::getPictureManagementRootPath(std::string & rootPath)
{
    if (!loaded) return false;
    tinyxml2::XMLElement* pictureElement = doc.FirstChildElement("NetworkConfigurationInfo")
        ->FirstChildElement("PictureManagement");
    if (pictureElement) {
        const char* path = pictureElement->FirstChildElement("RootPath")->GetText();  // Get root path text
        if (path) rootPath = path;
        return true;
    }
    return false;
}
 
bool NetworkConfig::getPreprocessedPictureRootPath(std::string & rootPath)
{
    if (!loaded) return false;
    tinyxml2::XMLElement* preprocessedElement = doc.FirstChildElement("NetworkConfigurationInfo")
        ->FirstChildElement("PreprocessedPicture");
    if (preprocessedElement) {
        const char* path = preprocessedElement->FirstChildElement("RootPath")->GetText();  // Get root path text
        if (path) rootPath = path;
        return true;
    }
    return false;
}
 
bool NetworkConfig::getVideoManagementRootPath(std::string & rootPath)
{
    if (!loaded) return false;
    tinyxml2::XMLElement* VideoElement = doc.FirstChildElement("NetworkConfigurationInfo")
        ->FirstChildElement("VideoManagement");
    if (VideoElement) {
        const char* path = VideoElement->FirstChildElement("RootPath")->GetText();  // Get root path text
        if (path) rootPath = path;
        return true;
    }
    return false;
}
 
bool NetworkConfig::getCNNModelSavePath(std::string & path)
{
    if (!loaded) return false;
    tinyxml2::XMLElement* cnnElement = doc.FirstChildElement("NetworkConfigurationInfo")
        ->FirstChildElement("CNNModelSave");
    if (cnnElement) {
        const char* cnnPath = cnnElement->FirstChildElement("Path")->GetText();  // Get save path text
        if (cnnPath) path = cnnPath;
        return true;
    }
    return false;
}
 
bool NetworkConfig::getVersionManagementRootPath(std::string & rootPath)
{
    if (!loaded) return false;
    tinyxml2::XMLElement* versionElement = doc.FirstChildElement("NetworkConfigurationInfo")
        ->FirstChildElement("VersionManagement");
    if (versionElement) {
        const char* path = versionElement->FirstChildElement("RootPath")->GetText();  // Get root path text
        if (path) rootPath = path;
        return true;
    }
    return false;
}
 
bool NetworkConfig::getStartupMode(std::string & mode)
{
    if (!loaded) return false;
    tinyxml2::XMLElement* startupElement = doc.FirstChildElement("NetworkConfigurationInfo")
        ->FirstChildElement("StartupMode");
    if (startupElement) {
        const char* startupMode = startupElement->FirstChildElement("Mode")->GetText();// Get startup mode
        if (startupMode) mode = startupMode;
        return true;
    }
    return false;
}
 
bool NetworkConfig::setStartupMode(const std::string & mode)
{
    if (!loaded || (mode != "cold_start" && mode != "hot_start")) return false;
    tinyxml2::XMLElement* startupElement = doc.FirstChildElement("NetworkConfigurationInfo")
        ->FirstChildElement("StartupMode");
    if (startupElement) {
        tinyxml2::XMLElement* modeElement = startupElement->FirstChildElement("Mode");
        if (modeElement) {
            modeElement->SetText(mode.c_str());
            return true;
        }
    }
    return false;
}
 
bool NetworkConfig::saveXML(const std::string & xmlFilePath)
{
    if (!loaded) return false;
    tinyxml2::XMLError eResult = doc.SaveFile(xmlFilePath.c_str());
    return (eResult == tinyxml2::XML_SUCCESS);
}
 
void NetworkConfig::coldStart()
{
    std::cout << "Cold start begins..." << std::endl;
    std::cout << "Initializing network connections...\n";
    std::string tcpIp;
    int tcpPort;
    if (this->getTCPConfig(tcpIp, tcpPort)) {
        std::cout << "Establishing TCP connection: " << tcpIp << ":" << tcpPort << std::endl;
    }
 
    std::string rtspIp;
    int rtspPort;
    if (this->getRTSPConfig(rtspIp, rtspPort)) {
        std::cout << "Establishing RTSP connection: " << rtspIp << ":" << rtspPort << std::endl;
    }
 
    std::string mysqlIp, mysqlDb, mysqlUser, mysqlPassword;
    int mysqlPort;
    if (this->getMySQLConfig(mysqlIp, mysqlPort, mysqlDb, mysqlUser, mysqlPassword)) {
        std::cout << "Connecting to MySQL database: " << mysqlIp << ":" << mysqlPort << " - " << mysqlDb << std::endl;
    }
 
    std::string cnnPath;
    if (this->getCNNModelSavePath(cnnPath)) {
        std::cout << "Loading CNN model: " << cnnPath << std::endl;
    }
    std::cout << "Cold start completed." << std::endl;
}
 
void NetworkConfig::hotStart(NetworkConfig & oldParser)
{
    std::cout << "Hot start begins..." << std::endl;
    std::string oldTcpIp, newTcpIp;
    int oldTcpPort, newTcpPort;
    if (oldParser.getTCPConfig(oldTcpIp, oldTcpPort) && this->getTCPConfig(newTcpIp, newTcpPort)) {
        if (oldTcpIp != newTcpIp || oldTcpPort != newTcpPort) {
            std::cout << "TCP configuration updated: " << oldTcpIp << ":" << oldTcpPort
                << " -> " << newTcpIp << ":" << newTcpPort << std::endl;
            if (setTCPConfig(newTcpIp, newTcpPort)) {
                std::cout << "TCP" << std::endl;
                std::cout << "    IP: " << newTcpIp << "\n    Port: " << newTcpPort << std::endl;
            }
        }
    }
 
    std::string oldRtspIp, newRtspIp;
    int oldRtspPort, newRtspPort;
    if (oldParser.getRTSPConfig(oldRtspIp, oldRtspPort) && this->getRTSPConfig(newRtspIp, newRtspPort)) {
        if (oldRtspIp != newRtspIp || oldRtspPort != newRtspPort) {
            std::cout << "RTSP configuration updated: " << oldRtspIp << ":" << oldRtspPort
                << " -> " << newRtspIp << ":" << newRtspPort << std::endl;
        }
    }
    std::string oldLogPath, newLogPath;
    int oldLogSize, newLogSize;
    if (oldParser.getLogConfig(oldLogPath, oldLogSize) && this->getLogConfig(newLogPath, newLogSize)) {
        if (oldLogPath != newLogPath || oldLogSize != newLogSize) {
            std::cout << "Log configuration updated: " << oldLogPath << "(" << oldLogSize << ")"
                << " -> " << newLogPath << "(" << newLogSize << ")" << std::endl;
        }
    }
    std::string oldMysqlIp, newMysqlIp, oldMysqlDb, newMysqlDb, oldMysqlUser, newMysqlUser, oldMysqlPassword, newMysqlPassword;
    int oldMysqlPort, newMysqlPort;
    if (oldParser.getMySQLConfig(oldMysqlIp, oldMysqlPort, oldMysqlDb, oldMysqlUser, oldMysqlPassword) &&
        this->getMySQLConfig(newMysqlIp, newMysqlPort, newMysqlDb, newMysqlUser, newMysqlPassword)) {
        if (oldMysqlIp != newMysqlIp || oldMysqlPort != newMysqlPort ||
            oldMysqlDb != newMysqlDb || oldMysqlUser != newMysqlUser || oldMysqlPassword != newMysqlPassword) {
            std::cout << "MySQL configuration updated: " << oldMysqlIp << ":" << oldMysqlPort << " - " << oldMysqlDb
                << " -> " << newMysqlIp << ":" << newMysqlPort << " - " << newMysqlDb << std::endl;
        }
    }
    std::string oldPictureRootPath, newPictureRootPath;
    if (oldParser.getPictureManagementRootPath(oldPictureRootPath) && this->getPictureManagementRootPath(newPictureRootPath)) {
        if (oldPictureRootPath != newPictureRootPath) {
            std::cout << "Picture management root path updated: " << oldPictureRootPath
                << " -> " << newPictureRootPath << std::endl;
        }
    }
    std::string oldVideoRootPath, newVideoRootPath;
    if (oldParser.getVideoManagementRootPath(oldVideoRootPath) && this->getVideoManagementRootPath(newVideoRootPath)) {
        if (oldVideoRootPath != newVideoRootPath) {
            std::cout << "Video management root path updated: " << oldVideoRootPath
                << " -> " << newVideoRootPath << std::endl;
        }
    }
    std::string oldCnnPath, newCnnPath;
    if (oldParser.getCNNModelSavePath(oldCnnPath) && this->getCNNModelSavePath(newCnnPath)) {
        if (oldCnnPath != newCnnPath) {
            std::cout << "CNN model save path updated: " << oldCnnPath
                << " -> " << newCnnPath << std::endl;
        }
    }
    std::string oldVersionRootPath, newVersionRootPath;
    if (oldParser.getVersionManagementRootPath(oldVersionRootPath) && this->getVersionManagementRootPath(newVersionRootPath)) {
        if (oldVersionRootPath != newVersionRootPath) {
            std::cout << "Version management root path updated: " << oldVersionRootPath
                << " -> " << newVersionRootPath << std::endl;
        }
    }
    std::cout << "Hot start completed." << std::endl;
}
 
bool NetworkConfig::isConfigUpdated(const std::string & xmlFilePath, std::time_t & lastModifiedTime)
{
    struct stat fileStat;
    if (stat(xmlFilePath.c_str(), &fileStat) == 0) {
        if (fileStat.st_mtime > lastModifiedTime) {
            lastModifiedTime = fileStat.st_mtime;
            return true;
        }
    }
    return false;
}
 
void NetworkConfig::restartServices(StartupMode currentMode, const std::string & xmlFilePath, std::time_t & lastModifiedTime)
{
    switch (currentMode) {
    case StartupMode::ColdStart:
        if (isConfigUpdated("Info.xml", lastModifiedTime)) {
            std::cout << "Configuration file parameters changed. Performing cold start to restart the entire service..." << std::endl;
            coldStart();
        }
        else {
            std::cout << "No changes in the configuration file. Cold start is not required." << std::endl;
        }
        break;
    case StartupMode::HotStart:
        if (this->isConfigUpdated(xmlFilePath, lastModifiedTime)) {
            NetworkConfig oldParser(xmlFilePath);
            this->hotStart(oldParser);
        }
        else {
            std::cout << "No changes in the configuration file. Hot start is not required." << std::endl;
        }
        break;
    }
}
 
bool NetworkConfig::setTCPConfig(const std::string & ipAddress, int portNumber)
{
    if (!loaded) return false;
    tinyxml2::XMLElement* tcpElement = doc.FirstChildElement("NetworkConfigurationInfo")
        ->FirstChildElement("Socket")->FirstChildElement("TCP");
 
    if (tcpElement) {
        tinyxml2::XMLElement* ipElement = tcpElement->FirstChildElement("IPAddress");
        if (ipElement) {
            ipElement->SetText(ipAddress.c_str());
        }
        tinyxml2::XMLElement* portElement = tcpElement->FirstChildElement("PortNumber");
        if (portElement) {
            portElement->SetText(portNumber);
        }
        return true;
    }
    return false;
}