#include "datasender.h"
//#include <QDebug>

extern YT_UNIT_SHM *ytShm;

DataSender::DataSender(QObject *parent, QString serverPath, QString id) :
    QObject(parent)
{
    waitting = false;
    ServerPath = serverPath;
    SelfId = id;
    timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(time_out()));
    queryManager = new QNetworkAccessManager(this);
    connect(queryManager,SIGNAL(finished(QNetworkReply*)),this,SLOT(query_finishedSlot(QNetworkReply*)));
    timer->start(5000);
}

void DataSender::query_finishedSlot(QNetworkReply *reply)
{
    if(reply->error()==QNetworkReply::NoError){
        QByteArray qj = reply->readAll();
//        qDebug()<<qj.data();
    }
    waitting = false;
}

void DataSender::reply_timeout()
{
    waitting = false;
}

void DataSender::time_out()
{
    if(!waitting){
        json_object *iotlist,*obj[5],*obj_list[5], *body, *json;
        int comm,adds,i;
        int count[5],alarm[5],out[5];
        char tmp[256];
        body = json_object_new_object();
        json = json_object_new_object();
        iotlist = json_object_new_array();
        for(i=0;i<5;i++)
        {
            count[i]=0;
            alarm[i]=0;
            out[i]=0;
            obj[i] = json_object_new_object();
            obj_list[i] = json_object_new_array();
        }
        for(comm=0;comm<4;comm++)
        {
            if(ytShm->commList.comm[comm].ENABLED==0x01){
                for(adds=0;adds<256;adds++)
                {
                    if(ytShm->sPointList.sPoint[comm][adds].ENABLED==0x01)
                    {
                        json_object *sp = json_object_new_object();
                        for(i=0;i<5;i++)
                        {
                            if((ytShm->sPointList.sPoint[comm][adds].SUB_TYPE&(0x01<<i))==(0x01<<i))
                                count[i]++;
                        }
                        if(ytShm->commStatusList.commStatus[comm].STATUS!=0x01)
                        {
                            if(ytShm->sPointList.sPoint[comm][adds].SUB_TYPE>0)
                            {
                                for(i=0;i<5;i++)
                                {
                                    if((ytShm->sPointList.sPoint[comm][adds].SUB_TYPE&(0x01<<i))==(0x01<<i))
                                        out[i]++;
                                }
                                json_object_object_add(sp,"STATUS_ID",json_object_new_int(1));
                                json_object_object_add(sp,"STATUS",json_object_new_string(QString::fromUtf8("离线").toUtf8().data()));
                            }
                        }else{
                            if(ytShm->spStatusList.spStatus[comm][adds].ALARM_STATUS!=0x00)
                            {
                                for(i=0;i<5;i++)
                                {
                                    if((ytShm->sPointList.sPoint[comm][adds].SUB_TYPE&(0x01<<i))==(0x01<<i))
                                        alarm[i]++;
                                }
                                json_object_object_add(sp,"STATUS_ID",json_object_new_int(2));
                                json_object_object_add(sp,"STATUS",json_object_new_string(QString::fromUtf8("告警").toUtf8().data()));
                            }else{
                                json_object_object_add(sp,"STATUS_ID",json_object_new_int(0));
                                json_object_object_add(sp,"STATUS",json_object_new_string(QString::fromUtf8("正常").toUtf8().data()));
                            }
                        }
                        json_object_object_add(sp,"COMM",json_object_new_int(comm));
                        json_object_object_add(sp,"ADDR",json_object_new_int(adds));
                        json_object_object_add(sp,"PNO_NAME",
                                               json_object_new_string(ytShm->sPointList.sPoint[comm][adds].NAME));
                        json_object_object_add(sp,"PNO_TYPE",json_object_new_int(ytShm->sPointList.sPoint[comm][adds].PNO_TYPE));
                        if(ytShm->sPointList.sPoint[comm][adds].PNO_TYPE==0x01){
                            if(ytShm->spStatusList.spStatus[comm][adds].REALTIME_VALUE==0.0)
                                json_object_object_add(sp,"VALUE",json_object_new_string("0"));
                            else
                                json_object_object_add(sp,"VALUE",json_object_new_string("1"));
                        }else{
                            sprintf(tmp,"%.2f",ytShm->spStatusList.spStatus[comm][adds].REALTIME_VALUE);
                            json_object_object_add(sp,"VALUE",json_object_new_string(tmp));
                        }
                        time_t t = ytShm->spStatusList.spStatus[comm][adds].REALTIME_TIMESTAMP;
                        struct tm *loc_time = localtime(&t);
                        sprintf(tmp,"%04d-%02d-%02d %02d:%02d:%02d",
                                loc_time->tm_year+1900,
                                loc_time->tm_mon+1,
                                loc_time->tm_mday,
                                loc_time->tm_hour,
                                loc_time->tm_min,
                                loc_time->tm_sec);
                        json_object_object_add(sp,"TIME",json_object_new_string(tmp));
                        for(i=0;i<5;i++)
                        {
                            if((ytShm->sPointList.sPoint[comm][adds].SUB_TYPE&(0x01<<i))==(0x01<<i))
                                json_object_array_add(obj_list[i],sp);
                        }
                    }
                }
            }
        }
        for(i=0;i<5;i++)
        {
            json_object_object_add(obj[i],"ID",json_object_new_int(i+1));
            switch(i){
            case 0:
                json_object_object_add(obj[i],"NAME",json_object_new_string(QString::fromUtf8("火灾监控实时信息").toUtf8().data()));
                break;
            case 1:
                json_object_object_add(obj[i],"NAME",json_object_new_string(QString::fromUtf8("电气火灾监控实时信息").toUtf8().data()));
                break;
            case 2:
                json_object_object_add(obj[i],"NAME",json_object_new_string(QString::fromUtf8("消防水系统监控实时信息").toUtf8().data()));
                break;
            case 3:
                json_object_object_add(obj[i],"NAME",json_object_new_string(QString::fromUtf8("通道监控实时信息").toUtf8().data()));
                break;
            case 4:
                json_object_object_add(obj[i],"NAME",json_object_new_string(QString::fromUtf8("消防电源监控实时信息").toUtf8().data()));
                break;
            }
            json_object_object_add(obj[i],"COUNT",json_object_new_int(count[i]));
            json_object_object_add(obj[i],"OUT",json_object_new_int(out[i]));
            json_object_object_add(obj[i],"ALARM",json_object_new_int(alarm[i]));
            json_object_object_add(obj[i],"LIST",obj_list[i]);
            json_object_array_add(iotlist,obj[i]);
        }
        json_object_object_add(body,"IOT_LIST",iotlist);
        json_object_object_add(json,"RESULT",body);
        json_object_object_add(json,"NAME",json_object_new_string(QString::fromUtf8("Usky_IoT-03").toUtf8().data()));
        json_object_object_add(json,"VER",json_object_new_string("1.01"));
        json_object_object_add(json,"Id",json_object_new_string(SelfId.toUtf8().data()));
        QNetworkRequest *req = new QNetworkRequest(QUrl(ServerPath));
        req->setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
        req->setRawHeader("Accept","text/html, application/xhtml+xml, */*");
        req->setRawHeader("Accept-Language","zh-CN");
        req->setRawHeader("Connection","keep-alive");
        req->setRawHeader("Cache-Control"," no-cache");
        QByteArray postdata;
        postdata.append(QString::fromUtf8(json_object_to_json_string(json)).toUtf8());
        req->setRawHeader("Content-Length",QString("%1").arg(postdata.length()).toUtf8());
        QNetworkReply *reply = queryManager->post(*req,postdata);
        QReplayTimeout *pTimeout = new QReplayTimeout(reply,10000);
        connect(pTimeout, SIGNAL(net_timeout()),this,SLOT(reply_timeout()));
    }
}
