#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) { 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(); 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)); 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::time_out() { ytShm->dogTimeList.dogTime[CORE_MAIN].LASTFEED = QDateTime::currentDateTime().toTime_t(); } bool BpCore::chk_table(QString tbname, QStringList tables) { for(int i=0;icommList.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; } qry.clear(); qry = db.exec("select CommId, BusAdd, Enabled, Name, PnoType, PnoAlarm, PnoValue, PnoDly, PnoAd, Offset, OffsetFlag, PnoMax, PnoMin, AlarmH, AlarmL 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(11).toUInt()&0xff; float pnomax = qry.value(12).toFloat(); float pnomin = qry.value(13).toFloat(); float alarmh = qry.value(14).toFloat(); float alarml = qry.value(15).toFloat(); 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; 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].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(); bool 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(); } 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; }