]> mj.ucw.cz Git - home-hw.git/blobdiff - influx/burrow-influx.c
burrow-influx: new MQTT server
[home-hw.git] / influx / burrow-influx.c
index 95213003b4ca213ec04c15c8a83054d9bfa6588d..23b671ce7e03921726241405cb7d432d1f0d49e3 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *     A gateway between MQTT and InfluxDB
  *
- *     (c) 2018--2019 Martin Mares <mj@ucw.cz>
+ *     (c) 2018--2020 Martin Mares <mj@ucw.cz>
  */
 
 #include <ucw/lib.h>
@@ -34,6 +34,8 @@ struct attr {
        const char *metric;
        const char *value_name;
        const char *topic;
+       uint timeout;
+       bool is_string;
 };
 
 static const struct attr attr_table[] = {
@@ -58,9 +60,9 @@ static const struct attr attr_table[] = {
                .topic = "burrow/temp/garage"
        },
        {
-               .metric = "temp,where=kitchen",
+               .metric = "temp,where=terarium",
                .value_name = "t",
-               .topic = "burrow/temp/kitchen"
+               .topic = "burrow/temp/terarium"
        },
        {
                .metric = "rh,where=ursarium",
@@ -183,6 +185,79 @@ static const struct attr attr_table[] = {
                .value_name = "kVArh",
                .topic = "burrow/power/reactive/energy",
        },
+       {
+               .metric = "heating_water_pressure",
+               .value_name = "p",
+               .topic = "burrow/heating/water-pressure",
+               .timeout = 300,
+       },
+       {
+               .metric = "heating_outside_temp",
+               .value_name = "t",
+               .topic = "burrow/heating/outside-temp",
+               .timeout = 660,
+       },
+       {
+               .metric = "heating_room_temp,circuit=1",
+               .value_name = "t",
+               .topic = "burrow/heating/circuit1/room-temp",
+               .timeout = 300,
+       },
+       {
+               .metric = "heating_mix-temp,circuit=1",
+               .value_name = "t",
+               .topic = "burrow/heating/circuit1/mix-temp",
+               .timeout = 300,
+       },
+       {
+               .metric = "heating_mix-valve,circuit=1",
+               .value_name = "x",
+               .topic = "burrow/heating/circuit1/mix-valve",
+               .timeout = 300,
+       },
+       {
+               .metric = "heating_active,circuit=1",
+               .value_name = "x",
+               .topic = "burrow/heating/circuit1/active",
+               .timeout = 660,
+       },
+       {
+               .metric = "heating_pump_active,circuit=1",
+               .value_name = "x",
+               .topic = "burrow/heating/circuit1/pump",
+               .timeout = 300,
+       },
+       {
+               .metric = "heating_room_temp,circuit=2",
+               .value_name = "t",
+               .topic = "burrow/heating/circuit2/room-temp",
+               .timeout = 300,
+       },
+       {
+               .metric = "heating_active,circuit=2",
+               .value_name = "x",
+               .topic = "burrow/heating/circuit2/active",
+               .timeout = 660,
+       },
+       {
+               .metric = "heating_active,circuit=water",
+               .value_name = "x",
+               .topic = "burrow/heating/water/active",
+               .timeout = 660,
+       },
+       {
+               .metric = "heating_error",
+               .value_name = "err",
+               .topic = "burrow/heating/error",
+               .timeout = 300,
+       },
+       {
+               .metric = "heating_clock",
+               .value_name = "t",
+               .topic = "burrow/heating/clock",
+               .timeout = 660,
+               .is_string = true,
+       },
 };
 
 /*** MQTT ***/
@@ -359,7 +434,10 @@ int main(int argc UNUSED, char **argv)
        if (mosquitto_will_set(mosq, "status/influx", 4, "dead", 0, true) != MOSQ_ERR_SUCCESS)
                die("Mosquitto: unable to set will");
 
-       if (mosquitto_connect_async(mosq, "10.32.184.5", 1883, 60) != MOSQ_ERR_SUCCESS)
+       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_connect_async(mosq, "burrow-mqtt", 8883, 60) != MOSQ_ERR_SUCCESS)
                die("Mosquitto: connect failed");
 
        if (mosquitto_loop_start(mosq))
@@ -386,11 +464,15 @@ int main(int argc UNUSED, char **argv)
                                continue;
                        if (fields >= 2) {
                                time_t t = atoll(w[1]);
-                               if (t < now - MEASUREMENT_TIMEOUT)
+                               uint timeout = a->timeout ? : MEASUREMENT_TIMEOUT;
+                               if (t < now - timeout)
                                        continue;
                        }
 
-                       bprintf(f, "%s %s=%s\n", a->metric, a->value_name, val);
+                       if (!a->is_string)
+                               bprintf(f, "%s %s=%s\n", a->metric, a->value_name, val);
+                       else
+                               bprintf(f, "%s %s=\"%s\"\n", a->metric, a->value_name, val);
                }
                influx_write_flush();
                sleep(INFLUX_INTERVAL);