/*
* 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>
#include <signal.h>
#include <sys/stat.h>
#include <libusb-1.0/libusb.h>
-#include <rrd.h>
#define DEFAULT_LOG_DIR "/var/log/arexxd"
#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;
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;
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
}
}
+#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)
{
/*
*/
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";
}
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)
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;
}
-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);
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++;
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();
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)
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);