#include <stdio.h>
#include <json-c/json.h>
#include "../ytCore/yt_unit_shm.h"
#include <string.h>
#include <sqlite3.h>
#include <time.h>
#include <sys/time.h>
#include <stdlib.h>
#include <unistd.h>

#define CMD_GET_REALTIME_STATUS 1
#define CMD_GET_HISTORY_VALUE 2
#define CMD_GET_COMM_PARAMS 3
#define CMD_SET_COMM_PARAMS 4
#define CMD_GET_SPNO_PARAMS 5
#define CMD_SET_SPNO_PARAMS 6
#define CMD_QRY_IOT_STATUS 16
#define CMD_CTL 64

YT_UNIT_SHM *ytShm;

bool load_shm()
{
    int shmid;
    key_t key;
    if((key = ftok(SHM_PATH,(int)SHM_PORT))==-1)
        return false;
    if((shmid = shmget(key,sizeof(YT_UNIT_SHM),IPC_CREAT|0666))==-1)
        return false;
    ytShm = (YT_UNIT_SHM *)shmat(shmid,NULL,0);
    return true;
}

void get_realtime_status(json_object *q_body, json_object *body)
{
    int commid,ncomm=0,comm_alarm=0,pno_alarm=0,alarmL1=0,alarmL2=0,total_count=0;
    char tmp[256];
    json_object *comm_list = json_object_new_array();
    for(commid=0;commid<4;commid++){
        if(ytShm->commList.comm[commid].ENABLED==0x01){
            int count=0,add;
            time_t t;
            struct tm *loc_time;
            json_object *o_comm = json_object_new_object();
            json_object *pno_list = json_object_new_array();
            ncomm++;
            for(add=0;add<256;add++){
                if((ytShm->sPointList.sPoint[commid][add].ENABLED==0x01)
                        &&(ytShm->sPointList.sPoint[commid][add].PNO_TYPE>0)
                        &&(ytShm->sPointList.sPoint[commid][add].PNO_TYPE<3)
                        ){
                    count++;
                    json_object *spno = json_object_new_object();
                    sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][add].COMM_IDX);
                    json_object_object_add(spno,"COMM",json_object_new_string(tmp));
                    sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][add].BUS_ADD);
                    json_object_object_add(spno,"ADDR",json_object_new_string(tmp));
                    json_object_object_add(spno,"NAME",json_object_new_string(ytShm->sPointList.sPoint[commid][add].NAME));
                    sprintf(tmp,"%ld",ytShm->sPointList.sPoint[commid][add].SUB_TYPE);
                    json_object_object_add(spno,"SUB_TYPE",json_object_new_string(tmp));
                    sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][add].PNO_TYPE);
                    json_object_object_add(spno,"PNO_TYPE",json_object_new_string(tmp));
                    if(ytShm->spStatusList.spStatus[commid][add].ALARM_STATUS>0){
                        pno_alarm++;
                        if(ytShm->spStatusList.spStatus[commid][add].ALARM_STATUS==2)
                            alarmL2++;
                        else if(ytShm->spStatusList.spStatus[commid][add].ALARM_STATUS==1)
                            alarmL1++;
                    }
                    sprintf(tmp,"%d",ytShm->spStatusList.spStatus[commid][add].ALARM_STATUS);
                    json_object_object_add(spno,"STATUS",json_object_new_string(tmp));
                    if(ytShm->sPointList.sPoint[commid][add].PNO_TYPE==1){
                        if(ytShm->spStatusList.spStatus[commid][add].REALTIME_VALUE==0)
                            json_object_object_add(spno,"VALUE",json_object_new_string("0"));
                        else
                            json_object_object_add(spno,"VALUE",json_object_new_string("1"));
                    }else{
                        sprintf(tmp,"%.03f",ytShm->spStatusList.spStatus[commid][add].REALTIME_VALUE);
                        json_object_object_add(spno,"VALUE",json_object_new_string(tmp));
                    }
                    t = ytShm->spStatusList.spStatus[commid][add].REALTIME_TIMESTAMP;
                    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(spno,"UPDATETIME",json_object_new_string(tmp));
                    sprintf(tmp,"%d",(ytShm->currentTime.TIMESTAMP-ytShm->spStatusList.spStatus[commid][add].REALTIME_TIMESTAMP));
                    json_object_object_add(spno,"DLYTIME",json_object_new_string(tmp));
                    json_object_array_add(pno_list,spno);
                }
            }
            total_count += count;
            sprintf(tmp,"%d",ytShm->commList.comm[commid].IDX);
            json_object_object_add(o_comm,"IDX",json_object_new_string(tmp));
            json_object_object_add(o_comm,"PNOLIST",pno_list);
            if(ytShm->commStatusList.commStatus[commid].STATUS!=0)
                comm_alarm++;
            sprintf(tmp,"%d",ytShm->commStatusList.commStatus[commid].STATUS);
            json_object_object_add(o_comm,"STATUS",json_object_new_string(tmp));
            json_object_array_add(comm_list,o_comm);
        }
    }
    json_object_object_add(body,"COMMLIST",comm_list);
}

void get_history_value(json_object *q_body, json_object *body)
{
    json_object *CommId=NULL, *BusAdd=NULL;
    json_object_object_foreach(q_body, key, val){
        if(strcmp("COMM",key)==0)
            CommId = val;
        else if(strcmp("ADDR",key)==0)
            BusAdd = val;
    }
    if((CommId!=NULL)&&(BusAdd!=NULL)){
        sqlite3 *db;
        time_t t;
        struct tm *tm_loc;
        char **azResult, *errMsg, sql[1024],tmp[256];
        int nrow,ncol,i;
        int commid = atoi(json_object_get_string(CommId));
        int busadd = atoi(json_object_get_string(BusAdd));
        sprintf(tmp,"%d",commid);
        json_object_object_add(body,"COMM",json_object_new_string(tmp));
        sprintf(tmp,"%d",busadd);
        json_object_object_add(body,"ADDR",json_object_new_string(tmp));
        if((commid>=0)&&(commid<4)&&(busadd>=0)&&(busadd<256)){
            if(sqlite3_open(REC_DB,&db)==SQLITE_OK){
                sprintf(sql,"select REALTIME_VALUE, AVE_VALUE, REALTIME_TIMESTAMP, MAX_VALUE, MAX_TIMESTAMP, MIN_VALUE, MIN_TIMESTAMP from yt_rec_pno where COMMID=%s and BUSADD=%s",
                        json_object_get_string(CommId), json_object_get_string(BusAdd));
//                printf("sql:%s<br>",sql);
                json_object *value_list = json_object_new_array();
                if(sqlite3_get_table(db,sql,&azResult,&nrow,&ncol,&errMsg)==SQLITE_OK){
                    if((nrow>0)&&(ncol>=7)){
                        for(i=1;i<(nrow+1);i++){
                            json_object *value = json_object_new_object();
                            sprintf(tmp,"%s",azResult[i*ncol+0]);
                            json_object_object_add(value,"REALTIME_VALUE",json_object_new_string(tmp));
                            sprintf(tmp,"%s",azResult[i*ncol+1]);
                            json_object_object_add(value,"AVE_VALUE",json_object_new_string(tmp));
                            t = atoll(azResult[i*ncol+2])&0xffffffff;
                            tm_loc = localtime(&t);
                            sprintf(tmp,"%04d-%02d-%02d %02d:%02d:%02d",
                                    tm_loc->tm_year,
                                    tm_loc->tm_mon+1,
                                    tm_loc->tm_mday,
                                    tm_loc->tm_hour,
                                    tm_loc->tm_min,
                                    tm_loc->tm_sec);
                            json_object_object_add(value,"REALTIME_TIMESTAMP",json_object_new_string(tmp));
                            sprintf(tmp,"%s",azResult[i*ncol+3]);
                            json_object_object_add(value,"MAX_VALUE",json_object_new_string(tmp));
                            t = atoll(azResult[i*ncol+4])&0xffffffff;
                            tm_loc = localtime(&t);
                            sprintf(tmp,"%04d-%02d-%02d %02d:%02d:%02d",
                                    tm_loc->tm_year,
                                    tm_loc->tm_mon+1,
                                    tm_loc->tm_mday,
                                    tm_loc->tm_hour,
                                    tm_loc->tm_min,
                                    tm_loc->tm_sec);
                            json_object_object_add(value,"MAX_TIMESTAMP",json_object_new_string(tmp));
                            sprintf(tmp,"%s",azResult[i*ncol+5]);
                            json_object_object_add(value,"MIN_VALUE",json_object_new_string(tmp));
                            t = atoll(azResult[i*ncol+6])&0xffffffff;
                            tm_loc = localtime(&t);
                            sprintf(tmp,"%04d-%02d-%02d %02d:%02d:%02d",
                                    tm_loc->tm_year,
                                    tm_loc->tm_mon+1,
                                    tm_loc->tm_mday,
                                    tm_loc->tm_hour,
                                    tm_loc->tm_min,
                                    tm_loc->tm_sec);
                            json_object_object_add(value,"MIN_TIMESTAMP",json_object_new_string(tmp));
                            json_object_array_add(value_list,value);
                        }
                        json_object_object_add(body,"VALUELIST",value_list);
                    }else{
                        json_object_object_add(body,"ErrMsg",json_object_new_string("table struct error"));
                    }
                    sqlite3_free_table(azResult);
                }else{
                    json_object_object_add(body,"ErrMsg",json_object_new_string("db gettable failed"));
                    sqlite3_free(errMsg);
                }
                sqlite3_close(db);
            }else{
                json_object_object_add(body,"ErrMsg",json_object_new_string("db open failed"));
            }
            json_object *pno_param = json_object_new_object();
            sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][busadd].ENABLED);
            json_object_object_add(pno_param,"ENABLED",json_object_new_string(tmp));
            json_object_object_add(pno_param,"NAME",json_object_new_string(ytShm->sPointList.sPoint[commid][busadd].NAME));
            sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][busadd].PNO_TYPE);
            json_object_object_add(pno_param,"PNO_TYPE",json_object_new_string(tmp));
            sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][busadd].PNO_ALARM);
            json_object_object_add(pno_param,"PNO_ALARM",json_object_new_string(tmp));
            sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][busadd].PNO_VALUE);
            json_object_object_add(pno_param,"PNO_VALUE",json_object_new_string(tmp));
            sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][busadd].PNO_DLY);
            json_object_object_add(pno_param,"PNO_DLY",json_object_new_string(tmp));
            sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][busadd].PNO_AD);
            json_object_object_add(pno_param,"PNO_AD",json_object_new_string(tmp));
            sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][busadd].OFFSET);
            json_object_object_add(pno_param,"OFFSET",json_object_new_string(tmp));
            sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][busadd].OFFSET_FLAG);
            json_object_object_add(pno_param,"OFFSET_FLAG",json_object_new_string(tmp));
            sprintf(tmp,"%.03f",ytShm->sPointList.sPoint[commid][busadd].PNO_MAX);
            json_object_object_add(pno_param,"PNO_MAX",json_object_new_string(tmp));
            sprintf(tmp,"%.03f",ytShm->sPointList.sPoint[commid][busadd].PNO_MIN);
            json_object_object_add(pno_param,"PNO_MIN",json_object_new_string(tmp));
            sprintf(tmp,"%.03f",ytShm->sPointList.sPoint[commid][busadd].ALARM_H);
            json_object_object_add(pno_param,"ALARM_H",json_object_new_string(tmp));
            sprintf(tmp,"%.03f",ytShm->sPointList.sPoint[commid][busadd].ALARM_L);
            json_object_object_add(pno_param,"ALARM_L",json_object_new_string(tmp));
            json_object_object_add(body,"PARAMS",pno_param);
        }else{
            json_object_object_add(body,"ErrMsg",json_object_new_string("query out of size"));
        }
    }else{
        json_object_object_add(body,"ErrMsg",json_object_new_string("query params error"));
    }
}

void get_comm_params(json_object *q_body, json_object *body)
{
    json_object *CommId=NULL;
    json_object_object_foreach(q_body, key, val){
        if(strcmp("COMM",key)==0)
            CommId = val;
    }
    if((CommId!=NULL)){
        char tmp[256];
        int commid = atoi(json_object_get_string(CommId));
        sprintf(tmp,"%d",commid);
        json_object_object_add(body,"COMM",json_object_new_string(tmp));
        if((commid>=0)&&(commid<4)){
            json_object *comm_param = json_object_new_object();
            sprintf(tmp,"%d",ytShm->commList.comm[commid].IDX);
            json_object_object_add(comm_param,"IDX",json_object_new_string(tmp));
            sprintf(tmp,"%d",ytShm->commList.comm[commid].ENABLED);
            json_object_object_add(comm_param,"ENABLED",json_object_new_string(tmp));
            sprintf(tmp,"%d",ytShm->commList.comm[commid].CommType);
            json_object_object_add(comm_param,"COMMTYPE",json_object_new_string(tmp));
            json_object_object_add(comm_param,"PATH",json_object_new_string(ytShm->commList.comm[commid].PATH));
            json_object_object_add(body,"PARAMS",comm_param);
        }else{
            json_object_object_add(body,"ErrMsg",json_object_new_string("query out of size"));
        }
    }else{
        json_object_object_add(body,"ErrMsg",json_object_new_string("query params error"));
    }
}

void set_comm_params(json_object *q_body, json_object *body)
{
    json_object *CommId=NULL, *CommParams=NULL;
    json_object_object_foreach(q_body, key, val){
        if(strcmp("COMM",key)==0)
            CommId = val;
        else if(strcmp("PARAMS",key)==0)
            CommParams = val;
    }
    if((CommId!=NULL)&&(CommParams!=NULL)){
        char tmp[256];
        int commid = atoi(json_object_get_string(CommId));
        sprintf(tmp,"%d",commid);
        json_object_object_add(body,"COMM",json_object_new_string(tmp));
        if((commid>=0)&&(commid<4)){
            json_object *Idx=NULL, *Enabled=NULL, *CommType=NULL, *Path=NULL;
            json_object_object_foreach(CommParams, key, val){
                if(strcmp("IDX",key)==0)
                    Idx = val;
                else if(strcmp("ENABLED",key)==0)
                    Enabled = val;
                else if(strcmp("COMMTYPE",key)==0)
                    CommType = val;
                else if(strcmp("PATH",key)==0)
                    Path = val;
            }
            if((Idx!=NULL)
                    &&(json_object_get_int(Idx)==commid)
                    &&(Enabled!=NULL)
                    &&(CommType!=NULL)
                    &&(Path!=NULL))
            {
                int enabled = json_object_get_int(Enabled);
                int commtype = json_object_get_int(CommType);
                char path[512];
                sprintf(path,"%s",json_object_get_string(Path));
                if((enabled>=0)&&(enabled<2)&&(commtype>=0)&&(strlen(path)<255)){
                    ytShm->commList.comm[commid].IDX = commid&0xff;
                    ytShm->commList.comm[commid].ENABLED = enabled&0xff;
                    ytShm->commList.comm[commid].CommType = commtype&0xff;
                    sprintf(ytShm->commList.comm[commid].PATH,"%s",path);
                    ytShm->commList.comm[commid].UNSAVED =0x01;
                    json_object_object_add(body,"SETUP",json_object_new_string("TRUE"));
                }else{
                    json_object_object_add(body,"SETUP",json_object_new_string("FALSE"));
                    json_object_object_add(body,"ErrMsg",json_object_new_string("set params out of size"));
                }
            }else{
                json_object_object_add(body,"SETUP",json_object_new_string("FALSE"));
                json_object_object_add(body,"ErrMsg",json_object_new_string("set params error"));
            }
        }else{
            json_object_object_add(body,"SETUP",json_object_new_string("FALSE"));
            json_object_object_add(body,"ErrMsg",json_object_new_string("query out of size"));
        }
    }else{
        json_object_object_add(body,"SETUP",json_object_new_string("FALSE"));
        json_object_object_add(body,"ErrMsg",json_object_new_string("query params error"));
    }
}

void get_spno_params(json_object *q_body, json_object *body)
{
    json_object *CommId=NULL, *Addr=NULL;
    json_object_object_foreach(q_body, key, val){
        if(strcmp("COMM",key)==0)
            CommId = val;
        else if(strcmp("ADDR",key)==0)
            Addr = val;
    }
    if((CommId!=NULL)&&(Addr!=NULL)){
        char tmp[256];
        int commid = json_object_get_int(CommId);
        int addr = json_object_get_int(Addr);
        sprintf(tmp,"%d",commid);
        json_object_object_add(body,"COMM",json_object_new_string(tmp));
        sprintf(tmp,"%d",addr);
        json_object_object_add(body,"ADDR",json_object_new_string(tmp));
        if((commid>=0)&&(commid<4)
                &&(addr>=0)&&(addr<256)){
            json_object *params = json_object_new_object();
            sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][addr].COMM_IDX);
            json_object_object_add(params,"COMM_IDX",json_object_new_string(tmp));
            sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][addr].BUS_ADD);
            json_object_object_add(params,"BUS_ADD",json_object_new_string(tmp));
            sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][addr].ENABLED);
            json_object_object_add(params,"ENABLED",json_object_new_string(tmp));
            json_object_object_add(params,"NAME",json_object_new_string(ytShm->sPointList.sPoint[commid][addr].NAME));
            sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][addr].PNO_TYPE);
            json_object_object_add(params,"PNO_TYPE",json_object_new_string(tmp));
            sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][addr].PNO_ALARM);
            json_object_object_add(params,"PNO_ALARM",json_object_new_string(tmp));
            sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][addr].PNO_VALUE);
            json_object_object_add(params,"PNO_VALUE",json_object_new_string(tmp));
            sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][addr].PNO_DLY);
            json_object_object_add(params,"PNO_DLY",json_object_new_string(tmp));
            sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][addr].PNO_AD);
            json_object_object_add(params,"PNO_AD",json_object_new_string(tmp));
            sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][addr].OFFSET);
            json_object_object_add(params,"OFFSET",json_object_new_string(tmp));
            sprintf(tmp,"%d",ytShm->sPointList.sPoint[commid][addr].OFFSET_FLAG);
            json_object_object_add(params,"OFFSET_FLAG",json_object_new_string(tmp));
            sprintf(tmp,"%.03f",ytShm->sPointList.sPoint[commid][addr].PNO_MAX);
            json_object_object_add(params,"PNO_MAX",json_object_new_string(tmp));
            sprintf(tmp,"%.03f",ytShm->sPointList.sPoint[commid][addr].PNO_MIN);
            json_object_object_add(params,"PNO_MIN",json_object_new_string(tmp));
            sprintf(tmp,"%.03f",ytShm->sPointList.sPoint[commid][addr].ALARM_H);
            json_object_object_add(params,"ALARM_H",json_object_new_string(tmp));
            sprintf(tmp,"%.03f",ytShm->sPointList.sPoint[commid][addr].ALARM_L);
            json_object_object_add(params,"ALARM_L",json_object_new_string(tmp));
            sprintf(tmp,"%ld",ytShm->sPointList.sPoint[commid][addr].SUB_TYPE);
            json_object_object_add(params,"SUB_TYPE",json_object_new_string(tmp));
            json_object_object_add(body,"PARAMS",params);
        }else{
            json_object_object_add(body,"ErrMsg",json_object_new_string("query out of size"));
        }
    }else{
        json_object_object_add(body,"ErrMsg",json_object_new_string("query params error"));
    }
}

void set_spno_params(json_object *q_body, json_object *body)
{
    json_object *CommId=NULL, *Addr=NULL, *Params=NULL;
    json_object_object_foreach(q_body, key, val){
        if(strcmp("COMM",key)==0)
            CommId = val;
        else if(strcmp("ADDR",key)==0)
            Addr = val;
        else if(strcmp("PARAMS",key)==0)
            Params = val;
    }
    if((CommId!=NULL)&&(Addr!=NULL)
            &&(Params!=NULL)){
        char tmp[256];
        int commid = json_object_get_int(CommId);
        int addr = json_object_get_int(Addr);
        sprintf(tmp,"%d",commid);
        json_object_object_add(body,"COMM",json_object_new_string(tmp));
        sprintf(tmp,"%d",addr);
        json_object_object_add(body,"ADDR",json_object_new_string(tmp));
        if((commid>=0)&&(commid<4)
                &&(addr>=0)&&(addr<256)){
            json_object *CommIdx=NULL, *BusAdd=NULL, *Enabled=NULL, *Name=NULL;
            json_object *PnoType=NULL, *PnoAlarm=NULL, *PnoValue=NULL, *PnoDly=NULL;
            json_object *PnoAd=NULL, *Offset=NULL, *OffsetFlag=NULL, *PnoMax=NULL;
            json_object *PnoMin=NULL, *AlarmH=NULL, *AlarmL=NULL, *SubType=NULL;
            json_object_object_foreach(Params, key, val){
                if(strcmp("COMM_IDX",key)==0)
                    CommIdx = val;
                else if(strcmp("BUS_ADD",key)==0)
                    BusAdd = val;
                else if(strcmp("ENABLED",key)==0)
                    Enabled = val;
                else if(strcmp("NAME",key)==0)
                    Name = val;
                else if(strcmp("PNO_TYPE",key)==0)
                    PnoType = val;
                else if(strcmp("PNO_ALARM",key)==0)
                    PnoAlarm = val;
                else if(strcmp("PNO_VALUE",key)==0)
                    PnoValue = val;
                else if(strcmp("PNO_DLY",key)==0)
                    PnoDly = val;
                else if(strcmp("PNO_AD",key)==0)
                    PnoAd = val;
                else if(strcmp("OFFSET",key)==0)
                    Offset = val;
                else if(strcmp("OFFSET_FLAG",key)==0)
                    OffsetFlag = val;
                else if(strcmp("PNO_MAX",key)==0)
                    PnoMax = val;
                else if(strcmp("PNO_MIN",key)==0)
                    PnoMin = val;
                else if(strcmp("ALARM_H",key)==0)
                    AlarmH = val;
                else if(strcmp("ALARM_L",key)==0)
                    AlarmL = val;
                else if(strcmp("SUB_TYPE",key)==0)
                    SubType = val;
            }
            if((CommIdx!=NULL)&&(BusAdd!=NULL)&&(Enabled!=NULL)
                    &&(Name!=NULL)&&(PnoType!=NULL)&&(PnoAlarm!=NULL)
                    &&(PnoValue!=NULL)&&(PnoDly!=NULL)&&(PnoAd!=NULL)
                    &&(Offset!=NULL)&&(OffsetFlag!=NULL)&&(PnoMax!=NULL)
                    &&(PnoMin!=NULL)&&(AlarmH!=NULL)&&(AlarmL!=NULL)
                    &&(SubType!=NULL)){
                int commidx = atoi(json_object_get_string(CommIdx));
                int busadd = atoi(json_object_get_string(BusAdd));
                int enabled = atoi(json_object_get_string(Enabled));
                int pnotype = atoi(json_object_get_string(PnoType));
                int pnoalarm = atoi(json_object_get_string(PnoAlarm));
                int pnovalue = atoi(json_object_get_string(PnoValue));
                int pnodly = atoi(json_object_get_string(PnoDly));
                int pnoad = atoi(json_object_get_string(PnoAd));
                int offset = atoi(json_object_get_string(Offset));
                int offset_flag = atoi(json_object_get_string(OffsetFlag));
                float pnomax = atof(json_object_get_string(PnoMax));
                float pnomin = atof(json_object_get_string(PnoMin));
                float alarmh = atof(json_object_get_string(AlarmH));
                float alarml = atof(json_object_get_string(AlarmL));
                uint subtype = (atoll(json_object_get_string(SubType)))&0xffffffff;
                if((commidx==commid)&&(busadd==addr)&&(enabled>=0)&&(enabled<2)
                        &&(pnotype>=0)&&(pnotype<5)&&(pnoalarm>=0)&&(pnoalarm<3)
                        &&(pnovalue>=0)&&(pnovalue<4)&&(pnodly>=0)&&(pnodly<256)
                        &&(pnoad>=0)&&(pnoad<17)&&(offset>=0)&&(offset<256)
                        &&(offset_flag>=0)&&(offset_flag<2)&&(strlen(json_object_get_string(Name))<=220)){
                    ytShm->sPointList.sPoint[commid][addr].COMM_IDX = commidx;
                    ytShm->sPointList.sPoint[commid][addr].BUS_ADD = busadd;
                    ytShm->sPointList.sPoint[commid][addr].ENABLED = enabled;
                    sprintf(ytShm->sPointList.sPoint[commid][addr].NAME,"%s",json_object_get_string(Name));
                    ytShm->sPointList.sPoint[commid][addr].PNO_TYPE = pnotype;
                    ytShm->sPointList.sPoint[commid][addr].PNO_ALARM = pnoalarm;
                    ytShm->sPointList.sPoint[commid][addr].PNO_VALUE = pnovalue;
                    ytShm->sPointList.sPoint[commid][addr].PNO_DLY = pnodly;
                    ytShm->sPointList.sPoint[commid][addr].PNO_AD = pnoad;
                    ytShm->sPointList.sPoint[commid][addr].OFFSET = offset;
                    ytShm->sPointList.sPoint[commid][addr].OFFSET_FLAG = offset_flag;
                    ytShm->sPointList.sPoint[commid][addr].PNO_MAX = pnomax;
                    ytShm->sPointList.sPoint[commid][addr].PNO_MIN = pnomin;
                    ytShm->sPointList.sPoint[commid][addr].ALARM_H = alarmh;
                    ytShm->sPointList.sPoint[commid][addr].ALARM_L = alarml;
                    ytShm->sPointList.sPoint[commid][addr].SUB_TYPE = subtype;
                    ytShm->sPointList.sPoint[commid][addr].UNSAVED = 0x01;
                    json_object_object_add(body,"SETUP",json_object_new_string("TRUE"));
                }else{
                    json_object_object_add(body,"SETUP",json_object_new_string("FALSE"));
                    json_object_object_add(body,"ErrMsg",json_object_new_string("set params out of size"));
                }
            }else{
                json_object_object_add(body,"SETUP",json_object_new_string("FALSE"));
                json_object_object_add(body,"ErrMsg",json_object_new_string("set params error"));
            }
        }else{
            json_object_object_add(body,"SETUP",json_object_new_string("FALSE"));
            json_object_object_add(body,"ErrMsg",json_object_new_string("query out of size"));
        }
    }else{
        json_object_object_add(body,"SETUP",json_object_new_string("FALSE"));
        json_object_object_add(body,"ErrMsg",json_object_new_string("query params error"));
    }
}

void qry_iot_status(json_object *q_body, json_object *body)
{
    int comm,adds,i;
    int count[5],alarm[5],out[5];
    json_object *iotlist,*obj[5],*obj_list[5];
    char tmp[256];
    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("离线"));
                        }
                    }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("告警"));
                        }else{
                            json_object_object_add(sp,"STATUS_ID",json_object_new_int(0));
                            json_object_object_add(sp,"STATUS",json_object_new_string("正常"));
                        }
                    }
                    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,"%.3f",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("火灾监控实时信息"));
            break;
        case 1:
            json_object_object_add(obj[i],"NAME",json_object_new_string("电气火灾监控实时信息"));
            break;
        case 2:
            json_object_object_add(obj[i],"NAME",json_object_new_string("消防水系统监控实时信息"));
            break;
        case 3:
            json_object_object_add(obj[i],"NAME",json_object_new_string("通道监控实时信息"));
            break;
        case 4:
            json_object_object_add(obj[i],"NAME",json_object_new_string("消防电源监控实时信息"));
            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);
}

void json_cmd(int cmd, json_object *q_body, json_object *body)
{
    switch (cmd) {
    case CMD_GET_REALTIME_STATUS:
        get_realtime_status(q_body,body);
        break;
    case CMD_GET_HISTORY_VALUE:
        get_history_value(q_body,body);
        break;
    case CMD_GET_COMM_PARAMS:
        get_comm_params(q_body,body);
        break;
    case CMD_SET_COMM_PARAMS:
        set_comm_params(q_body,body);
        break;
    case CMD_GET_SPNO_PARAMS:
        get_spno_params(q_body, body);
        break;
    case CMD_SET_SPNO_PARAMS:
        set_spno_params(q_body, body);
        break;
    case CMD_QRY_IOT_STATUS:
        qry_iot_status(q_body,body);
        break;
    case CMD_CTL:

        break;
    default:
        break;
    }
}

bool json_request_action(char *request, json_object *body)
{
    json_object *q_json = NULL, *q_ver = NULL, *q_body =NULL;
    json_object *q_cmd = NULL, *q_reply=NULL;
    int cmd;
    q_json = json_tokener_parse(request);
    if(q_json!=NULL){
        json_object_object_foreach(q_json,key,val){
            if(strcmp(key,"REPLY")==0){
                q_reply = val;
            }else if(strcmp(key,"VER")==0){
                q_ver = val;
            }else if(strcmp(key,"BODY")==0){
                q_body = val;
            }
        }
        if(q_reply==NULL){
            json_object_put(q_json);
            json_object_object_add(body,"ErrMsg",json_object_new_string("reply null"));
            return false;
        }else if(strcmp(json_object_get_string(q_reply),"0")!=0){
            json_object_put(q_json);
            json_object_object_add(body,"ErrMsg",json_object_new_string("reply error"));
            return false;
        }
        if(q_ver==NULL){
            json_object_put(q_json);
            json_object_object_add(body,"ErrMsg",json_object_new_string("version null"));
            return false;
        }else if(strcmp(json_object_get_string(q_ver),"1.00")!=0){
            json_object_put(q_json);
            json_object_object_add(body,"ErrMsg",json_object_new_string("version error"));
            return false;
        }
        if(q_body==NULL){
            json_object_put(q_json);
            json_object_object_add(body,"ErrMsg",json_object_new_string("body null"));
            return false;
        }
        json_object_object_foreach(q_body,key2,val2){
            if(strcmp(key2,"CMD")==0){
                q_cmd = val2;
            }
        }
        if(q_cmd==NULL){
            json_object_put(q_json);
            json_object_object_add(body,"ErrMsg",json_object_new_string("cmd null"));
            return false;
        }else if(json_object_get_type(q_cmd)!=json_type_int){
            json_object_put(q_json);
            json_object_object_add(body,"ErrMsg",json_object_new_string("cmd datatype error"));
            return false;
        }
        cmd = json_object_get_int(q_cmd);
        json_object_object_add(body,"CMD",json_object_new_int(cmd));
        json_cmd(cmd,q_body,body);
        json_object_put(q_json);
    }else{
        json_object_object_add(body,"ErrMsg",json_object_new_string("query null"));
        return false;
    }
    return true;
}

int main(void)
{
    char *input,*lenstr,*data;
    char tmpstr[1024];
    json_object *json,*json_body;
    int len;
    printf("%s%c%c\n","Content-Type:text/html;charset=UTF-8",13,10);
    json = json_object_new_object();
    json_object_object_add(json,"VER",json_object_new_string("1.00"));
    json_object_object_add(json,"NAME",json_object_new_string("Usky_IoT-03"));
    lenstr = getenv("CONTENT_LENGTH");
    if(lenstr){
        len = atoi(lenstr);
        if((len>0)&&(len<1024)){
            input = (char *)malloc((unsigned)(len+2));
            data = (char *)malloc((unsigned)(len+2));
            fgets(input,len+1,stdin);
            if(load_shm()){
                ytShm->dogTimeList.dogTime[63].LASTFEED=0x01;
                sprintf(tmpstr,"%04d-%02d-%02d %02d:%02d:%02d",
                        ytShm->currentTime.YEAR,
                        ytShm->currentTime.MONTH,
                        ytShm->currentTime.DAY,
                        ytShm->currentTime.HOUR,
                        ytShm->currentTime.MINUTE,
                        ytShm->currentTime.SECOND);
                json_object_object_add(json,"TIMESTAMP",json_object_new_string(tmpstr));
                json_body = json_object_new_object();
               if(json_request_action(input, json_body)){
                   json_object_object_add(json,"RESULT",json_object_new_string("1"));
                   json_object_object_add(json,"BODY",json_body);
               }else{
                   json_object_object_add(json,"RESULT",json_object_new_string("0"));
                   json_object_object_add(json,"MESSAGE",json_object_new_string("Query Json error."));
                   json_object_object_add(json,"BODY",json_body);
               }
               shmdt(ytShm);
            }else{
                json_object_object_add(json,"RESULT",json_object_new_string("0"));
                json_object_object_add(json,"MESSAGE",json_object_new_string("Load Share Memory failed."));
            }
            json_object_object_add(json,"VER",json_object_new_string("1.00"));
            json_object_object_add(json,"REPLY",json_object_new_string("1"));
            free(data);
            free(input);
        }else{
            json_object_object_add(json,"RESULT",json_object_new_string("0"));
            json_object_object_add(json,"MESSAGE",json_object_new_string("Length error."));
        }
    }else{
        json_object_object_add(json,"RESULT",json_object_new_string("0"));
        json_object_object_add(json,"MESSAGE",json_object_new_string("No Content error."));
    }
    printf("%s",json_object_to_json_string(json));
    json_object_put(json);
    return 0;
}
