update mqttd program, add ID support
| | |
| | | |
| | | if( !conf_file ) |
| | | { |
| | | strncpy(ctx->id, "\"x86host02\"", sizeof(ctx->id)); |
| | | |
| | | /* logger settings */ |
| | | strncpy(ctx->logfile, "/tmp/mqttd.log", sizeof(ctx->logfile)); |
| | | //strncpy(ctx->logfile, "/tmp/mqttd.log", sizeof(ctx->logfile)); |
| | | strncpy(ctx->logfile, DBG_LOG_FILE, sizeof(ctx->logfile)); |
| | | ctx->loglevel = LOG_LEVEL_DEBUG; |
| | | ctx->logsize = 1024; |
| | | |
| | |
| | | |
| | | log_nrml("Logger system initialise ok\n"); |
| | | |
| | | /*+------------------------------------------------------+ |
| | | *| parser production ID | |
| | | *+------------------------------------------------------+*/ |
| | | |
| | | if( !(str=iniparser_getstring(ini, "common:id", NULL)) ) |
| | | { |
| | | log_err("ERROR: Parser production ID failure\n"); |
| | | return -2; |
| | | } |
| | | snprintf(ctx->id, sizeof(ctx->id), "\"%s\"", str); |
| | | //strncpy(ctx->id, str, sizeof(ctx->id) ); |
| | | |
| | | log_nrml("Parser production ID [%s]\n", ctx->id); |
| | | |
| | | |
| | | /*+------------------------------------------------------+ |
| | |
| | | |
| | | typedef struct mqtt_ctx_s |
| | | { |
| | | char id[32]; /* production ID */ |
| | | |
| | | /* logger settings */ |
| | | char logfile[128]; /* logger record file */ |
| | | int loglevel; /* logger level */ |
| | |
| | | [common] |
| | | id="x86host02" |
| | | |
| | | [logger] |
| | | |
| | |
| | | void proc_json_items(cJSON *root) |
| | | { |
| | | int i; |
| | | cJSON *item; |
| | | char *value; |
| | | cJSON *item; |
| | | cJSON *array; |
| | | |
| | | if( !root ) |
| | | { |
| | |
| | | return ; |
| | | } |
| | | |
| | | for( i=0; i<cJSON_GetArraySize(root); i++ ) |
| | | item = cJSON_GetObjectItem(root, "camera"); |
| | | if( !item ) |
| | | { |
| | | item = cJSON_GetArrayItem(root, i); |
| | | 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) |
| | | { |
| | | cJSON *root = NULL; |
| | | char *out; |
| | | |
| | | mqtt_ctx_t *ctx = (mqtt_ctx_t *)userdata; |
| | | cJSON *root = NULL; |
| | | 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 ; |
| | | } |