From 97c8120b6c3c259afa345aee863e7ff86d378050 Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Sat, 15 Nov 2025 02:20:59 +0800
Subject: [PATCH] update thingsboard to support systemd service

---
 project/thingsboard/thingsboard.c           |   20 +++++++++++---------
 project/thingsboard/etc/thingsboard.conf    |    2 +-
 project/booster/logger.c                    |    2 +-
 project/thingsboard/config.c                |    2 +-
 project/thingsboard/config.h                |    2 +-
 project/thingsboard/etc/thingsboard.service |   15 +++++++++++++++
 6 files changed, 30 insertions(+), 13 deletions(-)

diff --git a/project/booster/logger.c b/project/booster/logger.c
index b973d06..c049684 100644
--- a/project/booster/logger.c
+++ b/project/booster/logger.c
@@ -101,7 +101,7 @@
     {
         if ( !(fp = fopen(fname, "a+")) )
         {
-            fprintf(stderr, "%s() failed: %s\n", __func__, strerror(errno));
+            fprintf(stderr, "%s() %s failed: %s\n", __func__, fname, strerror(errno));
             return -2;
         }
         L.fp = fp;
diff --git a/project/thingsboard/config.c b/project/thingsboard/config.c
index 8471e81..9fa08e0 100644
--- a/project/thingsboard/config.c
+++ b/project/thingsboard/config.c
@@ -14,7 +14,7 @@
 #include "logger.h"
 #include "iniparser.h"
 
-int mqttd_parser_conf(const char *conf_file, mqtt_ctx_t *ctx, int debug)
+int parser_conf(const char *conf_file, mqtt_ctx_t *ctx, int debug)
 {
     dictionary          *ini;
     const char          *str;
diff --git a/project/thingsboard/config.h b/project/thingsboard/config.h
index 8cf4e2e..07a6a39 100644
--- a/project/thingsboard/config.h
+++ b/project/thingsboard/config.h
@@ -66,7 +66,7 @@
 } mqtt_ctx_t;
 
 
-extern int mqttd_parser_conf(const char *conf_file, mqtt_ctx_t *ctx, int debug);
+extern int parser_conf(const char *conf_file, mqtt_ctx_t *ctx, int debug);
 
 #endif   /* ----- #ifndef _CONF_H_  ----- */
 
diff --git a/project/thingsboard/etc/thingsboard.conf b/project/thingsboard/etc/thingsboard.conf
index 26339d6..f3a44b5 100644
--- a/project/thingsboard/etc/thingsboard.conf
+++ b/project/thingsboard/etc/thingsboard.conf
@@ -13,7 +13,7 @@
 [logger]
 
 # 日志记录文件
-file=/tmp/thingsboard.log
+file=/var/log/thingsboard.log
 
 # 日志级别: 0:ERROR 1:WARN 2:INFO 3:DEBUG 4:TRACE 
 level=2
diff --git a/project/thingsboard/etc/thingsboard.service b/project/thingsboard/etc/thingsboard.service
new file mode 100644
index 0000000..b02edce
--- /dev/null
+++ b/project/thingsboard/etc/thingsboard.service
@@ -0,0 +1,15 @@
+[Unit]
+Description=RaspberryPi ThingsBoard Client Service
+After=network.target
+
+[Service]
+Type=simple
+ExecStartPre=/bin/rm -f /tmp/.thingsboard.pid /var/log/thingsboard.log
+ExecStart=/usr/bin/thingsboard -c /etc/thingsboard.conf
+
+Restart=always
+RestartSec=2
+
+[Install]
+WantedBy=multi-user.target
+
diff --git a/project/thingsboard/thingsboard.c b/project/thingsboard/thingsboard.c
index bfba740..c2f443f 100644
--- a/project/thingsboard/thingsboard.c
+++ b/project/thingsboard/thingsboard.c
@@ -41,6 +41,7 @@
     printf(" %s is LingYun studio MQTT daemon program 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 +53,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 +73,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 +81,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 +103,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;
     }
 

--
Gitblit v1.9.1