]> mj.ucw.cz Git - arexx.git/blobdiff - arexxd.c
Home: New MQTT server
[arexx.git] / arexxd.c
index b5f43da516979971e2da513cb6298f355be6b9bd..e083c52bce471caf59bca4ddc9f2b324fa4ba60a 100644 (file)
--- a/arexxd.c
+++ b/arexxd.c
@@ -1,7 +1,7 @@
 /*
  *     Linux Interfece for Arexx Data Loggers
  *
- *     (c) 2011-2016 Martin Mares <mj@ucw.cz>
+ *     (c) 2011-2020 Martin Mares <mj@ucw.cz>
  */
 
 #include <stdio.h>
@@ -17,7 +17,6 @@
 #include <signal.h>
 #include <sys/stat.h>
 #include <libusb-1.0/libusb.h>
-#include <rrd.h>
 
 #define DEFAULT_LOG_DIR "/var/log/arexxd"
 
@@ -35,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;
@@ -44,10 +46,14 @@ 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;
 
+#define UNUSED __attribute__((unused))
+
+static int data_point_counter;         // Since last log message
+static time_t packet_rx_time;
+
 static void die(char *fmt, ...)
 {
        va_list args;
@@ -98,8 +104,72 @@ static void log_pkt(char *fmt, ...)
        va_end(args);
 }
 
+/*** MQTT interface ***/
+
+#ifdef LOG_TO_MQTT
+
+#include <mosquitto.h>
+
+static struct mosquitto *mosq;
+
+static void mqtt_publish(const char *topic, const char *fmt, ...)
+{
+       va_list args;
+       va_start(args, fmt);
+       char m[256];
+       int l = vsnprintf(m, sizeof(m), fmt, args);
+       if (mosquitto_publish(mosq, NULL, topic, l, m, 0, true) != MOSQ_ERR_SUCCESS)
+               log_error("Mosquitto: publish failed");
+       va_end(args);
+}
+
+static void mqtt_conn_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, int status)
+{
+       if (!status)
+               mqtt_publish("status/arexxd", "ok");
+}
+
+static void mqtt_init(void)
+{
+       mosquitto_lib_init();
+       mosq = mosquitto_new("arexxd", 1, NULL);
+       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, "burrow-mqtt", 8883, 60) != MOSQ_ERR_SUCCESS)
+               die("Mosquitto: connect failed");
+
+       if (mosquitto_loop_start(mosq))
+               die("Mosquitto: cannot start service thread");
+}
+
+static void mqtt_point(time_t t, const char *name, double val, char *unit UNUSED)
+{
+       // We do not feed past data to MQTT (so MAX_PAST_TIME is stronger for us)
+       if (t < packet_rx_time - 30)
+               return;
+
+       char topic[64];
+       snprintf(topic, sizeof(topic), "burrow/temp/%s", name);
+       mqtt_publish(topic, "%.3f %lld", val, (long long) t);
+}
+
+#endif
+
 /*** RRD interface ***/
 
+#ifdef LOG_TO_RRD
+
+#include <rrd.h>
+
 #define MAX_ARGS 20
 #define MAX_ARG_SIZE 1024
 
@@ -172,13 +242,12 @@ 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
 
-static int data_point_counter;         // Since last log message
-static time_t packet_rx_time;
-
 static double correct_point(uint id, double val, const char **name)
 {
        /*
@@ -187,19 +256,19 @@ static double correct_point(uint id, double val, const char **name)
         */
        switch (id) {
                case 10415:
-                       *name = "ursarium";
+                       *name = "terarium";
                        return val - 0.93;
                case 10707:
                        *name = "catarium";
                        return val - 0.71;
                case 11699:
-                       *name = "machinarium";
+                       *name = "garage";
                        return val;
                case 19246:
-                       *name = "garage";
+                       *name = "ursarium";
                        return val + 0.49;
                case 19247:
-                       *name = "garage-rh";
+                       *name = "ursarium-rh";
                        return val;
                case 12133:
                        *name = "aquarium";
@@ -242,7 +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)
@@ -607,7 +681,7 @@ static void set_clock(void)
 static sigset_t term_sigs;
 static volatile sig_atomic_t want_shutdown;
 
-static void sigterm_handler(int sig __attribute__((unused)))
+static void sigterm_handler(int sig UNUSED)
 {
        want_shutdown = 1;
 }
@@ -640,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);
@@ -649,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++;
@@ -666,12 +739,9 @@ 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-2012 Martin Mares <mj@ucw.cz>\n");
+                               printf("(c) 2011-2018 Martin Mares <mj@ucw.cz>\n");
                                return 0;
                        default:
                                usage();
@@ -682,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)
@@ -706,6 +774,10 @@ 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);
        sigaction(SIGINT, &sa, NULL);