| | |
| | | void proc_json_items(cJSON *root) |
| | | { |
| | | int i; |
| | | char *value; |
| | | cJSON *item; |
| | | cJSON *array; |
| | | char *value; |
| | | |
| | | if( !root ) |
| | | { |
| | |
| | | return ; |
| | | } |
| | | |
| | | for( i=0; i<cJSON_GetArraySize(root); i++ ) |
| | | { |
| | | item = cJSON_GetArrayItem(root, i); |
| | | item = cJSON_GetObjectItem(root, "camera"); |
| | | if( !item ) |
| | | break; |
| | | |
| | | /* if item is cJSON_Object, then recursive call proc_json */ |
| | | if( cJSON_Object == item->type ) |
| | | { |
| | | proc_json_items(item); |
| | | } |
| | | else if( cJSON_Array == item->type ) |
| | | { |
| | | /* Logic C920 camera control */ |
| | | if( !strcasecmp(item->string, "camera") ) |
| | | { |
| | | array = cJSON_GetArrayItem(item, 0); |
| | | if( NULL != array ) |
| | | { |
| | | cJSON *led_item; |
| | | |
| | | if( NULL != (led_item=cJSON_GetObjectItem(array , "C920")) ) |
| | | { |
| | | log_nrml("turn Logic C920 camera '%s'\n", led_item->valuestring); |
| | | turn_camera( led_item->valuestring ); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | log_err("cJSON_Parse get camera failure: %s\n", cJSON_GetErrorPtr()); |
| | | return ; |
| | | } |
| | | |
| | | value = cJSON_PrintUnformatted(item); |
| | | |
| | | turn_camera(value); |
| | | |
| | | free(value); |
| | | } |
| | | |
| | | void sub_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message) |
| | | { |
| | | mqtt_ctx_t *ctx = (mqtt_ctx_t *)userdata; |
| | | cJSON *root = NULL; |
| | | char *out; |
| | | |
| | | cJSON *item; |
| | | char *value; |
| | | |
| | | if ( !message->payloadlen ) |
| | | { |
| | |
| | | return ; |
| | | } |
| | | |
| | | log_nrml("Subscriber receive message: '%s'\n", message->payload); |
| | | log_dbg("Subscriber receive message: '%s'\n", message->payload); |
| | | |
| | | root = cJSON_Parse(message->payload); |
| | | if( !root ) |
| | |
| | | return ; |
| | | } |
| | | |
| | | item = cJSON_GetObjectItem(root, "id"); |
| | | if( !item ) |
| | | { |
| | | log_err("cJSON_Parse get ID failure: %s\n", cJSON_GetErrorPtr()); |
| | | goto OUT; |
| | | } |
| | | |
| | | |
| | | value = cJSON_PrintUnformatted(item); |
| | | if( strcasecmp(value, ctx->id) ) |
| | | { |
| | | free(value); |
| | | goto OUT; |
| | | } |
| | | free(value); |
| | | |
| | | log_nrml("Subscriber[%s] receive message: '%s'\n", ctx->id, message->payload); |
| | | proc_json_items(root); |
| | | |
| | | OUT: |
| | | cJSON_Delete(root); /* must delete it, or it will result memory leak */ |
| | | return ; |
| | | } |