#include "ytcore.h" #include #include "yt_unit_shm.h" YT_UNIT_SHM *ytShm; extern bool myLog; extern bool myDebug; BpCore::BpCore(QObject *parent) : QObject(parent) { CHour = QDateTime::currentDateTime().time().hour(); Sid = "RQ0000000001"; Skey = "44c2710b-f7ff-4487-a556-bfae08232a09"; Spath = "http://sgdzpic.3322.org:8088"; DataServer = "http://172.16.120.250:8080/datashare/ws"; timer = new QTimer(this); connect(timer,SIGNAL(timeout()),this,SLOT(time_out())); db = QSqlDatabase::addDatabase("QSQLITE","conf_db"); db.setDatabaseName(QString(CONF_DB)); stadb = QSqlDatabase::addDatabase("QSQLITE","sta_db"); stadb.setDatabaseName(QString(STA_DB)); recdb = QSqlDatabase::addDatabase("QSQLITE","rec_db"); recdb.setDatabaseName(QString(REC_DB)); if((!db.open())||(!stadb.open())||(!recdb.open())) { system("echo 'Database open failed.\n'"); exit(-1); } db_init(); sender = new MessageSender(this,Spath+"/agbox/device/perception",Skey,Sid); connect(sender,SIGNAL(sent(QString)),this,SLOT(mse_send(QString))); if(!shm_load()) { system("echo 'Memory load failed.\n'"); exit(-1); } logThread = new LogThread(this); logThread->start(); if(myLog) logThread->appendLog(QString("[%1] main appliction start\r\n") .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))); dbThread = new DBThread(this,&db,&stadb,&recdb); connect(dbThread,SIGNAL(log(QString)),this,SLOT(log(QString))); connect(dbThread,SIGNAL(dbg_log(QString)),this,SLOT(dbg_log(QString))); ytShm->dogTimeList.dogTime[CORE_MAIN].IDX = CORE_MAIN; ytShm->dogTimeList.dogTime[CORE_MAIN].WAITSEC = 30; ytShm->dogTimeList.dogTime[CORE_MAIN].ENABLED = 0x01; timer->start(500); dbThread->start(); for(quint8 CommId=0;CommId<4;CommId++) { if(ytShm->commList.comm[CommId].ENABLED==0x01) { dnThread[CommId] = new DNCommThread(this,CommId,true,QString(ytShm->commList.comm[CommId].PATH)); dnThread[CommId]->setCommType(ytShm->commList.comm[CommId].CommType); connect(dnThread[CommId],SIGNAL(log(QString)),this,SLOT(log(QString))); connect(dnThread[CommId],SIGNAL(dbg_log(QString)),this,SLOT(dbg_log(QString))); connect(dnThread[CommId],SIGNAL(recsql(QString)),this,SLOT(recsql(QString))); connect(dnThread[CommId],SIGNAL(stasql(QString)),this,SLOT(stasql(QString))); connect(dnThread[CommId],SIGNAL(confsql(QString)),this,SLOT(confsql(QString))); dnThread[CommId]->start(); } } } void BpCore::run() { } void BpCore::log(QString txt) { if(myLog) logThread->appendLog(txt); } void BpCore::dbg_log(QString txt) { if(myDebug) logThread->appendDbgLog(txt); } void BpCore::confsql(QString sql) { dbThread->appendSQL(1,sql); } void BpCore::stasql(QString sql) { dbThread->appendSQL(2,sql); } void BpCore::recsql(QString sql) { dbThread->appendSQL(3,sql); } void BpCore::mse_send(QString id) { recsql(QString("update yt_rec_report set sent=1 where Id='%1'").arg(id)); } void BpCore::time_out() { ytShm->dogTimeList.dogTime[CORE_MAIN].LASTFEED = QDateTime::currentDateTime().toTime_t(); for(int cid=0;cid<4;cid++){ for(int pid=0;pid<256;pid++){ if(ytShm->sPointList.sPoint[cid][pid].ENABLED==0x01) { if(ytShm->spStatusList.spStatus[cid][pid].ALARM_SENT==0x00) { if((ytShm->dogTimeList.dogTime[CORE_MAIN].LASTFEED-ytShm->spStatusList.spStatus[cid][pid].ALARM_TIMESTAMP)>ytShm->sPointList.sPoint[cid][pid].PNO_DLY){ ytShm->spStatusList.spStatus[cid][pid].ALARM_SENT=0x01; int code = -1; if(pid==1){ if(ytShm->spStatusList.spStatus[cid][pid].ALARM_STATUS!=0x00) code = 89; else if(ytShm->spStatusList.spStatus[cid][2].ALARM_STATUS==0x00) code = 91; }else if(pid==2){ if(ytShm->spStatusList.spStatus[cid][pid].ALARM_STATUS!=0x00) code = 90; else if(ytShm->spStatusList.spStatus[cid][1].ALARM_STATUS==0x00) code = 91; }else if(pid==3){ if(ytShm->spStatusList.spStatus[cid][pid].ALARM_STATUS!=0x00) code = 92; else code = 93; }else if(pid==4){ if(ytShm->spStatusList.spStatus[cid][pid].ALARM_STATUS!=0x00) code = 94; else code = 95; }else if(pid==5){ if(ytShm->spStatusList.spStatus[cid][pid].ALARM_STATUS!=0x00) code = 96; else code = 97; }else if(pid==6){ if(ytShm->spStatusList.spStatus[cid][pid].ALARM_STATUS!=0x00) code = 98; else code = 99; }else if(pid==7){ if(ytShm->spStatusList.spStatus[cid][pid].ALARM_STATUS!=0x00) code = 100; else code = 101; } if(code>=0){ QString uid = QUuid::createUuid().toString().replace("{","").replace("}",""); QString sql =QString("insert into yt_rec_report (Id, channel, code, time, note, sent) values ('%1',%2,%3,%4,'',0);") .arg(uid).arg(cid+1).arg(code).arg(ytShm->spStatusList.spStatus[cid][pid].ALARM_TIMESTAMP); // printf("%s\n",sql.toUtf8().data()); dbThread->appendSQL(3,sql); Msg msg; msg.ID = uid; msg.MSG = QString("key=%1&json={\"jsonrpc\":\"2.0\",\"id\":\"%2\",\"method\":\"addEvent\",\"params\":{\"deviceId\":\"%2\",\"channel\":%3,\"triggerTime\":\"%4\",\"eventCode\":%5,\"note\":\"%6\"}}") .arg(Skey).arg(Sid).arg(cid+1) .arg(QDateTime::fromTime_t(ytShm->spStatusList.spStatus[cid][pid].ALARM_TIMESTAMP).toString("yyyy-MM-dd HH:mm:ss")) .arg(code).arg(""); sender->appendMsg(msg); } } } } } } if(CHour!=(QDateTime::currentDateTime().time().hour())){ CHour = QDateTime::currentDateTime().time().hour(); for(int cid=0;cid<4;cid++){ for(int pid=0;pid<256;pid++){ if(ytShm->sPointList.sPoint[cid][pid].ENABLED==0x01) { if(ytShm->spStatusList.spStatus[cid][pid].COUNT>0){ QString sql = QString("insert into yt_rec_data (Id, CommId, BusAdd, AveValue, StoreTime, MaxValue, MaxTime, MinValue, MinTime) values (NULL,%1,%2,%3,%4,%5,%6,%7,%8)") .arg(cid).arg(pid).arg(ytShm->spStatusList.spStatus[cid][pid].TOTAL_VALUE/ytShm->spStatusList.spStatus[cid][pid].COUNT) .arg(ytShm->spStatusList.spStatus[cid][pid].REALTIME_TIMESTAMP).arg(ytShm->spStatusList.spStatus[cid][pid].MAX_VALUE) .arg(ytShm->spStatusList.spStatus[cid][pid].MAX_TIMESTAMP).arg(ytShm->spStatusList.spStatus[cid][pid].MIN_VALUE) .arg(ytShm->spStatusList.spStatus[cid][pid].MIN_TIMESTAMP); dbThread->appendSQL(3,sql); ytShm->spStatusList.spStatus[cid][pid].COUNT = 0; } } } } } } bool BpCore::chk_table(QString tbname, QStringList tables) { for(int i=0;i0))||((busadd>127)&&(busadd<133)))){ if(busadd<11){ db.exec(QString("insert into yt_t_point (CommId, BusAdd, Enabled, Name, PnoType, PnoAlarm, PnoValue, PnoDly, PnoAd, Offset, OffsetFlag, PnoMax, PnoMin, AlarmH, AlarmL,SubType) values (%1,%2,1,'%3',1,1,0,0,0,0,0,0,0,0,0,%4)") .arg(commid).arg(busadd).arg(QString("P_%1_%2").arg(commid).arg(busadd,3,10,QChar('0'))).arg(0x01<<((busadd-1)/2))); }else{ db.exec(QString("insert into yt_t_point (CommId, BusAdd, Enabled, Name, PnoType, PnoAlarm, PnoValue, PnoDly, PnoAd, Offset, OffsetFlag, PnoMax, PnoMin, AlarmH, AlarmL,SubType) values (%1,%2,1,'%3',2,0,0,0,0,0,0,0,0,0,0,%4)") .arg(commid).arg(busadd).arg(QString("P_%1_%2").arg(commid).arg(busadd,3,10,QChar('0'))).arg(0x01<<(busadd-128))); } }else db.exec(QString("insert into yt_t_point (CommId, BusAdd, Enabled, Name, PnoType, PnoAlarm, PnoValue, PnoDly, PnoAd, Offset, OffsetFlag, PnoMax, PnoMin, AlarmH, AlarmL,SubType) values (%1,%2,0,'%3',0,0,0,0,0,0,0,0,0,0,0,0)") .arg(commid).arg(busadd).arg(QString("P_%1_%2").arg(commid).arg(busadd,3,10,QChar('0')))); } } db.commit(); } } if(!chk_table("yt_rec_report",recdb.tables())) { if(recdb.transaction()) { recdb.exec(QString("create table yt_rec_report (Id text, channel integer, code integer, time integer, note text, sent integer)")); recdb.commit(); } } if(!chk_table("yt_rec_data",recdb.tables())) { if(recdb.transaction()) { recdb.exec(QString("create table yt_rec_data (Id integer primary key autoincrement, CommId integer, BusAdd integer, AveValue real, StoreTime integer, MaxValue real, MaxTime integer, MinValue real, MinTime integer)")); recdb.commit(); } } if(!chk_table("yt_sta_comm",stadb.tables())) { if(stadb.transaction()) { stadb.exec(QString("create table yt_sta_comm (CommId integer, Status integer, Updatetime integer, Sent integer)")); for(int commid=0;commid<4;commid++) { stadb.exec(QString("insert into yt_sta_comm (CommId, Status, Updatetime, Sent) values (%1, 1, 0, 1)").arg(commid)); } stadb.commit(); } } if(!chk_table("yt_sta_pno",stadb.tables())) { stadb.exec(QString("create table yt_sta_pno (CommId integer, BusAdd integer, RealtimeValue real, RealtimeTimestamp integer, AlarmStatus integer, AlarmTimestamp integer, AlarmSent integer, MaxValue real, MaxTimestamp integer, MinValue real, MinTimestamp integer, TotalValue real, AveValue real, Count integer)")); for(int commid=0;commid<4;commid++) { for(int busadd=0;busadd<=255;busadd++) { stadb.exec(QString("insert into yt_sta_pno (CommId, BusAdd, RealtimeValue, RealtimeTimestamp, AlarmStatus, AlarmTimestamp, AlarmSent, MaxValue, MaxTimestamp, MinValue, MinTimestamp, TotalValue, AveValue, Count) values (%1, %2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0)") .arg(commid).arg(busadd)); } } } QSqlQuery qry = db.exec("select Id, SelfId, Key, UpUrl from yt_t_upcom"); if(qry.next()) { Sid = qry.value(1).toString(); Skey = qry.value(2).toString(); Spath = qry.value(3).toString(); } qry.clear(); qry = db.exec("select DataServer from yt_t_dataserver"); if(qry.next()) { DataServer = qry.value(0).toString(); } } void BpCore::shm_init() { QSqlQuery qry; qry = db.exec("select Id, CommType, CommPath, Enabled from yt_t_comm"); while(qry.next()) { quint8 id = qry.value(0).toUInt()&0xff; quint16 commType = qry.value(1).toUInt()&0xffff; QString commPath = qry.value(2).toString(); quint8 enabled = ((qry.value(3).toUInt()&0xff)==0x01)?0x01:0x00; ytShm->commList.comm[id].IDX = id; ytShm->commList.comm[id].CommType = commType; sprintf(ytShm->commList.comm[id].PATH,"%s",commPath.toUtf8().data()); ytShm->commList.comm[id].ENABLED = enabled; ytShm->commList.comm[id].UNSAVED = 0x00; // printf("ID:%d\t ENABLED:%d\tPATH:%s\n",id,enabled,commPath.toUtf8().data()); } qry.clear(); qry = db.exec("select CommId, BusAdd, Enabled, Name, PnoType, PnoAlarm, PnoValue, PnoDly, PnoAd, Offset, OffsetFlag, PnoMax, PnoMin, AlarmH, AlarmL,SubType from yt_t_point"); while(qry.next()) { quint8 commId = qry.value(0).toUInt()&0xff; quint8 busAdd = qry.value(1).toUInt()&0xff; quint8 enabled = ((qry.value(2).toUInt()&0xff)==0x01)?0x01:0x00; QString name = qry.value(3).toString(); quint8 pnotype = qry.value(4).toUInt()&0xff; quint8 pnoalarm = qry.value(5).toUInt()&0xff; quint8 pnovalue = qry.value(6).toUInt()&0xff; quint8 pnodly = qry.value(7).toUInt()&0xff; quint8 pnoad = qry.value(8).toUInt()&0xff; quint8 offset = qry.value(9).toUInt()&0xff; quint8 offsetflag = qry.value(10).toUInt()&0xff; float pnomax = qry.value(11).toFloat(); float pnomin = qry.value(12).toFloat(); float alarmh = qry.value(13).toFloat(); float alarml = qry.value(14).toFloat(); uint subtype = qry.value(15).toUInt(); if(commId<4){ ytShm->sPointList.sPoint[commId][busAdd].COMM_IDX = commId; ytShm->sPointList.sPoint[commId][busAdd].BUS_ADD = busAdd; ytShm->sPointList.sPoint[commId][busAdd].ENABLED = enabled; bzero(ytShm->sPointList.sPoint[commId][busAdd].NAME,sizeof(ytShm->sPointList.sPoint[commId][busAdd].NAME)); sprintf(ytShm->sPointList.sPoint[commId][busAdd].NAME,"%s",name.toUtf8().data()); ytShm->sPointList.sPoint[commId][busAdd].PNO_TYPE = pnotype; ytShm->sPointList.sPoint[commId][busAdd].PNO_ALARM = pnoalarm; ytShm->sPointList.sPoint[commId][busAdd].PNO_VALUE = pnovalue; ytShm->sPointList.sPoint[commId][busAdd].PNO_DLY = pnodly; ytShm->sPointList.sPoint[commId][busAdd].PNO_AD = pnoad; ytShm->sPointList.sPoint[commId][busAdd].OFFSET = offset; ytShm->sPointList.sPoint[commId][busAdd].OFFSET_FLAG = offsetflag; ytShm->sPointList.sPoint[commId][busAdd].PNO_MAX = pnomax; ytShm->sPointList.sPoint[commId][busAdd].PNO_MIN = pnomin; ytShm->sPointList.sPoint[commId][busAdd].ALARM_H = alarmh; ytShm->sPointList.sPoint[commId][busAdd].ALARM_L = alarml; ytShm->sPointList.sPoint[commId][busAdd].SUB_TYPE = subtype; ytShm->sPointList.sPoint[commId][busAdd].UNSAVED = 0x00; } } qry.clear(); qry = stadb.exec("select CommId, Status, Updatetime, Sent from yt_sta_comm"); while(qry.next()) { quint8 commid = qry.value(0).toUInt()&0xff; quint8 status = qry.value(1).toUInt()&0xff; uint updatetime = qry.value(2).toUInt(); bool alarmsent = (qry.value(3).toUInt()==0x01); if(commid<4) { ytShm->commStatusList.commStatus[commid].STATUS = status; ytShm->commStatusList.commStatus[commid].TIMESTAMP = updatetime; ytShm->commStatusList.commStatus[commid].SENT = alarmsent; ytShm->commStatusList.commStatus[commid].UNSAVED = 0x00; } } qry.clear(); qry = stadb.exec("select CommId, BusAdd, RealtimeValue, RealtimeTimestamp, AlarmStatus, AlarmTimestamp, AlarmSent, MaxValue, MaxTimestamp, MinValue, MinTimestamp, TotalValue, AveValue, Count from yt_sta_pno"); while(qry.next()) { quint8 commid = qry.value(0).toUInt()&0xff; quint8 busadd = qry.value(1).toUInt()&0xff; float realtimevalue = qry.value(2).toFloat(); uint realtimetimestamp = qry.value(3).toUInt(); quint8 alarmstatus =qry.value(4).toUInt()&0xff; uint alarmtimestamp = qry.value(5).toUInt(); quint8 alarmsent = qry.value(6).toUInt()&0x01; float maxvalue = qry.value(7).toFloat(); uint maxtimestamp = qry.value(8).toUInt()&0xff; float minvalue = qry.value(9).toFloat(); uint mintimestamp = qry.value(10).toUInt(); float totalvalue = qry.value(11).toFloat(); float avevalue = qry.value(12).toFloat(); uint count = qry.value(13).toUInt(); if(commid<4){ QDateTime vtime = QDateTime::fromTime_t(realtimetimestamp); ytShm->spStatusList.spStatus[commid][busadd].REALTIME_VALUE = realtimevalue; ytShm->spStatusList.spStatus[commid][busadd].REALTIME_TIMESTAMP = realtimetimestamp; ytShm->spStatusList.spStatus[commid][busadd].ALARM_STATUS = alarmstatus; ytShm->spStatusList.spStatus[commid][busadd].ALARM_TIMESTAMP = alarmtimestamp; ytShm->spStatusList.spStatus[commid][busadd].ALARM_SENT = alarmsent; ytShm->spStatusList.spStatus[commid][busadd].MAX_VALUE = maxvalue; ytShm->spStatusList.spStatus[commid][busadd].MAX_TIMESTAMP = maxtimestamp; ytShm->spStatusList.spStatus[commid][busadd].MIN_VALUE = minvalue; ytShm->spStatusList.spStatus[commid][busadd].MIN_TIMESTAMP = mintimestamp; ytShm->spStatusList.spStatus[commid][busadd].TOTAL_VALUE = totalvalue; ytShm->spStatusList.spStatus[commid][busadd].AVE_VALUE = avevalue; ytShm->spStatusList.spStatus[commid][busadd].COUNT = count; ytShm->spStatusList.spStatus[commid][busadd].UNSAVED = 0x00; } } qry.clear(); qry = recdb.exec("select Id, channel, code, time, note from yt_rec_report where sent=0"); while(qry.next()){ Msg msg; msg.ID = qry.value(0).toString(); msg.MSG = QString("key=%1&json={\"jsonrpc\":\"2.0\",\"id\":\"%2\",\"method\":\"addEvent\",\"params\":{\"deviceId\":\"%2\",\"channel\":%3,\"triggerTime\":\"%4\",\"eventCode\":%5,\"note\":\"%6\"}}") .arg(Skey).arg(Sid).arg(qry.value(1).toInt()) .arg(QDateTime::fromTime_t(qry.value(3).toUInt()).toString("yyyy-MM-dd HH:mm:ss")) .arg(qry.value(2).toInt()).arg(qry.value(4).toString()); sender->appendMsg(msg); } } bool BpCore::shm_load() { int shmid; key_t key; if((key = ftok(SHM_PATH,(int)SHM_PORT))==-1) return false; if((shmid = shmget(key,sizeof(YT_UNIT_SHM),IPC_CREAT|0666))==-1) return false; ytShm = (YT_UNIT_SHM *)shmat(shmid,NULL,0); shm_init(); return true; }