From c86a7485d83484c81b9bc0fa3e4ec51f60924b92 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sat, 11 Aug 2018 21:49:38 +0200 Subject: [PATCH] SSR MQTT: Daemonization and MQTT resiliency --- ssr/turris/Makefile | 2 +- ssr/turris/burrow-ssrd.c | 39 ++++++++++++++++++++++++++++++++------- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/ssr/turris/Makefile b/ssr/turris/Makefile index 35bccce..57d9081 100644 --- a/ssr/turris/Makefile +++ b/ssr/turris/Makefile @@ -21,6 +21,6 @@ ssr-control: ssr-control.c burrow-ssrd: burrow-ssrd.c upload: - rsync -av ssr-control burrow-ssrd micac: + rsync -av ssr-control burrow-ssrd micac:burrow/ .PHONY: upload diff --git a/ssr/turris/burrow-ssrd.c b/ssr/turris/burrow-ssrd.c index 8132225..93b1f2c 100644 --- a/ssr/turris/burrow-ssrd.c +++ b/ssr/turris/burrow-ssrd.c @@ -5,6 +5,7 @@ */ #include +#include #include #include #include @@ -102,6 +103,14 @@ static void mqtt_publish(const char *topic, const char *fmt, ...) va_end(args); } +static void mqtt_setup(void) +{ + if (mosquitto_subscribe(mosq, NULL, "burrow/loft/#", 1) != MOSQ_ERR_SUCCESS) + die("Mosquitto: subscribe failed"); + + mqtt_publish("burrow/loft/status", "ok"); +} + static void mqtt_log_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, int level, const char *message) { // msg(L_INFO, "MQTT(%d): %s", level, message); @@ -116,7 +125,7 @@ static void mqtt_msg_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, c } memcpy(val, m->payload, m->payloadlen); val[m->payloadlen] = 0; - msg(L_INFO, "MQTT < %s %s", m->topic, val); + msg(L_DEBUG, "MQTT < %s %s", m->topic, val); if (!strcmp(m->topic, "burrow/loft/fan")) { uint x; @@ -153,11 +162,16 @@ static void mqtt_msg_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, c } } +static int use_daemon; +static int use_debug; + static struct opt_section options = { OPT_ITEMS { OPT_HELP("A daemon for controlling the solid state relay module via MQTT"), OPT_HELP(""), OPT_HELP("Options:"), + OPT_BOOL('d', "debug", use_debug, 0, "\tLog debugging messages"), + OPT_BOOL(0, "daemon", use_daemon, 0, "\tDaemonize"), OPT_HELP_OPTION, OPT_CONF_OPTIONS, OPT_END @@ -168,10 +182,18 @@ int main(int argc UNUSED, char **argv) { opt_parse(&options, argv+1); + if (use_daemon) { + struct log_stream *ls = log_new_syslog("daemon", 0); + log_set_default_stream(ls); + } + if (!use_debug) + log_default_stream()->levels &= ~(1U << L_DEBUG); + int err; if (err = libusb_init(&usb_ctxt)) die("Cannot initialize libusb: error %d", err); - libusb_set_debug(usb_ctxt, 3); + if (use_debug) + libusb_set_debug(usb_ctxt, 3); open_device(); set_relays(); @@ -189,17 +211,20 @@ int main(int argc UNUSED, char **argv) if (mosquitto_connect(mosq, "127.0.0.1", 1883, 60) != MOSQ_ERR_SUCCESS) die("Mosquitto: connect failed"); - if (mosquitto_subscribe(mosq, NULL, "burrow/loft/#", 1) != MOSQ_ERR_SUCCESS) - die("Mosquitto: subscribe failed"); - - mqtt_publish("burrow/loft/status", "ok"); + mqtt_setup(); time_t next_run = 0; for (;;) { time_t now = time(NULL); if (now < next_run) { int err = mosquitto_loop(mosq, (next_run - now) * 1000, 1); - if (err != MOSQ_ERR_SUCCESS) + if (err == MOSQ_ERR_NO_CONN) { + err = mosquitto_reconnect(mosq); + if (err == MOSQ_ERR_SUCCESS) + mqtt_setup(); + else + msg(L_ERROR, "Mosquitto: cannot reconnect, error %d", err); + } else if (err != MOSQ_ERR_SUCCESS) msg(L_ERROR, "Mosquitto: loop returned error %d", err); continue; } -- 2.39.5