Procházet zdrojové kódy

优化ytDataCollector推送逻辑,增加访问118线上数据库,然后根据设备类型和公司编码获取对应的产品编码

james před 6 dny
rodič
revize
1f6dd08140

+ 14 - 5
ytDataCollector/ytDataCollectorCore/databasethread.cpp

@@ -12,13 +12,16 @@ DatabaseThread::DatabaseThread(QObject *parent) :
     QThread(parent)
 {
     hour = 255;
-    db = QSqlDatabase::addDatabase("QMYSQL","smokerdevicesensor_db");
+    db = QSqlDatabase::addDatabase("QMYSQL","smokerdevice_db");
     db.setHostName(QString(HostName));
     db.setPort(HostPort);
     db.setUserName(QString(UserName));
     db.setPassword(QString(PassWord));
     db.setDatabaseName(QString(DatabaseName));
     db.open();
+    printf("database thread init\n");
+
+    keep = false;
 }
 
 void DatabaseThread::check_devicelist()
@@ -28,18 +31,24 @@ void DatabaseThread::check_devicelist()
     while(qry.next()){
         printf("---nrow = %d, Device_Code = %s, Device_Name = %s,Company_Code = %s\n",nrow,qry.value(0).toString().toUtf8().data(),qry.value(1).toString().toUtf8().data(),qry.value(2).toString().toUtf8().data());
         sprintf(smokerDeviceList->sensors[nrow].Company_Code,"%s",qry.value(0).toString().toUtf8().data());
-        smokerDeviceList->sensors[nrow].Device_Type=qry.value(1).toInt();
+        sprintf(smokerDeviceList->sensors[nrow].Device_Type,"%s",qry.value(1).toString().toUtf8().data());
         sprintf(smokerDeviceList->sensors[nrow].product_code,"%s",qry.value(2).toString().toUtf8().data());
         nrow++;
     }
     qry.clear();
 }
 
+void DatabaseThread::stop()
+{
+    keep = false;
+}
+
 void DatabaseThread::run()
 {
+    keep = true;
     printf("database thread start\n");
 
-    while(1){
+    while(keep){
         if(!db.isOpen()){
             db.open();
         }else{
@@ -51,6 +60,6 @@ void DatabaseThread::run()
         }
         usleep(50000);
     }
-    //if(db.isOpen())
-        //db.close();
+    if(db.isOpen())
+        db.close();
 }

+ 2 - 0
ytDataCollector/ytDataCollectorCore/databasethread.h

@@ -17,6 +17,7 @@ public:
     explicit DatabaseThread(QObject *parent = nullptr);
     void appendSql(QString sql);
     void run();
+    void stop();
     void check_devicelist();
 signals:
     void AlarmReport(QString subtype, QString addr, quint64 insertid, QString type, QString time);
@@ -25,6 +26,7 @@ public slots:
 
 private:
     int hour;
+    bool keep;
     QSqlDatabase db;
 };
 

+ 1 - 0
ytDataCollector/ytDataCollectorCore/datacollectorcore.cpp

@@ -29,6 +29,7 @@ DataCollectorCore::DataCollectorCore(QObject *parent) : QObject(parent)
 DataCollectorCore::~DataCollectorCore()
 {
     logthread->stop();
+    dbthread->stop();
     dataPub->stop();
 
 }

+ 3 - 3
ytDataCollector/ytDataCollectorCore/datacollectorpub.cpp

@@ -176,9 +176,9 @@ void DataCollectorPub::run()
                             }
                         }
                         QString dt = dev_obj.value("deviceType").toString();
-                        for(int i;i<DEV_MAXCOUNT;i++){
-                            if((companyCode.compare(smokerDeviceList->sensors[i].Company_Code)==0)&&(dt.compare(QString::number(smokerDeviceList->sensors[i].Device_Type))==0)){
-                                productId = smokerDeviceList->sensors[i].product_code;
+                        for(int i=0;i<DEV_MAXCOUNT;i++){
+                            if((QString::compare(QString(smokerDeviceList->sensors[i].Company_Code),companyCode)==0)&&(QString::compare(QString(smokerDeviceList->sensors[i].Device_Type),dt)==0)){
+                                productId = QString(smokerDeviceList->sensors[i].product_code);
                                 break;
                             }
                         }

+ 4 - 1
ytDataCollector/ytDataCollectorCore/main.cpp

@@ -15,13 +15,16 @@ bool load_shm()
     if((shmid = shmget(key,sizeof(DataCollectorShm),IPC_CREAT|0666))==-1){
         return false;
     }
+    printf("shmid111: %d\n",shmid);
     dataColShm = static_cast<DataCollectorShm *>(shmat(shmid,nullptr,0));
-    if((key = ftok(SHM_PATH,static_cast<int>(SHM_DEVPORT))) == -1){
+    if((key = ftok(SHM_DEVPATH,static_cast<int>(SHM_DEVPORT))) == -1){
         return false;
     }
+    printf("shmid1111: %d\n",shmid);
     if((shmid = shmget(key,sizeof(SmokerDeviceSensorList),IPC_CREAT|0666))==-1){
         return false;
     }
+    printf("shmid1: %d\n",shmid);
     smokerDeviceList = static_cast<SmokerDeviceSensorList *>(shmat(shmid,nullptr,0));
 
     return true;

+ 2 - 1
ytDataCollector/ytDataCollectorDog/datacollectorshm.h

@@ -9,6 +9,7 @@
 #include <sys/ipc.h>
 
 #define SHM_PATH "/"
+#define SHM_DEVPATH "/"
 #define SHM_PORT 5915
 #define SHM_DEVPORT 5916
 #define DEV_MAXCOUNT  10240
@@ -21,7 +22,7 @@ typedef struct {
 
 typedef struct {
     char Company_Code[48];
-    int Device_Type;
+    char Device_Type[12];
     char product_code[48];
 }SmokerDeviceSensor;
 

+ 2 - 2
ytDataCollector/ytDataCollectorDog/dogcore.cpp

@@ -17,12 +17,12 @@ DogCore::~DogCore()
 void DogCore::time_out()
 {
     chkTime = QDateTime::currentDateTime().toTime_t();
-    if((chkTime - dataColShm->time)>30){
+    if((chkTime - dataColShm->time)>300){
         if((system("killall ytDataCollectorCore")) != -1){
             system("/root/bin/ytDataCollectorCore &");
         }
     }
-    if((chkTime - dataColShm->influtime)>30){
+    if((chkTime - dataColShm->influtime)>300){
         if((system("killall ytDeviceTypeInfluxdbWriter")) != -1){
             system("/root/bin/ytDeviceTypeInfluxdbWriter &");
         }

+ 4 - 1
ytDataCollector/ytDataCollectorDog/main.cpp

@@ -15,13 +15,16 @@ bool load_shm()
     if((shmid = shmget(key,sizeof(DataCollectorShm),IPC_CREAT|0666))==-1){
         return false;
     }
+    printf("shmid1: %d\n",shmid);
     dataColShm = static_cast<DataCollectorShm *>(shmat(shmid,nullptr,0));
-    if((key = ftok(SHM_PATH,static_cast<int>(SHM_DEVPORT))) == -1){
+    if((key = ftok(SHM_DEVPATH,static_cast<int>(SHM_DEVPORT))) == -1){
         return false;
     }
+    printf("shmid11: %d\n",shmid);
     if((shmid = shmget(key,sizeof(SmokerDeviceSensorList),IPC_CREAT|0666))==-1){
         return false;
     }
+    printf("shmid2: %d\n",shmid);
     smokerDeviceList = static_cast<SmokerDeviceSensorList *>(shmat(shmid,nullptr,0));
 
     return true;