#include "databasethread.h" //#define HostName "172.17.35.51" //#define HostName "124.71.140.20" //#define HostName "172.16.120.165" #define HostName "uskycloud000.mysql.rds.aliyuncs.com" #define HostPort 3306 #define UserName "usky_admin" //"usky" #define PassWord "Usky@2023!@#$_" //"Yt#75Usky" #define DatabaseName "usky-data" DatabaseThread::DatabaseThread(QObject *parent) : QThread(parent) { hour = 255; keep=false; sqlList.clear(); alarmList.clear(); db = QSqlDatabase::addDatabase("QMYSQL","write_db"); db.setHostName(QString(HostName)); db.setPort(HostPort); db.setUserName(QString(UserName)); db.setPassword(QString(PassWord)); db.setDatabaseName(QString(DatabaseName)); } void DatabaseThread::appendSql(QString sql) { sqlList.append(sql); } void DatabaseThread::appendAlarm(QString sql) { alarmList.append(sql); } void DatabaseThread::stop() { keep = false; } void DatabaseThread::run() { if(!db.open()){ emit dbdata_log(QString("[%1] writeProcess DatabaseThread db open failed").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz"))); }else{ QString ip,port,username,password; QSqlQuery mqttqry = db.exec(QString("select mqtt_ip,mqtt_port,username,passwd from yt_mqtt where item_name = 'data-write'")); while (mqttqry.next()) { ip = mqttqry.value(0).toString(); port = mqttqry.value(1).toString(); username = mqttqry.value(2).toString(); password = mqttqry.value(3).toString(); } mqttqry.clear(); emit dbdata_log(QString("mqttinfo %1 %2 %3 %4").arg(ip).arg(port).arg(username).arg(password)); emit mqtt_conf(ip,port,username,password); db.close(); } keep = true; while (keep) { if(!db.open()){ db.open(); }else{ if(sqlList.length()>0){ while (sqlList.length()>0) { db.exec(sqlList.first()); emit dbdata_log(QString("[%1] sqlList.length() %2 writeProcess sql %3").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz")).arg(sqlList.length()).arg(sqlList.first())); sqlList.removeFirst(); usleep(1000); } } if(alarmList.length()>0){ while (alarmList.length()>0) { db.exec(alarmList.first()); emit dbdata_log(QString("[%1] writeProcess alarm sql %2").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz")).arg(alarmList.first())); alarmList.removeFirst(); usleep(1000); } } } usleep(50000); } }