X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=aircon%2Fdaemon%2Fburrow-aircond.c;h=7fb6c4248efa7de6beb675814d5759b26052249e;hb=HEAD;hp=c297a9f0d7a04ae61d27ca45003fd7f73c8d5fe5;hpb=c710b472d3f8c38279425c4bf0350f4ce68a7db7;p=home-hw.git diff --git a/aircon/daemon/burrow-aircond.c b/aircon/daemon/burrow-aircond.c index c297a9f..7fb6c42 100644 --- a/aircon/daemon/burrow-aircond.c +++ b/aircon/daemon/burrow-aircond.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -57,7 +58,7 @@ static void mqtt_publish(const char *topic, const char *fmt, ...) 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); } @@ -116,11 +117,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; @@ -135,21 +136,24 @@ static bool mb_connect(void) 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_tcp("127.0.0.1", 4301); + // modbus = modbus_new_rtu("/dev/modbus-aircon", 19200, 'E', 8, 1); if (!modbus) { - mb_error("open"); + mb_error("open", true); return false; } + // modbus_set_debug(modbus, 1); modbus_set_slave(modbus, 42); + modbus_set_response_timeout(modbus, 5, 0); if (modbus_connect(modbus) < 0) { - mb_error("connect"); + mb_error("connect", true); return false; } mb_is_open = true; + return true; } /*** Main loop ***/ @@ -166,7 +170,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 +203,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 +212,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); } }