APUE Learning Example Source Code
guowenxue
2020-01-01 7ac2c9846be1f8c82dacf17cf36551c5ccaf278a
pj1_mqttd/main.c
@@ -241,14 +241,14 @@
    if( running && strcasestr(status, "off") )
    {
        log_warn("Stop mjpg_streamer working on C920 now\n");
        system("/bin/video stop");
        system("/bin/video stop &");
        return 0;
    }
    if( !running && strcasestr(status, "on") )
    {
        log_nrml("Start mjpg_streamer working on C920 now\n");
        system("/bin/video start");
        system("/bin/video start &");
    }
    return 0;
@@ -257,9 +257,8 @@
void proc_json_items(cJSON *root)
{
    int                    i;
    cJSON                 *item;
    char                  *value;
    cJSON                 *item;
    cJSON                 *array;
    if( !root )
    {
@@ -267,44 +266,26 @@
        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 )
    {
@@ -312,7 +293,7 @@
        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 )
@@ -321,8 +302,26 @@
        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 receive message: '%s'\n", message->payload);
    proc_json_items(root); 
OUT:
    cJSON_Delete(root); /* must delete it, or it will result memory leak */
    return ;
}