/****************HEADER FILE******************/
#include "huayun_common.h"
/**********************************************/


/******************INTERN MACRO*****************/
/**************************************************/


/**************INTERN FUNCTION DECLERATION**********/
static int huayun_cfg_parse(huayun_var_t *var, const char *str);
/**************************************************/


/****************EXTERN FUNCTION******************/
void huayun_load_config(huayun_var_t *var)
{
    char *json_str = NULL;
    json_str = read_file_data(HUAYUN_CFG);
    if (!json_str)
    {
        return ;
    }
    huayun_cfg_parse(var, json_str);
    free(json_str);
}

/**************************************************/


/****************INTERN FUNCTION******************/
static int huayun_cfg_parse(huayun_var_t *var, const char *str)
{
	char buff[128] = {0};
	int ret 	   = -1;
	int i		   = 0;
	
	if (!str) {
        return -1;
    }

    cJSON *root = cJSON_Parse(str);
    if (!root)
    {
        return -1;
    }

    cJSON *nodes = cJSON_GetObjectItem(root, "huayun_cfg");
    if (!nodes)
    {
        ret = -1;
        goto __cleanup;
    }

    int size = cJSON_GetArraySize(nodes);

    for (i = 0; i < size ; i++)
    {
        cJSON *node = cJSON_GetArrayItem(nodes, i);
        if (!node)
        {
            ret = i + 1;
            goto __cleanup;
        }
		
        GET_JSON_VALUE_STRING(node, "server_ip", var->server_ip);
		GET_JSON_VALUE_INT(node, "port", var->port);
		GET_JSON_VALUE_INT(node, "report_period", var->period);
		dy_syslog(LOG_INFO,"==server_ip:%s port:%d period:%d==",var->server_ip,var->port,var->period);
    }
    ret = 0;

__cleanup:
    cJSON_Delete(root);
    return ret;
}
/**************************************************/

