]> mj.ucw.cz Git - arexx.git/blobdiff - arexxd.c
Home: New MQTT server
[arexx.git] / arexxd.c
index 1bde4a26014be519c73ac4f506797038c9ef12b4..e083c52bce471caf59bca4ddc9f2b324fa4ba60a 100644 (file)
--- a/arexxd.c
+++ b/arexxd.c
@@ -1,7 +1,7 @@
 /*
  *     Linux Interfece for Arexx Data Loggers
  *
- *     (c) 2011-2018 Martin Mares <mj@ucw.cz>
+ *     (c) 2011-2020 Martin Mares <mj@ucw.cz>
  */
 
 #include <stdio.h>
@@ -34,6 +34,9 @@
 #define MAX_FUTURE_TIME 300
 #define IGNORE_UNKNOWN_SENSORS
 
+#undef LOG_TO_RRD
+#define LOG_TO_MQTT
+
 typedef unsigned char byte;
 typedef unsigned int uint;
 static libusb_context *usb_ctxt;
@@ -43,7 +46,6 @@ static int use_syslog;
 static int debug_mode;
 static int debug_packets;
 static int debug_raw_data;
-static int debug_usb;
 static char *log_dir = DEFAULT_LOG_DIR;
 static int no_fork;
 
@@ -104,6 +106,8 @@ static void log_pkt(char *fmt, ...)
 
 /*** MQTT interface ***/
 
+#ifdef LOG_TO_MQTT
+
 #include <mosquitto.h>
 
 static struct mosquitto *mosq;
@@ -132,12 +136,15 @@ static void mqtt_init(void)
        if (!mosq)
                die("Mosquitto: initialization failed");
 
+       if (mosquitto_tls_set(mosq, "/etc/burrow-mqtt/ca.crt", NULL, "/etc/burrow-mqtt/client.crt", "/etc/burrow-mqtt/client.key", NULL) != MOSQ_ERR_SUCCESS)
+               die("Mosquitto: unable to set TLS parameters");
+
        if (mosquitto_will_set(mosq, "status/arexxd", 4, "dead", 0, true) != MOSQ_ERR_SUCCESS)
                die("Mosquitto: unable to set will");
 
        mosquitto_connect_callback_set(mosq, mqtt_conn_callback);
 
-       if (mosquitto_connect(mosq, "10.32.184.5", 1883, 60) != MOSQ_ERR_SUCCESS)
+       if (mosquitto_connect(mosq, "burrow-mqtt", 8883, 60) != MOSQ_ERR_SUCCESS)
                die("Mosquitto: connect failed");
 
        if (mosquitto_loop_start(mosq))
@@ -155,8 +162,12 @@ static void mqtt_point(time_t t, const char *name, double val, char *unit UNUSED
        mqtt_publish(topic, "%.3f %lld", val, (long long) t);
 }
 
+#endif
+
 /*** RRD interface ***/
 
+#ifdef LOG_TO_RRD
+
 #include <rrd.h>
 
 #define MAX_ARGS 20
@@ -231,6 +242,8 @@ static void rrd_point(time_t t, const char *name, double val, char *unit)
        }
 }
 
+#endif
+
 /*** Transforms ***/
 
 #define TIME_OFFSET 946681200          // Timestamp of 2000-01-01 00:00:00
@@ -243,7 +256,7 @@ static double correct_point(uint id, double val, const char **name)
         */
        switch (id) {
                case 10415:
-                       *name = "kitchen";
+                       *name = "terarium";
                        return val - 0.93;
                case 10707:
                        *name = "catarium";
@@ -298,8 +311,12 @@ static void cooked_point(time_t t, uint id, double val, char *unit, int q)
        }
 
        data_point_counter++;
+#ifdef LOG_TO_RRD
        rrd_point(t, name, val2, unit);
+#endif
+#ifdef LOG_TO_MQTT
        mqtt_point(t, name, val2, unit);
+#endif
 }
 
 static void raw_point(uint t, uint id, int raw, int q)
@@ -697,7 +714,6 @@ Options:\n\
 -n, --no-fork          Do not fork\n\
 -p, --debug-packets    Log all packets sent and received\n\
 -r, --debug-raw                Log conversion from raw values\n\
--u, --debug-usb                Enable libusb debug messages (to stdout/stderr)\n\
 -V, --version          Show daemon version\n\
 ");
        exit(1);
@@ -706,7 +722,7 @@ Options:\n\
 int main(int argc, char **argv)
 {
        int opt;
-       while ((opt = getopt_long(argc, argv, "dl:npruV", long_options, NULL)) >= 0)
+       while ((opt = getopt_long(argc, argv, "dl:nprV", long_options, NULL)) >= 0)
                switch (opt) {
                        case 'd':
                                debug_mode++;
@@ -723,9 +739,6 @@ int main(int argc, char **argv)
                        case 'r':
                                debug_raw_data++;
                                break;
-                       case 'u':
-                               debug_usb++;
-                               break;
                        case 'V':
                                printf("arexxd " AREXXD_VERSION "\n");
                                printf("(c) 2011-2018 Martin Mares <mj@ucw.cz>\n");
@@ -739,8 +752,6 @@ int main(int argc, char **argv)
        int err;
        if (err = libusb_init(&usb_ctxt))
                die("Cannot initialize libusb: error %d", err);
-       if (debug_usb)
-               libusb_set_debug(usb_ctxt, 3);
 
        if (!debug_mode) {
                if (chdir(log_dir) < 0)
@@ -763,7 +774,9 @@ int main(int argc, char **argv)
                use_syslog = 1;
        }
 
+#ifdef LOG_TO_MQTT
        mqtt_init();
+#endif
 
        struct sigaction sa = { .sa_handler = sigterm_handler };
        sigaction(SIGTERM, &sa, NULL);