From 56397e98e7d4a2a52bd1085d40db8682c0ad3e00 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Thu, 18 Jul 2019 01:32:28 +0200 Subject: [PATCH] Aircon daemon: aircon-remote --- aircon/daemon/burrow-aircond.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/aircon/daemon/burrow-aircond.c b/aircon/daemon/burrow-aircond.c index c297a9f..00bfc7a 100644 --- a/aircon/daemon/burrow-aircond.c +++ b/aircon/daemon/burrow-aircond.c @@ -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); } } -- 2.39.2