#include "mqttpub.h"
#include "../agVideoProcess/boxdeviceshm.h"

MqttPub::MqttPub(QObject *parent) : QThread(parent)
{
    keep = false;
    mqttDataList.clear();


}

void MqttPub::stop()
{
    keep = false;

}


void MqttPub::run()
{
    keep = true;
    while (keep) {
        if(mqttDataList.length()>0){
            while (mqttDataList.length()>0) {
                MqttData md = mqttDataList.first();
                QString topic = md.topic;
                QByteArray data = md.data;


                QJsonParseError parseErr;
                QJsonDocument doc = QJsonDocument::fromJson(data,&parseErr);
//                printf("environment parseErr.error %d\n",parseErr.error);
                if(parseErr.error==QJsonParseError::NoError){
                    QString nodeInfo = "";
                    QString deviceId = "";
                    QString companyCode = "";
                    QString deviceType = "";
                    QJsonObject obj_doc = doc.object();
                    QString sn = obj_doc.value("sn").toString();
                    int ts = obj_doc.value("ts").toInt();
                    QJsonValue dev_value = obj_doc.value("devs");
                    if(dev_value.isArray()){
                        QJsonArray list_array = dev_value.toArray();
                        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());
                            for(int i=0;i<1024;i++){
                                if(boxDeviceShm->device[i].Enabled == 0x01){
                                    if(deviceCode.compare(QString(boxDeviceShm->device[i].device_code))==0){
                                        deviceId = QString(boxDeviceShm->device[i].device_id);
                                        companyCode = QString(boxDeviceShm->device[i].company_code);
                                        deviceType = QString::number(boxDeviceShm->device[i].device_type);
                                        boxDeviceShm->device->lastTime = QDateTime::currentDateTime().toTime_t();
                                        break;
                                    }
                                }
                            }

                            QJsonValue d_value = obj_dev.value("d");
                            if(d_value.isArray()){
                                QJsonArray d_array = d_value.toArray();
                                if(d_array.size()==8){
                                    for(int i=0;i<d_array.size();i++){
                                        QJsonObject list_object = d_array.at(i).toObject();
                                        QString name = list_object.value("m").toString();
                                        double value = list_object.value("v").toDouble();

                                        for(int i=0;i<300;i++){
                                            if(name.compare(QString(boxDeviceShm->procuctattrbute[i].attribute_name))==0){
                                                nodeInfo.append(QString("\"%1\":%2,").arg(QString(boxDeviceShm->procuctattrbute[i].attribute_code)).arg(value));
                                                break;
                                            }
                                        }

                                    }
                                    nodeInfo.append(QString("\"device_status\":1"));
                                    emit mqttData(QString("{\"device_id\":\"%1\",\"device_code\":\"%2\",\"product_id\":\"%3\",\"timestamp\":%4,\"tags\":{\"conn_type\":\"\",\"type\":\"\"},\"metrics\":{%5},\"device_type\":\"%6-ev\"}")
                                                  .arg(deviceId).arg(deviceCode).arg(companyCode).arg(ts).arg(nodeInfo).arg(deviceType));

                                }
                            }

                        }
                    }

                }


                mqttDataList.removeFirst();
                usleep(1000);
            }
        }


        usleep(50000);
    }
}

void MqttPub::devMessage(MqttData dev)
{
    mqttDataList.append(dev);
}


















