#include "db_syncthread.h" #include "ytservicecore.h" #include #include #include //void chkmd5(const char *data, char *md5){ // MD5_CTX ctx; // unsigned char tmp,md[16]; // int i; // MD5_Init(&ctx); // MD5_Update(&ctx,data,strlen(data)); // MD5_Final(md,&ctx); // for(i=0;i<16;i++){ // tmp = (md[i]>>4)&0x0f; // if(tmp<10) // md5[i*2+0] = (char)(tmp+0x30); // else // md5[i*2+0] = (char)(tmp+0x37); // tmp = md[i]&0x0f; // if(tmp<10) // md5[i*2+1] = (char)(tmp+0x30); // else // md5[i*2+1] = (char)(tmp+0x37); // } // md5[32] = 0x00; //} bool compareAlarmSet(const AlarmSet &a1, const AlarmSet &a2) { if(a1.AlarmTime>a2.AlarmTime) return true; return false; } DB_SyncThread::DB_SyncThread(QObject *parent) : QThread(parent) { isWorking = false; deviceDataList.clear(); day = 255; initTime = true; // logThread = new LogThread(this); // logThread->start(); ProgramConf *conf = new ProgramConf(this); mdb = QSqlDatabase::addDatabase(conf->getDbDrive(),conf->getDbConnectName()); mdb.setDatabaseName(conf->getDbName()); mdb.setHostName(conf->getDbHost()); mdb.setPort(conf->getDbPort()); mdb.setUserName(conf->getDbUser()); mdb.setPassword(conf->getDbPassword()); mdbOpened = mdb.open(); } bool DB_SyncThread::userInfoSync(QString devid,QString deviceCode, uint sync_id,QString tbname,QDate chkday,uint ID, QString companyCode) { QString qrysql,sql; QString upsql_0="select 1;",upsql_1="select 1;"; bool found = false; bool updated = false; int count=0; QSqlQuery qry2 = mdb.exec(QString("select count(*) as count, sum(clzt) as sum from sp_hj2017 where device_code='%1' and data1<>'U44' and time0?qry2.value(1).toInt():0) .arg(devid)); } qry2.clear(); qry2 = mdb.exec(QString("select count(*) as count, sum(clzt) as sum from sp_hj2017 where device_code='%1' and data1='U44' and time0?qry2.value(1).toInt():0) .arg(devid)); } qry2.clear(); for(int i=0;i %2 and time>'%3-%4-%5 00:00:00' order by id").arg(tbname).arg(sync_id).arg(chkday.year(),4,10,QChar('0')).arg(chkday.month(),2,10,QChar('0')).arg(chkday.day(),2,10,QChar('0')); qry2 = mdb.exec(qrysql); while(qry2.next()){ setCompanyAlarmSet(deviceCode, AlarmSet(0xffffffff,"",QString::fromUtf8("%1,%2").arg(qry2.value(4).toString()).arg(qry2.value(5).toString()),1,qry2.value(3).toDateTime().toTime_t(),1)); updated = true; if(count==0) sql = QString("insert into sp_owner_sync_data (id, native_id, device_id, point_code, point_data, data_time, content) values (NULL,%1,'%2','%3','%4','%5','%6 %7')") .arg(qry2.value(0).toUInt()).arg(devid).arg(qry2.value(1).isNull()?"0":"1") .arg(qry2.value(2).isNull()?"":qry2.value(2).toString()).arg(qry2.value(3).isNull()?"1970-01-01 08:00:00":qry2.value(3).toDateTime().toString("yyyy-MM-dd HH:mm:ss")) .arg(qry2.value(4).isNull()?"":qry2.value(4).toString()).arg(qry2.value(5).isNull()?"":qry2.value(5).toString()); else sql += QString(", (NULL,%1,'%2','%3','%4','%5','%6 %7')") .arg(qry2.value(0).toUInt()).arg(devid).arg(qry2.value(1).isNull()?"0":"1") .arg(qry2.value(2).isNull()?"":qry2.value(2).toString()).arg(qry2.value(3).isNull()?"1970-01-01 08:00:00":qry2.value(3).toDateTime().toString("yyyy-MM-dd HH:mm:ss")) .arg(qry2.value(4).isNull()?"":qry2.value(4).toString()).arg(qry2.value(5).isNull()?"":qry2.value(5).toString()); if(qry2.value(1).isNull()) upsql_0 = QString::fromUtf8("update sp_owner_status set point_data='%1', data_time='%2', content='%3 %4', point_name='通信报警',dwtype=%5 where device_id='%6' and point_code='%7';") .arg(qry2.value(2).isNull()?"":qry2.value(2).toString()).arg(qry2.value(3).isNull()?"1970-01-01 08:00:00":qry2.value(3).toDateTime().toString("yyyy-MM-dd HH:mm:ss")) .arg(qry2.value(4).isNull()?"":qry2.value(4).toString()).arg(qry2.value(5).isNull()?"":qry2.value(5).toString()) .arg(1).arg(devid).arg("0"); else upsql_1 = QString::fromUtf8("update sp_owner_status set point_data='%1', data_time='%2', content='%3 %4', point_name='主机报警',dwtype=%5 where device_id='%6' and point_code!='0';") .arg(qry2.value(2).isNull()?"":qry2.value(2).toString()).arg(qry2.value(3).isNull()?"1970-01-01 08:00:00":qry2.value(3).toDateTime().toString("yyyy-MM-dd HH:mm:ss")) .arg(qry2.value(4).isNull()?"":qry2.value(4).toString()).arg(qry2.value(5).isNull()?"":qry2.value(5).toString()) .arg(1).arg(devid); sync_id = qry2.value(0).toUInt(); count++; if(count>=100){ time_t tmp_time = time(static_cast(Q_NULLPTR)); ytShm->updatetime[DB_THREAD] = tmp_time; sql.append(";"); mdb.exec(sql); mdb.exec(QString("update sp_owner_sync set sync_id=%1 where id=%2").arg(sync_id).arg(ID)); mdb.exec(upsql_0); mdb.exec(upsql_1); sql = ""; count=0; } } if(count>0){ time_t tmp_time = time(static_cast(Q_NULLPTR)); ytShm->updatetime[DB_THREAD] = tmp_time; sql.append(";"); mdb.exec(sql); mdb.exec(QString("update sp_owner_sync set sync_id=%1 where id=%2").arg(sync_id).arg(ID)); mdb.exec(upsql_0); mdb.exec(upsql_1); sql = ""; count=0; } return updated; } bool DB_SyncThread::waterInfoSync(QString devid, QString deviceCode, uint sync_id, QString tbname, QDate chkday, uint ID,int dwtype, QString companyCode) { QString qrysql,sql,upsql; int count=0; bool updated = false; bool found = false; QSqlQuery qry2 = mdb.exec(QString("select count(*) as count, sum(clzt) as sum from sp_sj2017 where device_code='%1' and data1<>'WP4' and data1<>'LL4' and time0?qry2.value(1).toInt():0) .arg(devid)); } qry2.clear(); qry2 = mdb.exec(QString("select count(*) as count, sum(clzt) as sum from sp_sj2017 where device_code='%1' and ( data1='WP4' or data1='LL4' ) and time0?qry2.value(1).toInt():0) .arg(devid)); } qry2.clear(); for(int i=0;i%2 and time>'%3-%4-01 00:00:00' and time=100){ time_t tmp_time = time(static_cast(Q_NULLPTR)); ytShm->updatetime[DB_THREAD] = tmp_time; sql.append(";"); mdb.exec(sql); mdb.exec(QString("update sp_owner_sync set sync_id=%1 where id=%2").arg(sync_id).arg(ID)); mdb.exec(upsql); count=0; sql=""; } } if(count>0){ time_t tmp_time = time(static_cast(Q_NULLPTR)); ytShm->updatetime[DB_THREAD] = tmp_time; sql.append(";"); mdb.exec(sql); mdb.exec(QString("update sp_owner_sync set sync_id=%1 where id=%2").arg(sync_id).arg(ID)); mdb.exec(upsql); count=0; sql=""; } return updated; } bool DB_SyncThread::manholeCoverSync(QString devid, QString deviceCode, uint sync_id, QString tbname, QDate chkday, uint ID,int dwtype, QString companyCode) { QString qrysql,sql,upsql; int count=0; bool updated = false; bool found = false; QSqlQuery qry2 = mdb.exec(QString("select count(*) as count, sum(clzt) as sum from sp_iw2017 where device_code='%1' and time0?qry2.value(1).toInt():0) .arg(devid)); } qry2.clear(); // qry2 = mdb.exec(QString("select count(*) as count, sum(clzt) as sum from sp_sj2017 where device_code='%1' and ( data1='WP4' or data1='LL4' ) and time0?qry2.value(1).toInt():0) // .arg(devid)); // } // qry2.clear(); for(int i=0;i%2 and addtime>'%3-%4-01 00:00:00' and addtime=100){ time_t tmp_time = time(static_cast(Q_NULLPTR)); ytShm->updatetime[DB_THREAD] = tmp_time; sql.append(";"); mdb.exec(sql); mdb.exec(QString("update sp_owner_sync set sync_id=%1 where id=%2").arg(sync_id).arg(ID)); mdb.exec(upsql); count=0; sql=""; } } if(count>0){ time_t tmp_time = time(static_cast(Q_NULLPTR)); ytShm->updatetime[DB_THREAD] = tmp_time; sql.append(";"); mdb.exec(sql); mdb.exec(QString("update sp_owner_sync set sync_id=%1 where id=%2").arg(sync_id).arg(ID)); mdb.exec(upsql); count=0; sql=""; } return updated; } bool DB_SyncThread::smokerInfoSync(QString devid, QString deviceCode, uint sync_id, QString tbname, QDate chkday, uint ID, QString companyCode) { QString qrysql,sql,upsql; int count=0; bool found = false; bool updated = false; for(int i=0;i'U44' and time0?qry2.value(1).toInt():0) .arg(devid)); } qry2.clear(); qrysql = QString("select id, DeviceStatus, BatteryLevel, SensorLevel, storeTime,TData from %1 where id>%2 and storeTime>'%3-%4-01 00:00:00' and storeTime=100){ time_t tmp_time = time(static_cast(Q_NULLPTR)); ytShm->updatetime[DB_THREAD] = tmp_time; sql.append(";"); mdb.exec(sql); mdb.exec(QString("update sp_owner_sync set sync_id=%1 where id=%2").arg(sync_id).arg(ID)); mdb.exec(upsql); count=0; sql=""; } } if(count>0){ time_t tmp_time = time(static_cast(Q_NULLPTR)); ytShm->updatetime[DB_THREAD] = tmp_time; sql.append(";"); mdb.exec(sql); mdb.exec(QString("update sp_owner_sync set sync_id=%1 where id=%2").arg(sync_id).arg(ID)); mdb.exec(upsql); count=0; sql=""; } return updated; } bool DB_SyncThread::rtuInfoSync(QString devid, QString deviceCode, uint sync_id, QString tbname, QDate chkday, uint ID, QString companyCode) { QString qrysql,sql,upsql=""; int count=0; bool found = false; bool updated= false; QSqlQuery qry2 = mdb.exec(QString("select count(*) as count, sum(clzt) as sum from sp_rtu2017 where device_code='%1' and status<>'44' and time0?qry2.value(1).toInt():0) .arg(devid)); } qry2.clear(); qry2 = mdb.exec(QString("select count(*) as count, sum(clzt) as sum from sp_rtu2017 where device_code='%1' and status='44' and time0?qry2.value(1).toInt():0) .arg(devid)); } qry2.clear(); found = false; for(int i=0;i%2 and time>'%3-%4-%5 00:00:00' and time typeList,portList; while(qry3.next()){ typeList.append(qry3.value(0).toInt()); portList.append(qry3.value(1).toInt()); nameList.append(qry3.value(2).toString()); vonameList.append(qry3.value(3).toString()); found = false; if(qry3.value(0).toInt()==1){ for(int i=0;i0){ // QString pv = "0"; // QString pname = "", vname=""; // for(int Idx=0;Idx0){ // QString pname = ""; // for(int Idx=0;Idx=100){ time_t tmp_time = time(static_cast(Q_NULLPTR)); ytShm->updatetime[DB_THREAD] = tmp_time; // sql.append(";"); // mdb.exec(sql); mdb.exec(QString("update sp_owner_sync set sync_id=%1 where id=%2").arg(sync_id).arg(ID)); // mdb.exec(upsql); count=0; // sql=""; // upsql=""; } } if(count>0){ time_t tmp_time = time(static_cast(Q_NULLPTR)); ytShm->updatetime[DB_THREAD] = tmp_time; // sql.append(";"); // mdb.exec(sql); mdb.exec(QString("update sp_owner_sync set sync_id=%1 where id=%2").arg(sync_id).arg(ID)); // mdb.exec(upsql); count=0; // sql=""; // upsql=""; } return updated; } bool DB_SyncThread::videoalarmSync(QString devid, QString deviceCode, uint sync_id, QString tbname, QDate chkday, uint ID, QString companyCode) { QString sql,upsql; int count=0; bool updated = false; QString csql = QString("select count(*) as count, sum(clzt) as sum from sp_video2017 where device_code='%1' and time0?qry2.value(1).toInt():0) .arg(devid); // printf("%s\n",csql.toUtf8().data()); mdb.exec(csql); } qry2.clear(); qry2 =mdb.exec(QString("select count(*) from sp_owner_status where device_id='%1'").arg(devid)); if(qry2.next()){ if(qry2.value(0).toInt()==0){ mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',16,'占道侦测告警',1,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',16,'火点侦测告警',2,0,'1970-01-01 08:00:00','')").arg(devid)); } } qry2.clear(); // printf("%s\n",QString("select id,port,time from sp_video2017 where id>%1 and time>'%2-%3-%4 00:00:00' and device_code='%5'").arg(sync_id).arg(chkday.year(),4,10,QChar('0')).arg(chkday.month(),2,10,QChar('0')).arg(chkday.day(),2,10,QChar('0')).arg(devid).toUtf8().data()); qry2 = mdb.exec(QString("select id,port,time,data4 from sp_video2017 where id>%1 and time>'%2-%3-%4 00:00:00' and time> /root/data.txt").arg(deviceCode).arg(qry2.value(2).toString()).toUtf8().data()); setCompanyAlarmSet(deviceCode,AlarmSet(0xffffffff,"",qry2.value(3).toString(),16,qry2.value(2).toDateTime().toTime_t(),1)); if(count==0){ if(qry2.value(1).toInt()==1){ sql = QString("insert into sp_owner_sync_data (id, native_id, device_id, point_code, point_data, data_time, content) values (NULL,%1,'%2','%3','%4','%5',''),(NULL,%6,'%7','%8','%9','%10','')") .arg(qry2.value(0).toInt()).arg(devid).arg(1).arg(1).arg(qry2.value(2).isNull()?"1970-01-01 08:00:00":qry2.value(2).toDateTime().toString("yyyy-MM-dd HH:mm:ss")) .arg(qry2.value(0).toInt()).arg(devid).arg(2).arg(0).arg(qry2.value(2).isNull()?"1970-01-01 08:00:00":qry2.value(2).toDateTime().toString("yyyy-MM-dd HH:mm:ss")); }else{ sql = QString("insert into sp_owner_sync_data (id, native_id, device_id, point_code, point_data, data_time, content) values (NULL,%1,'%2','%3','%4','%5',''),(NULL,%6,'%7','%8','%9','%10','')") .arg(qry2.value(0).toInt()).arg(devid).arg(1).arg(0).arg(qry2.value(2).isNull()?"1970-01-01 08:00:00":qry2.value(2).toDateTime().toString("yyyy-MM-dd HH:mm:ss")) .arg(qry2.value(0).toInt()).arg(devid).arg(2).arg(1).arg(qry2.value(2).isNull()?"1970-01-01 08:00:00":qry2.value(2).toDateTime().toString("yyyy-MM-dd HH:mm:ss")); } }else{ if(qry2.value(1).toInt()==1) sql += QString(", (NULL,%1,'%2','%3','%4','%5',''),(NULL,%6,'%7','%8','%9','%10','')") .arg(qry2.value(0).toInt()).arg(devid).arg(1).arg(1).arg(qry2.value(2).isNull()?"1970-01-01 08:00:00":qry2.value(2).toDateTime().toString("yyyy-MM-dd HH:mm:ss")) .arg(qry2.value(0).toInt()).arg(devid).arg(2).arg(0).arg(qry2.value(2).isNull()?"1970-01-01 08:00:00":qry2.value(2).toDateTime().toString("yyyy-MM-dd HH:mm:ss")); else sql += QString(", (NULL,%1,'%2','%3','%4','%5',''),(NULL,%6,'%7','%8','%9','%10','')") .arg(qry2.value(0).toInt()).arg(devid).arg(1).arg(0).arg(qry2.value(2).isNull()?"1970-01-01 08:00:00":qry2.value(2).toDateTime().toString("yyyy-MM-dd HH:mm:ss")) .arg(qry2.value(0).toInt()).arg(devid).arg(2).arg(1).arg(qry2.value(2).isNull()?"1970-01-01 08:00:00":qry2.value(2).toDateTime().toString("yyyy-MM-dd HH:mm:ss")); } if(qry2.value(1).toInt()==1) upsql = QString::fromUtf8("update sp_owner_status set point_data=%1, data_time='%2',point_name='占道侦测告警' where device_id='%3' and point_code=1;") .arg(1).arg(qry2.value(2).isNull()?"1970-01-01 08:00:00":qry2.value(2).toDateTime().toString("yyyy-MM-dd HH:mm:ss")).arg(devid); else upsql = QString::fromUtf8("update sp_owner_status set point_data=%1, data_time='%2',point_name='火点侦测告警' where device_id='%3' and point_code=2;") .arg(1).arg(qry2.value(2).isNull()?"1970-01-01 08:00:00":qry2.value(2).toDateTime().toString("yyyy-MM-dd HH:mm:ss")).arg(devid); sync_id = qry2.value(0).toUInt(); count++; if(count>=100){ time_t tmp_time = time(static_cast(Q_NULLPTR)); ytShm->updatetime[DB_THREAD] = tmp_time; sql.append(";"); // printf("%s\n%s",sql.toUtf8().data(),upsql.toUtf8().data()); mdb.exec(sql); mdb.exec(upsql); mdb.exec(QString("update sp_owner_sync set sync_id=%1 where id=%2").arg(sync_id).arg(ID)); count = 0; sql=""; upsql=""; } } if(count>0){ time_t tmp_time = time(static_cast(Q_NULLPTR)); ytShm->updatetime[DB_THREAD] = tmp_time; sql.append(";"); // printf("%s\n%s",sql.toUtf8().data(),upsql.toUtf8().data()); mdb.exec(sql); mdb.exec(upsql); mdb.exec(QString("update sp_owner_sync set sync_id=%1 where id=%2").arg(sync_id).arg(ID)); count = 0; sql=""; upsql=""; } return updated; } bool DB_SyncThread::efireInfoSync(QString devid, QString deviceCode, uint sync_id, QString tbname, QDate chkday, uint ID, QString companyCode, QString owner_xh) { QString upsql1, upsql2, upsql3, upsql4,upsql5,upsql6,sql; int count=0; bool updated = false; QSqlQuery qry2 = mdb.exec(QString("select count(*) as count, sum(clzt) as sum from sp_ef2017 where device_code='%1' and data1='EF9' and time0?qry2.value(1).toInt():0) .arg(devid)); } qry2.clear(); qry2 = mdb.exec(QString("select count(*) as count, sum(clzt) as sum from sp_ef2017 where device_code='%1' and data1<>'EF9' and time0?qry2.value(1).toInt():0) .arg(devid)); } qry2.clear(); qry2 =mdb.exec(QString("select count(*) from sp_owner_status where device_id='%1'").arg(devid)); if(qry2.next()){ if(owner_xh.compare("ARCM300D")==0){ mdb.exec(QString::fromUtf8("delete from sp_owner_status where device_id='%1'").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'供电过压',1,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'供电低压',2,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'供电过流',3,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'电压',65,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'电流',68,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'电能',71,0,'1970-01-01 08:00:00','')").arg(devid)); }else{ if(qry2.value(0).toInt()==0){ mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'供电过压',1,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'供电低压',2,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'供电过流',3,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'漏电报警',4,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'A相线缆温度超高',5,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'B相线缆温度超高',6,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'C相线缆温度超高',7,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'A相电压',65,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'B相电压',66,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'C相电压',67,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'A相电流',68,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'B相电流',69,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'C相电流',70,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'A相线缆温度',71,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'B相线缆温度',72,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'C相线缆温度',73,0,'1970-01-01 08:00:00','')").arg(devid)); }else if(qry2.value(0).toInt()==6){ mdb.exec(QString::fromUtf8("delete from sp_owner_status where device_id='%1'").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'供电过压',1,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'供电低压',2,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'供电过流',3,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'漏电报警',4,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'A相线缆温度超高',5,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'B相线缆温度超高',6,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'C相线缆温度超高',7,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'A相电压',65,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'B相电压',66,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'C相电压',67,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'A相电流',68,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'B相电流',69,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'C相电流',70,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'A相线缆温度',71,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'B相线缆温度',72,0,'1970-01-01 08:00:00','')").arg(devid)); mdb.exec(QString::fromUtf8("insert into sp_owner_status (id, device_id, dwtype, point_name, point_code, point_data, data_time, content) values (NULL,'%1',7,'C相线缆温度',73,0,'1970-01-01 08:00:00','')").arg(devid)); } } } qry2.clear(); qry2 = mdb.exec(QString("select id from %1 where id>%2 and data_time>'%3-%4-%5 00:00:00' and data_time=100){ // time_t tmp_time = time(static_cast(Q_NULLPTR)); // ytShm->updatetime[DB_THREAD] = tmp_time; // sql.append(";"); // mdb.exec(sql); // mdb.exec(QString("update sp_owner_sync set sync_id=%1 where id=%2").arg(sync_id).arg(ID)); // mdb.exec(upsql1); // mdb.exec(upsql2); // mdb.exec(upsql3); // mdb.exec(upsql4); // mdb.exec(upsql5); // mdb.exec(upsql6); // count=0; // sql=""; // upsql1=""; // upsql2=""; // upsql3=""; // upsql4=""; // upsql5=""; // upsql6=""; // } } // if(count>0){ // time_t tmp_time = time(static_cast(nullptr)); // ytShm->updatetime[DB_THREAD] = tmp_time; // sql.append(";"); // mdb.exec(sql); mdb.exec(QString("update sp_owner_sync set sync_id=%1 where id=%2").arg(sync_id).arg(ID)); // mdb.exec(upsql1); // mdb.exec(upsql2); // mdb.exec(upsql3); // mdb.exec(upsql4); // mdb.exec(upsql5); // mdb.exec(upsql6); // count=0; // sql=""; // upsql1=""; // upsql2=""; // upsql3=""; // upsql4=""; // upsql5=""; // upsql6=""; // } return updated; } void DB_SyncThread::appendDevDataList(QString deviceid) { deviceDataList.append(deviceid); } void DB_SyncThread::chkInList(QString devid) { QSqlQuery qry; QString sql; bool found = false; for(int i=0;i> /root/test.log").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).toUtf8().data()); // system(QString("echo \"[YTFC] dev:%1 obj:%2 start:%3 rec:%4\">> /root/test.log").arg(syncDevList.at(i).OwnerCode).arg(syncDevList.at(i).ObjCode).arg(syncDevList.at(i).StartPort).arg(syncDevList.at(i).RecCur).toUtf8().data()); QDateTime dataTime = QDateTime::fromTime_t(0); QString PowerAlarm="off"; QString ManualAct = "off"; QString DevWorking01 = "off"; QString DevWorking02 = "off"; QString DevAlarm01 = "off"; QString DevAlarm02 = "off"; QString FireAutoAct = "off"; sql = QString("select dwtype,point_code,point_data,data_time from sp_owner_status where device_id='%1'").arg(devid); // system(QString("echo \"[YTFC] %1\" >> /root/test.log").arg(sql).toUtf8().data()); // qDebug()<0)&&(code<8)){ int value = qry.value(2).toInt(); if(dataTime.toTime_t()syncDevList.at(i).RecCur) { mdb.exec(QString("update sp_yangpu_share set rec_cur=%1 where object_code='%2'").arg(dataTime.toTime_t()).arg(syncDevList.at(i).ObjCode)); QString jsonstr = QString("{\"dataCode\":\"DATA_FIRECONT\",\"deviceId\":\"%1\",\"postTime\":\"%2\",\"data\":{\"PowerAlarm\":\"%3\",\"ManualAct\":\"%4\",\"DevWorking01\":\"%5\",\"DevWorking02\":\"%6\",\"DevAlarm01\":\"%7\",\"DevAlarm02\":\"%8\",\"FireAutoAct\":\"%9\"}}") .arg(syncDevList.at(i).ObjCode).arg(dataTime.toString("yyyy-MM-dd HH:mm:ss")).arg(PowerAlarm).arg(ManualAct).arg(DevWorking01).arg(DevWorking02).arg(DevAlarm01).arg(DevAlarm02).arg(FireAutoAct); // system(QString("echo \"[YTFC] %1\" >> /root/test.log").arg(jsonstr).toUtf8().data()); emit SyncData(QUuid::createUuid().toString().replace("{","").replace("}",""), jsonstr); } }else if(syncDevList.at(i).ObjType.compare("YTCI")==0){ found = true; // system(QString("echo \"%1\" >> /root/test.log").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).toUtf8().data()); // system(QString("echo \"[YTCI] dev:%1 obj:%2 start:%3 rec:%4\">> /root/test.log").arg(syncDevList.at(i).OwnerCode).arg(syncDevList.at(i).ObjCode).arg(syncDevList.at(i).StartPort).arg(syncDevList.at(i).RecCur).toUtf8().data()); QString Temperature = "0.0"; QString Humidity = "0.0"; QDateTime dataTime = QDateTime::fromTime_t(0); sql = QString("select dwtype,point_code,point_data,data_time from sp_owner_status where device_id='%1' and point_code>%2 order by point_code limit 0,2").arg(devid).arg((syncDevList.at(i).StartPort>0)?(syncDevList.at(i).StartPort+63):0); // system(QString("echo \"[YTCI]%1\" >> /root/test.log").arg(sql).toUtf8().data()); // qDebug()<syncDevList.at(i).RecCur) { mdb.exec(QString("update sp_yangpu_share set rec_cur=%1 where object_code='%2'").arg(dataTime.toTime_t()).arg(syncDevList.at(i).ObjCode)); QString jsonstr = QString("{\"dataCode\":\"DATA_ENVIRONM\",\"deviceId\":\"%1\",\"postTime\":\"%2\",\"data\":{\"Temperature\":%3,\"Humidity\":%4}}") .arg(syncDevList.at(i).ObjCode).arg(dataTime.toString("yyyy-MM-dd HH:mm:ss")).arg(Temperature).arg(Humidity); // system(QString("echo \"[YTCI] %1\" >> /root/test.log").arg(jsonstr).toUtf8().data()); emit SyncData(QUuid::createUuid().toString().replace("{","").replace("}",""), jsonstr); } }else if(syncDevList.at(i).ObjType.compare("YTWP")==0){ found = true; // system(QString("echo \"%1\" >> /root/test.log").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).toUtf8().data()); // system(QString("echo \"[YTWP] dev:%1 obj:%2 start:%3 rec:%4\">> /root/test.log").arg(syncDevList.at(i).OwnerCode).arg(syncDevList.at(i).ObjCode).arg(syncDevList.at(i).StartPort).arg(syncDevList.at(i).RecCur).toUtf8().data()); QString WaterPressure = "0.0"; QDateTime dataTime = QDateTime::fromTime_t(0); sql = QString("select dwtype,point_code,point_data,data_time from sp_owner_status where device_id='%1' and point_code>%2 order by point_code limit 0,1").arg(devid).arg((syncDevList.at(i).StartPort>0)?(syncDevList.at(i).StartPort+63):3); // system(QString("echo \"[YTWP]%1\" >> /root/test.log").arg(sql).toUtf8().data()); // qDebug()<syncDevList.at(i).RecCur) { mdb.exec(QString("update sp_yangpu_share set rec_cur=%1 where object_code='%2'").arg(dataTime.toTime_t()).arg(syncDevList.at(i).ObjCode)); QString jsonstr = QString("{\"dataCode\":\"DATA_WATERPRE\",\"deviceId\":\"%1\",\"postTime\":\"%2\",\"data\":{\"WaterPressure\":%3}}") .arg(syncDevList.at(i).ObjCode).arg(dataTime.toString("yyyy-MM-dd HH:mm:ss")).arg(WaterPressure); // system(QString("echo \"[YTWP] %1\" >> /root/test.log").arg(jsonstr).toUtf8().data()); emit SyncData(QUuid::createUuid().toString().replace("{","").replace("}",""), jsonstr); } } }else if(syncDevList.at(i).ObjType.compare("YTLL")==0){ found = true; // system(QString("echo \"%1\" >> /root/test.log").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).toUtf8().data()); // system(QString("echo \"[YTLL] dev:%1 obj:%2 start:%3 rec:%4\">> /root/test.log").arg(syncDevList.at(i).OwnerCode).arg(syncDevList.at(i).ObjCode).arg(syncDevList.at(i).StartPort).arg(syncDevList.at(i).RecCur).toUtf8().data()); QString LiquidLevel = "0.0"; QDateTime dataTime = QDateTime::fromTime_t(0); sql = QString("select dwtype,point_code,point_data,data_time from sp_owner_status where device_id='%1' and point_code>%2 order by point_code limit 0,1").arg(devid).arg((syncDevList.at(i).StartPort>0)?(syncDevList.at(i).StartPort+63):3); // system(QString("echo \"[YTLL]%1\" >> /root/test.log").arg(sql).toUtf8().data()); // qDebug()<syncDevList.at(i).RecCur) { mdb.exec(QString("update sp_yangpu_share set rec_cur=%1 where object_code='%2'").arg(dataTime.toTime_t()).arg(syncDevList.at(i).ObjCode)); QString jsonstr = QString("{\"dataCode\":\"DATA_LIQUIDLE\",\"deviceId\":\"%1\",\"postTime\":\"%2\",\"data\":{\"LiquidLevel\":%3}}") .arg(syncDevList.at(i).ObjCode).arg(dataTime.toString("yyyy-MM-dd HH:mm:ss")).arg(LiquidLevel); // system(QString("echo \"[YTLL] %1\" >> /root/test.log").arg(jsonstr).toUtf8().data()); emit SyncData(QUuid::createUuid().toString().replace("{","").replace("}",""), jsonstr); } } }else if(syncDevList.at(i).ObjType.compare("YTEF")==0){ found = true; // system(QString("echo \"%1\" >> /root/test.log").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).toUtf8().data()); // system(QString("echo \"[YTEF] dev:%1 obj:%2 start:%3 rec:%4\">> /root/test.log").arg(syncDevList.at(i).OwnerCode).arg(syncDevList.at(i).ObjCode).arg(syncDevList.at(i).StartPort).arg(syncDevList.at(i).RecCur).toUtf8().data()); QString HighVoltageAlarm ="off"; QString LowVoltageAlarm = "off"; QString HighCurrentAlarm = "off"; QString ElectricityLeakage = "off"; QString HighTemperatureA = "off"; QString HighTemperatureB = "off"; QString HighTemperatureC = "off"; QString VoltageA = "0.0"; QString VoltageB = "0.0"; QString VoltageC = "0.0"; QString CurrentA = "0.0"; QString CurrentB = "0.0"; QString CurrentC = "0.0"; QString TemperatureA = "0.0"; QString TemperatureB = "0.0"; QString TemperatureC = "0.0"; QDateTime dataTime = QDateTime::fromTime_t(0); sql =QString("select dwtype,point_code,point_data,data_time from sp_owner_status where device_id='%1'").arg(devid); // system(QString("echo \"[YTEF]%1\" >> /root/test.log").arg(sql).toUtf8().data()); // qDebug()<syncDevList.at(i).RecCur) { mdb.exec(QString("update sp_yangpu_share set rec_cur=%1 where object_code='%2'").arg(dataTime.toTime_t()).arg(syncDevList.at(i).ObjCode)); QString jsonstr = QString("{\"dataCode\":\"DATA_ElECTRIC\",\"deviceId\":\"%1\",\"postTime\":\"%2\",\"data\":{\"HighVoltageAlarm\":\"%3\",\"LowVoltageAlarm\":\"%4\",\"HighCurrentAlarm\":\"%5\",\"ElectricityLeakage\":\"%6\",\"HighTemperatureA\":\"%7\",\"HighTemperatureB\":\"%8\",\"HighTemperatureC\":\"%9\",\"VoltageA\":%10,\"VoltageB\":%11,\"VoltageC\":%12,\"CurrentA\":%13,\"CurrentB\":%14,\"CurrentC\":%15,\"TemperatureA\":%16,\"TemperatureB\":%17,\"TemperatureC\":%18}}") .arg(syncDevList.at(i).ObjCode).arg(dataTime.toString("yyyy-MM-dd HH:mm:ss")).arg(HighVoltageAlarm).arg(LowVoltageAlarm).arg(HighCurrentAlarm).arg(ElectricityLeakage).arg(HighTemperatureA).arg(HighTemperatureB).arg(HighTemperatureC).arg(VoltageA).arg(VoltageB).arg(VoltageC).arg(CurrentA).arg(CurrentB).arg(CurrentC).arg(TemperatureA).arg(TemperatureB).arg(TemperatureC); // system(QString("echo \"[YTEF] %1\" >> /root/test.log").arg(jsonstr).toUtf8().data()); emit SyncData(QUuid::createUuid().toString().replace("{","").replace("}",""), jsonstr); } }else if(syncDevList.at(i).ObjType.compare("YTVA")==0){ found = true; QString VideoAlarm01 = "off"; QString VideoAlarm02 = "off"; QDateTime dataTime = QDateTime::fromTime_t(0); sql =QString("select dwtype,point_code,point_data,data_time from sp_owner_status where device_id='%1'").arg(devid); // system(QString("echo \"%1\" >> /root/test.log").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).toUtf8().data()); qry = mdb.exec(sql); while(qry.next()){ if(dataTime.toTime_t()> /root/test.log").arg(jsonstr).toUtf8().data()); emit SyncData(QUuid::createUuid().toString().replace("{","").replace("}",""), jsonstr); } } } // if(!found){ // for(int i=0;i0){ // QList pnoList = getPnoValue(cnDevList.at(i).StartPort); // for(int i=0;i0?pnoList.at(i).Default:"0")); // else // jdata.append(QString(",\"%1\":\"%2\"").arg(pnoList.at(i).PName).arg(pnoList.at(i).Default)); // } // } // while(qry.next()){ // uint cTime = qry.value(2).toDateTime().toTime_t(); // if(cTime>MaxT) // MaxT = cTime; // for(int i=0;i=0) // jdata.append(QString(",\"%1\":%2").arg(pnoList.at(i).PName).arg(qry.value(1).toInt())); // else // jdata.append(QString(",\"%1\":%2").arg(pnoList.at(i).PName).arg(qry.value(1).toString())); // break; // } // } // } // }else{ // while(qry.next()){ // uint cTime = qry.value(2).toDateTime().toTime_t(); // if(cTime>MaxT) // MaxT = cTime; // if(cndev.ObjType.compare("YTWP")==0||cndev.ObjType.compare("YTLL")==0){ // if(qry.value(0).toInt()==1) // jdata.append(QString(",\"devStatus\":%1").arg(qry.value(1).toInt())); // else if(qry.value(0).toInt()==2) // jdata.append(QString(",\"power\":%1").arg(qry.value(1).toInt())); // else if(qry.value(0).toInt()==3) // jdata.append(QString(",\"signal\":%1").arg((qry.value(1).toInt()==5)?100:(qry.value(1).toInt()*20))); // else if(qry.value(0).toInt()==4) // jdata.append(QString(",\"value\":%1").arg(qry.value(1).toDouble())); // } // } // if(cndev.ObjType.compare("YTWP")==0){ // jdata.append(QString(",\"unit\":\"MPa\"")); // }else if(cndev.ObjType.compare("YTLL")==0){ // jdata.append(QString(",\"unit\":\"m\"")); // } // } // jdata.append(QString(",\"logTime\":\"%1\"").arg(QDateTime::fromTime_t(MaxT).toString("yyyy-MM-dd HH:mm:ss"))); // emit cnSyncData(0,QUuid::createUuid().toString().replace("{","").replace("}",""), "{"+jdata+"}",cndev.devCategory); // }else{ // } // } // } // } } //QList DB_SyncThread::getJaPnoValue(QString pnoset) //{ // QList list; // QJsonParseError json_error; // QJsonDocument jsonDoc(QJsonDocument::fromJson(pnoset.toUtf8(),&json_error)); // if(json_error.error == QJsonParseError::NoError){ // QJsonArray json_root = jsonDoc.array(); // for(int i=0;i-2)&&(name.length()>0)) // list.append(JaPno(name,attr,port,defvalue1,defvalue2)); // } // } // return list; //} QList DB_SyncThread::getPnoValue(QString pnoset) { QList list; QJsonParseError json_error; QJsonDocument jsonDoc(QJsonDocument::fromJson(pnoset.toUtf8(),&json_error)); if(json_error.error == QJsonParseError::NoError){ QJsonArray json_root = jsonDoc.array(); for(int i=0;i-2)&&(name.length()>0)) list.append(CnPno(name,port,defvalue)); } } // int i,port; // json_object *pno, *obj; // char name[1024],value[1024]; // obj = json_tokener_parse(pnoset.toUtf8().data()); // if(json_object_get_type(obj)==json_type_array){ // for(i=0;i-2)&&(strlen(name)>0)){ // list.append(CnPno(QString::fromUtf8(name),port,QString::fromUtf8(value))); // } // } // } // json_object_put(obj); return list; } void DB_SyncThread::updatePortName() { QSqlQuery qry = mdb.exec("select s.owner_code, o.owner_name, o.unitinfo from sp_owner_sync s, sp_owner o where s.owner_code=o.owner_code and (s.owner_name<>o.owner_name or s.unitinfo<>o.unitinfo) group by s.owner_code"); while(qry.next()){ QString sql = QString("update sp_owner_sync set owner_name='%1', unitinfo='%2' where owner_code='%3'") .arg(qry.value(1).toString()).arg(qry.value(2).toString()).arg(qry.value(0).toString()); printf("echo '%s' >> /root/sql.txt\n",sql.toUtf8().data()); mdb.exec(sql); } } void DB_SyncThread::va_heartbeat(){ int ret = 0; for(int i=0;i> /root/heartbeat.log").arg(jsonstr).toUtf8().data()); emit SyncData(QUuid::createUuid().toString().replace("{","").replace("}",""), jsonstr); } } } void DB_SyncThread::set_cndevlist(QList list){ cnDevList.clear(); cnDevList.append(list); } void DB_SyncThread::time_out() { printf("DB_SyncThread time_out start\n"); QSqlQuery qry,qry2; QString qrysql; if(!isWorking) { printf("update memory at %s\n",QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz").toUtf8().data()); UpdateTime = QDateTime::currentDateTime().toTime_t(); isWorking = true; if(!mdbOpened) { mdbOpened = mdb.open(); } else { if(((times++)&0x0f)==0x0f){//智慧大屏 UpdateTime = QDateTime::currentDateTime().toTime_t(); checkUsers(); checkCompany(); checkDevice(); updatePortName(); } syncDevList.clear(); qry = mdb.exec("select owner_code,dwtype,object_code,start_port,rec_cur from sp_yangpu_share"); while(qry.next()){ syncDevList.append(YPSyncDev(qry.value(0).toString(), qry.value(1).toString(), qry.value(2).toString(), qry.value(3).toInt(), qry.value(4).toUInt())); } qry.clear(); if(day!=QDate::currentDate().day()){ day = QDate::currentDate().day(); va_heartbeat(); } cnDevList.clear(); qry = mdb.exec("select owner_code,dwtype,object_code,start_port,rec_cur,object_name, ownBid, devCategory, type from sp_changning_share"); while(qry.next()){ cnDevList.append(CNSyncDev(qry.value(0).toString(), qry.value(5).toString(), qry.value(1).toString(), qry.value(2).toString(), qry.value(3).toString().trimmed(), qry.value(4).toUInt(), qry.value(6).toString(), qry.value(7).toInt(), qry.value(8).toInt())); } qry.clear(); qry2 = mdb.exec("select device_id,point_code from sp_owner_status"); dList.clear(); pcList.clear(); while(qry2.next()){ dList.append(qry2.value(0).toString()); pcList.append(qry2.value(1).toInt()); } qry2.clear(); qry = mdb.exec("select id, owner_code, owner_name, unitinfo, install_time, dwtype, company, rtmp, owner_xh from sp_owner"); while(qry.next()) { time_t tmp_time = time(static_cast(Q_NULLPTR)); ytShm->updatetime[DB_THREAD] = tmp_time; int dwtype = qry.value(5).toInt(); uint ID = qry.value(0).toUInt(); QString companyCode = qry.value(6).toString(); QDate chkday = QDate::currentDate().addDays(-7); bool need_sync = false; qry2 =mdb.exec(QString("select count(*) from sp_device_sync where device_id='%1'").arg(qry.value(1).toString())); if(qry2.next()){ if(qry2.value(0).toInt()==0){ mdb.exec(QString("insert into sp_device_sync (device_id,com_count,com_confirm,alm_count,alm_confirm) values ('%1',0,0,0,0)").arg(qry.value(1).toString())); } } qry2.clear(); qrysql = QString("select count(*),table_name,sync_id from sp_owner_sync where id=%1").arg(ID); qry2 = mdb.exec(qrysql); if(qry2.next()){ QString deviceCode = qry.value(1).toString(); QString tbname = QString("sp_%1%2").arg(qry.value(5).toInt()==3?"y":(qry.value(5).toInt()==7?"e":(qry.value(5).toInt()==128?"iw":"d"))).arg(deviceCode); uint sync_id = 0; if(qry2.value(0).toInt()==0){ mdb.exec(QString("insert into sp_owner_sync (id, owner_code, owner_name, unitinfo, install_time, dwtype, company, rtmp, table_name, sync_id) values (%1,'%2','%3', '%4', '%5', %6, '%7', '%8', '%9', %10) ") .arg(qry.value(0).toInt()).arg(qry.value(1).toString()).arg(qry.value(2).toString()).arg(qry.value(3).toString()).arg(qry.value(4).toString()) .arg(qry.value(5).toInt()).arg(qry.value(6).toString()).arg(qry.value(7).toString()).arg(tbname).arg(sync_id)); }else{ sync_id = qry2.value(2).toUInt(); } qry2.clear(); switch(dwtype){ case 1://userinfo need_sync = userInfoSync(qry.value(1).toString(),deviceCode,sync_id, tbname, chkday,ID,companyCode); break; case 2://water need_sync = waterInfoSync(qry.value(1).toString(),deviceCode,sync_id,tbname,chkday,ID,dwtype,companyCode); break; case 4: need_sync = waterInfoSync(qry.value(1).toString(),deviceCode,sync_id,tbname,chkday,ID,dwtype,companyCode); break; case 5: need_sync = waterInfoSync(qry.value(1).toString(),deviceCode,sync_id,tbname,chkday,ID,dwtype,companyCode); break; case 3: need_sync = smokerInfoSync(qry.value(1).toString(),deviceCode,sync_id,tbname,chkday,ID,companyCode); break; case 6: need_sync = rtuInfoSync(qry.value(1).toString(),deviceCode,sync_id,tbname,chkday,ID,companyCode); break; case 7: need_sync = efireInfoSync(qry.value(1).toString(),deviceCode,sync_id,tbname,chkday,ID,companyCode,qry.value(8).toString()); break; case 16: need_sync = videoalarmSync(qry.value(1).toString(),deviceCode,sync_id,tbname,chkday,ID,companyCode); // need_sync = false;//debug break; case 128: need_sync = manholeCoverSync(qry.value(1).toString(),deviceCode,sync_id,tbname,chkday,ID,dwtype,companyCode); break; } qry2.clear(); if(need_sync) chkInList(qry.value(1).toString()); } usleep(5000); } if(deviceDataList.length()>0){ while (deviceDataList.length()>0) { chkInList(deviceDataList.first()); deviceDataList.removeFirst(); usleep(5000); } } qry.clear(); qry = mdb.exec("select a.company as company, a.dwtype as dwtype, sum(a.devCount) as devCount, sum(a.com_count) as comCount, sum(a.com_confirm) as comConfirm, sum(a.alm_count) as almCount, sum(a.alm_confirm) as almConfirm from (select sp_owner.company, sp_device_sync.device_id, if(sp_owner.dwtype=3,1,sp_owner.dwtype) as dwtype, 1 as devCount, sp_device_sync.com_count, sp_device_sync.com_confirm, sp_device_sync.alm_count, sp_device_sync.alm_confirm from sp_owner, sp_device_sync where sp_owner.owner_code = sp_device_sync.device_id) a where a.dwtype in (1,2,5,6,7,16) and a.company<>'' group by a.company, a.dwtype"); while(qry.next()){ QString sql2=""; qry2 = mdb.exec(QString("select count(*) from sp_company_sync where company_code='%1'").arg(qry.value(0).toString())); if(qry2.next()){ if(qry2.value(0).toInt()==0){ sql2 = QString("insert into sp_company_sync (company_code, device_00_count, device_00_out, device_00_out_confirm, device_00_alarm, device_00_alarm_confirm,device_01_count, device_01_out, device_01_out_confirm, device_01_alarm, device_01_alarm_confirm, device_02_count, device_02_out, device_02_out_confirm, device_02_alarm, device_02_alarm_confirm,device_06_count, device_06_out, device_06_out_confirm, device_06_alarm, device_06_alarm_confirm,device_07_count, device_07_out, device_07_out_confirm, device_07_alarm, device_07_alarm_confirm,device_16_count, device_16_out, device_16_out_confirm, device_16_alarm, device_16_alarm_confirm) values ('%1',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);") .arg(qry.value(0).toString()); } } qry2.clear(); if(qry.value(1).toInt()==1){ sql2 += QString("update sp_company_sync set device_01_count=%1, device_01_out=%2, device_01_out_confirm=%3, device_01_alarm=%4, device_01_alarm_confirm=%5 where company_code='%6'") .arg(qry.value(2).toInt()).arg(qry.value(3).toInt()).arg(qry.value(4).toInt()).arg(qry.value(5).toInt()).arg(qry.value(6).toInt()).arg(qry.value(0).toString()); update_company_alarmstatus(qry.value(0).toString(),1,qry.value(2).toUInt(),qry.value(3).toUInt(),qry.value(4).toUInt(),qry.value(5).toUInt(),qry.value(6).toUInt()); }else if(qry.value(1).toInt()==2){ sql2 += QString("update sp_company_sync set device_02_count=%1, device_02_out=%2, device_02_out_confirm=%3, device_02_alarm=%4, device_02_alarm_confirm=%5 where company_code='%6'") .arg(qry.value(2).toInt()).arg(qry.value(3).toInt()).arg(qry.value(4).toInt()).arg(qry.value(5).toInt()).arg(qry.value(6).toInt()).arg(qry.value(0).toString()); update_company_alarmstatus(qry.value(0).toString(),2,qry.value(2).toUInt(),qry.value(3).toUInt(),qry.value(4).toUInt(),qry.value(5).toUInt(),qry.value(6).toUInt()); }else if(qry.value(1).toInt()==3){ sql2 += QString("update sp_company_sync set device_03_count=%1, device_03_out=%2, device_03_out_confirm=%3, device_03_alarm=%4, device_03_alarm_confirm=%5 where company_code='%6'") .arg(qry.value(2).toInt()).arg(qry.value(3).toInt()).arg(qry.value(4).toInt()).arg(qry.value(5).toInt()).arg(qry.value(6).toInt()).arg(qry.value(0).toString()); update_company_alarmstatus(qry.value(0).toString(),1,qry.value(2).toUInt(),qry.value(3).toUInt(),qry.value(4).toUInt(),qry.value(5).toUInt(),qry.value(6).toUInt()); }else if(qry.value(1).toInt()==5){ sql2 += QString("update sp_company_sync set device_00_count=%1, device_00_out=%2, device_00_out_confirm=%3, device_00_alarm=%4, device_00_alarm_confirm=%5 where company_code='%6'") .arg(qry.value(2).toInt()).arg(qry.value(3).toInt()).arg(qry.value(4).toInt()).arg(qry.value(5).toInt()).arg(qry.value(6).toInt()).arg(qry.value(0).toString()); update_company_alarmstatus(qry.value(0).toString(),0,qry.value(2).toUInt(),qry.value(3).toUInt(),qry.value(4).toUInt(),qry.value(5).toUInt(),qry.value(6).toUInt()); }else if(qry.value(1).toInt()==6){ sql2 += QString("update sp_company_sync set device_06_count=%1, device_06_out=%2, device_06_out_confirm=%3, device_06_alarm=%4, device_06_alarm_confirm=%5 where company_code='%6'") .arg(qry.value(2).toInt()).arg(qry.value(3).toInt()).arg(qry.value(4).toInt()).arg(qry.value(5).toInt()).arg(qry.value(6).toInt()).arg(qry.value(0).toString()); update_company_alarmstatus(qry.value(0).toString(),6,qry.value(2).toUInt(),qry.value(3).toUInt(),qry.value(4).toUInt(),qry.value(5).toUInt(),qry.value(6).toUInt()); }else if(qry.value(1).toInt()==7){ sql2 += QString("update sp_company_sync set device_07_count=%1, device_07_out=%2, device_07_out_confirm=%3, device_07_alarm=%4, device_07_alarm_confirm=%5 where company_code='%6'") .arg(qry.value(2).toInt()).arg(qry.value(3).toInt()).arg(qry.value(4).toInt()).arg(qry.value(5).toInt()).arg(qry.value(6).toInt()).arg(qry.value(0).toString()); update_company_alarmstatus(qry.value(0).toString(),7,qry.value(2).toUInt(),qry.value(3).toUInt(),qry.value(4).toUInt(),qry.value(5).toUInt(),qry.value(6).toUInt()); }else if(qry.value(1).toInt()==16){ sql2 += QString("update sp_company_sync set device_16_count=%1, device_16_out=%2, device_16_out_confirm=%3, device_16_alarm=%4, device_16_alarm_confirm=%5 where company_code='%6'") .arg(qry.value(2).toInt()).arg(qry.value(3).toInt()).arg(qry.value(4).toInt()).arg(qry.value(5).toInt()).arg(qry.value(6).toInt()).arg(qry.value(0).toString()); update_company_alarmstatus(qry.value(0).toString(),16,qry.value(2).toUInt(),qry.value(3).toUInt(),qry.value(4).toUInt(),qry.value(5).toUInt(),qry.value(6).toUInt()); } mdb.exec(sql2); qry2.clear(); usleep(5000); } qry.clear(); QDate d_day = QDate::currentDate().addDays(-7); QTime current_time =QTime::currentTime(); int hour = current_time.hour();//当前的小时 QString delsql = QString("delete from sp_owner_sync_data where data_time<'%1-%2-%3 %4:00:00'").arg(d_day.year(),4,10,QChar('0')).arg(d_day.month(),2,10,QChar('0')).arg(d_day.day(),2,10,QChar('0')).arg(hour); mdb.exec(delsql); qry = mdb.exec("select sp_owner_status.id, sp_owner_status.device_id, sp_owner_status.data_time, sp_owner.dwtype from sp_owner_status, sp_owner where sp_owner_status.device_id=sp_owner.owner_code and sp_owner.dwtype=1 and sp_owner_status.point_data !='0'"); while(qry.next()){ qry2 = mdb.exec(QString("select clzt from sp_hj2017 where device_code=%1 and time='%2'") .arg(qry.value(1).toString()).arg(qry.value(2).toDateTime().toString("yyyy-MM-dd HH:mm:ss"))); if((qry2.next())&&(qry2.value(0).toInt()==1)) mdb.exec(QString("update sp_owner_status set point_data='0' where id=%1").arg(qry.value(0).toInt())); qry.clear(); usleep(5000); } qry.clear(); qry = mdb.exec("select sp_owner_status.id, sp_owner_status.device_id, sp_owner_status.data_time, sp_owner_status.point_data from sp_owner_status, sp_owner where sp_owner_status.point_code=1 and sp_owner_status.device_id=sp_owner.owner_code and (sp_owner.dwtype=2 or sp_owner.dwtype=5) and sp_owner_status.point_data !='0' and sp_owner_status.point_data !='WP0'"); while(qry.next()){ qry2 = mdb.exec(QString("select clzt from sp_sj2017 where device_code=%1 and time='%2'") .arg(qry.value(1).toString()).arg(qry.value(2).toDateTime().toString("yyyy-MM-dd HH:mm:ss"))); if((qry2.next())&&(qry2.value(0).toInt()==1)) mdb.exec(QString("update sp_owner_status set point_data='0',data_time='%1' where device_id='%2' and point_code=0").arg(qry.value(2).toDateTime().toString("yyyy-MM-dd HH:mm:ss")).arg(qry.value(1).toString())); else mdb.exec(QString("update sp_owner_status set point_data='%1',data_time='%2' where device_id='%3' and point_code=0").arg(qry.value(3).toString()).arg(qry.value(2).toDateTime().toString("yyyy-MM-dd HH:mm:ss")).arg(qry.value(1).toString())); usleep(5000); } mdb.close(); mdbOpened = false; } isWorking = false; } } void DB_SyncThread::setCompanyAlarmSet(QString deviceCode, AlarmSet set) { QString companyCode; quint16 companyIdx = 0xffff; quint32 deviceIdx = 0xffffffff; AlarmSet tmp = AlarmSet(0xffffffff,"","",0xff,0,0); for(quint32 i=0;i<102400;i++){ if(i>=webData->DevicesCount) break; if(QString::compare(QString(webData->devices[i].DevCode),deviceCode)==0){ deviceIdx = i; companyCode = QString(webData->devices[i].CompanyCode); set.DeviceIdx = deviceIdx; set.DevName = QString(webData->devices[i].DevName); if(set.dwType==16){ system(QString("echo '%1, %2, %3' >> /root/data.txt").arg(webData->devices[i].CompanyCode) .arg(deviceCode).arg(QDateTime::fromTime_t(set.AlarmTime).toString("yyyy-MM-dd HH:mm:ss")) .toUtf8().data()); } setLastAlarmSet(static_cast(i),set.dwType,set); break; } } for(quint16 i=0;i<10240;i++){ if(i>=webData->CompanysCount) break; if(QString::compare(QString(webData->companys[i].CompanyCode),companyCode)==0){ companyIdx = i; break; } } if(companyIdxCompanysCount){ for(quint8 i=0;i<10;i++){ if(set.AlarmTime>webData->CompanyAlarm[companyIdx].alarmRec[i].Time_T){ for(quint8 j=9;j>i;j--){ get_AlarmSet(companyIdx,j-1,&tmp); set_AlarmSet(companyIdx,j,tmp); } set_AlarmSet(companyIdx,i,set); break; } } for(quint8 i=0;i<10;i++){ if(set.AlarmTime>webData->CompanyAlarm[10239].alarmRec[i].Time_T){ for(quint8 j=9;j>i;j--){ get_AlarmSet(10239,j-1,&tmp); set_AlarmSet(10239,j,tmp); } set_AlarmSet(10239,i,set); break; } } } } void DB_SyncThread::set_AlarmSet(quint16 companyIdx, quint8 alarmIdx, AlarmSet set) { if(((companyIdxCompanysCount)||(companyIdx==10239))&&(alarmIdx<10)){ if(set.Enabled==0x01){ webData->CompanyAlarm[companyIdx].alarmRec[alarmIdx].DeviceIdx = set.DeviceIdx; sprintf(webData->CompanyAlarm[companyIdx].alarmRec[alarmIdx].DevName,"%s",set.DevName.toUtf8().data()); sprintf(webData->CompanyAlarm[companyIdx].alarmRec[alarmIdx].Data,"%s",set.AlarmData.toUtf8().data()); sprintf(webData->CompanyAlarm[companyIdx].alarmRec[alarmIdx].Time,"%s",QDateTime::fromTime_t(set.AlarmTime).toString("yyyy-MM-dd HH:mm:ss").toUtf8().data()); webData->CompanyAlarm[companyIdx].alarmRec[alarmIdx].dwType = set.dwType; webData->CompanyAlarm[companyIdx].alarmRec[alarmIdx].Time_T = set.AlarmTime; webData->CompanyAlarm[companyIdx].alarmRec[alarmIdx].ShowFlag = 0x01; }else webData->CompanyAlarm[companyIdx].alarmRec[alarmIdx].ShowFlag = 0x00; webData->CompanyAlarm[companyIdx].UpdateTime = UpdateTime; } } void DB_SyncThread::setLastAlarmSet(quint16 companyIdx, quint8 dwtype, AlarmSet set) { int page=0, dwIdx=0; // if(dwtype==16){ // system(QString("echo '16 to mem: %1 %2 %3 %4' >> /root/data.txt") // .arg(companyIdx).arg(webData->CompanysCount).arg(set.DeviceIdx).arg(webData->DevicesCount) // .toUtf8().data()); // } if(((companyIdxCompanysCount)||(companyIdx==10239))&&(dwtype<20)){ if(dwtype==1){ page = 0; dwIdx = 1; }else if(dwtype==2){ page = 0; dwIdx = 2; }else if(dwtype==6){ page = 0; dwIdx = 3; }else if(dwtype==7){ page = 0; dwIdx = 4; }else if(dwtype==16){ page = 0; dwIdx = 5; } if(set.Enabled==0x01) { // if(dwtype==16){ // system(QString("echo 'lastAlarmRec %1 %2 %3 companyIdx:%4 page:%5 dwIdx:%6' >> /root/data.txt") // .arg(set.DevName).arg(set.AlarmData).arg(QDateTime::fromTime_t(set.AlarmTime).toString("yyyy-MM-dd HH:mm:ss")) // .arg(companyIdx).arg(page).arg(dwIdx).toUtf8().data()); // } webData->LastAlarmRec[companyIdx][page].alarmRec[dwIdx].DeviceIdx = set.DeviceIdx; sprintf(webData->LastAlarmRec[companyIdx][page].alarmRec[dwIdx].DevName,"%s",set.DevName.toUtf8().data()); sprintf(webData->LastAlarmRec[companyIdx][page].alarmRec[dwIdx].Data,"%s",set.AlarmData.toUtf8().data()); sprintf(webData->LastAlarmRec[companyIdx][page].alarmRec[dwIdx].Time,"%s",QDateTime::fromTime_t(set.AlarmTime).toString("yyyy-MM-dd HH:mm:ss").toUtf8().data()); webData->LastAlarmRec[companyIdx][page].alarmRec[dwIdx].dwType = set.dwType; webData->LastAlarmRec[companyIdx][page].alarmRec[dwIdx].Time_T = set.AlarmTime; webData->LastAlarmRec[companyIdx][page].alarmRec[dwIdx].ShowFlag = 0x01; if(!initTime) emit companyAlarmChanged(QString(webData->companys[companyIdx].CompanyCode)); }else webData->LastAlarmRec[companyIdx][page].alarmRec[dwIdx].ShowFlag = 0x00; webData->LastAlarmRec[companyIdx][page].UpdateTime = UpdateTime; } } void DB_SyncThread::get_AlarmSet(quint16 companyIdx, quint8 alarmIdx, AlarmSet *set) { if(((companyIdxCompanysCount)||(companyIdx==10239))&&(alarmIdx<10)){ if(webData->CompanyAlarm[companyIdx].alarmRec[alarmIdx].ShowFlag==0x01){ set->Enabled = 0x01; set->DeviceIdx = webData->CompanyAlarm[companyIdx].alarmRec[alarmIdx].DeviceIdx; set->dwType = webData->CompanyAlarm[companyIdx].alarmRec[alarmIdx].dwType; set->AlarmTime = webData->CompanyAlarm[companyIdx].alarmRec[alarmIdx].Time_T; set->DevName = QString::fromUtf8(webData->CompanyAlarm[companyIdx].alarmRec[alarmIdx].DevName); set->AlarmData = QString::fromUtf8(webData->CompanyAlarm[companyIdx].alarmRec[alarmIdx].Data); }else{ set->Enabled = 0x00; } } } void DB_SyncThread::getLastAlarmSet(quint16 companyIdx, quint8 dwtype, AlarmSet *set) { int page=0, dwIdx=0; if(((companyIdxCompanysCount)||(companyIdx==10239))&&(dwtype<20)){ if(dwtype==1){ page = 0; dwIdx = 1; }else if(dwtype==2){ page = 0; dwIdx = 2; }else if(dwtype==6){ page = 0; dwIdx = 3; }else if(dwtype==7){ page = 0; dwIdx = 4; }else if(dwtype==16){ page = 0; dwIdx = 5; } if(webData->LastAlarmRec[companyIdx][page].alarmRec[dwIdx].ShowFlag==0x01){ set->Enabled = 0x01; set->DeviceIdx = webData->LastAlarmRec[companyIdx][page].alarmRec[dwIdx].DeviceIdx; set->dwType = webData->LastAlarmRec[companyIdx][page].alarmRec[dwIdx].dwType; set->AlarmTime = webData->LastAlarmRec[companyIdx][page].alarmRec[dwIdx].Time_T; set->DevName = QString::fromUtf8(webData->LastAlarmRec[companyIdx][page].alarmRec[dwIdx].DevName); set->AlarmData = QString::fromUtf8(webData->LastAlarmRec[companyIdx][page].alarmRec[dwIdx].Data); }else{ set->Enabled = 0x00; } } } void DB_SyncThread::update_company_alarmstatus(QString companycode, quint8 alarmidx, uint devCount, uint devOut, uint devOutConfirm, uint devAlarmCount, uint devAlarmConfirm) { uint dC=0,dO=0,dOC=0,dA=0,dAC=0; quint16 companyIdx = 0xffff; for(quint16 i=0;i<10240;i++){ if(i>=webData->CompanysCount) break; if(QString::compare(QString(webData->companys[i].CompanyCode),companycode)==0){ companyIdx = i; break; } } if(companyIdxCompanysCount){ for(quint16 i=0;i<10239;i++){ if(i==companyIdx){ webData->companys[i].subType[alarmidx].DevCount = devCount; webData->companys[i].subType[alarmidx].DevOutLineCount = devOut; webData->companys[i].subType[alarmidx].DevOutConfirmCount = devOutConfirm; webData->companys[i].subType[alarmidx].DevAlarmCount = devAlarmCount; webData->companys[i].subType[alarmidx].DevAlarmConfirmCount = devAlarmConfirm; } dC += webData->companys[i].subType[alarmidx].DevCount; dO += webData->companys[i].subType[alarmidx].DevOutLineCount; dOC += webData->companys[i].subType[alarmidx].DevOutConfirmCount; dA += webData->companys[i].subType[alarmidx].DevAlarmCount; dAC += webData->companys[i].subType[alarmidx].DevAlarmConfirmCount; } webData->companys[10239].subType[alarmidx].DevCount = dC; webData->companys[10239].subType[alarmidx].DevOutLineCount = dO; webData->companys[10239].subType[alarmidx].DevOutConfirmCount = dOC; webData->companys[10239].subType[alarmidx].DevAlarmCount = dA; webData->companys[10239].subType[alarmidx].DevAlarmConfirmCount = dAC; } } void DB_SyncThread::webShmInit() { if(!mdbOpened) { mdbOpened = mdb.open(); } printf("init memory at %s\n",QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz").toUtf8().data()); UpdateTime = QDateTime::currentDateTime().toTime_t(); checkUsers(); checkCompany(); checkDevice(); checkAlarmStatus(); checkAlarmList(); initTime = false; mdb.close(); mdbOpened = false; } quint32 DB_SyncThread::getDeviceIdxByDeviceCode(QString deviceCode) { for(quint32 i=0;i<102400;i++){ if(webData->devices[i].Enabled==0x01){ if(QString::compare(deviceCode,QString(webData->devices[i].DevCode))==0){ return i; } } } return 0xffffffff; } void DB_SyncThread::checkAlarmList() { printf("checkAlarmList start\n"); QSqlQuery qry; QList companyAlarmList; QList allAlarmList; int nrow; allAlarmList.clear(); for(quint16 i=0;iCompanysCount;i++){ if(strlen(webData->companys[i].CompanyCode)>0){ // printf("CompanyCode:%s\n",QString(webData->companys[i].CompanyCode).toUtf8().data()); companyAlarmList.clear(); qry = mdb.exec(QString("select h.device_code, h.time, h.data2, h.data4, o.owner_name from sp_hj2017 h, sp_owner o where o.owner_code = h.device_code and o.company='%1' and h.timecompanys[i].CompanyCode)); nrow = 0; while(qry.next()){ // printf("alarmName:%s\n",qry.value(2).toString().toUtf8().data()); AlarmSet CompanyAlarm = AlarmSet(getDeviceIdxByDeviceCode(qry.value(0).toString()),qry.value(4).toString(), QString::fromUtf8("%1,%2").arg(qry.value(2).toString()).arg(qry.value(3).toString()), 1,qry.value(1).toDateTime().toTime_t(),1); if(nrow==0) setLastAlarmSet(i,1,CompanyAlarm); companyAlarmList.append(CompanyAlarm); allAlarmList.append(CompanyAlarm); time_t tmp_time = time(static_cast(nullptr)); ytShm->updatetime[DB_THREAD] = tmp_time; nrow++; } qry.clear(); nrow = 0; qry = mdb.exec(QString("select s.device_code, s.time, s.data1, s.data4, o.owner_name from sp_sj2017 s, sp_owner o where o.owner_code = s.device_code and o.company='%1' and s.timecompanys[i].CompanyCode)); while(qry.next()){ QString dat1 = qry.value(2).toString(); QString data1 = QString::compare(dat1,"WP1")==0?QString::fromUtf8("低压"): (QString::compare(dat1,"WP2")==0?QString::fromUtf8("高压"): (QString::compare(dat1,"WP3")==0?QString::fromUtf8("故障"): (QString::compare(dat1,"WP4")==0?QString::fromUtf8("离线"): (QString::compare(dat1,"WP0")==0?QString::fromUtf8("恢复"): (QString::compare(dat1,"LL1")==0?QString::fromUtf8("低液位"): (QString::compare(dat1,"LL2")==0?QString::fromUtf8("高液位"): (QString::compare(dat1,"LL3")==0?QString::fromUtf8("故障"): (QString::compare(dat1,"LL4")==0?QString::fromUtf8("离线"): (QString::compare(dat1,"LL0")==0?QString::fromUtf8("恢复"):""))))))))); AlarmSet CompanyAlarm = AlarmSet(getDeviceIdxByDeviceCode(qry.value(0).toString()),qry.value(4).toString(), data1+QString::fromUtf8(",")+qry.value(3).toString(), 2,qry.value(1).toDateTime().toTime_t(),1); if(nrow==0) setLastAlarmSet(i,2,CompanyAlarm); companyAlarmList.append(CompanyAlarm); allAlarmList.append(CompanyAlarm); time_t tmp_time = time(static_cast(nullptr)); ytShm->updatetime[DB_THREAD] = tmp_time; nrow++; } qry.clear(); nrow=0; qry = mdb.exec(QString("select r.device_code, r.time, r.data2, r.data4, o.owner_name from sp_rtu2017 r, sp_owner o where o.owner_code = r.device_code and o.company='%1' and r.timecompanys[i].CompanyCode)); while(qry.next()){ AlarmSet CompanyAlarm = AlarmSet(getDeviceIdxByDeviceCode(qry.value(0).toString()),qry.value(4).toString(), qry.value(2).toString()+QString::fromUtf8(",")+qry.value(3).toString(), 6,qry.value(1).toDateTime().toTime_t(),1); if(nrow==0) setLastAlarmSet(i,6,CompanyAlarm); companyAlarmList.append(CompanyAlarm); allAlarmList.append(CompanyAlarm); time_t tmp_time = time(static_cast(nullptr)); ytShm->updatetime[DB_THREAD] = tmp_time; nrow++; } qry.clear(); nrow=0; qry = mdb.exec(QString("select e.device_code, e.time, e.data1, o.owner_name from sp_ef2017 e, sp_owner o where o.owner_code = e.device_code and o.company='%1' and e.timecompanys[i].CompanyCode)); while(qry.next()){ QString dat1 = qry.value(2).toString(); AlarmSet CompanyAlarm = AlarmSet(getDeviceIdxByDeviceCode(qry.value(0).toString()),qry.value(3).toString(), QString::compare(dat1,"EF1")==0?QString::fromUtf8("欠压报警"): (QString::compare(dat1,"EF2")==0?QString::fromUtf8("过压报警"): (QString::compare(dat1,"EF3")==0?QString::fromUtf8("过流告警"): (QString::compare(dat1,"EF4")==0?QString::fromUtf8("漏电报警"): (QString::compare(dat1,"EF5")==0?QString::fromUtf8("#1线缆温度超高报警"): (QString::compare(dat1,"EF6")==0?QString::fromUtf8("#2线缆温度超高报警"): (QString::compare(dat1,"EF7")==0?QString::fromUtf8("#3线缆温度超高报警"): (QString::compare(dat1,"EF8")==0?QString::fromUtf8("#4线缆温度超高报警"): (QString::compare(dat1,"EF9")==0?QString::fromUtf8("设备离线"): (QString::compare(dat1,"EF0")==0?QString::fromUtf8("报警撤销"):""))))))))), 7,qry.value(1).toDateTime().toTime_t(),1); if(nrow==0) setLastAlarmSet(i,7,CompanyAlarm); companyAlarmList.append(CompanyAlarm); allAlarmList.append(CompanyAlarm); time_t tmp_time = time(static_cast(nullptr)); ytShm->updatetime[DB_THREAD] = tmp_time; nrow++; } qry.clear(); nrow=0; QString sql = QString("select v.device_code, v.time, v.data4, o.owner_name from sp_video2017 v, sp_owner o where o.owner_code = v.device_code and o.company='%1' and v.timecompanys[i].CompanyCode); // system(QString("echo '%1' >> /root/sql.txt").arg(sql).toUtf8().data()); qry = mdb.exec(sql); while(qry.next()){ AlarmSet CompanyAlarm = AlarmSet(getDeviceIdxByDeviceCode(qry.value(0).toString()),qry.value(3).toString(), qry.value(2).toString(),16,qry.value(1).toDateTime().toTime_t(),1); if(nrow==0){ // system(QString("echo '%1 %2 %3' >> /root/data.txt ").arg(webData->companys[i].CompanyCode).arg(qry.value(1).toString()).arg(qry.value(2).toString()).toUtf8().data()); setLastAlarmSet(i,16,CompanyAlarm); } companyAlarmList.append(CompanyAlarm); allAlarmList.append(CompanyAlarm); time_t tmp_time = time(static_cast(nullptr)); ytShm->updatetime[DB_THREAD] = tmp_time; nrow++; } qry.clear(); qSort(companyAlarmList.begin(),companyAlarmList.end(),compareAlarmSet); qSort(allAlarmList.begin(),allAlarmList.end(),compareAlarmSet); while(companyAlarmList.length()>10) companyAlarmList.removeLast(); while(allAlarmList.length()>10) allAlarmList.removeLast(); for(int j=0;j<10;j++){ if(jCompanyAlarm[i].alarmRec[j].DeviceIdx=alarm.DeviceIdx; sprintf(webData->CompanyAlarm[i].alarmRec[j].DevName,"%s",alarm.DevName.toUtf8().data()); sprintf(webData->CompanyAlarm[i].alarmRec[j].Data,"%s",alarm.AlarmData.toUtf8().data()); sprintf(webData->CompanyAlarm[i].alarmRec[j].Time,"%s",QDateTime::fromTime_t(alarm.AlarmTime).toString("yyyy-MM-dd HH:mm:ss").toUtf8().data()); webData->CompanyAlarm[i].alarmRec[j].dwType = alarm.dwType; webData->CompanyAlarm[i].alarmRec[j].Time_T = alarm.AlarmTime; webData->CompanyAlarm[i].alarmRec[j].ShowFlag = 0x01; printf("companyAlarmList:%s\n",webData->CompanyAlarm[i].alarmRec[j].DevName); printf("alarmRec:%s\n",webData->CompanyAlarm[i].alarmRec[j].Data); }else webData->CompanyAlarm[i].alarmRec[j].ShowFlag = 0x00; webData->CompanyAlarm[i].UpdateTime = UpdateTime; } } usleep(5000); } for(int j=0;j<10;j++){ if(jCompanyAlarm[10239].alarmRec[j].DeviceIdx = alarm.DeviceIdx; sprintf(webData->CompanyAlarm[10239].alarmRec[j].DevName,"%s",alarm.DevName.toUtf8().data()); sprintf(webData->CompanyAlarm[10239].alarmRec[j].Data,"%s",alarm.AlarmData.toUtf8().data()); sprintf(webData->CompanyAlarm[10239].alarmRec[j].Time,"%s",QDateTime::fromTime_t(alarm.AlarmTime).toString("yyyy-MM-dd HH:mm:ss").toUtf8().data()); webData->CompanyAlarm[10239].alarmRec[j].dwType = alarm.dwType; webData->CompanyAlarm[10239].alarmRec[j].Time_T = alarm.AlarmTime; webData->CompanyAlarm[10239].alarmRec[j].ShowFlag = 0x01; }else webData->CompanyAlarm[10239].alarmRec[j].ShowFlag = 0x00; webData->CompanyAlarm[10239].UpdateTime = UpdateTime; } } void DB_SyncThread::checkAlarmStatus() { printf("checkAlarmStatus start\n"); char company_code[50]; QSqlQuery qry = mdb.exec("select company_code, device_01_count, device_01_out, device_01_out_confirm, device_01_alarm, device_01_alarm_confirm, device_02_count, device_02_out, device_02_out_confirm, device_02_alarm, device_02_alarm_confirm, device_06_count, device_06_out, device_06_out_confirm, device_06_alarm, device_06_alarm_confirm, device_07_count, device_07_out, device_07_out_confirm, device_07_alarm, device_07_alarm_confirm, device_16_count, device_16_out, device_16_out_confirm, device_16_alarm, device_16_alarm_confirm from sp_company_sync"); while(qry.next()){ sprintf(company_code,"%s",qry.value(0).toString().toUtf8().data()); if(strlen(company_code)>0){ for(unsigned int i=0;i<10240;i++){ if(i>=webData->CompanysCount) break; if(strcmp(company_code,webData->companys[i].CompanyCode)==0){ webData->companys[i].subType[1].DevCount = qry.value(1).toUInt(); webData->companys[i].subType[1].DevOutLineCount = qry.value(2).toUInt(); webData->companys[i].subType[1].DevOutConfirmCount = qry.value(3).toUInt(); webData->companys[i].subType[1].DevAlarmCount = qry.value(4).toUInt(); webData->companys[i].subType[1].DevAlarmConfirmCount = qry.value(5).toUInt(); webData->companys[i].subType[2].DevCount = qry.value(6).toUInt(); webData->companys[i].subType[2].DevOutLineCount = qry.value(7).toUInt(); webData->companys[i].subType[2].DevOutConfirmCount = qry.value(8).toUInt(); webData->companys[i].subType[2].DevAlarmCount = qry.value(9).toUInt(); webData->companys[i].subType[2].DevAlarmConfirmCount = qry.value(10).toUInt(); webData->companys[i].subType[6].DevCount = qry.value(11).toUInt(); webData->companys[i].subType[6].DevOutLineCount = qry.value(12).toUInt(); webData->companys[i].subType[6].DevOutConfirmCount = qry.value(13).toUInt(); webData->companys[i].subType[6].DevAlarmCount = qry.value(14).toUInt(); webData->companys[i].subType[6].DevAlarmConfirmCount = qry.value(15).toUInt(); webData->companys[i].subType[7].DevCount = qry.value(16).toUInt(); webData->companys[i].subType[7].DevOutLineCount = qry.value(17).toUInt(); webData->companys[i].subType[7].DevOutConfirmCount = qry.value(18).toUInt(); webData->companys[i].subType[7].DevAlarmCount = qry.value(19).toUInt(); webData->companys[i].subType[7].DevAlarmConfirmCount = qry.value(20).toUInt(); webData->companys[i].subType[16].DevCount = qry.value(21).toUInt(); webData->companys[i].subType[16].DevOutLineCount = qry.value(22).toUInt(); webData->companys[i].subType[16].DevOutConfirmCount = qry.value(23).toUInt(); webData->companys[i].subType[16].DevAlarmCount = qry.value(24).toUInt(); webData->companys[i].subType[16].DevAlarmConfirmCount = qry.value(25).toUInt(); emit appendLog(QString("checkAlarmStatus[company_code:%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]").arg(company_code).arg(webData->companys[i].subType[1].DevCount).arg(webData->companys[i].subType[1].DevOutLineCount).arg(webData->companys[i].subType[1].DevOutConfirmCount).arg(webData->companys[i].subType[1].DevAlarmCount) .arg(webData->companys[i].subType[1].DevAlarmConfirmCount).arg(webData->companys[i].subType[2].DevCount).arg(webData->companys[i].subType[2].DevOutLineCount).arg(webData->companys[i].subType[2].DevOutConfirmCount).arg(webData->companys[i].subType[2].DevAlarmCount) .arg(webData->companys[i].subType[2].DevAlarmConfirmCount).arg(webData->companys[i].subType[6].DevCount).arg(webData->companys[i].subType[6].DevOutLineCount).arg(webData->companys[i].subType[6].DevOutConfirmCount).arg(webData->companys[i].subType[6].DevAlarmCount) .arg(webData->companys[i].subType[6].DevAlarmConfirmCount).arg(webData->companys[i].subType[7].DevCount).arg(webData->companys[i].subType[7].DevOutLineCount).arg(webData->companys[i].subType[7].DevOutConfirmCount).arg(webData->companys[i].subType[7].DevAlarmCount) .arg(webData->companys[i].subType[7].DevAlarmConfirmCount).arg(webData->companys[i].subType[16].DevCount).arg(webData->companys[i].subType[16].DevOutLineCount).arg(webData->companys[i].subType[16].DevOutConfirmCount).arg(webData->companys[i].subType[16].DevAlarmCount) .arg(webData->companys[i].subType[16].DevAlarmConfirmCount)); break; } } } { time_t tmp_time = time(static_cast(nullptr)); ytShm->updatetime[DB_THREAD] = tmp_time; } usleep(5000); } qry.clear(); } void DB_SyncThread::checkDevice() { printf("checkDevice start\n"); unsigned int count = 0; QSqlQuery qry = mdb.exec("SELECT id, owner_code, company, dwtype, owner_name, unitinfo from sp_owner"); while(qry.next()){ if((webData->devices[count].dwType!=(qry.value(3).toUInt()&0xff)) ||(QString::compare(QString::fromUtf8(webData->devices[count].DevCode),qry.value(1).toString())!=0) ||(QString::compare(QString::fromUtf8(webData->devices[count].CompanyCode),qry.value(2).toString())!=0) ||(QString::compare(QString::fromUtf8(webData->devices[count].DevName),qry.value(4).toString())!=0) ||(QString::compare(QString::fromUtf8(webData->devices[count].DevInfo),qry.value(5).toString())!=0)){ webData->devices[count].DevIdx = count&0xffffffff; sprintf(webData->devices[count].DevCode,"%s",qry.value(1).toString().toUtf8().data()); sprintf(webData->devices[count].CompanyCode,"%s",qry.value(2).toString().toUtf8().data()); webData->devices[count].dwType = qry.value(3).toUInt()&0xff; sprintf(webData->devices[count].DevName,"%s",qry.value(4).toString().toUtf8().data()); sprintf(webData->devices[count].DevInfo,"%s",qry.value(5).toString().toUtf8().data()); webData->devices[count].Enabled = 0x01; webData->devices[count].UpdateTime = UpdateTime; } count++; if(count>webData->DevicesCount) webData->DevicesCount = count; if(count>102399) break; usleep(5000); time_t tmp_time = time(static_cast(nullptr)); ytShm->updatetime[DB_THREAD] = tmp_time; } qry.clear(); } void DB_SyncThread::checkCompany() { printf("checkCompany start\n"); unsigned int count = 0; QSqlQuery qry = mdb.exec("select c.owner_id, c.owner_name, c.xmlx, p.username, p.phone, c.building_id, b.build_name, ifnull(concat(b.addr1,'.',b.addr2,'.',b.addr3,'.',b.address),''),ifnull(b.posistion,''),b.address from jdxf.sp_owner_company c, jdxf.sp_building b, ytIoT.ytiot_t_company_phone p where c.building_id=b.id and c.owner_id = p.owner_code"); while(qry.next()){ float Lng=0.0,Lat=0.0; QString position = qry.value(8).toString(); if(position.length()>0){ QStringList pos = position.split(","); if(pos.length()>1){ Lng = pos.at(0).toFloat(); Lat = pos.at(1).toFloat(); } } if( (webData->buildings[count].Lng!=Lng) ||(webData->buildings[count].Lat!=Lat) || (QString::compare(QString::fromUtf8(webData->companys[count].CompanyCode),qry.value(0).toString())!=0) ||(QString::compare(QString::fromUtf8(webData->companys[count].CompanyName),qry.value(1).toString())!=0) ||(QString::compare(QString::fromUtf8(webData->companys[count].ProjectType),qry.value(2).toString())!=0) ||(QString::compare(QString::fromUtf8(webData->companys[count].UserName),qry.value(3).toString())!=0) ||(QString::compare(QString::fromUtf8(webData->companys[count].Phone),qry.value(4).toString())!=0) ||(QString::compare(QString::fromUtf8(webData->buildings[count].CompanyCode),qry.value(0).toString())!=0) ||(QString::compare(QString::fromUtf8(webData->buildings[count].BuildingCode),qry.value(5).toString())!=0) ||(QString::compare(QString::fromUtf8(webData->buildings[count].BuildingName),qry.value(6).toString())!=0) ||(QString::compare(QString::fromUtf8(webData->buildings[count].FullAddress),qry.value(7).toString())!=0) ||(QString::compare(QString::fromUtf8(webData->buildings[count].Address),qry.value(9).toString())!=0)){ sprintf(webData->companys[count].CompanyCode,"%s",qry.value(0).toString().toUtf8().data()); sprintf(webData->companys[count].CompanyName,"%s",qry.value(1).toString().toUtf8().data()); sprintf(webData->companys[count].ProjectType,"%s",qry.value(2).toString().toUtf8().data()); sprintf(webData->companys[count].UserName,"%s",qry.value(3).toString().toUtf8().data()); sprintf(webData->companys[count].Phone,"%s",qry.value(4).toString().toUtf8().data()); sprintf(webData->buildings[count].CompanyCode,"%s",qry.value(0).toString().toUtf8().data()); sprintf(webData->buildings[count].BuildingCode,"%s",qry.value(5).toString().toUtf8().data()); sprintf(webData->buildings[count].BuildingName,"%s",qry.value(6).toString().toUtf8().data()); sprintf(webData->buildings[count].FullAddress,"%s",qry.value(7).toString().toUtf8().data()); sprintf(webData->buildings[count].Address,"%s",qry.value(9).toString().toUtf8().data()); webData->buildings[count].Lng = Lng; webData->buildings[count].Lat = Lat; webData->companys[count].UpdateTime = UpdateTime; webData->buildings[count].UpdateTime = UpdateTime; } printf("CompanyCode:%s\n",qry.value(0).toString().toUtf8().data()); printf("CompanysCount:%d\n",count); count++; if(count>webData->BuildingsCount) webData->BuildingsCount = count; if(count>webData->CompanysCount) webData->CompanysCount = count; if(count>webData->CompanysAlarmCount) webData->CompanysAlarmCount = count; if(count>10239) break; usleep(5000); { time_t tmp_time = time(static_cast(nullptr)); ytShm->updatetime[DB_THREAD] = tmp_time; } } webData->CompanysCount = count; webData->BuildingsCount = count; webData->CompanysAlarmCount = count; qry.clear(); } void DB_SyncThread::checkUsers() { printf("checkUsers start\n"); unsigned int count = 0; QSqlQuery qry = mdb.exec("select username, password, truename, status from sp_user order by id"); while(qry.next()){ printf("LoginName:%s\n",qry.value(0).toString().toUtf8().data()); if((webData->users[count].Status!=(qry.value(3).toUInt()&0xff)) ||(QString::compare(QString::fromUtf8(webData->users[count].LoginName),qry.value(0).toString())!=0) ||(QString::compare(QString::fromUtf8(webData->users[count].Password),qry.value(1).toString())!=0) ||(QString::compare(QString::fromUtf8(webData->users[count].UserName),qry.value(2).toString())!=0)){ printf("LoginName123456:%s\n",qry.value(0).toString().toUtf8().data()); sprintf(webData->users[count].LoginName,"%s",qry.value(0).toString().toUtf8().data()); sprintf(webData->users[count].Password,"%s",qry.value(1).toString().toUtf8().data()); sprintf(webData->users[count].UserName,"%s",qry.value(2).toString().toUtf8().data()); webData->users[count].Status = qry.value(3).toUInt()&0xff; webData->users[count].UpdateTime = UpdateTime; } count++; if(count>webData->UsersCount) webData->UsersCount = count; if(count>10239) break; usleep(5000); { time_t tmp_time = time(static_cast(nullptr)); ytShm->updatetime[DB_THREAD] = tmp_time; } } qry.clear(); count = 0; qry = mdb.exec("select agentid, owner_code from sp_owner_weibao order by id"); while(qry.next()){ if((QString::compare(QString::fromUtf8(webData->userCompanys[count].Loginname),qry.value(0).toString())!=0) ||(QString::compare(QString::fromUtf8(webData->userCompanys[count].CompanyCode),qry.value(1).toString())!=0)){ sprintf(webData->userCompanys[count].Loginname,"%s",qry.value(0).toString().toUtf8().data()); sprintf(webData->userCompanys[count].CompanyCode,"%s",qry.value(1).toString().toUtf8().data()); webData->userCompanys[count].UpdateTime = UpdateTime; } count++; if(count>webData->UserCompanyCount) webData->UserCompanyCount = count; if(count>20479) break; usleep(5000); { time_t tmp_time = time(static_cast(nullptr)); ytShm->updatetime[DB_THREAD] = tmp_time; } } qry.clear(); } void DB_SyncThread::run() { printf("DB_SyncThread start\n"); webShmInit(); times = 0; while(1) { time_t tmp_time = time(static_cast(nullptr)); ytShm->updatetime[DB_THREAD] = tmp_time; time_out(); sleep(5); } }