From 3ef6cf39749c00366b12aef9f023aac3bda16f2d Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Mon, 17 Nov 2025 23:39:25 +0800
Subject: [PATCH] update modules for tsl2561 and ds18b20

---
 project/thingsboard/thingsboard.c |   75 +++++++++++++++++++++----------------
 1 files changed, 42 insertions(+), 33 deletions(-)

diff --git a/project/thingsboard/thingsboard.c b/project/thingsboard/thingsboard.c
index bfba740..70c872a 100644
--- a/project/thingsboard/thingsboard.c
+++ b/project/thingsboard/thingsboard.c
@@ -31,16 +31,16 @@
 #define PROG_VERSION               "v1.0.0"
 #define DAEMON_PIDFILE             "/tmp/.thingsboard.pid"
 
-void *mqtt_sub_worker(void *args);
-void *mqtt_pub_worker(void *args);
+void *thingsboard_subsciber(void *args);
+void *thingsboard_publisher(void *args);
 
 static void program_usage(char *progname)
 {
-
     printf("Usage: %s [OPTION]...\n", progname);
-    printf(" %s is LingYun studio MQTT daemon program running on RaspberryPi\n", progname);
+    printf(" %s is LingYun studio thingsboard client running on RaspberryPi\n", progname);
 
     printf("\nMandatory arguments to long options are mandatory for short options too:\n");
+    printf(" -b[daemon  ]  Running in daemon mode\n");
     printf(" -d[debug   ]  Running in debug mode\n");
     printf(" -c[conf    ]  Specify configure file\n");
     printf(" -h[help    ]  Display this help information\n");
@@ -52,16 +52,17 @@
 
 int main (int argc, char **argv)
 {
-    int                daemon = 1;
+    int                daemon = 0;
     pthread_t          tid;
     mqtt_ctx_t         ctx;
-    char               *conf_file="/etc/mqttd.conf";
+    char               *conf_file="/etc/thingsboard.conf";
     int                debug = 0;
     int                opt;
     char              *progname=NULL;
 
     struct option long_options[] = {
         {"conf", required_argument, NULL, 'c'},
+        {"daemon", no_argument, NULL, 'b'},
         {"debug", no_argument, NULL, 'd'},
         {"version", no_argument, NULL, 'v'},
         {"help", no_argument, NULL, 'h'},
@@ -71,7 +72,7 @@
     progname = (char *)basename(argv[0]);
 
     /* parser the command line parameters */
-    while ((opt = getopt_long(argc, argv, "c:dvh", long_options, NULL)) != -1)
+    while ((opt = getopt_long(argc, argv, "c:bdvh", long_options, NULL)) != -1)
     {
         switch (opt)
         {
@@ -79,8 +80,11 @@
                 conf_file = optarg;
                 break;
 
+            case 'b': /* Set daemon running */
+                daemon = 1;
+                break;
+
             case 'd': /* Set debug running */
-                daemon = 0;
                 debug = 1;
                 break;
 
@@ -98,13 +102,10 @@
 
     }
 
-    if( !conf_file )
-        debug = 1;
-
     /* parser configure file */
-    if( mqttd_parser_conf(conf_file, &ctx, debug)<0 )
+    if( parser_conf(conf_file, &ctx, debug)<0 )
     {
-        fprintf(stderr, "Parser mqtted configure file failure\n");
+        fprintf(stderr, "Parser thingsboard configure file failure\n");
         return -2;
     }
 
@@ -118,21 +119,30 @@
     /* initial mosquitto library */
     mosquitto_lib_init();
 
-    /* create MQTT subsciber thread  */
-    if( thread_start(&tid, mqtt_sub_worker, &ctx ) < 0 )
+    /*
+     * +--------------------------------+
+     * |    MQTT Subscriber Thread      |
+     * +--------------------------------+
+     */
+    if( thread_start(&tid, thingsboard_subsciber, &ctx ) < 0 )
     {
         log_error("Start MQTT subsciber worker thread failure\n");
         goto cleanup;
     }
     log_info("Start MQTT subsciber worker thread ok\n");
 
-    /* create MQTT publisher thread  */
-    if( thread_start(&tid, mqtt_pub_worker, &ctx) < 0 )
+    /*
+     * +--------------------------------+
+     * |     MQTT publisher Thread      |
+     * +--------------------------------+
+     */
+    if( thread_start(&tid, thingsboard_publisher, &ctx) < 0 )
     {
         log_error("Start MQTT publisher worker thread failure\n");
         goto cleanup;
     }
     log_info("Start MQTT publisher worker thread ok\n");
+
 
     while( ! g_signal.stop )
     {
@@ -144,7 +154,7 @@
     log_close();
 
     return 0;
-} /* ----- End of main() ----- */
+}
 
 void pub_connect_callback(struct mosquitto *mosq, void *userdata, int result)
 {
@@ -170,9 +180,10 @@
 
         log_debug("SHT2X temperature and humidity sensor enabled, start broadcast it\n");
 
-        if( 0 == sht2x_get_temp_humidity(&temp, &rh) )
-            snprintf(msg, sizeof(msg), "{\"temperature\":%.2f, \"humidity\":%.2f}", temp, rh);
+        if( sht2x_get_temp_humidity(&temp, &rh)<0 )
+            return ;
 
+        snprintf(msg, sizeof(msg), "{\"temperature\":%.2f, \"humidity\":%.2f}", temp, rh);
         rv = mosquitto_publish(mosq, NULL, ctx->pubTopic, strlen(msg), msg, ctx->pubQos, retain);
         if( rv )
         {
@@ -191,8 +202,7 @@
     return ;
 }
 
-
-void *mqtt_pub_worker(void *args)
+void *thingsboard_publisher(void *args)
 {
     mqtt_ctx_t             *ctx = (mqtt_ctx_t *)args;
     struct mosquitto       *mosq;
@@ -277,7 +287,7 @@
     item_method = cJSON_GetObjectItem(root, "method");
     if (item_method == NULL || !cJSON_IsString(item_method))
     {
-        printf("JSON missing method\n");
+        log_error("JSON missing method\n");
         return;
     }
     method = item_method->valuestring;
@@ -285,7 +295,7 @@
     /* 只处理 setValue 命令 */
     if (strcmp(method, "setValue") != 0)
     {
-        printf("Ignore method: %s\n", method);
+        log_error("Ignore method: %s\n", method);
         return;
     }
 
@@ -293,7 +303,7 @@
     item_params = cJSON_GetObjectItem(root, "params");
     if (item_params == NULL || !cJSON_IsObject(item_params))
     {
-        printf("JSON missing params\n");
+        log_error("JSON missing params\n");
         return;
     }
 
@@ -301,7 +311,7 @@
     item_device = cJSON_GetObjectItem(item_params, "device");
     if (item_device == NULL || !cJSON_IsString(item_device))
     {
-        printf("JSON missing params.device\n");
+        log_error("JSON missing params.device\n");
         return;
     }
     device = item_device->valuestring;
@@ -310,21 +320,21 @@
     item_status = cJSON_GetObjectItem(item_params, "status");
     if (item_status == NULL || !(cJSON_IsBool(item_status)))
     {
-        printf("JSON missing params.status\n");
+        log_error("JSON missing params.status\n");
         return;
     }
     status = cJSON_IsTrue(item_status) ? ON : OFF;
 
     /* 映射 LED 名称并调用控制函数 */
-    if (strcmp(device, "RedLed") == 0)
+    if ( !strcmp(device, "RedLed") )
     {
         turn_led(LED_R, status);
     }
-    else if (strcmp(device, "GreenLed") == 0)
+    else if ( !strcmp(device, "GreenLed") )
     {
         turn_led(LED_G, status);
     }
-    else if (strcmp(device, "BlueLed") == 0)
+    else if ( !strcmp(device, "BlueLed") )
     {
         turn_led(LED_B, status);
     }
@@ -344,7 +354,7 @@
         return ;
     }
 
-    log_debug("Subscriber receive message: '%s'\n", message->payload);
+    log_info("Subscriber receive message: '%s'\n", message->payload);
 
     root = cJSON_Parse(message->payload);
     if( !root )
@@ -361,8 +371,7 @@
     return ;
 }
 
-
-void *mqtt_sub_worker(void *args)
+void *thingsboard_subsciber(void *args)
 {
     mqtt_ctx_t             *ctx = (mqtt_ctx_t *)args;
     struct mosquitto       *mosq;

--
Gitblit v1.9.1