#include "dealmqttthread.h"

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


}

void DealMqttThread::stop()
{
    keep = false;
}

void DealMqttThread::appendMqttData(MqttData mqttData)
{
    mqttDataList.append(mqttData);
}

void DealMqttThread::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("data write arseErr.error %d\n",parseErr.error);
                if(parseErr.error==QJsonParseError::NoError){
                    QJsonObject obj = doc.object();
                    QString deviceId = obj.value("device_id").toString();
                    QString deviceCode = obj.value("device_code").toString();
                    QString companyCode = obj.value("product_id").toString();
                    int timeStamp = obj.value("timestamp").toInt();
                    QString dataTime = QDateTime::fromTime_t(timeStamp).toString("yyyy-MM-dd HH:mm:ss");
                    QString deviceType = obj.value("device_type").toString();
                    QStringList spit = deviceType.split("-");
                    QString devType = "";
                    QString devBrief = "";
                    if(spit.length()>1){
                        devType = spit.at(0);
                        devBrief = spit.at(1);
                    }

                    if((devType.length()==3)&&(devType.startsWith("5"))){
                        QJsonValue metric_value = obj.value("metrics");

                        QJsonObject metric_obj = metric_value.toObject();
                        QVariantMap fd = metric_obj.toVariantMap();
                        QVariantMap::Iterator iter;
                        for(iter=fd.begin();iter!=fd.end();iter++){
                            QString sql = QString("insert into data_real_time (id, device_id, device_code, device_type, attribute_name, attribute_data, data_time, insert_time) values(null,'%1','%2',%3,'%4','%5','%6','%7') ON DUPLICATE KEY UPDATE  attribute_data = values(attribute_data),data_time = values(data_time),insert_time = values(insert_time);")
                                    .arg(deviceId).arg(deviceCode).arg(devType).arg(iter.key()).arg(iter.value().toString()).arg(dataTime).arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"));
                            sql.append(QString("insert into data_%1_history(id,device_id,device_code,device_type,attribute_name,attribute_data,data_time,insert_time) values(NULL,'%2','%3',%4,'%5','%6','%7','%8');")
                                       .arg(devBrief).arg(deviceId).arg(deviceCode).arg(devType).arg(iter.key()).arg(iter.value().toString()).arg(dataTime).arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")));
                            emit dmqSql(sql);
                        }
                    }

                }


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


        usleep(50000);
    }
}
