]> mj.ucw.cz Git - home-hw.git/blob - aircon/daemon/burrow-aircond.c
7db3864ca9f4bf3b9fb425f3eb724bb7c1eaf0db
[home-hw.git] / aircon / daemon / burrow-aircond.c
1 /*
2  *      A MQTT Gateway Daemon for the Air Conditioning Controller
3  *
4  *      (c) 2019 Martin Mares <mj@ucw.cz>
5  */
6
7 #include <ucw/lib.h>
8 #include <ucw/log.h>
9 #include <ucw/opt.h>
10 #include <ucw/strtonum.h>
11 #include <ucw/stkstring.h>
12
13 #include <errno.h>
14 #include <stdio.h>
15 #include <stdarg.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <syslog.h>
19 #include <time.h>
20 #include <unistd.h>
21
22 #include <modbus.h>
23 #include <mosquitto.h>
24
25 #include "../firmware/registers.h"
26
27 /*** MQTT ***/
28
29 static struct mosquitto *mosq;
30 static bool mqtt_connected;
31
32 static void mqtt_error(const char *operation, int err, bool teardown)
33 {
34         msg(L_ERROR, "Mosquitto: %s failed: error %d", operation, err);
35
36         if (teardown) {
37                 mosquitto_destroy(mosq);
38                 mosq = NULL;
39                 mqtt_connected = false;
40         } else if (err == MOSQ_ERR_NO_CONN || err == MOSQ_ERR_CONN_REFUSED || err == MOSQ_ERR_CONN_LOST) {
41                 mqtt_connected = false;
42         }
43 }
44
45 static void mqtt_publish(const char *topic, const char *fmt, ...)
46 {
47         va_list args;
48         va_start(args, fmt);
49
50         if (mqtt_connected) {
51                 char m[256];
52                 int l = vsnprintf(m, sizeof(m), fmt, args);
53                 int err = mosquitto_publish(mosq, NULL, topic, l, m, 0, true);
54                 if (err != MOSQ_ERR_SUCCESS)
55                         mqtt_error("publish", err, false);
56         }
57
58         va_end(args);
59 }
60
61 static void mqtt_log_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, int level UNUSED, const char *message UNUSED)
62 {
63         // msg(L_INFO, "MQTT(%d): %s", level, message);
64 }
65
66 static void mqtt_msg_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, const struct mosquitto_message *m);
67
68 static bool mqtt_connect(void)
69 {
70         int err;
71
72         if (mqtt_connected)
73                 return true;
74
75         if (!mosq) {
76                 mosq = mosquitto_new("aircond", 1, NULL);
77                 if (!mosq)
78                         die("Mosquitto: initialization failed");
79
80                 mosquitto_log_callback_set(mosq, mqtt_log_callback);
81                 mosquitto_message_callback_set(mosq, mqtt_msg_callback);
82
83                 err = mosquitto_will_set(mosq, "status/aircon", 4, "dead", 0, true);
84                 if (err != MOSQ_ERR_SUCCESS) {
85                         mqtt_error("will_set", err, true);
86                         return false;
87                 }
88
89                 err = mosquitto_connect(mosq, "127.0.0.1", 1883, 60);
90                 if (err != MOSQ_ERR_SUCCESS) {
91                         mqtt_error("connect", err, true);
92                         return false;
93                 }
94         } else {
95                 err = mosquitto_reconnect(mosq);
96                 if (err != MOSQ_ERR_SUCCESS) {
97                         mqtt_error("reconnect", err, false);
98                         return false;
99                 }
100         }
101
102         err = mosquitto_subscribe(mosq, NULL, "burrow/air/#", 1);
103         if (err != MOSQ_ERR_SUCCESS) {
104                 mqtt_error("subscribe", err, false);
105                 return false;
106         }
107
108         mqtt_connected = true;
109
110         mqtt_publish("status/aircon", "ok");
111
112         return mqtt_connected;
113 }
114
115 /*** MODBUS ***/
116
117 static modbus_t *modbus;
118 static bool mb_is_open;
119
120 static void mb_error(const char *operation, bool need_close)
121 {
122         msg(L_ERROR, "MODBUS: %s failed: %s", operation, modbus_strerror(errno));
123
124         if (need_close && modbus) {
125                 if (mb_is_open) {
126                         modbus_close(modbus);
127                         mb_is_open = false;
128                 }
129                 modbus_free(modbus);
130                 modbus = NULL;
131         }
132 }
133
134 static bool mb_connect(void)
135 {
136         if (modbus)
137                 return true;
138
139         modbus = modbus_new_rtu("/dev/modbus-aircon", 19200, 'E', 8, 1);
140         if (!modbus) {
141                 mb_error("open", true);
142                 return false;
143         }
144
145         modbus_set_slave(modbus, 42);
146
147         if (modbus_connect(modbus) < 0) {
148                 mb_error("connect", true);
149                 return false;
150         }
151
152         mb_is_open = true;
153         return true;
154 }
155
156 /*** Main loop ***/
157
158 static void scan_temperatures(time_t now)
159 {
160         static const char * const temp_names[] = {
161                 "inside-intake",
162                 "inside-exhaust",
163                 "outside-intake",
164                 "outside-exhaust",
165                 "mixed",
166         };
167
168         u16 regs[5];
169         if (modbus_read_input_registers(modbus, AIRCON_IREG_TEMP_FROM_INSIDE, 5, regs) < 0) {
170                 mb_error("read", true);
171                 return;
172         }
173
174         for (uint i=0; i<5; i++) {
175                 if (regs[i] != 0x8000) {
176                         int t = regs[i];
177                         if (t >= 0x8000)
178                                 t -= 0x10000;
179                         mqtt_publish(stk_printf("burrow/air/%s", temp_names[i]), "%.3f %llu", t / 100., (unsigned long long) now);
180                 }
181         }
182 }
183
184 static void mqtt_msg_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, const struct mosquitto_message *m)
185 {
186         char val[256];
187         if (m->payloadlen >= sizeof(val) - 1) {
188                 msg(L_ERROR, "Invalid value for topic %s", m->topic);
189                 return;
190         }
191         memcpy(val, m->payload, m->payloadlen);
192         val[m->payloadlen] = 0;
193         msg(L_DEBUG, "MQTT < %s %s", m->topic, val);
194
195         if (!strcmp(m->topic, "burrow/air/bypass")) {
196                 uint x;
197                 const char *err;
198                 if ((err = str_to_uint(&x, val, NULL, 10 | STN_WHOLE)) || x >= 2) {
199                         msg(L_ERROR, "Received invalid bypass setting %s: %s", val, err);
200                         return;
201                 }
202                 if (modbus_write_bit(modbus, AIRCON_COIL_EXCHANGER_BYPASS, x) < 0)
203                         mb_error("coil write", false);
204         } else if (!strcmp(m->topic, "burrow/air/exchanger-fan")) {
205                 uint x;
206                 const char *err;
207                 if ((err = str_to_uint(&x, val, NULL, 10 | STN_WHOLE)) || x >= 256) {
208                         msg(L_ERROR, "Received invalid exchanger fan setting %s: %s", val, err);
209                         return;
210                 }
211                 if (modbus_write_register(modbus, AIRCON_HREG_EXCHANGER_FAN, x) < 0)
212                         mb_error("fan register write", false);
213         } else if (!strcmp(m->topic, "burrow/air/aircon-remote")) {
214                 if (strlen(val) != 1) {
215                         msg(L_ERROR, "Received invalid aircon remote command %s", val);
216                         return;
217                 }
218                 if (modbus_write_register(modbus, AIRCON_HREG_REMOTE_CONTROL, val[0]) < 0)
219                         mb_error("remote control register write", false);
220         }
221 }
222
223 static int use_daemon;
224 static int use_debug;
225
226 static struct opt_section options = {
227         OPT_ITEMS {
228                 OPT_HELP("A daemon for controlling the air conditioning controller via MQTT"),
229                 OPT_HELP(""),
230                 OPT_HELP("Options:"),
231                 OPT_BOOL('d', "debug", use_debug, 0, "\tLog debugging messages"),
232                 OPT_BOOL(0, "daemon", use_daemon, 0, "\tDaemonize"),
233                 OPT_HELP_OPTION,
234                 OPT_CONF_OPTIONS,
235                 OPT_END
236         }
237 };
238
239 int main(int argc UNUSED, char **argv)
240 {
241         log_init(argv[0]);
242         opt_parse(&options, argv+1);
243
244         if (use_daemon) {
245                 struct log_stream *ls = log_new_syslog("daemon", LOG_PID);
246                 log_set_default_stream(ls);
247         }
248         if (!use_debug)
249                 log_default_stream()->levels &= ~(1U << L_DEBUG);
250
251         mosquitto_lib_init();
252
253         time_t next_run = 0;
254         for (;;) {
255                 if (!mqtt_connect() || !mb_connect()) {
256                         sleep(5);
257                         continue;
258                 }
259
260                 time_t now = time(NULL);
261                 if (now < next_run) {
262                         int err = mosquitto_loop(mosq, (next_run - now) * 1000, 1);
263                         if (err != MOSQ_ERR_SUCCESS)
264                                 mqtt_error("loop", err, false);
265                         continue;
266                 }
267
268                 next_run = now + 10;
269                 scan_temperatures(now);
270         }
271 }