json-c使用例子
static void *message_handler(void *msg)
{
int i;
int flag = 1;
int state_flag = -1;
char deviceId[ID_LEN] = {0};
strncpy(deviceId,"LS",2);
struct json_object *new_object,*agtid_object,*idx_object,*deviceType_object,*me_object,*val_object,*data_object;
new_object = json_tokener_parse(msg);
json_object_object_foreach(new_object,key,value)
{
DBG_vPrintf(DBG_LIBLIFESMART,"UDP server1 revc key:%s value:%s\n",key,json_object_get_string(value));
if(!strcmp(key,"agtid")){
strncat(deviceId,json_object_get_string(value),10);
}
if(json_object_is_type(value,json_type_array)){
if(!strcmp(key,"chg")){
for (i = 0; i < json_object_array_length(value); i++)
{
idx_object = json_object_array_get_idx(value, i);
char device_type[16] ={0};
char device_meteName[16] ={0};
int mete_value;
//json_object_object_get_ex(idx_object,"devtype",&devtype_object);
//json_object_object_get_ex(idx_object,"me",&me_object);
//json_object_object_get_ex(idx_object,"me",&me_object);
json_object_object_foreach(idx_object,key,value)
{
DBG_vPrintf(DBG_LIBLIFESMART,"UDP server2 revc key:%s value:%s\n",key,json_object_get_string(value));
if(!strcmp(key,"me") && flag){
strncat(deviceId,json_object_get_string(value),strlen(json_object_get_string(value)));
DBG_vPrintf(DBG_LIBLIFESMART,"deviceId :%s\n",deviceId);
flag = 0;
}
if(!strcmp(key,"devtype")){
strncpy(device_type,json_object_get_string(value),strlen(json_object_get_string(value)));
DBG_vPrintf(DBG_LIBLIFESMART,"device_type :%s\n",device_type);
}
if(!strcmp(key,"stat")){
state_flag = json_object_get_int(value);
DBG_vPrintf(DBG_LIBLIFESMART,"device_state :%d\n",state_flag);
}
if(json_object_is_type(value,json_type_object)){
strncpy(device_meteName,key,strlen(key));
json_object_object_get_ex(value,"v",&val_object);
mete_value = json_object_get_int(val_object);
DBG_vPrintf(DBG_LIBLIFESMART,"UDP server3 revc device key:%s,value 3:%d\n",key,mete_value);
}
}
if(state_flag != -1){
LifeSmart_device_online_cb(deviceId,state_flag);
state_flag = -1;
}
else
LifeSmart_device_state_cb(deviceId,device_type,device_meteName,mete_value);
json_object_put(val_object);
json_object_put(idx_object );
sleep(1);
}
}
if(!strcmp(key,"add")){
for (i = 0; i < json_object_array_length(value); i++)
{
char device_type[16] ={0};
idx_object = json_object_array_get_idx(value, i);
me_object = json_object_object_get(idx_object,"me");
if(flag){
strncat(deviceId,json_object_get_string(me_object),strlen(json_object_get_string(me_object)));
DBG_vPrintf(DBG_LIBLIFESMART,"new deviceId :%s\n",deviceId);
flag = 0;
}
deviceType_object = json_object_object_get(idx_object,"devtype");
strncpy(device_type,json_object_get_string(deviceType_object),strlen(json_object_get_string(deviceType_object)));
DBG_vPrintf(DBG_LIBLIFESMART,"new devtype :%s\n",device_type);
LifeSmart_device_join_cb(deviceId,device_type);
json_object_put(deviceType_object );
json_object_put(me_object );
json_object_put(idx_object );
sleep(1);
}
}
if(!strcmp(key,"del"))
{
for (i = 0; i < json_object_array_length(value); i++)
{
idx_object = json_object_array_get_idx(value, i);
me_object = json_object_object_get(idx_object,"me");
if(flag){
strncat(deviceId,json_object_get_string(me_object),strlen(json_object_get_string(me_object)));
DBG_vPrintf(DBG_LIBLIFESMART,"deviceId :%s\n",deviceId);
flag = 0;
}
LifeSmart_device_leave_cb(deviceId);
json_object_put(me_object );
json_object_put(idx_object );
}
}
}
}
json_object_put(new_object);
}