]> mj.ucw.cz Git - home-hw.git/blobdiff - aircon/daemon/burrow-aircond.c
Updated to new MQTT server and fixed a couple of bugs
[home-hw.git] / aircon / daemon / burrow-aircond.c
index c297a9f0d7a04ae61d27ca45003fd7f73c8d5fe5..7db3864ca9f4bf3b9fb425f3eb724bb7c1eaf0db 100644 (file)
@@ -16,6 +16,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <syslog.h>
 #include <stdlib.h>
 #include <string.h>
 #include <syslog.h>
+#include <time.h>
 #include <unistd.h>
 
 #include <modbus.h>
 #include <unistd.h>
 
 #include <modbus.h>
@@ -57,7 +58,7 @@ static void mqtt_publish(const char *topic, const char *fmt, ...)
        va_end(args);
 }
 
        va_end(args);
 }
 
-static void mqtt_log_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, int level, const char *message)
+static void mqtt_log_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, int level UNUSED, const char *message UNUSED)
 {
        // msg(L_INFO, "MQTT(%d): %s", level, message);
 }
 {
        // msg(L_INFO, "MQTT(%d): %s", level, message);
 }
@@ -116,11 +117,11 @@ static bool mqtt_connect(void)
 static modbus_t *modbus;
 static bool mb_is_open;
 
 static modbus_t *modbus;
 static bool mb_is_open;
 
-static void mb_error(const char *operation)
+static void mb_error(const char *operation, bool need_close)
 {
        msg(L_ERROR, "MODBUS: %s failed: %s", operation, modbus_strerror(errno));
 
 {
        msg(L_ERROR, "MODBUS: %s failed: %s", operation, modbus_strerror(errno));
 
-       if (modbus) {
+       if (need_close && modbus) {
                if (mb_is_open) {
                        modbus_close(modbus);
                        mb_is_open = false;
                if (mb_is_open) {
                        modbus_close(modbus);
                        mb_is_open = false;
@@ -135,21 +136,21 @@ static bool mb_connect(void)
        if (modbus)
                return true;
 
        if (modbus)
                return true;
 
-       // FIXME: Find the right device. Reconnect if needed.
-       modbus = modbus_new_rtu("/dev/ttyUSB1", 19200, 'E', 8, 1);
+       modbus = modbus_new_rtu("/dev/modbus-aircon", 19200, 'E', 8, 1);
        if (!modbus) {
        if (!modbus) {
-               mb_error("open");
+               mb_error("open", true);
                return false;
        }
 
        modbus_set_slave(modbus, 42);
 
        if (modbus_connect(modbus) < 0) {
                return false;
        }
 
        modbus_set_slave(modbus, 42);
 
        if (modbus_connect(modbus) < 0) {
-               mb_error("connect");
+               mb_error("connect", true);
                return false;
        }
 
        mb_is_open = true;
                return false;
        }
 
        mb_is_open = true;
+       return true;
 }
 
 /*** Main loop ***/
 }
 
 /*** Main loop ***/
@@ -166,7 +167,7 @@ static void scan_temperatures(time_t now)
 
        u16 regs[5];
        if (modbus_read_input_registers(modbus, AIRCON_IREG_TEMP_FROM_INSIDE, 5, regs) < 0) {
 
        u16 regs[5];
        if (modbus_read_input_registers(modbus, AIRCON_IREG_TEMP_FROM_INSIDE, 5, regs) < 0) {
-               mb_error("read");
+               mb_error("read", true);
                return;
        }
 
                return;
        }
 
@@ -199,7 +200,7 @@ static void mqtt_msg_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, c
                        return;
                }
                if (modbus_write_bit(modbus, AIRCON_COIL_EXCHANGER_BYPASS, x) < 0)
                        return;
                }
                if (modbus_write_bit(modbus, AIRCON_COIL_EXCHANGER_BYPASS, x) < 0)
-                       msg(L_ERROR, "Modbus write failed: %s", modbus_strerror(errno));
+                       mb_error("coil write", false);
        } else if (!strcmp(m->topic, "burrow/air/exchanger-fan")) {
                uint x;
                const char *err;
        } else if (!strcmp(m->topic, "burrow/air/exchanger-fan")) {
                uint x;
                const char *err;
@@ -208,7 +209,14 @@ static void mqtt_msg_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, c
                        return;
                }
                if (modbus_write_register(modbus, AIRCON_HREG_EXCHANGER_FAN, x) < 0)
                        return;
                }
                if (modbus_write_register(modbus, AIRCON_HREG_EXCHANGER_FAN, x) < 0)
-                       msg(L_ERROR, "Modbus write failed: %s", modbus_strerror(errno));
+                       mb_error("fan register write", false);
+       } else if (!strcmp(m->topic, "burrow/air/aircon-remote")) {
+               if (strlen(val) != 1) {
+                       msg(L_ERROR, "Received invalid aircon remote command %s", val);
+                       return;
+               }
+               if (modbus_write_register(modbus, AIRCON_HREG_REMOTE_CONTROL, val[0]) < 0)
+                       mb_error("remote control register write", false);
        }
 }
 
        }
 }