]> mj.ucw.cz Git - home-hw.git/commitdiff
Aircon daemon: aircon-remote
authorMartin Mares <mj@ucw.cz>
Wed, 17 Jul 2019 23:32:28 +0000 (01:32 +0200)
committerMartin Mares <mj@ucw.cz>
Wed, 17 Jul 2019 23:32:28 +0000 (01:32 +0200)
aircon/daemon/burrow-aircond.c

index c297a9f0d7a04ae61d27ca45003fd7f73c8d5fe5..00bfc7abb2924489ef5f6901dbece4c815d6c187 100644 (file)
@@ -116,11 +116,11 @@ static bool mqtt_connect(void)
 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));
 
-       if (modbus) {
+       if (need_close && modbus) {
                if (mb_is_open) {
                        modbus_close(modbus);
                        mb_is_open = false;
@@ -138,14 +138,14 @@ static bool mb_connect(void)
        // FIXME: Find the right device. Reconnect if needed.
        modbus = modbus_new_rtu("/dev/ttyUSB1", 19200, 'E', 8, 1);
        if (!modbus) {
-               mb_error("open");
+               mb_error("open", true);
                return false;
        }
 
        modbus_set_slave(modbus, 42);
 
        if (modbus_connect(modbus) < 0) {
-               mb_error("connect");
+               mb_error("connect", true);
                return false;
        }
 
@@ -166,7 +166,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) {
-               mb_error("read");
+               mb_error("read", true);
                return;
        }
 
@@ -199,7 +199,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)
-                       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;
@@ -208,7 +208,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)
-                       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);
        }
 }