ソースを参照

调整2.0单位设备上报数据到3.0平台的逻辑,通过2.0上报mqtt中的设备类型和company值查询3.0对应设备信息表记录找到对应的产品编码

james 1 週間 前
コミット
9f42155daf

+ 56 - 0
ytDataCollector/ytDataCollectorCore/databasethread.cpp

@@ -0,0 +1,56 @@
+#include "databasethread.h"
+#include "../ytDataCollectorDog/datacollector.h"
+
+#define HostName  "47.111.81.118"
+#define HostPort 13307
+#define UserName "usky"
+#define PassWord "Yt#75Usky"
+#define DatabaseName "usky-cloud"
+
+
+DatabaseThread::DatabaseThread(QObject *parent) :
+    QThread(parent)
+{
+    hour = 255;
+    db = QSqlDatabase::addDatabase("QMYSQL","smokerdevicesensor_db");
+    db.setHostName(QString(HostName));
+    db.setPort(HostPort);
+    db.setUserName(QString(UserName));
+    db.setPassword(QString(PassWord));
+    db.setDatabaseName(QString(DatabaseName));
+    db.open();
+}
+
+void DatabaseThread::check_devicelist()
+{
+    QSqlQuery qry = db.exec("select distinct company_code,device_type,product_code from dmp_device where company_code is not null");
+    int nrow= 0;
+    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].product_code,"%s",qry.value(2).toString().toUtf8().data());
+        nrow++;
+    }
+    qry.clear();
+}
+
+void DatabaseThread::run()
+{
+    printf("database thread start\n");
+
+    while(1){
+        if(!db.isOpen()){
+            db.open();
+        }else{
+            if(QTime::currentTime().hour()!=hour){
+                hour=QTime::currentTime().hour();
+               check_devicelist();
+            }
+            db.close();
+        }
+        usleep(50000);
+    }
+    //if(db.isOpen())
+        //db.close();
+}

+ 31 - 0
ytDataCollector/ytDataCollectorCore/databasethread.h

@@ -0,0 +1,31 @@
+#ifndef DATABASETHREAD_H
+#define DATABASETHREAD_H
+
+#include <QThread>
+#include <QDateTime>
+#include <QSqlDatabase>
+#include <QSqlQuery>
+#include <QSqlError>
+#include <QVariant>
+#include <QStringList>
+#include <QList>
+
+class DatabaseThread : public QThread
+{
+    Q_OBJECT
+public:
+    explicit DatabaseThread(QObject *parent = nullptr);
+    void appendSql(QString sql);
+    void run();
+    void check_devicelist();
+signals:
+    void AlarmReport(QString subtype, QString addr, quint64 insertid, QString type, QString time);
+    void mqttinfo(QString ip,QString port,QString name,QString passwd);
+public slots:
+
+private:
+    int hour;
+    QSqlDatabase db;
+};
+
+#endif // DATABASETHREAD_H

+ 4 - 1
ytDataCollector/ytDataCollectorCore/datacollectorcore.cpp

@@ -7,6 +7,9 @@ DataCollectorCore::DataCollectorCore(QObject *parent) : QObject(parent)
     logthread = new logThread(this);
     logthread->start();
 
+    dbthread = new DatabaseThread(this);
+    dbthread->start();
+
     dataSub = new DataCollectorSub(this);
     connect(dataSub,&DataCollectorSub::sendDevData,this,&DataCollectorCore::receiveDevData);
 
@@ -46,7 +49,7 @@ void DataCollectorCore::publishDevData(QByteArray data)
     if((m_client->connectionState()==QMQTT::STATE_DISCONNECTED)||(m_client->connectionState()==QMQTT::STATE_INIT)){
         m_client->connectToHost();
     }
-    m_client->publish(QMQTT::Message(mqttIdx++,"data-collector",data));
+    m_client->publish(QMQTT::Message(mqttIdx++,"data-collector",data,2));
     if(mqttIdx > 9999)
     {
         mqttIdx = 1;

+ 2 - 0
ytDataCollector/ytDataCollectorCore/datacollectorcore.h

@@ -6,6 +6,7 @@
 #include "logthread.h"
 #include "datacollectorsub.h"
 #include "datacollectorpub.h"
+#include "databasethread.h"
 
 class DataCollectorCore : public QObject
 {
@@ -25,6 +26,7 @@ public slots:
 
 private:
     logThread *logthread;
+    DatabaseThread *dbthread;
     DataCollectorSub *dataSub;
     DataCollectorPub *dataPub;
     quint16 mqttIdx;

+ 55 - 16
ytDataCollector/ytDataCollectorCore/datacollectorpub.cpp

@@ -1,4 +1,5 @@
 #include "datacollectorpub.h"
+#include "../ytDataCollectorDog/datacollector.h"
 
 DataCollectorPub::DataCollectorPub(QObject *parent) : QThread(parent)
 {
@@ -28,6 +29,7 @@ void DataCollectorPub::run()
                 QString topic = devdata.topic;
                 QByteArray data = devdata.data;
                 QString devType = (topic.split("/")).at(2);
+                QString companyCode = (topic.split("/")).at(3);
                 QString productId = "";
                 QString deviceId = (topic.split("/")).at(4);
 
@@ -67,6 +69,9 @@ void DataCollectorPub::run()
                     QString connType = root_obj.value("connType").toString();
                     QString dataType = root_obj.value("type").toString();
                     QString timeStamp = root_obj.value("timeStamp").toString();
+                    if(timeStamp.isEmpty()){
+                        timeStamp = QString("%1").arg(QDateTime::currentDateTime().toTime_t());
+                    }
                     QJsonValue dev_value = root_obj.value("devs");
 
                     if(dev_value.isArray()){
@@ -94,6 +99,9 @@ void DataCollectorPub::run()
                                     //printf("WaterPL: %s\n",WaterPL.toUtf8().data());
                                 }else if((name.compare("电量")==0)||(name.compare("剩余电量")==0)){
                                     battery = value;
+                                    if(battery.isEmpty()){
+                                        battery = "0";
+                                    }
                                     //printf("battery: %s\n",battery.toUtf8().data());
                                 }else if((name.compare("信号")==0)||(name.compare("无线信号")==0)){
                                     signal = value;
@@ -168,50 +176,79 @@ 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;
+                                break;
+                            }
+                        }
+
                         if(dt.compare("1")==0){
                             deviceType = "1-fhj";
-                            productId = "1_0001";
+                            if(productId == ""){
+                                productId = "1_0001";
+                            }
+
                             jsonStr = QString("{\"device_id\":\"%1\",\"product_id\":\"%2\",\"timestamp\":%3,\"tags\":{\"conn_type\":\"%4\",\"type\":\"%5\"},\"metrics\":{\"status\":%6},\"device_type\":\"%7\"}").arg(deviceId).arg(productId).arg(timeStamp).arg(connType).arg(dataType).arg(Status).arg(deviceType);
                         }else if(dt.compare("2")==0){
                             deviceType = "2-fsy";
-                            productId = "2_0001";
+                            if(productId == ""){
+                                productId = "2_0001";
+                            }
+
                             jsonStr = QString("{\"device_id\":\"%1\",\"product_id\":\"%2\",\"timestamp\":%3,\"tags\":{\"conn_type\":\"%4\",\"type\":\"%5\"},\"metrics\":{\"battery\":%6,\"signal\":%7,\"water_pl\":%8,\"status\":%9},\"device_type\":\"%10\"}").arg(deviceId).arg(productId).arg(timeStamp).arg(connType).arg(dataType).arg(battery).arg(signal).arg(WaterPL).arg(Status).arg(deviceType);
                         }else if(dt.compare("3")==0){
 
                             deviceType = "3-fyg";
-                            if(deviceId.compare("860348063655387")==0){
-                                productId = "3_17089462";
-                            }else{
-                                productId = "3_0001";
+                            if(productId == ""){
+                                if(deviceId.compare("860348063655387")==0){
+                                    productId = "3_17089462";
+                                }else{
+                                    productId = "3_0001";
+                                }
                             }
+
                             
                             jsonStr = QString("{\"device_id\":\"%1\",\"product_id\":\"%2\",\"timestamp\":%3,\"tags\":{\"conn_type\":\"%4\",\"type\":\"%5\"},\"metrics\":{\"battery\":%6,\"signal\":%7,\"temperature\":%8,\"density\":%9,\"status\":%10},\"device_type\":\"%11\"}").arg(deviceId).arg(productId).arg(timeStamp).arg(connType).arg(dataType).arg(battery).arg(signal).arg(Temperature).arg(density).arg(Status).arg(deviceType);
                         }else if(dt.compare("4")==0){
                             deviceType = "4-fxhs";
-                            productId = "4_0001";
+                            if(productId == ""){
+                                productId = "4_0001";
+                            }
+
                             jsonStr = QString("{\"device_id\":\"%1\",\"product_id\":\"%2\",\"timestamp\":%3,\"tags\":{\"conn_type\":\"%4\",\"type\":\"%5\"},\"metrics\":{\"battery\":%6,\"signal\":%7,\"water_pl\":%8,\"status\":%9},\"device_type\":\"%10\"}").arg(deviceId).arg(productId).arg(timeStamp).arg(connType).arg(dataType).arg(battery).arg(signal).arg(WaterPL).arg(Status).arg(deviceType);
                         }else if(dt.compare("5")==0){
                             deviceType = "5-fyw";
-                            productId = "5_0001";
+                            if(productId == ""){
+                                productId = "5_0001";
+                            }
                             jsonStr = QString("{\"device_id\":\"%1\",\"product_id\":\"%2\",\"timestamp\":%3,\"tags\":{\"conn_type\":\"%4\",\"type\":\"%5\"},\"metrics\":{\"battery\":%6,\"signal\":%7,\"water_pl\":%8,\"status\":%9},\"device_type\":\"%10\"}").arg(deviceId).arg(productId).arg(timeStamp).arg(connType).arg(dataType).arg(battery).arg(signal).arg(WaterPL).arg(Status).arg(deviceType);
                         }else if(dt.compare("6")==0){
                             deviceType = "6-frtu";
-                            productId = "6_0001";
+                            if(productId == ""){
+                                productId = "6_0001";
+                            }
                             jsonStr = QString("{\"device_id\":\"%1\",\"product_id\":\"%2\",\"timestamp\":%3,\"tags\":{\"conn_type\":\"%4\",\"type\":\"%5\"},\"metrics\":{\"power_alarm\":%6,\"manual_act\":%7,\"dev_working01\":%8,\"dev_working02\":%9,\"dev_alarm01\":%10,\"dev_alarm02\":%11,\"fire_auto_act\":%12},\"device_type\":\"%13\"}").arg(deviceId).arg(productId).arg(timeStamp).arg(connType).arg(dataType).arg(PowerAlarm).arg(ManualAct).arg(DevWorking01).arg(DevWorking02).arg(DevAlarm01).arg(DevAlarm02).arg(FireAutoAct).arg(deviceType);
                         }else if(dt.compare("7")==0){
                             deviceType = "7-fef";
-                            productId = "7_0001";
+                            if(productId == ""){
+                                productId = "7_0001";
+                            }
                             jsonStr = QString("{\"device_id\":\"%1\",\"product_id\":\"%2\",\"timestamp\":%3,\"tags\":{\"conn_type\":\"%4\",\"type\":\"%5\"},\"metrics\":{\"voltage_a\":%6,\"voltage_b\":%7,\"voltage_c\":%8,\"current_a\":%9,\"current_b\":%10,\"current_c\":%11,\"temperature_a\":%12,\"temperature_b\":%13,\"temperature_c\":%14},\"device_type\":\"%15\"}").arg(deviceId).arg(productId).arg(timeStamp).arg(connType).arg(dataType).arg(VoltageA).arg(VoltageB).arg(VoltageC).arg(CurrentA).arg(CurrentB).arg(CurrentC).arg(TemperatureA).arg(TemperatureB).arg(TemperatureC).arg(deviceType);
                         }else if(dt.compare("16")==0){
                             deviceType = "16-fvd";
                             productId = "16_0001";
                         }else if(dt.compare("128")==0){
                             deviceType = "128-fiw";
-                            productId = "128_0001";
+                            if(productId == ""){
+                                productId = "128_0001";
+                            }
                             jsonStr = QString("{\"device_id\":\"%1\",\"product_id\":\"%2\",\"timestamp\":%3,\"tags\":{\"conn_type\":\"%4\",\"type\":\"%5\"},\"metrics\":{\"angle\":%6,\"battery\":%7,\"signal\":%8,\"liquid\":%9,\"status\":%10},\"device_type\":\"%11\"}").arg(deviceId).arg(productId).arg(timeStamp).arg(connType).arg(dataType).arg(angle).arg(battery).arg(signal).arg(WaterPL).arg(Status).arg(deviceType);
                         }else if(dt.compare("129")==0){
                             deviceType = "129-fdc";
-                            productId = "129_0001";
+                            if(productId == ""){
+                                productId = "129_0001";
+                            }
                             jsonStr = QString("{\"device_id\":\"%1\",\"product_id\":\"%2\",\"timestamp\":%3,\"tags\":{\"conn_type\":\"%4\",\"type\":\"%5\"},\"metrics\":{\"battery\":%6,\"signal\":%7,\"status\":%8},\"device_type\":\"%9\"}").arg(deviceId).arg(productId).arg(timeStamp).arg(connType).arg(dataType).arg(battery).arg(signal).arg(Status).arg(deviceType);
                         }else if(dt.compare("130")==0){
                             deviceType = "130-feg";
@@ -221,10 +258,12 @@ void DataCollectorPub::run()
                             productId = "131_0001";
                         }else if(dt.compare("137")==0){
                             deviceType = "137-fjjan";
-                            if(deviceId.compare("862335058858769")==0){
-                                productId = "137_17089450";
-                            }else{
-                                productId = "137_0001";
+                            if(productId == ""){
+                                if(deviceId.compare("862335058858769")==0){
+                                    productId = "137_17089450";
+                                }else{
+                                    productId = "137_0001";
+                                }
                             }
 
                             jsonStr = QString("{\"device_id\":\"%1\",\"product_id\":\"%2\",\"timestamp\":%3,\"tags\":{\"conn_type\":\"%4\",\"type\":\"%5\"},\"metrics\":{\"battery\":%6,\"signal\":%7,\"temperature\":%8,\"density\":%9,\"status\":%10},\"device_type\":\"%11\"}").arg(deviceId).arg(productId).arg(timeStamp).arg(connType).arg(dataType).arg(battery).arg(signal).arg(Temperature).arg(density).arg(Status).arg(deviceType);

+ 11 - 4
ytDataCollector/ytDataCollectorCore/main.cpp

@@ -3,19 +3,26 @@
 #include "datacollectorcore.h"
 
 DataCollectorShm *dataColShm;
+SmokerDeviceSensorList *smokerDeviceList;
 
 bool load_shm()
 {
-    int shmid;
     key_t key;
-
-    if((key=ftok(SHM_PATH,static_cast<int>(SHM_PORT)))==-1){
+    int shmid;
+    if((key = ftok(SHM_PATH,static_cast<int>(SHM_PORT))) == -1){
         return false;
     }
-    if((shmid=shmget(key,sizeof(DataCollectorShm),IPC_CREAT|0666))==-1){
+    if((shmid = shmget(key,sizeof(DataCollectorShm),IPC_CREAT|0666))==-1){
         return false;
     }
     dataColShm = static_cast<DataCollectorShm *>(shmat(shmid,nullptr,0));
+    if((key = ftok(SHM_PATH,static_cast<int>(SHM_DEVPORT))) == -1){
+        return false;
+    }
+    if((shmid = shmget(key,sizeof(SmokerDeviceSensorList),IPC_CREAT|0666))==-1){
+        return false;
+    }
+    smokerDeviceList = static_cast<SmokerDeviceSensorList *>(shmat(shmid,nullptr,0));
 
     return true;
 }

+ 5 - 3
ytDataCollector/ytDataCollectorCore/ytDataCollectorCore.pro

@@ -1,5 +1,5 @@
 QT -= gui
-QT += core network websockets
+QT += core network websockets sql
 
 CONFIG += c++11 console
 CONFIG -= app_bundle
@@ -20,7 +20,8 @@ SOURCES += \
     datacollectorcore.cpp \
     datacollectorsub.cpp \
     datacollectorpub.cpp \
-    logthread.cpp
+    logthread.cpp \
+    databasethread.cpp
 
 # Default rules for deployment.
 qnx: target.path = /tmp/$${TARGET}/bin
@@ -33,6 +34,7 @@ HEADERS += \
     datacollectorcore.h \
     datacollectorsub.h \
     datacollectorpub.h \
-    logthread.h
+    logthread.h \
+    databasethread.h
 
 LIBS += -lQt5Qmqtt

+ 1 - 0
ytDataCollector/ytDataCollectorDog/datacollector.h

@@ -3,5 +3,6 @@
 
 #include "datacollectorshm.h"
 extern DataCollectorShm *dataColShm;
+extern SmokerDeviceSensorList *smokerDeviceList;
 
 #endif // DATACOLLECTOR_H

+ 13 - 0
ytDataCollector/ytDataCollectorDog/datacollectorshm.h

@@ -10,6 +10,8 @@
 
 #define SHM_PATH "/"
 #define SHM_PORT 5915
+#define SHM_DEVPORT 5916
+#define DEV_MAXCOUNT  10240
 
 typedef struct {
     uint time;
@@ -17,5 +19,16 @@ typedef struct {
 } DataCollectorShm;
 
 
+typedef struct {
+    char Company_Code[48];
+    int Device_Type;
+    char product_code[48];
+}SmokerDeviceSensor;
+
+typedef struct {
+    SmokerDeviceSensor sensors[DEV_MAXCOUNT];
+}SmokerDeviceSensorList;
+
+
 
 #endif // DATACOLLECTORSHM_H

+ 8 - 0
ytDataCollector/ytDataCollectorDog/main.cpp

@@ -3,6 +3,7 @@
 #include "datacollector.h"
 
 DataCollectorShm *dataColShm;
+SmokerDeviceSensorList *smokerDeviceList;
 
 bool load_shm()
 {
@@ -15,6 +16,13 @@ bool load_shm()
         return false;
     }
     dataColShm = static_cast<DataCollectorShm *>(shmat(shmid,nullptr,0));
+    if((key = ftok(SHM_PATH,static_cast<int>(SHM_DEVPORT))) == -1){
+        return false;
+    }
+    if((shmid = shmget(key,sizeof(SmokerDeviceSensorList),IPC_CREAT|0666))==-1){
+        return false;
+    }
+    smokerDeviceList = static_cast<SmokerDeviceSensorList *>(shmat(shmid,nullptr,0));
 
     return true;
 }