From b64d6ff04401ed85fce543b8e20c8c360fb284f5 Mon Sep 17 00:00:00 2001
From: Guo Wenxue <guowenxue@gmail.com>
Date: Thu, 27 Jun 2019 13:25:23 +0800
Subject: [PATCH] Update mqttd hal code, can control relay now

---
 mqttd/main.c |   33 +++++++++++++++++++++++++++------
 1 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/mqttd/main.c b/mqttd/main.c
index 8808585..aa88835 100644
--- a/mqttd/main.c
+++ b/mqttd/main.c
@@ -115,7 +115,7 @@
         return -1;
     }
     log_nrml("HAL initialise ok\n");
-    
+
     install_proc_signal();
 
     if( check_set_program_running(daemon) < 0 ) 
@@ -202,9 +202,9 @@
 
     memset(msg, 0, sizeof(msg));
     if( ds18b20_get_temperature(&temp) < 0 )
-        strncpy(msg, "{ \"temp\":\"error\" }", sizeof(msg));
+        snprintf(msg, sizeof(msg), "{ \"id\":%s, \"temp\":\"error\" }", ctx->id);
     else 
-        snprintf(msg, sizeof(msg), "{ \"temp\":\"%.2f\" }", temp);
+        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 )
@@ -388,8 +388,11 @@
 
 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 )
@@ -398,7 +401,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 )
@@ -407,8 +410,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 ;
 }

--
Gitblit v1.9.1