From 937cb95ee88ce4f57cb4510be459a72033fbbb94 Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Wed, 26 Jun 2019 12:59:06 +0800
Subject: [PATCH] update mqttd program, add ID support

---
 pj1_mqttd/etc/conf.c     |   17 ++++++++
 pj1_mqttd/etc/mqttd.conf |    2 +
 pj1_mqttd/main.c         |   67 ++++++++++++++++-----------------
 pj1_mqttd/etc/conf.h     |    2 +
 4 files changed, 53 insertions(+), 35 deletions(-)

diff --git a/pj1_mqttd/etc/conf.c b/pj1_mqttd/etc/conf.c
index 19b4dea..ed05e55 100644
--- a/pj1_mqttd/etc/conf.c
+++ b/pj1_mqttd/etc/conf.c
@@ -25,9 +25,11 @@
 
     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;
 
@@ -92,6 +94,19 @@
 
     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);
 
 
     /*+------------------------------------------------------+
diff --git a/pj1_mqttd/etc/conf.h b/pj1_mqttd/etc/conf.h
index 24c439a..b6b41b2 100644
--- a/pj1_mqttd/etc/conf.h
+++ b/pj1_mqttd/etc/conf.h
@@ -34,6 +34,8 @@
 
 typedef struct mqtt_ctx_s
 {
+    char          id[32];       /* production ID */
+
     /* logger settings */
     char          logfile[128]; /* logger record file */
     int           loglevel;     /* logger level  */
diff --git a/pj1_mqttd/etc/mqttd.conf b/pj1_mqttd/etc/mqttd.conf
index 37bde36..f875af9 100644
--- a/pj1_mqttd/etc/mqttd.conf
+++ b/pj1_mqttd/etc/mqttd.conf
@@ -1,3 +1,5 @@
+[common]
+id="x86host02"
 
 [logger]
 
diff --git a/pj1_mqttd/main.c b/pj1_mqttd/main.c
index 54c23bb..942e51d 100644
--- a/pj1_mqttd/main.c
+++ b/pj1_mqttd/main.c
@@ -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[%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 ;
 }

--
Gitblit v1.9.1