#include "datacollectorpub.h"
#include "../ytDataCollectorDog/datacollector.h"

DataCollectorPub::DataCollectorPub(QObject *parent) : QThread(parent)
{
    keep = false;
    devMsgList.clear();


}

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

}


void DataCollectorPub::run()
{
    keep = true;
    while (keep) {

        if(devMsgList.length()>0){
            while(devMsgList.length()>0){
                DevData devdata = devMsgList.first();
                //this->publishData(devdata.topic,devdata.data);

                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);

                QString jsonStr = "";
                QJsonParseError json_err;
                QJsonDocument doc = QJsonDocument::fromJson(data,&json_err);
                if(json_err.error == QJsonParseError::NoError){
                    QString Status = "0";
                    QString PowerAlarm="0";
                    QString ManualAct = "0";
                    QString DevWorking01 = "0";
                    QString DevWorking02 = "0";
                    QString DevAlarm01 = "0";
                    QString DevAlarm02 = "0";
                    QString FireAutoAct = "0";
                    QString Temperature = "0.0";
                    QString Humidity = "0.0";
                    QString WaterPL = "0.0";
                    QString VoltageA = "0.0";
                    QString VoltageB = "0.0";
                    QString VoltageC = "0.0";
                    QString CurrentA = "0.0";
                    QString CurrentB = "0.0";
                    QString CurrentC = "0.0";
                    QString TemperatureA = "0.0";
                    QString TemperatureB = "0.0";
                    QString TemperatureC = "0.0";
                    QString battery = "0.0";
                    QString signal = "0.0";
                    QString smoke = "7";
                    QString sjstatus = "1";
                    QString density = "0";
                    QString angle = "0";
                    QString deviceType = "";

                    QJsonObject root_obj = doc.object();
                    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()){
                        QJsonObject dev_obj = dev_value.toArray().at(0).toObject();

                        QJsonValue dp_value = dev_obj.value("dp");
                        if(dp_value.isArray()){
                            QJsonArray dp_array = dp_value.toArray();
                            for(int i=0;i<dp_array.size();i++){
                                QJsonObject dp_obj = dp_array.at(i).toObject();
                                QString name = dp_obj.value("property").toString();
                                QString value = "";
                                if(devType.compare("ytDP0008")==0){
                                    value = QString("%1").arg(dp_obj.value("value").toDouble());
                                }else{
                                    value = dp_obj.value("value").toString();
                                }


                                if((name.compare("状态")==0)||(name.compare("井盖状态")==0)){
                                    Status = value;
                                    //printf("Status: %s\n",Status.toUtf8().data());
                                }else if((name.compare("水压")==0)||(name.compare("水位")==0)||(name.compare("液位")==0)){
                                    WaterPL = value;
                                    //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;
                                    //printf("signal: %s\n",signal.toUtf8().data());
                                }else if(name.compare("角度")==0){
                                    angle = value;
                                    //printf("angle: %s\n",angle.toUtf8().data());
                                }else if(name.contains("电源") && value.compare("00")==0){
                                    PowerAlarm = "1";
                                    //printf("PowerAlarm: %s\n",PowerAlarm.toUtf8().data());
                                }else if(name.contains("手自动状态") && value.compare("00")==0){
                                    ManualAct = "1";
                                    //printf("ManualAct: %s\n",ManualAct.toUtf8().data());
                                }else if(name.contains("运行状态") && name.contains("1#") && value.compare("00")==0){
                                    DevWorking01 = "1";
                                    //printf("DevWorking01: %s\n",DevWorking01.toUtf8().data());
                                }else if((name.compare("泵运行状态")==0||name.compare("消防泵运行状态")==0) && value.compare("00")==0){
                                    DevWorking01 = "1";
                                    //printf("DevWorking01: %s\n",DevWorking01.toUtf8().data());
                                }else if(name.contains("运行状态") && name.contains("2#") && value.compare("00")==0){
                                    DevWorking02 = "1";
                                    //printf("DevWorking02: %s\n",DevWorking02.toUtf8().data());
                                }else if(name.contains("故障") && name.contains("1#") && value.compare("00")==0){
                                    DevAlarm01 = "1";
                                    //printf("DevAlarm01: %s\n",DevAlarm01.toUtf8().data());
                                }else if(name.contains("故障") && name.contains("2#") && value.compare("00")==0){
                                    DevAlarm02 = "1";
                                    //printf("DevAlarm02: %s\n",DevAlarm02.toUtf8().data());
                                }else if(name.contains("消防联动") && value.compare("00")==0){
                                    FireAutoAct = "1";
                                    //printf("FireAutoAct: %s\n",FireAutoAct.toUtf8().data());
                                }else if((name.compare("1#温度")==0)||(name.compare("温度")==0)){
                                    Temperature = value;
                                    //printf("Temperature: %s\n",Temperature.toUtf8().data());
                                }else if(name.compare("1#湿度")==0){
                                    Humidity = value;
                                    //printf("Humidity: %s\n",Humidity.toUtf8().data());
                                }else if(name.compare("浓度")==0){
                                    density = value;
                                    //printf("density: %s\n",density.toUtf8().data());
                                }else if(name.compare("A相电压")==0){
                                    VoltageA = value;
                                    //printf("VoltageA: %s\n",VoltageA.toUtf8().data());
                                }else if(name.compare("B相电压")==0){
                                    VoltageB = value;
                                    //printf("VoltageB: %s\n",VoltageB.toUtf8().data());
                                }else if(name.compare("C相电压")==0){
                                    VoltageC = value;
                                    //printf("VoltageC: %s\n",VoltageC.toUtf8().data());
                                }else if(name.compare("A相电流")==0){
                                    CurrentA = value;
                                    //printf("CurrentA: %s\n",CurrentA.toUtf8().data());
                                }else if(name.compare("B相电流")==0){
                                    CurrentB = value;
                                    //printf("CurrentB: %s\n",CurrentB.toUtf8().data());
                                }else if(name.compare("C相电流")==0){
                                    CurrentC = value;
                                    //printf("CurrentC: %s\n",CurrentC.toUtf8().data());
                                }else if(name.compare("A相线缆温度")==0){
                                    TemperatureA = value;
                                    //printf("property = %s, value = %s, TemperatureA = %s\n",name.toUtf8().data(),value.toUtf8().data(),TemperatureA.toUtf8().data());
                                }else if(name.compare("B相线缆温度")==0){
                                    TemperatureB = value;
                                    //printf("TemperatureB: %s\n",TemperatureB.toUtf8().data());
                                }else if(name.compare("C相线缆温度")==0){
                                    TemperatureC = value;
                                    //printf("TemperatureC: %s\n",TemperatureC.toUtf8().data());
                                }

                                //printf("name = %s, value = %s, battery = %s, signal = %s\n",name.toUtf8().data(),value.toUtf8().data(),battery.toUtf8().data(),signal.toUtf8().data());

                            }
                        }
                        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";
                            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";
                            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(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";
                            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";
                            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";
                            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";
                            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";
                            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";
                            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";
                            productId = "130_0001";
                        }else if(dt.compare("131")==0){
                            deviceType = "131-fgas";
                            productId = "131_0001";
                        }else if(dt.compare("137")==0){
                            deviceType = "137-fjjan";
                            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);
                        }
                    }


                    emit dataListLog(QString("[%1] devMsgList.length: %2, topic: %3, jsonStr: %4").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).arg(devMsgList.length()).arg(topic).arg(jsonStr));
                    emit publishDevData(jsonStr.toUtf8());

                }else{
                    printf("parse json error\n");
                }


                devMsgList.removeFirst();
                usleep(1000);
            }
        }
        usleep(50000);
    }
}

void DataCollectorPub::devMessage(DevData dev)
{
    devMsgList.append(dev);
}


















