guowenxue
2019-06-27 45e62dbd9e964c74b24f7fcadad9b418f117e008
mqttd/main.c
@@ -109,19 +109,23 @@
        return -2;
    }
    if( hal_init() < 0 )
    if( ctx.hwconf.enable  )
    {
        log_err("Initialise hardware failure\n");
        if( hal_init(&ctx.hwconf) < 0 )
            log_err("Initialise hardware failure\n");
        else
            log_nrml("HAL initialise ok\n");
        return -1;
    }
    log_nrml("HAL initialise ok\n");
    else
    {
    }
    install_proc_signal();
    if( check_set_program_running(daemon) < 0 ) 
        goto OUT;
    if( !debug )
    mosquitto_lib_init();
@@ -200,22 +204,26 @@
    
    log_nrml("Publisher connect to broker server[%s:%d] successfully\n", ctx->host, ctx->port); 
    memset(msg, 0, sizeof(msg));
    if( ds18b20_get_temperature(&temp) < 0 )
        snprintf(msg, sizeof(msg), "{ \"id\":%s, \"temp\":\"error\" }", ctx->id);
    else
        snprintf(msg, sizeof(msg), "{ \"id\":%s, \"temp\":\"%.2f\" }", ctx->id, temp);
    rv = mosquitto_publish(mosq, NULL, ctx->pubTopic, strlen(msg)+1, msg, ctx->pubQos, retain);
    if( rv )
    if( ctx->hwconf.ds18b20 )
    {
        log_err("Publisher broadcast message '%s' failure: %d\n", msg, rv);
    }
    else
    {
        log_nrml("Publisher broadcast message '%s' ok\n", msg);
    }
        memset(msg, 0, sizeof(msg));
        if( ds18b20_get_temperature(&temp) < 0 )
            snprintf(msg, sizeof(msg), "{ \"id\":%s, \"temp\":\"error\" }", ctx->id);
        else
            snprintf(msg, sizeof(msg), "{ \"id\":%s, \"temp\":\"%.2f\" }", ctx->id, temp);
        rv = mosquitto_publish(mosq, NULL, ctx->pubTopic, strlen(msg)+1, msg, ctx->pubQos, retain);
        if( rv )
        {
            log_err("Publisher broadcast message '%s' failure: %d\n", msg, rv);
        }
        else
        {
            log_nrml("Publisher broadcast message '%s' ok\n", msg);
        }
    }
    log_nrml("Publisher broadcast over and disconnect broker now\n", ctx->host, ctx->port); 
    mosquitto_disconnect(mosq);
@@ -294,12 +302,13 @@
        turn_led(which, OFF);
}
void proc_json_items(cJSON *root)
void proc_json_items(cJSON *root, mqtt_ctx_t *ctx)
{
    int                    i;
    char                  *value;
    cJSON                 *item; 
    cJSON                 *array;
    hwconf_t              *hwconf = &ctx->hwconf;
    if( !root )
    {
@@ -316,12 +325,12 @@
        /* if item is cJSON_Object, then recursive call proc_json */
        if( cJSON_Object == item->type )
        {
            proc_json_items(item);
            proc_json_items(item, ctx);
        }
        else if( cJSON_Array == item->type )
        {
            /* RGB colors led control  */
            if( !strcasecmp(item->string, "led") )
            if( hwconf->leds && !strcasecmp(item->string, "led") )
            {
                array = cJSON_GetArrayItem(item, 0);
                if( NULL != array )
@@ -353,7 +362,7 @@
            value = cJSON_Print(item);
            /* light controled by relay */
            if( !strcasecmp(item->string, "light") )
            if( hwconf->relay && !strcasecmp(item->string, "light") )
            {
                if( strcasestr(value, "on") )
                {
@@ -368,7 +377,7 @@
            }
            /* buzzer */
            if( !strcasecmp(item->string, "buzzer") )
            if( hwconf->beep && !strcasecmp(item->string, "buzzer") )
            {
                if( strcasestr(value, "on") )
                {
@@ -427,7 +436,7 @@
    free(value); 
    log_nrml("Subscriber receive message: '%s'\n", message->payload);
    proc_json_items(root);
    proc_json_items(root, ctx);
OUT:
    cJSON_Delete(root); /* must delete it, or it will result memory leak */