#include "dbthread.h" #include "yt_unit_shm.h" extern YT_UNIT_SHM *ytShm; DBThread::DBThread(QObject *parent, QSqlDatabase *db, QSqlDatabase *db2, QSqlDatabase *db3) : QThread(parent) { Db = db; Db2 = db2; Db3 = db3; sqlList.clear(); sqlList2.clear(); sqlList3.clear(); } void DBThread::appendSQL(quint8 sqlid, QString sql) { if(sqlid==1) sqlList.append(sql); else if(sqlid==2) sqlList2.append(sql); else if(sqlid==3) sqlList3.append(sql); } void DBThread::run() { ytShm->dogTimeList.dogTime[DB_THREAD].IDX = DB_THREAD; ytShm->dogTimeList.dogTime[DB_THREAD].WAITSEC = 30; ytShm->dogTimeList.dogTime[DB_THREAD].ENABLED = 0x01; emit log(QString("[%1] db thread start\r\n") .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))); while(1) { ytShm->dogTimeList.dogTime[DB_THREAD].LASTFEED = QDateTime::currentDateTime().toTime_t(); for(int comm=0; comm<4; comm++) { if(ytShm->commList.comm[comm].UNSAVED==0x01) { appendSQL(1,QString("update `yt_t_comm` set `CommType`=%1, `CommPath`='%2', Enabled=%3 where Id=%4") .arg(ytShm->commList.comm[comm].CommType) .arg(QString(ytShm->commList.comm[comm].PATH)) .arg(ytShm->commList.comm[comm].ENABLED) .arg(ytShm->commList.comm[comm].IDX)); ytShm->commList.comm[comm].UNSAVED = 0x00; } if(ytShm->commStatusList.commStatus[comm].UNSAVED==0x01) { appendSQL(2,QString("update `yt_sta_comm` set `Status`=%1, Updatetime=%2, Sent=%3 where CommId=%4") .arg(ytShm->commStatusList.commStatus[comm].STATUS) .arg(ytShm->commStatusList.commStatus[comm].TIMESTAMP) .arg(ytShm->commStatusList.commStatus[comm].SENT?1:0) .arg(comm)); ytShm->commStatusList.commStatus[comm].UNSAVED = 0x00; } for(int addr=0; addr<256; addr++) { if(ytShm->sPointList.sPoint[comm][addr].UNSAVED==0x01) { appendSQL(1,QString("update `yt_t_point` set `Enabled`=%1, `Name`='%2', `PnoType`=%3, `PnoAlarm`=%4, `PnoValue`=%5, `PnoDly`=%6, `PnoAd`=%7, `Offset`=%8, `OffsetFlag`=%9, `PnoMax`=%10, `PnoMin`=%11, `AlarmH`=%12, `AlarmL`=%13, `SubType`=%14 where `CommId`=%15 and `BusAdd`=%16") .arg(ytShm->sPointList.sPoint[comm][addr].ENABLED) .arg(QString(ytShm->sPointList.sPoint[comm][addr].NAME)) .arg(ytShm->sPointList.sPoint[comm][addr].PNO_TYPE) .arg(ytShm->sPointList.sPoint[comm][addr].PNO_ALARM) .arg(ytShm->sPointList.sPoint[comm][addr].PNO_VALUE) .arg(ytShm->sPointList.sPoint[comm][addr].PNO_DLY) .arg(ytShm->sPointList.sPoint[comm][addr].PNO_AD) .arg(ytShm->sPointList.sPoint[comm][addr].OFFSET) .arg(ytShm->sPointList.sPoint[comm][addr].OFFSET_FLAG) .arg(ytShm->sPointList.sPoint[comm][addr].PNO_MAX) .arg(ytShm->sPointList.sPoint[comm][addr].PNO_MIN) .arg(ytShm->sPointList.sPoint[comm][addr].ALARM_H) .arg(ytShm->sPointList.sPoint[comm][addr].ALARM_L) .arg(ytShm->sPointList.sPoint[comm][addr].SUB_TYPE) .arg(ytShm->sPointList.sPoint[comm][addr].COMM_IDX) .arg(ytShm->sPointList.sPoint[comm][addr].BUS_ADD)); ytShm->sPointList.sPoint[comm][addr].UNSAVED = 0x00; } if(ytShm->spStatusList.spStatus[comm][addr].UNSAVED==0x01) { appendSQL(2,QString("update `yt_sta_pno` set `RealtimeValue`=%1, `RealtimeTimestamp`=%2, `AlarmStatus`=%3, `AlarmTimestamp`=%4, `AlarmSent`=%5, `MaxValue`=%6, `MaxTimestamp`=%7, `MinValue`=%8, `MinTimestamp`=%9, `TotalValue`=%10, `AveValue`=%11, `Count`=%12 where `CommId`=%13 and `BusAdd`=%14") .arg(ytShm->spStatusList.spStatus[comm][addr].REALTIME_VALUE) .arg(ytShm->spStatusList.spStatus[comm][addr].REALTIME_TIMESTAMP) .arg(ytShm->spStatusList.spStatus[comm][addr].ALARM_STATUS) .arg(ytShm->spStatusList.spStatus[comm][addr].ALARM_TIMESTAMP) .arg(ytShm->spStatusList.spStatus[comm][addr].ALARM_SENT) .arg(ytShm->spStatusList.spStatus[comm][addr].MAX_VALUE) .arg(ytShm->spStatusList.spStatus[comm][addr].MAX_TIMESTAMP) .arg(ytShm->spStatusList.spStatus[comm][addr].MIN_VALUE) .arg(ytShm->spStatusList.spStatus[comm][addr].MIN_TIMESTAMP) .arg(ytShm->spStatusList.spStatus[comm][addr].TOTAL_VALUE) .arg(ytShm->spStatusList.spStatus[comm][addr].AVE_VALUE) .arg(ytShm->spStatusList.spStatus[comm][addr].COUNT) .arg(comm).arg(addr)); ytShm->spStatusList.spStatus[comm][addr].UNSAVED = 0x00; } } } if(sqlList.length()>0) { if(Db->transaction()) { while(sqlList.length()>0) { emit dbg_log(QString("[%1] do sql: \"%2\"\r\n") .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")) .arg(sqlList.first())); Db->exec(sqlList.first()); sqlList.removeFirst(); usleep(10000); } Db->commit(); } } else if(sqlList2.length()>0) { if(Db2->transaction()) { while(sqlList2.length()>0) { emit dbg_log(QString("[%1] do sql: \"%2\"\r\n") .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")) .arg(sqlList2.first())); Db2->exec(sqlList2.first()); sqlList2.removeFirst(); usleep(10000); } Db2->commit(); } } else if(sqlList3.length()>0) { if(Db3->transaction()) { while(sqlList3.length()>0) { emit dbg_log(QString("[%1] do sql: \"%2\"\r\n") .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")) .arg(sqlList3.first())); printf("%s\n",sqlList3.first().toUtf8().data()); Db3->exec(sqlList3.first()); sqlList3.removeFirst(); usleep(10000); } Db3->commit(); } } usleep(500000); } }