/* Includes ------------------------------------------------------------------*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "file.h"
#include "ca_main.h"
#include "debug.h"
#include "ca_param.h"

//cau初始化参数名
char *cauInitCfgName[7] = {"Range","Ratio","ZeroPoint","ChangeThreshold","HighLimit","LowLimit","chSwitch"};

int ip2array(char *ip, unsigned char arr[])
{
    char *pstr = ip;
    char *p1 = ip, *p2 = ip;
    int n  = 0;
    while (*pstr != '\0')
    {
        if ((p2 = strchr(p1, '.')) != NULL)
        {
            *p2 = '\0';
            arr[n++] = atoi(p1);
            p1 = p2 + 1;
        }
        pstr++;
    }
    arr[n] = atoi(p1);
    return 0;
}

//加载MQTT配置
int _param_load_mqtt_cfg(cJSON *pCfgJsRoot)
{
    int ret = 0;
    cJSON *pJson = cJSON_GetObjectItem(pCfgJsRoot, "MQTTConfig");
    if (pJson == NULL)
    {
        LOG("cJSON_GetObjectItem fail");
        return 1;
    }
    mqtt_param_t *pParam = &pparam_config->mqttCfg;
    
    strcpy((char *)pParam->client_id,cJSON_GetStringValue(cJSON_GetObjectItem(pJson, "ClientID")));
    if (pParam->client_id[0] == '\0')
    {
        strcpy((char *)pParam->client_id, CSG_CLIENT_ID_DEFAULT); 
        ret = 1;
    }
    strcpy((char *)pParam->server_addr,cJSON_GetStringValue(cJSON_GetObjectItem(pJson, "ServiceIP")));
    if (pParam->server_addr[0] == '\0')
    {
        strcpy((char *)pParam->server_addr, CSG_SERVER_ADDRESS_DEFAULT);
        ret = 1;
    }
    pParam->server_port = cJSON_GetNumberValue(cJSON_GetObjectItem(pJson, "ServicePort"));
    if (pParam->server_port < 1024)
    {
        pParam->server_port = CSG_SERVER_PORT_DEFAULT;
        ret = 1;
    }
    strcpy((char *)pParam->password,cJSON_GetStringValue(cJSON_GetObjectItem(pJson, "PassWord")));
    if (pParam->password[0] == '\0')
    {
        strcpy((char *)pParam->password, CSG_PASSWORD_DEFAULT);
        ret = 1;
    }
    strcpy((char *)pParam->user_name,cJSON_GetStringValue(cJSON_GetObjectItem(pJson, "UserName")));
    if (pParam->user_name[0] == '\0')
    {
        strcpy((char *)pParam->user_name, CSG_USER_NAME_DEFAULT);
        ret = 1;
    }
    strcpy((char *)pParam->device_id,cJSON_GetStringValue(cJSON_GetObjectItem(pJson, "DeviceID")));
    if (pParam->device_id[0] == '\0')
    {
        strcpy((char *)pParam->device_id, CSG_DEVICE_ID_DEFAULT);
        ret = 1;
    }
    pParam->keep_live_time = cJSON_GetNumberValue(cJSON_GetObjectItem(pJson, "KeepAliveTime"));
    if (pParam->keep_live_time < 0 || pParam->keep_live_time > 500)
    {
        pParam->keep_live_time = CSG_KEEP_LIVE_TIME_DEFAULT;
        ret = 1;
    }
    return ret;
}

bool _param_save_mqtt_cfg(cJSON *pCfgJsRoot)
{
    mqtt_param_t *pParam = &pparam_config->mqttCfg;
    cJSON *pJson = cJSON_CreateObject();
    if (pJson == NULL)
    {
        return -1;
    }

    cJSON_AddStringToObject(pJson,"ClientID",(char *)pParam->client_id);
    cJSON_AddStringToObject(pJson,"ServiceIP",(char *)pParam->server_addr);
    cJSON_AddNumberToObject(pJson, "ServicePort", pParam->server_port);
    cJSON_AddStringToObject(pJson,"DeviceID",(char *)pParam->device_id);
    cJSON_AddStringToObject(pJson,"PassWord",(char *)pParam->password);
    cJSON_AddNumberToObject(pJson, "KeepAliveTime", pParam->keep_live_time);
    cJSON_AddStringToObject(pJson,"UserName",(char *)pParam->user_name);
    cJSON_AddItemToObject(pCfgJsRoot, "MQTTConfig", pJson);
    return 0;
}

int _param_load_dev_cfg(cJSON *pCfgJsRoot)
{
    int i;
    cJSON *pJson = cJSON_GetObjectItem(pCfgJsRoot, "DevCfg");
    if (pJson == NULL)
    {
        return -1;
    }

    pparam_config->devSize = cJSON_GetArraySize(pJson);
    printh("devSize = %d\n", pparam_config->devSize);
    for (i = 0; i < pparam_config->devSize; i++)
    {
        cJSON *pJsonArr = cJSON_GetArrayItem(pJson, i);//根据下标获取cjson对象数组中的对象
        DevCfg_t *pParam = &pparam_config->devs[i];
        pParam->enable = cJSON_GetNumberValue(cJSON_GetObjectItem(pJsonArr, "enable"));
        pParam->space = cJSON_GetNumberValue(cJSON_GetObjectItem(pJsonArr, "space"));
        pParam->level = cJSON_GetNumberValue(cJSON_GetObjectItem(pJsonArr, "level"));
        strcpy((char *)pParam->devName, cJSON_GetStringValue(cJSON_GetObjectItem(pJsonArr, "devName")));
        strcpy((char *)pParam->comProtocol, cJSON_GetStringValue(cJSON_GetObjectItem(pJsonArr, "comProtocol")));
        strcpy((char *)pParam->portName, cJSON_GetStringValue(cJSON_GetObjectItem(pJsonArr, "portName")));
        printh("设备名称=%s, 通讯协议=%s, 是否使能=%d, 采集间隔=%d, 优先级别=%d\n",pParam->devName,pParam->portName,\
            pParam->enable,pParam->space,pParam->level);
    }
    return 0;
}

int _param_save_dev_cfg(cJSON *pJsRoot)
{
    int i = 0;
    cJSON *pJsonArr = cJSON_CreateArray();
    if (pJsonArr == NULL)
    {
        printh("[%s:%d] JSON Create failed.\n", __FUNCTION__, __LINE__);
        return -1;
    }
    for (i = 0; i < pparam_config->devSize && i < DEV_MAX_COUNT; i++)
    {
        DevCfg_t *pCfg = &pparam_config->devs[i];
        cJSON *jPort = cJSON_CreateObject();
        if (jPort == NULL)
        {
            printh("[%s:%d] JSON Create failed.\n", __FUNCTION__, __LINE__);
            return -1;
        }
        cJSON_AddNumberToObject(jPort, "enable", pCfg->enable);
        cJSON_AddNumberToObject(jPort, "space", pCfg->space);
        cJSON_AddNumberToObject(jPort, "level", pCfg->level);
        cJSON_AddStringToObject(jPort, "devName", (char *)pCfg->devName);
        cJSON_AddStringToObject(jPort, "comProtocol", (char *)pCfg->comProtocol);
        cJSON_AddStringToObject(jPort, "portName", (char *)pCfg->portName);
        cJSON_AddItemToArray(pJsonArr, jPort);
    }
    cJSON_AddItemToObject(pJsRoot, "DevCfg", pJsonArr);
    return 0;
}


int _param_load_cau_cfg(cJSON *pCfgJsRoot)
{
    int i;
    cJSON *pJson = cJSON_GetObjectItem(pCfgJsRoot, "CAUInit");
    if (pJson == NULL)
    {
        return -1;
    }

    for (i = 0; i < 7; i++)
    {
        char *str =cJSON_GetStringValue(cJSON_GetObjectItem(pJson, cauInitCfgName[i]));
        
        char *p = strsep(&str,",");
        int j = 0;
        while(p != NULL)
        {
            if(j >= 24) break;
            
            //cau_param_t *pParam = &pparam_config->CAUInit[i];
            if(i == 0)
            {
                pparam_config->cauParam[j].Range = atof(p);
            }
            else if(i == 1)
            {
                pparam_config->cauParam[j].Ratio = atof(p);
            }
            else if(i == 2)
            {
                pparam_config->cauParam[j].ZeroPoint = atof(p);
            }
            else if(i == 3)
            {
                pparam_config->cauParam[j].ChangeThreshold = atof(p);
            }
            else if(i == 4)
            {
                pparam_config->cauParam[j].HighLimit = atof(p);
            }
            else if(i == 5)
            {
                pparam_config->cauParam[j].LowLimit = atof(p);
            }
            else 
            {
                pparam_config->cauParam[j].chSwitch = atoi(p);
            }

            p = strsep(&str, ",");
            j++;
        }
#if 0
        printf("%s:\n", cauInitCfgName[i]);
        for (int a = 0; a < 24; a++)
        {
            //for (int b = 0; b < 8; b++)
            {
                //printf("%f ", pparam_config->cauParam[a].Range);
            }
        }
        //printf("\n");
#endif
    }
    return 0;
}

int _param_save_cau_cfg(cJSON *pJsRoot)
{
    cJSON *jPort = cJSON_CreateObject();
    if (jPort == NULL)
    {
        printh("[%s:%d] JSON Create failed.\n", __FUNCTION__, __LINE__);
        return -1;
    }
    int i = 0,j = 0;
    for(i = 0;i < 7;i ++)
    {
        char buf[1024] = {0};
        for(j = 0;j < 24;j ++)
        {
            char temp[20] = {0};
            switch (i)
            {
            case 0:
                sprintf(temp,"%.1f,",pparam_config->cauParam[j].Range);
                break;
            case 1:
                sprintf(temp,"%.1f,",pparam_config->cauParam[j].Ratio);
                break;
            case 2:
                sprintf(temp,"%.1f,",pparam_config->cauParam[j].ZeroPoint);
                break;
            case 3:
                sprintf(temp,"%.1f,",pparam_config->cauParam[j].ChangeThreshold);
                break;
            case 4:
                sprintf(temp,"%.1f,",pparam_config->cauParam[j].HighLimit);
                break;
            case 5:
                sprintf(temp,"%.1f,",pparam_config->cauParam[j].LowLimit);
                break;
            case 6:
                sprintf(temp,"%d,",pparam_config->cauParam[j].chSwitch);
                break;
            }
            strcat(buf,temp);
        }
        buf[strlen(buf)-1] = 0;
        printh("%s:%s\n", cauInitCfgName[i], buf);
        cJSON_AddStringToObject(jPort, cauInitCfgName[i], buf);
    }
    cJSON_AddItemToObject(pJsRoot, "CAUInit", jPort);
    return 1;
}

int _param_load_gateway_cfg(cJSON *pCfgJsRoot)
{
    cJSON *pJson = cJSON_GetObjectItem(pCfgJsRoot, "gateway");
    if (pJson == NULL)
    {
        return -1;
    }
    
    GateWay_t *pParam = &pparam_config->gateway;
  
    pParam->CommunicationMode = cJSON_GetNumberValue(cJSON_GetObjectItem(pJson, "CommunicationMode"));
    pParam->radioType = cJSON_GetNumberValue(cJSON_GetObjectItem(pJson, "radioType"));
    pParam->uploadSpace = cJSON_GetNumberValue(cJSON_GetObjectItem(pJson, "uploadSpace"));
    pParam->EnGPS = cJSON_GetNumberValue(cJSON_GetObjectItem(pJson, "EnGPS"));
    strcpy((char *)pParam->apn, cJSON_GetStringValue(cJSON_GetObjectItem(pJson, "APN")));

    char *pbuf = cJSON_GetStringValue(cJSON_GetObjectItem(pJson, "Server1"));
    ip2array(pbuf, pParam->ServerIP1);

    pbuf = cJSON_GetStringValue(cJSON_GetObjectItem(pJson, "Server2"));
    ip2array(pbuf, pParam->ServerIP2);

    pParam->ServerIP1_Port = cJSON_GetNumberValue(cJSON_GetObjectItem(pJson, "Server1Port"));
    pParam->ServerIP2_Port = cJSON_GetNumberValue(cJSON_GetObjectItem(pJson, "Server2Port"));

    pParam->vBatChangeStart = cJSON_GetNumberValue(cJSON_GetObjectItem(pJson, "vBatChangeStart"));
    pParam->vBatChangeEnd = cJSON_GetNumberValue(cJSON_GetObjectItem(pJson, "vBatChangeEnd"));
    pParam->vscValue = cJSON_GetNumberValue(cJSON_GetObjectItem(pJson, "vscValue"));

    printh("CommunicationMode=%d\n", pParam->CommunicationMode);
    printh("radioType=%d\n", pParam->radioType);
    printh("uploadSpace=%d\n", pParam->uploadSpace);
    printh("EnGPS=%d\n", pParam->EnGPS);
    printh("apn=%s\n", pParam->apn);
    printh("ServerIP1:%d.%d.%d.%d\n", pParam->ServerIP1[0], pParam->ServerIP1[1], pParam->ServerIP1[2], pParam->ServerIP1[3]);
    printh("ServerIP2:%d.%d.%d.%d\n", pParam->ServerIP2[0], pParam->ServerIP2[1], pParam->ServerIP2[2], pParam->ServerIP2[3]);
    printh("ServerIP1_Port=%d\n", pParam->ServerIP1_Port);
    printh("ServerIP2_Port=%d\n", pParam->ServerIP2_Port);
    printh("vBatChangeStart=%d\n", pParam->vBatChangeStart);
    printh("vBatChangeEnd=%d\n", pParam->vBatChangeEnd);
    printh("vscValue=%d\n", pParam->vscValue);
    //printf("CommunicationMode=%d\n", pParam->CommunicationMode);
    return 0;
}

int _param_save_gateway_cfg(cJSON *pJsRoot)
{
    int i;
    GateWay_t *pParam = &pparam_config->gateway;
    cJSON *pJson = cJSON_CreateObject();
    if (pJson == NULL)
    {
        printh("[%s:%d] JSON Create failed.\n", __FUNCTION__, __LINE__);
        return -1;
    }

    cJSON_AddStringToObject(pJson, "APN", (char *)pParam->apn);
    cJSON_AddNumberToObject(pJson, "CommunicationMode", pParam->CommunicationMode);
    cJSON_AddNumberToObject(pJson, "radioType", pParam->radioType);
    cJSON_AddNumberToObject(pJson, "Server1Port", pParam->ServerIP1_Port);
    cJSON_AddNumberToObject(pJson, "Server2Port", pParam->ServerIP2_Port);
    cJSON_AddNumberToObject(pJson, "EnGPS", pParam->EnGPS);
    cJSON_AddNumberToObject(pJson, "uploadSpace", pParam->uploadSpace);

    cJSON_AddNumberToObject(pJson, "vBatChangeStart", pParam->vBatChangeStart);
    cJSON_AddNumberToObject(pJson, "vBatChangeEnd", pParam->vBatChangeEnd);
    cJSON_AddNumberToObject(pJson, "vscValue", pParam->vscValue);
  
    char sbuf[32] = {0};
    snprintf(sbuf, 31, "%d.%d.%d.%d", pParam->ServerIP1[0], pParam->ServerIP1[1], pParam->ServerIP1[2], pParam->ServerIP1[3]);
    cJSON_AddStringToObject(pJson, "Server1", sbuf);
    
    memset(sbuf,0,sizeof(sbuf));
    snprintf(sbuf, 31, "%d.%d.%d.%d", pParam->ServerIP2[0], pParam->ServerIP2[1], pParam->ServerIP2[2], pParam->ServerIP2[3]);
    cJSON_AddStringToObject(pJson, "Server2", sbuf);

    memset(sbuf,0,sizeof(sbuf));
    char buf[128] = {0};
    for(i = 0;i < 8;i++)
    {
        sprintf(sbuf,"%d,",pParam->Switch[i]);
        strcat(buf,sbuf);
    }
    buf[strlen(buf)-1] = 0;
    cJSON_AddStringToObject(pJson, "Switch", buf);

    cJSON_AddItemToObject(pJsRoot, "gateway", pJson);

    if(pParam->radioType == 0)
    {
        system("cp /etc/network/S80SetEth0-net  /etc/network/S80SetEth0");
    }
    else
    {
        system("cp /etc/network/S80SetEth0-ppp  /etc/network/S80SetEth0");
    }

    return 0;
}

int _param_load_land_hex_cfg()
{
    int i;
    LandDevCfg_t *pParam = &pparam_config->landHexCfg;

    for (i = 0; i < pparam_config->devSize; i++)
    {
        DevCfg_t *dev = &pparam_config->devs[i];
        if (dev == NULL)
            continue;

        if(strcmp((char*)dev->devName,"DAU") == 0)
        {
            pParam->hfEn = dev->enable;
            pParam->hfLeven = dev->level;
        }
        else if(strcmp((char*)dev->devName,"CAU1") == 0)
        {
            pParam->cau1En = dev->enable;
            pParam->cau1level = dev->level;
            pParam->cau1Space = dev->space;
        }
        else if(strcmp((char*)dev->devName,"CAU2") == 0)
        {
            pParam->cau2En = dev->enable;
            pParam->cau2level = dev->level;
            pParam->cau2Space = dev->space;
        }
        else if(strcmp((char*)dev->devName,"CAU3") == 0)
        {
            pParam->cau3En = dev->enable;
            pParam->cau3level = dev->level;
            pParam->cau3Space = dev->space;
        }
        else if(strcmp((char*)dev->devName,"EnvDev") == 0)
        {
            pParam->enDevLevel = dev->level;
            pParam->enDevSpace = dev->space;
        }
        else if(strcmp((char*)dev->devName,"TempShake") == 0)
        {
            //DBG("温度振动JSON");
            pParam->tempsnakeEn = dev->enable;
            pParam->tempsnakelevel = dev->level;
            pParam->tempsnakeSpace = dev->space;
        }
    }

    pParam->cau1Ch1 = pparam_config->cauParam[0].chSwitch;
    pParam->cau1Ch2 = pparam_config->cauParam[1].chSwitch;
    pParam->cau1Ch3 = pparam_config->cauParam[2].chSwitch;
    pParam->cau1Ch4 = pparam_config->cauParam[3].chSwitch;
    pParam->cau1Ch5 = pparam_config->cauParam[4].chSwitch;
    pParam->cau1Ch6 = pparam_config->cauParam[5].chSwitch;
    pParam->cau1Ch7 = pparam_config->cauParam[6].chSwitch;
    pParam->cau1Ch8 = pparam_config->cauParam[7].chSwitch;
    pParam->cau2Ch1 = pparam_config->cauParam[8].chSwitch;
    pParam->cau2Ch2 = pparam_config->cauParam[9].chSwitch;
    pParam->cau2Ch3 = pparam_config->cauParam[10].chSwitch;
    pParam->cau2Ch4 = pparam_config->cauParam[11].chSwitch;
    pParam->cau2Ch5 = pparam_config->cauParam[12].chSwitch;
    pParam->cau2Ch6 = pparam_config->cauParam[13].chSwitch;
    pParam->cau2Ch7 = pparam_config->cauParam[14].chSwitch;
    pParam->cau2Ch8 = pparam_config->cauParam[15].chSwitch;
    pParam->cau3Ch1 = pparam_config->cauParam[16].chSwitch;
    pParam->cau3Ch2 = pparam_config->cauParam[17].chSwitch;
    pParam->cau3Ch3 = pparam_config->cauParam[18].chSwitch;
    pParam->cau3Ch4 = pparam_config->cauParam[19].chSwitch;
    pParam->cau3Ch5 = pparam_config->cauParam[20].chSwitch;
    pParam->cau3Ch6 = pparam_config->cauParam[21].chSwitch;
    pParam->cau3Ch7 = pparam_config->cauParam[22].chSwitch;
    pParam->cau3Ch8 = pparam_config->cauParam[23].chSwitch;

    pParam->tempsnakeType = pparam_config->tempSnakeCfg.devType;
    pParam->tempLimit = pparam_config->tempSnakeCfg.TempLimit;
    pParam->snakeLimit = pparam_config->tempSnakeCfg.ShakeLimit;

    pParam->thEn = pparam_config->envDevCfg[0].enable;
    pParam->thRs485Ch = pparam_config->envDevCfg[0].RS485Ch;
    pParam->thAddr = pparam_config->envDevCfg[0].Addr;

    pParam->waterEn = pparam_config->envDevCfg[1].enable;
    pParam->waterRs485Ch = pparam_config->envDevCfg[1].RS485Ch;
    pParam->waterAddr = pparam_config->envDevCfg[1].Addr;   


    pParam->o2En = pparam_config->envDevCfg[2].enable;
    pParam->o2Rs485Ch = pparam_config->envDevCfg[2].RS485Ch;
    pParam->o2Addr = pparam_config->envDevCfg[2].Addr; 

    pParam->ch4En = pparam_config->envDevCfg[3].enable;
    pParam->ch4Rs485Ch = pparam_config->envDevCfg[3].RS485Ch;
    pParam->ch4Addr = pparam_config->envDevCfg[3].Addr; 

    pParam->h2sEn = pparam_config->envDevCfg[4].enable;
    pParam->h2sRs485Ch = pparam_config->envDevCfg[4].RS485Ch;
    pParam->h2sAddr = pparam_config->envDevCfg[4].Addr; 

    pParam->co2En = pparam_config->envDevCfg[5].enable;
    pParam->co2Rs485Ch = pparam_config->envDevCfg[5].RS485Ch;
    pParam->co2Addr = pparam_config->envDevCfg[5].Addr; 

    pParam->cameraEn = pparam_config->cameraCfg.enable;
    pParam->camera485Ch = pparam_config->cameraCfg.RS485Ch;
    pParam->cameraDi = pparam_config->cameraCfg.di;
    pParam->cameraTriger = pparam_config->cameraCfg.trigger;
    pParam->cameraDelay = pparam_config->cameraCfg.delay;
    pParam->cameraSize = pparam_config->cameraCfg.size;
    return 0;
}

int _param_save_land_hex_cfg()
{
    LandDevCfg_t *cfg = &pparam_config->landHexCfg;
    int i = 0;
    for (i = 0; i < pparam_config->devSize ; i++)
    {
        DevCfg_t *dev = &pparam_config->devs[i];
        if (!dev)continue;

        if(strcmp((char *)dev->devName,"DAU") == 0)
        {
            dev->enable = cfg->hfEn;
            dev->level = cfg->hfLeven;
        }
        else if(strcmp((char *)dev->devName,"CAU1") == 0)
        {
            dev->enable = cfg->cau1En;
            dev->level = cfg->cau1level;
            dev->space = cfg->cau1Space;
        }
        else if(strcmp((char *)dev->devName,"CAU2") == 0)
        {
            dev->enable = cfg->cau2En;
            dev->level = cfg->cau2level;
            dev->space = cfg->cau2Space;
        }
        else if(strcmp((char *)dev->devName,"CAU3") == 0)
        {
            dev->enable = cfg->cau3En;
            dev->level = cfg->cau3level;
            dev->space = cfg->cau3Space;
        }
        else if(strcmp((char *)dev->devName,"EnvDev") == 0)
        {
            dev->level = cfg->enDevLevel;
            dev->space = cfg->enDevSpace;
        }
        else if(strcmp((char *)dev->devName,"TempShake") == 0)
        {
            dev->enable = cfg->tempsnakeEn;
            dev->level = cfg->tempsnakelevel;
            dev->space = cfg->tempsnakeSpace;
        }
    }

    pparam_config->cauParam[0].chSwitch  = cfg->cau1Ch1;
    pparam_config->cauParam[1].chSwitch  = cfg->cau1Ch2;
    pparam_config->cauParam[2].chSwitch  = cfg->cau1Ch3;
    pparam_config->cauParam[3].chSwitch  = cfg->cau1Ch4;
    pparam_config->cauParam[4].chSwitch  = cfg->cau1Ch5;
    pparam_config->cauParam[5].chSwitch  = cfg->cau1Ch6;
    pparam_config->cauParam[6].chSwitch  = cfg->cau1Ch7;
    pparam_config->cauParam[7].chSwitch  = cfg->cau1Ch8;
    pparam_config->cauParam[8].chSwitch  = cfg->cau2Ch1;
    pparam_config->cauParam[9].chSwitch  = cfg->cau2Ch2;
    pparam_config->cauParam[10].chSwitch = cfg->cau2Ch3;
    pparam_config->cauParam[11].chSwitch = cfg->cau2Ch4;
    pparam_config->cauParam[12].chSwitch = cfg->cau2Ch5;
    pparam_config->cauParam[13].chSwitch = cfg->cau2Ch6;
    pparam_config->cauParam[14].chSwitch = cfg->cau2Ch7;
    pparam_config->cauParam[15].chSwitch = cfg->cau2Ch8;
    pparam_config->cauParam[16].chSwitch = cfg->cau3Ch1;
    pparam_config->cauParam[17].chSwitch = cfg->cau3Ch2;
    pparam_config->cauParam[18].chSwitch = cfg->cau3Ch3;
    pparam_config->cauParam[19].chSwitch = cfg->cau3Ch4;
    pparam_config->cauParam[20].chSwitch = cfg->cau3Ch5;
    pparam_config->cauParam[21].chSwitch = cfg->cau3Ch6;
    pparam_config->cauParam[22].chSwitch = cfg->cau3Ch7;
    pparam_config->cauParam[23].chSwitch = cfg->cau3Ch8;    

    pparam_config->tempSnakeCfg.devType = cfg->tempsnakeType;
    pparam_config->tempSnakeCfg.TempLimit = cfg->tempLimit;
    pparam_config->tempSnakeCfg.ShakeLimit = cfg->snakeLimit;

    pparam_config->envDevCfg[0].enable = cfg->thEn;
    pparam_config->envDevCfg[0].RS485Ch = cfg->thRs485Ch;
    pparam_config->envDevCfg[0].Addr = cfg->thAddr;

    pparam_config->envDevCfg[1].enable = cfg->waterEn;
    pparam_config->envDevCfg[1].RS485Ch = cfg->waterRs485Ch;
    pparam_config->envDevCfg[1].Addr = cfg->waterAddr;   

    pparam_config->envDevCfg[2].enable = cfg->o2En;
    pparam_config->envDevCfg[2].RS485Ch = cfg->o2Rs485Ch;
    pparam_config->envDevCfg[2].Addr = cfg->o2Addr; 

    pparam_config->envDevCfg[3].enable = cfg->ch4En;
    pparam_config->envDevCfg[3].RS485Ch = cfg->ch4Rs485Ch ;
    pparam_config->envDevCfg[3].Addr = cfg->ch4Addr; 

    pparam_config->envDevCfg[4].enable = cfg->h2sEn;
    pparam_config->envDevCfg[4].RS485Ch = cfg->h2sRs485Ch;
    pparam_config->envDevCfg[4].Addr = cfg->h2sAddr; 

    pparam_config->envDevCfg[5].enable = cfg->co2En;
    pparam_config->envDevCfg[5].RS485Ch = cfg->co2Rs485Ch;
    pparam_config->envDevCfg[5].Addr = cfg->co2Addr; 

    pparam_config->cameraCfg.enable = cfg->cameraEn;
    pparam_config->cameraCfg.RS485Ch = cfg->camera485Ch;
    pparam_config->cameraCfg.di = cfg->cameraDi;
    pparam_config->cameraCfg.trigger = cfg->cameraTriger;
    pparam_config->cameraCfg.delay = cfg->cameraDelay;
    pparam_config->cameraCfg.size = cfg->cameraSize;
    return 0;
}


int param_load()
{
    int ret = 0;
    cJSON *pCfgJsRoot = readJsonFromFile(CONFIG_FILE);
    if(pCfgJsRoot == NULL) 
    {
        printh("加载配置文件失败,尝试加载备用配置");
        pCfgJsRoot = readJsonFromFile(BACK_CONFIG_FILE);
        if (pCfgJsRoot == NULL)
        {
            printh("load file:%s failed\n", BACK_CONFIG_FILE);
            return -1;
        }
    }
        
    if(pparam_config) 
    {
        free(pparam_config);
    }
        
    pparam_config = (ca_param_config_t *)calloc(1, sizeof(ca_param_config_t));
    if (pparam_config == NULL)
    {
        printh("calloc struct(ca_param_config_t) failed\n");
        return -1;
    }

    memset(pparam_config, 0, sizeof(ca_param_config_t));
    
    if (_param_load_gateway_cfg(pCfgJsRoot) < 0)
    {
        return -1;
    }
    if (_param_load_dev_cfg(pCfgJsRoot) < 0)
    {
        return -1;
    }
    if (_param_load_cau_cfg(pCfgJsRoot) < 0)
    {
        return -1;
    }

    ret += _param_load_mqtt_cfg(pCfgJsRoot);
    cJSON_Delete(pCfgJsRoot);
    
    _param_load_land_hex_cfg();

    if (ret > 0)
    {
        LOG("Param init failure,save again");
        param_save();
    }
    return 0;
}

int param_save()
{
    cJSON *pJsRoot = cJSON_CreateObject();
    if (pJsRoot == NULL || pparam_config == NULL)
    {
        printh("[%s:%d] JSON Create failed.\n", __FUNCTION__, __LINE__);
        return -1;
    }

    _param_save_land_hex_cfg();

    do 
    {
        if (_param_save_gateway_cfg(pJsRoot)  < 0) 
        {
            printh("_param_save_gateway_cfg failed\n");
            break;
        }

        if (_param_save_dev_cfg(pJsRoot) < 0)
        {
            printh("_param_save_dev_cfg failed\n");
            break;
        }

        if (_param_save_cau_cfg(pJsRoot) < 0)
        {
            printf("_param_save_dev_cfg failed\n");
            break;
        } 

        if (_param_save_mqtt_cfg(pJsRoot) < 0)
        {
            printf("_param_save_mqtt_cfg failed\n");
            break;
        }

        saveJsonToFile(CONFIG_FILE, pJsRoot);
        saveJsonToFile(BACK_CONFIG_FILE, pJsRoot);
    } while(0);    
    cJSON_Delete(pJsRoot);
    return 0;
}
