2025-08-20 23:06:28 +08:00
|
|
|
#include "SubThread.h"
|
2025-09-15 22:28:43 +08:00
|
|
|
#include <DataCenter/MySQL.h>
|
|
|
|
|
MySQL sql;
|
2025-08-20 23:06:28 +08:00
|
|
|
SubThread::SubThread()
|
|
|
|
|
{
|
|
|
|
|
isPause = false;
|
|
|
|
|
isLoop = true;
|
|
|
|
|
}
|
|
|
|
|
void SubThread::ReStart(){
|
|
|
|
|
isPause = false;
|
|
|
|
|
}
|
|
|
|
|
void SubThread::PauseThread()
|
|
|
|
|
{
|
|
|
|
|
isPause = true;
|
|
|
|
|
}
|
|
|
|
|
void SubThread::KillThread()
|
|
|
|
|
{
|
|
|
|
|
isLoop = false;
|
2025-09-15 22:28:43 +08:00
|
|
|
sql.close();
|
2025-08-20 23:06:28 +08:00
|
|
|
wait();
|
|
|
|
|
thread()->quit();
|
|
|
|
|
}
|
2025-09-15 22:28:43 +08:00
|
|
|
QStringList All_SqlTable;
|
|
|
|
|
QMap<QString, QStringList> Sql_TableAndField;
|
|
|
|
|
void createTablesOnce()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/* 建表 */
|
|
|
|
|
QMap<QString, QString> AlarmHistory;
|
|
|
|
|
AlarmHistory["故障代码"] = "TEXT";
|
|
|
|
|
AlarmHistory["报警内容"] = "TEXT";
|
|
|
|
|
AlarmHistory["排查方法"] = "TEXT";
|
|
|
|
|
sql.createTable("AlarmHistory", AlarmHistory);
|
|
|
|
|
|
|
|
|
|
for (uint16_t i = 0; i < gOPC_NodeList.length(); ++i) {
|
|
|
|
|
const QString &tbl = gOPC_SqlTable[gOPC_NodeList[i]];
|
|
|
|
|
if (!All_SqlTable.contains(tbl)) {
|
|
|
|
|
All_SqlTable.append(tbl);
|
|
|
|
|
}
|
|
|
|
|
Sql_TableAndField[tbl].append(gOPC_SqlField[gOPC_NodeList[i]]);
|
|
|
|
|
}
|
2025-08-20 23:06:28 +08:00
|
|
|
|
2025-09-15 22:28:43 +08:00
|
|
|
for (uint16_t i = 0; i < All_SqlTable.length(); ++i) {
|
|
|
|
|
QMap<QString, QString> otherFields;
|
|
|
|
|
for (const QString &field : Sql_TableAndField[All_SqlTable[i]]) {
|
|
|
|
|
otherFields[field] = "TEXT";
|
|
|
|
|
}
|
|
|
|
|
#if defined(Q_OS_LINUX)
|
|
|
|
|
sql.createTable(All_SqlTable[i], otherFields);
|
|
|
|
|
#elif defined(Q_OS_WIN)
|
|
|
|
|
//qDebug() << All_SqlTable[i] << otherFields;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
uint16_t OldAlarm = 0;
|
2025-08-20 23:06:28 +08:00
|
|
|
void SubThread::executeThreadLogic()
|
|
|
|
|
{
|
|
|
|
|
// 在这里实现线程的逻辑
|
|
|
|
|
|
2025-09-15 22:28:43 +08:00
|
|
|
if (!sql.open("127.0.0.1", 3306, "zmj", "Zmj@123...", "DataRecord"))
|
|
|
|
|
qDebug()<<"连接数据库失败!";
|
|
|
|
|
|
|
|
|
|
createTablesOnce();
|
2025-08-20 23:06:28 +08:00
|
|
|
while (isLoop)
|
|
|
|
|
{
|
|
|
|
|
QThread::msleep(CONFIG_EN_SUB_THREAD_TIME);//这个是子线程的基础时间,意在多长时间循环一次,单位毫秒
|
|
|
|
|
if(isPause) continue;
|
|
|
|
|
|
2025-09-15 22:28:43 +08:00
|
|
|
uint16_t NewAlarm = gOPC_NodeValue["ns=6;s=::AsGlobalPV:AlarmCode.New"] .toUInt();
|
|
|
|
|
uint8_t NewAlarm_Dev = NewAlarm / 256;
|
|
|
|
|
uint8_t NewAlarm_Ind = NewAlarm % 256;
|
|
|
|
|
if(NewAlarm != OldAlarm && OldAlarm != 0){
|
|
|
|
|
QString Hex = QString("%1").arg(NewAlarm, 4, 16, QLatin1Char('0')).toUpper();
|
|
|
|
|
QString Text1 = AlarmText[NewAlarm_Dev][NewAlarm_Ind].AlarmText;
|
|
|
|
|
QString Text2 = AlarmText[NewAlarm_Dev][NewAlarm_Ind].Troubleshoot;
|
|
|
|
|
#if defined(Q_OS_LINUX) // 判断操作系统是否为Linux
|
|
|
|
|
sql.insertPartialV("AlarmHistory", {"故障代码", "报警内容","排查方法"}, {Hex, Text1,Text2});
|
|
|
|
|
#elif defined(Q_OS_WIN) // 判断操作系统是否为Windows
|
|
|
|
|
qDebug()<<"写数据库 AlarmHistory" << Hex << Text1 << Text2;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OldAlarm = NewAlarm;
|
|
|
|
|
/* 插入示例 */
|
|
|
|
|
for(uint16_t i=0;i<All_SqlTable.length();i++)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
QMap<QString, QString> otherFields;
|
|
|
|
|
QStringList Field;
|
|
|
|
|
QStringList Value;
|
|
|
|
|
for(uint16_t j=0;j<Sql_TableAndField[All_SqlTable[i]].length();j++){
|
|
|
|
|
const QString &field = Sql_TableAndField[All_SqlTable[i]][j];
|
|
|
|
|
QString nodeId = QStringLiteral("ns=6;s=::AsGlobalPV:%1.%2")
|
|
|
|
|
.arg(All_SqlTable[i], field);
|
|
|
|
|
|
|
|
|
|
QString raw = gOPC_NodeValue[nodeId].toString();
|
|
|
|
|
|
|
|
|
|
Field.append(field);
|
|
|
|
|
Value.append(raw);
|
|
|
|
|
}
|
|
|
|
|
#if defined(Q_OS_LINUX) // 判断操作系统是否为Linux
|
|
|
|
|
sql.insertPartial(All_SqlTable[i], Field,Value);
|
|
|
|
|
#elif defined(Q_OS_WIN) // 判断操作系统是否为Windows
|
|
|
|
|
//qDebug()<<All_SqlTable[i]<<Field<<Value;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2025-08-20 23:06:28 +08:00
|
|
|
}
|
|
|
|
|
}
|
2025-09-15 22:28:43 +08:00
|
|
|
|
|
|
|
|
|
2025-08-20 23:06:28 +08:00
|
|
|
void SubThread::run()
|
|
|
|
|
{
|
|
|
|
|
executeThreadLogic();
|
|
|
|
|
}
|