Browse Source

Merge branch 'dev-zyj' of usky-cloud-paas/data-agbox into master

James 1 year ago
parent
commit
17a9d3e3c7

+ 2 - 2
AGBoxDog/dogcore.cpp

@@ -188,7 +188,7 @@ void DogCore::time_out()
             }
         }
         if((agBoxShm->processStatus[6].t_time>0)
-                &&(static_cast<int>(chkTime1-agBoxShm->processStatus[6].t_time)>60)){
+                &&(static_cast<int>(chkTime1-agBoxShm->processStatus[6].t_time)>90)){
             if(system("killall agDP0006")!=-1){
                 if(system("/root/bin/agDP0006 &")!=-1){
                     logThread->appendData(QString("[%1 agBoxDog %2] agDP0006 time out: %3")
@@ -208,7 +208,7 @@ void DogCore::time_out()
 
 void DogCore::start(){
     time_out();
-    timer->start(5000);
+    timer->start(15000);
 }
 
 

+ 4 - 3
agEnvironmentProcess/environmentcore.cpp

@@ -90,17 +90,17 @@ void EnvironmentCore::start()
     mqttsub->start();
     mqttsub->mqtt_conf(ip,port,username,password);
 
-    mqttsub->start();
+    mqttpub->start();
 }
 
 void EnvironmentCore::time_out()
 {
     for(int i=0;i<1024;i++){
-        if(boxDeviceShm->device[i].Enabled == 0x01){
+        if((boxDeviceShm->device[i].Enabled == 0x01)&&(boxDeviceShm->device[i].device_type == 509)){
             uint curTime = QDateTime::currentDateTime().toTime_t();
             if((curTime - boxDeviceShm->device[i].lastTime)>7200){
                 boxDeviceShm->device[i].lastTime = QDateTime::currentDateTime().toTime_t();
-                this->mqtt_data(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"conn_type\":\"\",\"type\":\"\"},\"metrics\":{\"device_status\":0},\"device_type\":\"%6-ev\"}")
+                this->mqtt_data(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"conn_type\":\"\",\"type\":\"\"},\"metrics\":{\"device_status\":0},\"device_type\":\"%5-ev\"}")
                               .arg(boxDeviceShm->device[i].device_id).arg(boxDeviceShm->device[i].device_code).arg(boxDeviceShm->device[i].company_code).arg(curTime).arg(boxDeviceShm->device[i].device_type));
             }
         }
@@ -120,6 +120,7 @@ void EnvironmentCore::dataLog(QString log)
 void EnvironmentCore::mqtt_data(QString mqtt_msg)
 {
     printf("test5555 [%s]\n",mqtt_msg.toUtf8().data());
+    logthread->appendData(QString("[%1] %2").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).arg(mqtt_msg));
     if((m_client->connectionState()==QMQTT::STATE_INIT)||(m_client->connectionState()==QMQTT::STATE_DISCONNECTED)){
         m_client->connectToHost();
     }

+ 2 - 2
agEnvironmentProcess/mqttpub.cpp

@@ -29,7 +29,7 @@ void MqttPub::run()
 
                 QJsonParseError parseErr;
                 QJsonDocument doc = QJsonDocument::fromJson(data,&parseErr);
-                printf("environment parseErr.error %d\n",parseErr.error);
+//                printf("environment parseErr.error %d\n",parseErr.error);
                 if(parseErr.error==QJsonParseError::NoError){
                     QString nodeInfo = "";
                     QString deviceId = "";
@@ -44,7 +44,7 @@ void MqttPub::run()
                         for(int i=0;i<list_array.size();i++){
                             QJsonObject obj_dev = list_array.at(i).toObject();
                             QString deviceCode = sn + "-" + obj_dev.value("dev").toString();
-                            printf("deviceCode: %s\n",deviceCode.toUtf8().data());
+//                            printf("deviceCode: %s\n",deviceCode.toUtf8().data());
                             for(int i=0;i<1024;i++){
                                 if(boxDeviceShm->device[i].Enabled == 0x01){
                                     if(deviceCode.compare(QString(boxDeviceShm->device[i].device_code))==0){

+ 6 - 3
agEnvironmentProcess/mqttsub.cpp

@@ -30,9 +30,12 @@ void MqttSub::mqtt_conf(QString ip,QString port,QString username,QString passwor
     mqtt_passwd = password;
 
 
-    m_client = new QMQTT::Client(QHostAddress(mqtt_ip),static_cast<quint16>(mqtt_port.toInt()),this);
-    m_client->setUsername(mqtt_username);
-    m_client->setPassword(mqtt_passwd.toLatin1());
+//    m_client = new QMQTT::Client(QHostAddress(mqtt_ip),static_cast<quint16>(mqtt_port.toInt()),this);
+//    m_client->setUsername(mqtt_username);
+//    m_client->setPassword(mqtt_passwd.toLatin1());
+    m_client = new QMQTT::Client(QHostAddress("172.17.35.51"),1883,this);
+    m_client->setUsername("admin");
+    m_client->setPassword("public");
     m_client->setCleanSession(true);
     connect(m_client,&QMQTT::Client::connected,this,&MqttSub::onConnected);
     connect(m_client,&QMQTT::Client::received,this,&MqttSub::onReceived);

+ 4 - 2
agGuardProcess/agGuardProcess.pro

@@ -12,7 +12,8 @@ SOURCES += \
     logthread.cpp \
     databasethread.cpp \
     httpthread.cpp \
-    guardcore.cpp
+    guardcore.cpp \
+    tcpserver.cpp
 
 INCLUDEPATH += ../qmqtt-master/src/mqtt/
 
@@ -22,7 +23,8 @@ HEADERS += \
     ../QReplyTimeout.h \
     databasethread.h \
     httpthread.h \
-    guardcore.h
+    guardcore.h \
+    tcpserver.h
 
 
 LIBS += -lQt5Qmqtt

+ 4 - 1
agGuardProcess/guardcore.cpp

@@ -72,6 +72,9 @@ GuardCore::GuardCore(QObject *parent) : QObject(parent)
     connect(httpthread,&HttpThread::mqttData,this,&GuardCore::mqtt_data);
     connect(httpthread,&HttpThread::appendSql,this,&GuardCore::append_sql);
 
+    tcpserver = new TcpServer(this);
+    connect(tcpserver,&TcpServer::tcp_log,this,&GuardCore::dataLog);
+
 }
 
 
@@ -84,7 +87,7 @@ GuardCore::~GuardCore()
 
 void GuardCore::start()
 {
-
+    tcpserver->start();
 }
 void GuardCore::onConnected()
 {

+ 2 - 0
agGuardProcess/guardcore.h

@@ -8,6 +8,7 @@
 #include "logthread.h"
 #include "httpthread.h"
 #include "databasethread.h"
+#include "tcpserver.h"
 
 class GuardCore : public QObject
 {
@@ -31,6 +32,7 @@ private:
     LogThread *logthread;
     HttpThread *httpthread;
     DatabaseThread *dbthread;
+    TcpServer *tcpserver;
 
     QMQTT::Client *m_client;
     QSqlDatabase db;

+ 104 - 0
agGuardProcess/tcpserver.cpp

@@ -0,0 +1,104 @@
+#include "tcpserver.h"
+#include "../agVideoProcess/boxdeviceshm.h"
+
+TcpServer::TcpServer(QObject *parent) : QObject(parent)
+{
+    svr = new QTcpServer(this);
+    connect(svr,SIGNAL(newConnection()),this,SLOT(new_incomming()));
+
+    networkManager = new QNetworkAccessManager(this);
+    connect(networkManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finishedSlot(QNetworkReply *)));
+}
+
+void TcpServer::start()
+{
+    if(!svr->listen(QHostAddress::Any,15227)){
+        printf("tcpserver listen 15227 failed\n");
+        exit(-1);
+    }
+    printf("tcpserver listen 15227 success\n");
+}
+
+void TcpServer::new_incomming()
+{
+    so = svr->nextPendingConnection();
+    connect(so,SIGNAL(readyRead()),this,SLOT(read_data()));
+}
+
+void TcpServer::finishedSlot(QNetworkReply *reply)
+{
+    if(reply->error() == QNetworkReply::NoError){
+        QString str = QString::fromUtf8(reply->readAll());
+        QJsonParseError parse_error;
+        QJsonDocument doc = QJsonDocument::fromJson(str.toUtf8(),&parse_error);
+        if(parse_error.error==QJsonParseError::NoError){
+            QJsonObject obj = doc.object();
+            QString code = obj.value("code").toString();
+            QString msg = obj.value("msg").toString();
+            QString msgrec = QString("HTTP/1.1 200 OK\r\nAccess-Control-Allow-Origin: *\r\nContent-type: text/plain\r\n\r\n{\"code\":\"%1\",\"msg\":\"%2\",\"data\":\"\"}").arg(code).arg(msg);
+            so->write(msgrec.toUtf8());
+            so->close();
+        }
+    }
+
+    reply->abort();
+    reply->deleteLater();
+}
+
+void TcpServer::read_data()
+{
+    QString str = QString::fromUtf8(so->readAll());
+    int begin = str.indexOf("{",0);
+    int len = str.length();
+    QString datetime = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
+    QString data = str.mid(begin,len-begin+1);
+    emit tcp_log(QString("[%1] video tcp data %2").arg(datetime).arg(data));
+    QByteArray ba = data.toLocal8Bit();
+    ba = QString::fromLocal8Bit(ba).toUtf8();
+
+    QJsonParseError parse_error;
+    QJsonDocument jsonDoc = QJsonDocument::fromJson(ba,&parse_error);
+    printf("tcpserver parse_error %d\n",parse_error.error);
+    if(parse_error.error == QJsonParseError::NoError){
+        QJsonObject doc = jsonDoc.object();
+        QJsonValue param_val = doc.value("params");
+        QJsonObject param_obj = param_val.toObject();
+        QString deviceId = param_obj.value("device_id").toString();
+        int controlType = param_obj.value("controlType").toInt();
+        for(int i=0;i<1024;i++){
+            if(boxDeviceShm->device[i].Enabled == 0x01){
+                if(deviceId.compare(QString(boxDeviceShm->device[i].device_id))==0){
+                    QNetworkRequest *req = new QNetworkRequest();
+                    req->setUrl(QUrl("https://172.17.200.250:443/artemis/api/acs/v1/door/doControl"));
+                    req->setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
+                    req->setRawHeader("Accept","*/*");
+                    req->setRawHeader("Connection","keep-alive");
+                    req->setRawHeader("Cache-Control"," no-cache");
+                    req->setRawHeader("X-Ca-Key","27997984");
+                    req->setRawHeader("X-Ca-Signature","azbiDyuW3AhipLvGYQQnU015ah2EVVofkBX19GRaZ1g=");
+                    req->setRawHeader("X-Ca-Signature-Headers","x-ca-key");
+                    config = req->sslConfiguration();
+                    config.setPeerVerifyMode(QSslSocket::VerifyNone);
+                    config.setProtocol(QSsl::TlsV1_0OrLater);
+                    req->setSslConfiguration(config);
+                    QString postdata = QString("{\"doorIndexCodes\": [\"%1\"],\"controlType\": %2}")
+                            .arg(boxDeviceShm->device[i].device_code).arg(controlType);
+                    req->setRawHeader("Content_Length",QString("%1").arg(postdata.length()).toUtf8());
+                    networkManager->post(*req,postdata.toUtf8());
+                    break;
+                }
+            }
+        }
+
+    }
+
+
+}
+
+
+
+
+
+
+
+

+ 42 - 0
agGuardProcess/tcpserver.h

@@ -0,0 +1,42 @@
+#ifndef TCPSERVER_H
+#define TCPSERVER_H
+
+#include <QObject>
+#include <QDateTime>
+#include <QTcpServer>
+#include <QTcpSocket>
+#include <QNetworkAccessManager>
+#include <QNetworkRequest>
+#include <QNetworkReply>
+#include <QSslConfiguration>
+#include <QVariant>
+#include <QJsonDocument>
+#include <QJsonObject>
+#include <QJsonValue>
+#include <QJsonParseError>
+
+class TcpServer : public QObject
+{
+    Q_OBJECT
+public:
+    explicit TcpServer(QObject *parent = nullptr);
+    void start();
+
+signals:
+    void tcp_log(QString log);
+
+public slots:
+    void new_incomming();
+    void read_data();
+    void finishedSlot(QNetworkReply *reply);
+
+private:
+    QTcpServer *svr;
+    QTcpSocket *so;
+
+    QNetworkAccessManager *networkManager;
+    QSslConfiguration config;
+
+};
+
+#endif // TCPSERVER_H

+ 12 - 17
agLightingProcess/agLightingProcess.pro

@@ -1,4 +1,4 @@
-QT += core sql
+QT += core sql network
 QT -= gui
 
 CONFIG += c++11 console
@@ -6,25 +6,20 @@ CONFIG -= app_bundle
 
 TARGET = agDP0006
 
-# The following define makes your compiler emit warnings if you use
-# any feature of Qt which as been marked deprecated (the exact warnings
-# depend on your compiler). Please consult the documentation of the
-# deprecated API in order to know how to port your code away from it.
-DEFINES += QT_DEPRECATED_WARNINGS
-
-# You can also make your code fail to compile if you use deprecated APIs.
-# In order to do so, uncomment the following line.
-# You can also select to disable deprecated APIs only up to a certain version of Qt.
-#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
 
 SOURCES += \
         main.cpp \
-    logthread.cpp
+    logthread.cpp \
+    clientthread.cpp \
+    lightingcore.cpp \
+    tcpserver.cpp
 
-# Default rules for deployment.
-qnx: target.path = /tmp/$${TARGET}/bin
-else: unix:!android: target.path = /opt/$${TARGET}/bin
-!isEmpty(target.path): INSTALLS += target
+INCLUDEPATH += ../qmqtt-master/src/mqtt/
 
 HEADERS += \
-    logthread.h
+    logthread.h \
+    clientthread.h \
+    lightingcore.h \
+    tcpserver.h
+
+LIBS += -lQt5Qmqtt

+ 173 - 0
agLightingProcess/clientthread.cpp

@@ -0,0 +1,173 @@
+#include "clientthread.h"
+#include "../AGBoxDog/boxshm.h"
+#include "../agVideoProcess/boxdeviceshm.h"
+
+ClientThread::ClientThread(QObject *parent) : QThread(parent)
+{
+    keep = false;
+    CmdList.clear();
+    devCode.clear();
+    switch_status = 0;
+}
+
+void ClientThread::stop()
+{
+    keep = false;
+}
+
+void ClientThread::deviceStatus()
+{
+    for(int i=0;i<1024;i++){
+        if((boxDeviceShm->device[i].Enabled == 0x01)&&(boxDeviceShm->device[i].device_type == 510)){
+            int cur=0;
+            unsigned char data[256];
+            data[cur++] = 0x00;
+            data[cur++] = 0x00;
+            data[cur++] = 0x00;
+            data[cur++] = 0x00;
+            data[cur++] = 0x00;
+            data[cur++] = 0x06;
+            data[cur++] = 0x01;
+            data[cur++] = 0x03;
+
+            QByteArray deviceCode = QByteArray::fromHex(QString(boxDeviceShm->device[i].device_code).toLatin1());
+            emit dataListLog(QString("deviceCode %1").arg(boxDeviceShm->device[i].device_code));
+            printf("deviceCode %s\n",boxDeviceShm->device[i].device_code);
+            data[cur++] = deviceCode.at(0)&0xff;
+            data[cur++] = deviceCode.at(1)&0xff;
+            data[cur++] = 0x00;
+            data[cur++] = 0x01;
+
+            QString DataStr;
+            QByteArray rtn = QByteArray((const char *)data,12);
+            for(int i=0;i<rtn.length();i++){
+                DataStr.append(QString("%1").arg(rtn.at(i)&0xff,2,16,QChar('0')));
+            }
+//            printf("4444 %s\n",DataStr.toUtf8().data());
+            CmdList.append(rtn);
+        }
+    }
+
+}
+
+void ClientThread::controlData(QString deviceId,int switchStatus)
+{
+    int cur=0;
+    unsigned char data[256];
+    data[cur++] = 0x00;
+    data[cur++] = 0x00;
+    data[cur++] = 0x00;
+    data[cur++] = 0x00;
+    data[cur++] = 0x00;
+    data[cur++] = 0x09;
+    data[cur++] = 0x01;
+    data[cur++] = 0x10;
+
+    for(int i=0;i<1024;i++){
+        if(deviceId.compare(boxDeviceShm->device[i].device_id)==0){
+            QByteArray deviceCode = QByteArray::fromHex(QString(boxDeviceShm->device[i].device_code).toLatin1());
+            emit dataListLog(QString("deviceCode %1").arg(boxDeviceShm->device[i].device_code));
+            printf("deviceCode %s\n",boxDeviceShm->device[i].device_code);
+            data[cur++] = deviceCode.at(0)&0xff;
+            data[cur++] = deviceCode.at(1)&0xff;
+            break;
+        }
+    }
+
+    data[cur++] = 0x00;
+    data[cur++] = 0x01;
+    data[cur++] = 0x02;
+    switch_status = switchStatus;
+    if(switchStatus == 1){
+        data[cur++] = 0x00;
+        data[cur++] = 0x01;
+    }else{
+        data[cur++] = 0x00;
+        data[cur++] = 0x00;
+    }
+
+
+    QString DataStr;
+    QByteArray rtn = QByteArray((const char *)data,15);
+    for(int i=0;i<rtn.length();i++){
+        DataStr.append(QString("%1").arg(rtn.at(i)&0xff,2,16,QChar('0')));
+    }
+    printf("777777 %s\n",DataStr.toUtf8().data());
+    CmdList.append(rtn);
+}
+
+void ClientThread::readData(QTcpSocket *so,QString devCode)
+{
+
+    QByteArray Rtn = so->readAll();
+    QString DataStr;
+    for(int i=0;i<Rtn.length();i++){
+        DataStr.append(QString("%1").arg(Rtn.at(i)&0xff,2,16,QChar('0')));
+    }
+    emit dataListLog(QString("[%1] devCode %2 %3").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).arg(devCode).arg(DataStr));
+
+    if((Rtn.at(7)&0xff) == 0x03){
+        for(int i=0;i<1024;i++){
+            if(devCode.compare(boxDeviceShm->device[i].device_code)==0){
+                int switchStatus = ((Rtn.at(9)&0x00ff)<<8)|(Rtn.at(10)&0x00ff);
+                boxDeviceShm->device[i].lastTime = QDateTime::currentDateTime().toTime_t();
+                emit mqttData(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"conn_type\":\"\",\"type\":\"\"},\"metrics\":{\"switch_status\":%5,\"device_status\":1},\"device_type\":\"%6-lg\"}")
+                              .arg(boxDeviceShm->device[i].device_id).arg(boxDeviceShm->device[i].device_code).arg(boxDeviceShm->device[i].company_code).arg(QDateTime::currentDateTime().toTime_t()).arg(switchStatus).arg(boxDeviceShm->device[i].device_type));
+                break;
+            }
+        }
+    }else if((Rtn.at(7)&0xff) == 0x10){
+        for(int i=0;i<1024;i++){
+            if(devCode.compare(boxDeviceShm->device[i].device_code)==0){
+                boxDeviceShm->device[i].lastTime = QDateTime::currentDateTime().toTime_t();
+                emit mqttData(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"conn_type\":\"\",\"type\":\"\"},\"metrics\":{\"switch_status\":%5,\"device_status\":1},\"device_type\":\"%6-lg\"}")
+                              .arg(boxDeviceShm->device[i].device_id).arg(boxDeviceShm->device[i].device_code).arg(boxDeviceShm->device[i].company_code).arg(QDateTime::currentDateTime().toTime_t()).arg(switch_status).arg(boxDeviceShm->device[i].device_type));
+                emit responseMsg(QString("{\"code\":\"0\",\"msg\":\"success\",\"data\":\"\"}"));
+                break;
+            }
+        }
+    }
+
+
+}
+
+void ClientThread::run()
+{
+    bool connect = false;
+    keep = true;
+
+    so = new QTcpSocket;
+    while (keep) {
+        if(!connect){
+            so->connectToHost(QHostAddress("172.17.203.45"),502);
+            if(so->waitForConnected(3000)){
+                printf("connect success\n");
+                connect = true;
+            }else{
+                printf("connect failed\n");
+                so->abort();
+            }
+        }else{
+            if(so->waitForReadyRead(100)){
+                readData(so,devCode);
+            }else if(CmdList.length()>0){
+                QByteArray cmd = CmdList.first();
+                QString DataStr;
+                for(int i=0;i<cmd.length();i++){
+                    DataStr.append(QString("%1").arg(cmd.at(i)&0xff,2,16,QChar('0')));
+                }
+                devCode.clear();
+                devCode = (QString("%1%2").arg(cmd.at(8)&0xff,2,16,QChar('0')).arg(cmd.at(9)&0xff,2,16,QChar('0'))).toUpper();
+                emit dataListLog(QString("[%1] CmdList.first() %2").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).arg(DataStr));
+                so->write(cmd);
+                if(!so->waitForBytesWritten(1000)){
+                    so->abort();
+                    connect = false;
+                }else{
+                    CmdList.removeFirst();
+                }
+            }
+        }
+        usleep(100000);
+    }
+}

+ 36 - 0
agLightingProcess/clientthread.h

@@ -0,0 +1,36 @@
+#ifndef CLIENTTHREAD_H
+#define CLIENTTHREAD_H
+
+#include <QThread>
+#include <QTcpSocket>
+#include <QDateTime>
+#include <QHostAddress>
+
+class ClientThread : public QThread
+{
+    Q_OBJECT
+public:
+    explicit ClientThread(QObject *parent = nullptr);
+    void run();
+    void stop();
+    void deviceStatus();
+    void readData(QTcpSocket *so,QString devCode);
+    void controlData(QString deviceId,int switchStatus);
+
+signals:
+    void mqttData(QString msg);
+    void dataListLog(QString log);
+    void responseMsg(QString msg);
+
+public slots:
+
+private:
+    QTcpSocket *so;
+    bool keep;
+    QList<QByteArray> CmdList;
+    QString devCode;
+    int switch_status;
+
+};
+
+#endif // CLIENTTHREAD_H

+ 145 - 0
agLightingProcess/lightingcore.cpp

@@ -0,0 +1,145 @@
+#include "lightingcore.h"
+#include "../AGBoxDog/boxshm.h"
+#include "../agVideoProcess/boxdeviceshm.h"
+AGBoxShm *agBoxShm;
+BoxDeviceShm *boxDeviceShm;
+
+void LightingCore::shm_init(){
+    QSqlQuery qry;
+    QString sql = QString("select mqtt_ip,mqtt_port,user_name,pass_word from yt_t_mqtt where item_name = 'data-agbox'");
+    qry = db.exec(sql);
+    while (qry.next()) {
+        ip = qry.value(0).toString();
+        port = qry.value(1).toString();
+        username = qry.value(2).toString();
+        password = qry.value(3).toString();
+    }
+    qry.clear();
+    sql.clear();
+
+}
+
+bool LightingCore::shm_load(){
+    key_t key;
+    int shmid;
+    if((key=ftok(SHM_PATH,static_cast<int>(SHM_PORT)))==-1){
+        return false;
+    }
+    if((shmid=shmget(key,sizeof(AGBoxShm),IPC_CREAT|0666))==-1){
+        return false;
+    }
+    agBoxShm = static_cast<AGBoxShm *>(shmat(shmid,nullptr,0));
+
+    if((key=ftok(VIDEO_PATH,static_cast<int>(VIDEO_PORT)))==-1){
+        return false;
+    }
+    if((shmid=shmget(key,sizeof(BoxDeviceShm),IPC_CREAT|0666))==-1){
+        return false;
+    }
+    boxDeviceShm = static_cast<BoxDeviceShm *>(shmat(shmid,nullptr,0));
+
+    shm_init();
+    return true;
+}
+
+LightingCore::LightingCore(QObject *parent) : QObject(parent)
+{
+    logthread = new LogThread(this);
+    logthread->start();
+
+    db = QSqlDatabase::addDatabase("QSQLITE","conf_db");
+    db.setDatabaseName(QString("/opt/db/yt_conf.db"));
+    if(!db.open()){
+        logthread->appendData(QString("[agenvironment] open yt_conf.db failed"));
+    }
+    if(shm_load()){
+        logthread->appendData(QString("[agenvironment] shm load success"));
+    }
+
+    mqttIdx = 1;
+    m_client = new QMQTT::Client(QHostAddress(ip),static_cast<quint16>(port.toInt()),this);
+    connect(m_client,&QMQTT::Client::connected,this,&LightingCore::onConnected);
+    m_client->setUsername(username);
+    m_client->setPassword(password.toLatin1());
+    m_client->setCleanSession(true);
+    m_client->connectToHost();
+
+    clientthread = new ClientThread(this);
+    connect(clientthread,&ClientThread::mqttData,this,&LightingCore::mqtt_data);
+    connect(clientthread,&ClientThread::dataListLog,this,&LightingCore::dataLog);
+    connect(clientthread,&ClientThread::responseMsg,this,&LightingCore::response_msg);
+
+    tcpserver = new TcpServer(this);
+    connect(tcpserver,&TcpServer::control_data,this,&LightingCore::control_data);
+    connect(tcpserver,&TcpServer::tcp_log,this,&LightingCore::dataLog);
+
+    timer = new QTimer(this);
+    connect(timer,&QTimer::timeout,this,&LightingCore::time_out);
+    timer->start(60000);
+
+}
+
+
+LightingCore::~LightingCore()
+{
+    logthread->stop();
+    clientthread->stop();
+}
+
+void LightingCore::start()
+{
+    clientthread->start();
+    tcpserver->start();
+
+    time_out();
+}
+
+void LightingCore::control_data(QString deviceId,int switchStatus)
+{
+    clientthread->controlData(deviceId,switchStatus);
+}
+
+void LightingCore::time_out()
+{
+
+    agBoxShm->processStatus[6].t_time = QDateTime::currentDateTime().toTime_t();
+
+    clientthread->deviceStatus();
+    for(int i=0;i<1024;i++){
+        if((boxDeviceShm->device[i].Enabled == 0x01)&&(boxDeviceShm->device[i].device_type == 510)){
+            uint curTime = QDateTime::currentDateTime().toTime_t();
+            if((curTime - boxDeviceShm->device[i].lastTime)>120){
+                boxDeviceShm->device[i].lastTime = curTime;
+                this->mqtt_data(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"conn_type\":\"\",\"type\":\"\"},\"metrics\":{\"device_status\":0},\"device_type\":\"%5-lg\"}")
+                              .arg(boxDeviceShm->device[i].device_id).arg(boxDeviceShm->device[i].device_code).arg(boxDeviceShm->device[i].company_code).arg(curTime).arg(boxDeviceShm->device[i].device_type));
+            }
+        }
+    }
+}
+
+void LightingCore::response_msg(QString msg)
+{
+    tcpserver->response_msg(msg);
+}
+
+void LightingCore::onConnected()
+{
+    logthread->appendData(QString("mqtt onConnected"));
+}
+
+void LightingCore::dataLog(QString log)
+{
+    logthread->appendData(log);
+}
+
+void LightingCore::mqtt_data(QString mqtt_msg)
+{
+    printf("test6666 [%s]\n",mqtt_msg.toUtf8().data());
+    if((m_client->connectionState()==QMQTT::STATE_INIT)||(m_client->connectionState()==QMQTT::STATE_DISCONNECTED)){
+        m_client->connectToHost();
+    }
+    m_client->publish(QMQTT::Message(mqttIdx++,"data-collector",mqtt_msg.toUtf8()));
+    if(mqttIdx > 9999){
+        mqttIdx = 1;
+    }
+}

+ 46 - 0
agLightingProcess/lightingcore.h

@@ -0,0 +1,46 @@
+#ifndef LIGHTINGCORE_H
+#define LIGHTINGCORE_H
+
+#include <QObject>
+#include <qmqtt.h>
+#include <QSqlDatabase>
+#include <QSqlQuery>
+#include <QDateTime>
+#include <QTimer>
+#include "logthread.h"
+#include "clientthread.h"
+#include "tcpserver.h"
+
+class LightingCore : public QObject
+{
+    Q_OBJECT
+public:
+    explicit LightingCore(QObject *parent = nullptr);
+    ~LightingCore();
+    void start();
+    void shm_init();
+    bool shm_load();
+
+signals:
+
+public slots:
+    void onConnected();
+    void dataLog(QString log);
+    void mqtt_data(QString mqtt_msg);
+    void control_data(QString deviceId,int switchStatus);
+    void time_out();
+    void response_msg(QString msg);
+
+private:
+    LogThread *logthread;
+    ClientThread *clientthread;
+    TcpServer *tcpserver;
+
+    QMQTT::Client *m_client;
+    QSqlDatabase db;
+    quint16 mqttIdx;
+    QString ip,port,username,password;
+    QTimer *timer;
+};
+
+#endif // LIGHTINGCORE_H

+ 4 - 0
agLightingProcess/main.cpp

@@ -1,8 +1,12 @@
 #include <QCoreApplication>
+#include "lightingcore.h"
 
 int main(int argc, char *argv[])
 {
     QCoreApplication a(argc, argv);
 
+    LightingCore *core = new LightingCore(nullptr);
+    core->start();
+
     return a.exec();
 }

+ 63 - 0
agLightingProcess/tcpserver.cpp

@@ -0,0 +1,63 @@
+#include "tcpserver.h"
+
+TcpServer::TcpServer(QObject *parent) : QObject(parent)
+{
+    svr = new QTcpServer(this);
+    connect(svr, SIGNAL(newConnection()), this, SLOT(new_incomming()));
+
+}
+
+void TcpServer::start()
+{
+    if(!svr->listen(QHostAddress::Any,15224)){
+        printf("15224 listen failed\n");
+        exit(-1);
+    }
+    printf("15224 listen success\n");
+
+}
+
+
+void TcpServer::new_incomming()
+{
+    so = svr->nextPendingConnection();
+    connect(so,SIGNAL(readyRead()),this,SLOT(read_data()));
+
+}
+
+void TcpServer::read_data()
+{
+    QString str = QString::fromUtf8(so->readAll());
+    int begin = str.indexOf("{",0);
+    int len = str.length();
+    QString data = str.mid(begin,len-begin+1);
+    QByteArray ba = data.toLocal8Bit();
+    ba = QString::fromLocal8Bit(ba).toUtf8();
+
+    emit tcp_log(QString("[%1] lighting tcp %2").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).arg(data));
+
+    QJsonParseError json_error;
+    QJsonDocument jsonDoc = QJsonDocument::fromJson(ba,&json_error);
+    printf("tcpServer parseErr.error %d\n",json_error.error);
+    if(json_error.error==QJsonParseError::NoError){
+        QJsonObject root = jsonDoc.object();
+        QJsonValue param_val = root.value("params");
+        QJsonObject param_obj = param_val.toObject();
+        QString deviceId = param_obj.value("device_id").toString();
+        int switchStatus = param_obj.value("switch_status").toInt();
+        emit control_data(deviceId,switchStatus);
+
+    }
+
+}
+
+void TcpServer::response_msg(QString msg)
+{
+    QString msgrec = QString("HTTP/1.1 200 OK\r\nAccess-Control-Allow-Origin: *\r\nContent-type: text/plain\r\n\r\n%1").arg(msg);
+    so->write(msgrec.toUtf8());
+    so->close();
+}
+
+
+
+

+ 37 - 0
agLightingProcess/tcpserver.h

@@ -0,0 +1,37 @@
+#ifndef TCPSERVER_H
+#define TCPSERVER_H
+
+#include <QObject>
+#include <QTcpServer>
+#include <QTcpSocket>
+#include <QDateTime>
+#include <QJsonDocument>
+#include <QJsonObject>
+#include <QJsonValue>
+#include <QJsonArray>
+#include <QJsonParseError>
+#include <QVariant>
+
+class TcpServer : public QObject
+{
+    Q_OBJECT
+public:
+    explicit TcpServer(QObject *parent = nullptr);
+    void start();
+    void response_msg(QString msg);
+
+signals:
+    void control_data(QString deviceId,int switchStatus);
+    void tcp_log(QString log);
+
+public slots:
+    void new_incomming();
+    void read_data();
+
+private:
+    QTcpServer *svr;
+    QTcpSocket *so;
+
+};
+
+#endif // TCPSERVER_H

+ 6 - 2
agVideoProcess/agVideoProcess.pro

@@ -11,7 +11,9 @@ SOURCES += \
         main.cpp \
     httpthread.cpp \
     logthread.cpp \
-    videocore.cpp
+    videocore.cpp \
+    tcpserver.cpp \
+    tcpserver1.cpp
 
 
 INCLUDEPATH += ../qmqtt-master/src/mqtt/
@@ -23,7 +25,9 @@ HEADERS += \
     agboxdeviceshm.h \
     boxdeviceshm.h \
     logthread.h \
-    videocore.h
+    videocore.h \
+    tcpserver.h \
+    tcpserver1.h
 
 
 LIBS += -lQt5Qmqtt

+ 102 - 0
agVideoProcess/tcpserver.cpp

@@ -0,0 +1,102 @@
+#include "tcpserver.h"
+#include "boxdeviceshm.h"
+
+TcpServer::TcpServer(QObject *parent) : QObject(parent)
+{
+    svr = new QTcpServer(this);
+    connect(svr,SIGNAL(newConnection()),this,SLOT(new_incomming()));
+
+    networkManager = new QNetworkAccessManager(this);
+    connect(networkManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finishedSlot(QNetworkReply *)));
+}
+
+void TcpServer::start()
+{
+    if(!svr->listen(QHostAddress::Any,15225)){
+        printf("listen 15225 failed\n");
+        exit(-1);
+    }
+
+    printf("listen 15225 success\n");
+}
+
+void TcpServer::finishedSlot(QNetworkReply *reply)
+{
+    if(reply->error() == QNetworkReply::NoError){
+        QString msg = QString::fromUtf8(reply->readAll());
+        QString msgrec = QString("HTTP/1.1 200 OK\r\nAccess-Control-Allow-Origin: *\r\nContent-type: text/plain\r\n\r\n%1").arg(msg);
+        so->write(msgrec.toUtf8());
+        so->close();
+    }
+
+    reply->abort();
+    reply->deleteLater();
+}
+
+void TcpServer::new_incomming()
+{
+    so = svr->nextPendingConnection();
+    connect(so,SIGNAL(readyRead()),this,SLOT(read_data()));
+}
+
+void TcpServer::read_data()
+{
+    QString str = QString::fromUtf8(so->readAll());
+    int begin = str.indexOf("{",0);
+    int len = str.length();
+    QString datetime = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
+    QString data = str.mid(begin,len-begin+1);
+    emit tcp_log(QString("[%1] video tcp data %2").arg(datetime).arg(data));
+    QByteArray ba = data.toLocal8Bit();
+    ba = QString::fromLocal8Bit(ba).toUtf8();
+
+    QJsonParseError parse_error;
+    QJsonDocument jsonDoc = QJsonDocument::fromJson(ba,&parse_error);
+    printf("tcpserver parse_error %d\n",parse_error.error);
+    if(parse_error.error == QJsonParseError::NoError){
+        QJsonObject doc = jsonDoc.object();
+        QJsonValue param_val = doc.value("params");
+        QJsonObject param_obj = param_val.toObject();
+        QString deviceId = param_obj.value("device_id").toString();
+        int streamType = param_obj.value("streamType").toInt();
+        QString protocol = param_obj.value("protocol").toString();
+        int transMode = param_obj.value("transmode").toInt();
+        for(int i=0;i<1024;i++){
+            if(boxDeviceShm->device[i].Enabled == 0x01){
+                if(deviceId.compare(QString(boxDeviceShm->device[i].device_id))==0){
+                    QNetworkRequest *req = new QNetworkRequest();
+                    req->setUrl(QUrl("https://172.17.200.250:443/artemis/api/video/v1/cameras/previewURLs"));
+                    req->setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
+                    req->setRawHeader("Accept","*/*");
+                    req->setRawHeader("Connection","keep-alive");
+                    req->setRawHeader("Cache-Control"," no-cache");
+                    req->setRawHeader("X-Ca-Key","27997984");
+                    req->setRawHeader("X-Ca-Signature","DGavOjaJIV81XPQUMz7F3UwpSWMYy+s9jYOzHKkbk6o=");
+                    req->setRawHeader("X-Ca-Signature-Headers","x-ca-key");
+                    config = req->sslConfiguration();
+                    config.setPeerVerifyMode(QSslSocket::VerifyNone);
+                    config.setProtocol(QSsl::TlsV1_0OrLater);
+                    req->setSslConfiguration(config);
+                    QString postdata = QString("{\"cameraIndexCode\": \"%1\",\"streamType\": %2,\"protocol\":\"%3\",\"transmode\":%4}")
+                            .arg(boxDeviceShm->device[i].device_code).arg(streamType).arg(protocol).arg(transMode);
+                    req->setRawHeader("Content_Length",QString("%1").arg(postdata.length()).toUtf8());
+                    networkManager->post(*req,postdata.toUtf8());
+                    break;
+                }
+            }
+        }
+
+    }
+
+
+}
+
+
+
+
+
+
+
+
+
+

+ 42 - 0
agVideoProcess/tcpserver.h

@@ -0,0 +1,42 @@
+#ifndef TCPSERVER_H
+#define TCPSERVER_H
+
+#include <QObject>
+#include <QTcpServer>
+#include <QTcpSocket>
+#include <QDateTime>
+#include <QNetworkAccessManager>
+#include <QNetworkRequest>
+#include <QNetworkReply>
+#include <QSslConfiguration>
+#include <QJsonDocument>
+#include <QJsonObject>
+#include <QJsonValue>
+#include <QJsonArray>
+#include <QJsonParseError>
+#include <QVariant>
+
+class TcpServer : public QObject
+{
+    Q_OBJECT
+public:
+    explicit TcpServer(QObject *parent = nullptr);
+    void start();
+
+signals:
+    void tcp_log(QString log);
+
+public slots:
+    void new_incomming();
+    void read_data();
+    void finishedSlot(QNetworkReply *reply);
+
+private:
+    QTcpServer *svr;
+    QTcpSocket *so;
+
+    QNetworkAccessManager *networkManager;
+    QSslConfiguration config;
+};
+
+#endif // TCPSERVER_H

+ 104 - 0
agVideoProcess/tcpserver1.cpp

@@ -0,0 +1,104 @@
+#include "tcpserver1.h"
+#include "boxdeviceshm.h"
+
+TcpServer1::TcpServer1(QObject *parent) : QObject(parent)
+{
+    svr = new QTcpServer(this);
+    connect(svr,SIGNAL(newConnection()),this,SLOT(new_incomming()));
+
+    networkManager = new QNetworkAccessManager(this);
+    connect(networkManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finishedSlot(QNetworkReply *)));
+
+}
+
+void TcpServer1::start()
+{
+    if(!svr->listen(QHostAddress::Any,15226)){
+        printf("tcpserver1 listen 15226 failed\n");
+        exit(-1);
+    }
+    printf("tcpserver1 listen 15226 success\n");
+
+}
+
+void TcpServer1::finishedSlot(QNetworkReply *reply)
+{
+    if(reply->error() == QNetworkReply::NoError){
+        QString data = QString::fromUtf8(reply->readAll());
+        QString recmsg = QString("HTTP/1.1 200 OK\r\nAccess-Control-Allow-Origin: *\r\nContent-type: text/plain\r\n\r\n%1").arg(data);
+        so->write(recmsg.toUtf8());
+        so->close();
+    }
+
+    reply->abort();
+    reply->deleteLater();
+}
+
+void TcpServer1::new_incomming()
+{
+    so = svr->nextPendingConnection();
+    connect(so,SIGNAL(readyRead()),this,SLOT(read_data()));
+}
+
+void TcpServer1::read_data()
+{
+    QString str = QString::fromUtf8(so->readAll());
+    int begin = str.indexOf("{",0);
+    int len = str.length();
+    QString data = str.mid(begin,len-begin+1);
+    QString dt = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
+    emit tcp_log(QString("[%1] tcpserver1 data %2").arg(dt).arg(data));
+    QByteArray ba = data.toLocal8Bit();
+    ba = QString::fromLocal8Bit(ba).toUtf8();
+    QJsonParseError parse_error;
+    QJsonDocument jsonDoc = QJsonDocument::fromJson(ba,&parse_error);
+    printf("parse_error.error %d\n",parse_error.error);
+    if(parse_error.error == QJsonParseError::NoError){
+        QJsonObject doc_obj = jsonDoc.object();
+        QJsonValue param_val = doc_obj.value("params");
+        QJsonObject param_obj = param_val.toObject();
+        QString deviceId = param_obj.value("device_id").toString();
+        int action = param_obj.value("action").toInt();
+        QString command = param_obj.value("command").toString();
+        int speed = param_obj.value("speed").toInt();
+        int presetIndex = param_obj.value("presetIndex").toInt();
+        for(int i=0;i<1024;i++){
+            if(boxDeviceShm->device[i].Enabled == 0x01){
+                if(deviceId.compare(QString(boxDeviceShm->device[i].device_id))==0){
+                    QNetworkRequest *req = new QNetworkRequest();
+                    req->setUrl(QUrl("https://172.17.200.250:443/artemis/api/video/v1/ptzs/controlling"));
+                    req->setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
+                    req->setRawHeader("Accept","*/*");
+                    req->setRawHeader("Connection","keep-alive");
+                    req->setRawHeader("Cache-Control"," no-cache");
+                    req->setRawHeader("X-Ca-Key","27997984");
+                    req->setRawHeader("X-Ca-Signature","C/1aEVkfDZhGXuCH64tGv1vfySvDMTSMkqKd5PXgOM8=");
+                    req->setRawHeader("X-Ca-Signature-Headers","x-ca-key");
+                    config = req->sslConfiguration();
+                    config.setPeerVerifyMode(QSslSocket::VerifyNone);
+                    config.setProtocol(QSsl::TlsV1_0OrLater);
+                    req->setSslConfiguration(config);
+                    QString postdata = QString("{\"cameraIndexCode\": \"%1\",\"action\": %2,\"command\":\"%3\",\"speed\":%4,\"presetIndex\":%5}")
+                            .arg(boxDeviceShm->device[i].device_code).arg(action).arg(command).arg(speed).arg(presetIndex);
+                    req->setRawHeader("Content_Length",QString("%1").arg(postdata.length()).toUtf8());
+                    networkManager->post(*req,postdata.toUtf8());
+                    break;
+                }
+            }
+        }
+
+    }
+
+}
+
+
+
+
+
+
+
+
+
+
+
+

+ 43 - 0
agVideoProcess/tcpserver1.h

@@ -0,0 +1,43 @@
+#ifndef TCPSERVER1_H
+#define TCPSERVER1_H
+
+#include <QObject>
+#include <QTcpServer>
+#include <QTcpSocket>
+#include <QDateTime>
+#include <QNetworkAccessManager>
+#include <QNetworkRequest>
+#include <QNetworkReply>
+#include <QSslConfiguration>
+#include <QVariant>
+#include <QJsonDocument>
+#include <QJsonObject>
+#include <QJsonValue>
+#include <QJsonArray>
+#include <QJsonParseError>
+
+class TcpServer1 : public QObject
+{
+    Q_OBJECT
+public:
+    explicit TcpServer1(QObject *parent = nullptr);
+    void start();
+
+signals:
+    void tcp_log(QString log);
+
+public slots:
+    void new_incomming();
+    void read_data();
+    void finishedSlot(QNetworkReply *reply);
+
+private:
+    QTcpServer *svr;
+    QTcpSocket *so;
+
+    QNetworkAccessManager *networkManager;
+    QSslConfiguration config;
+
+};
+
+#endif // TCPSERVER1_H

+ 9 - 1
agVideoProcess/videocore.cpp

@@ -104,6 +104,13 @@ VideoCore::VideoCore(QObject *parent) : QObject(parent)
     httpthread = new HttpThread(this);
     connect(httpthread,&HttpThread::dataLog,this,&VideoCore::dataLog);
     connect(httpthread,&HttpThread::mqttData,this,&VideoCore::mqtt_data);
+
+    tcpserver = new TcpServer(this);
+    connect(tcpserver,&TcpServer::tcp_log,this,&VideoCore::dataLog);
+
+    tcpserver1 = new TcpServer1(this);
+    connect(tcpserver1,&TcpServer1::tcp_log,this,&VideoCore::dataLog);
+
 }
 
 VideoCore::~VideoCore()
@@ -114,7 +121,8 @@ VideoCore::~VideoCore()
 
 void VideoCore::start()
 {
-
+    tcpserver->start();
+    tcpserver1->start();
 }
 
 void VideoCore::onConnected()

+ 4 - 0
agVideoProcess/videocore.h

@@ -7,6 +7,8 @@
 #include <QSqlQuery>
 #include "logthread.h"
 #include "httpthread.h"
+#include "tcpserver.h"
+#include "tcpserver1.h"
 
 class VideoCore : public QObject
 {
@@ -28,6 +30,8 @@ public slots:
 private:
     LogThread *logthread;
     HttpThread *httpthread;
+    TcpServer *tcpserver;
+    TcpServer1 *tcpserver1;
 
     QMQTT::Client *m_client;
     QSqlDatabase db;