coolers/mosquitto.c

changeset 41
f534ace74eea
parent 40
dafbbd5e9922
child 42
01b96a24ae7c
equal deleted inserted replaced
40:dafbbd5e9922 41:f534ace74eea
1 /*****************************************************************************
2 * Copyright (C) 2014
3 *
4 * Michiel Broek <mbroek at mbse dot eu>
5 *
6 * This file is part of the mbsePi-apps
7 *
8 * This is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * mbsePi-apps is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with EC-65K; see the file COPYING. If not, write to the Free
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 *****************************************************************************/
22
23 #include "../lib/mbselib.h"
24 #include "mosquitto.h"
25
26 #ifdef HAVE_WIRINGPI_H
27
28
29 #define STATUS_CONNECTING 0
30 #define STATUS_CONNACK_RECVD 1
31 #define STATUS_WAITING 2
32
33 /* Global variables for use in callbacks. */
34 struct mosquitto *mymosq = NULL;
35 char *myhostname;
36 static int qos = 0;
37 static int status = STATUS_CONNECTING;
38 static int mid_sent = 0;
39 static int last_mid = -1;
40 static int last_mid_sent = -1;
41 static bool connected = true;
42 static bool disconnect_sent = false;
43 static bool connect_lost = false;
44
45 extern bool my_shutdown;
46 extern bool debug;
47 extern sys_config Config;
48 extern int lcdHandle;
49 extern int lcdupdate;
50
51
52 void my_connect_callback(struct mosquitto *mosq, void *obj, int result)
53 {
54 if (connect_lost) {
55 connect_lost = false;
56 syslog(LOG_NOTICE, "Reconnect: %s", mosquitto_connack_string(result));
57 }
58
59 if (!result) {
60 status = STATUS_CONNACK_RECVD;
61 } else {
62 syslog(LOG_NOTICE, "my_connect_callback: %s\n", mosquitto_connack_string(result));
63 }
64 }
65
66
67
68 void my_disconnect_callback(struct mosquitto *mosq, void *obj, int rc)
69 {
70 if (my_shutdown) {
71 syslog(LOG_NOTICE, "Acknowledged DISCONNECT from %s", Config.mosq_host);
72 connected = false;
73 } else {
74 /*
75 * The remove server was brought down. We must keep running
76 */
77 syslog(LOG_NOTICE, "Received DISCONNECT from %s, connection lost", Config.mosq_host);
78 connect_lost = true;
79 }
80 }
81
82
83
84 void my_publish_callback(struct mosquitto *mosq, void *obj, int mid)
85 {
86 last_mid_sent = mid;
87 }
88
89
90
91 void my_log_callback(struct mosquitto *mosq, void *obj, int level, const char *str)
92 {
93 syslog(LOG_NOTICE, "MQTT: %s", str);
94 printf("MQTT: %s\n", str);
95 }
96
97
98
99 int my_mosquitto_init(void)
100 {
101 char *id = NULL, *state = NULL;
102 char buf[1024];
103 int try, rc, keepalive = 60;
104 unsigned int max_inflight = 20;
105 char err[1024];
106 w1_therm *tmp1, *old1;
107 rc_switch *tmp2, *old2;
108 char *alias;
109
110 /*
111 * Initialize mosquitto communication
112 */
113 mosquitto_lib_init();
114
115 gethostname(buf, 255);
116 myhostname = xstrcpy(buf);
117
118 /*
119 * Build MQTT id
120 */
121 id = xstrcpy((char *)"coolers/");
122 id = xstrcat(id, myhostname);
123 if(strlen(id) > MOSQ_MQTT_ID_MAX_LENGTH) {
124 /*
125 * Enforce maximum client id length of 23 characters
126 */
127 id[MOSQ_MQTT_ID_MAX_LENGTH] = '\0';
128 }
129
130 mymosq = mosquitto_new(id, true, NULL);
131 if (!mymosq) {
132 switch(errno) {
133 case ENOMEM:
134 syslog(LOG_NOTICE, "mosquitto_new: Out of memory");
135 break;
136 case EINVAL:
137 syslog(LOG_NOTICE, "mosquitto_new: Invalid id");
138 break;
139 }
140 mosquitto_lib_cleanup();
141 return 1;
142 }
143
144 if (debug) {
145 mosquitto_log_callback_set(mymosq, my_log_callback);
146 }
147
148 /*
149 * Set our will
150 */
151 state = xstrcpy((char *)"clients/");
152 state = xstrcat(state, myhostname);
153 state = xstrcat(state, (char *)"/coolers/state");
154 sprintf(buf, "0");
155 if ((rc = mosquitto_will_set(mymosq, state, strlen(buf), buf, qos, true))) {
156 if (rc == MOSQ_ERR_INVAL) {
157 syslog(LOG_NOTICE, "mosquitto_will_set: input parameters invalid");
158 } else if (rc == MOSQ_ERR_NOMEM) {
159 syslog(LOG_NOTICE, "mosquitto_will_set: Out of Memory");
160 } else if (rc == MOSQ_ERR_PAYLOAD_SIZE) {
161 syslog(LOG_NOTICE, "mosquitto_will_set: invalid payload size");
162 }
163 mosquitto_lib_cleanup();
164 return rc;
165 }
166
167 mosquitto_max_inflight_messages_set(mymosq, max_inflight);
168 mosquitto_connect_callback_set(mymosq, my_connect_callback);
169 mosquitto_disconnect_callback_set(mymosq, my_disconnect_callback);
170 mosquitto_publish_callback_set(mymosq, my_publish_callback);
171
172 try = 10; rc = -1;
173 while (try && rc) {
174 if ((rc = mosquitto_connect(mymosq, Config.mosq_host, Config.mosq_port, keepalive))) {
175 if (rc == MOSQ_ERR_ERRNO) {
176 strerror_r(errno, err, 1024);
177 syslog(LOG_NOTICE, "mosquitto_connect: error: %s, try=%d", err, 11-try);
178 } else {
179 syslog(LOG_NOTICE, "mosquitto_connect: unable to connect (%d)", rc);
180 }
181 sleep(2);
182 try--;
183 }
184 }
185 if (rc) {
186 syslog(LOG_NOTICE, "mosquitto_connect: too many tries, giving up");
187 mosquitto_lib_cleanup();
188 return rc;
189 }
190 syslog(LOG_NOTICE, "Connected with %s:%d", Config.mosq_host, Config.mosq_port);
191
192 /*
193 * Initialise is complete, report our presence state
194 */
195 mosquitto_loop_start(mymosq);
196 sprintf(buf, "1");
197 rc = mosquitto_publish(mymosq, &mid_sent, state, strlen(buf), buf, qos, 1);
198
199 /*
200 * Report alias names
201 */
202 for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) {
203 old1 = tmp1->next;
204
205 alias = xstrcpy((char *)"/raw/");
206 alias = xstrcat(alias, myhostname);
207 alias = xstrcat(alias, (char *)"/coolers/w1/");
208 alias = xstrcat(alias, tmp1->master);
209 alias = xstrcat(alias, (char *)"/");
210 alias = xstrcat(alias, tmp1->name);
211 alias = xstrcat(alias, (char *)"/alias");
212
213 sprintf(buf, "%s", tmp1->alias);
214 if ((rc = mosquitto_publish(mymosq, &mid_sent, alias, strlen(buf), buf, qos, 1))) {
215 if (rc == MOSQ_ERR_NO_CONN)
216 mosquitto_reconnect(mymosq);
217 else
218 syslog(LOG_NOTICE, "mainloop: error %d from mosquitto_publish", rc);
219 }
220
221 free(alias);
222 alias = NULL;
223 }
224
225 for (tmp2 = Config.rcswitch; tmp2; tmp2 = old2) {
226 old2 = tmp2->next;
227
228 alias = xstrcpy((char *)"/raw/");
229 alias = xstrcat(alias, myhostname);
230 alias = xstrcat(alias, (char *)"/coolers/rcswitch/");
231 alias = xstrcat(alias, tmp2->address);
232 alias = xstrcat(alias, (char *)"/alias");
233
234 sprintf(buf, "%s", tmp2->alias);
235 if ((rc = mosquitto_publish(mymosq, &mid_sent, alias, strlen(buf), buf, qos, 1))) {
236 if (rc == MOSQ_ERR_NO_CONN)
237 mosquitto_reconnect(mymosq);
238 else
239 syslog(LOG_NOTICE, "my_mosquitto_init: error %d from mosquitto_publish", rc);
240 }
241
242 free(alias);
243 alias = NULL;
244 my_mosquitto_switch(tmp2->address, 0);
245 }
246
247 return 0;
248 }
249
250
251
252 int my_mosquitto_switch(char *address, int state)
253 {
254 char *cmd = NULL, buf[10];
255 int rc;
256
257 cmd = xstrcpy(address);
258 if (state)
259 cmd = xstrcat(cmd, (char *)",1");
260 else
261 cmd = xstrcat(cmd, (char *)",0");
262 rc = toggleSwitch(cmd);
263 if (debug)
264 fprintf(stdout, "Switch %s rc=%d\n", cmd, rc);
265 syslog(LOG_NOTICE, "Switch %s rc=%d", cmd, rc);
266 free(cmd);
267
268 cmd = xstrcpy((char *)"/raw/");
269 cmd = xstrcat(cmd, myhostname);
270 cmd = xstrcat(cmd, (char *)"/coolers/rcswitch/");
271 cmd = xstrcat(cmd, address);
272 cmd = xstrcat(cmd, (char *)"/state");
273 sprintf(buf, "%d", state);
274
275 if ((rc = mosquitto_publish(mymosq, &mid_sent, cmd, strlen(buf), buf, qos, 1))) {
276 if (rc == MOSQ_ERR_NO_CONN)
277 mosquitto_reconnect(mymosq);
278 else
279 syslog(LOG_NOTICE, "my_mosquitto_switch: error %d from mosquitto_publish", rc);
280 }
281
282 free(cmd);
283 cmd = NULL;
284
285 return rc;
286 }
287
288
289
290 int my_mosquitto_loop(void)
291 {
292 w1_therm *tmp1, *old1;
293 char buf[1024], *alias, *state = NULL;
294 int rc;
295
296 if (status == STATUS_CONNACK_RECVD) {
297 /*
298 * Here send our 1-wire sensors values
299 */
300 for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) {
301 old1 = tmp1->next;
302
303 if (tmp1->update) {
304 /*
305 * Build path and alias topic
306 */
307 alias = xstrcpy((char *)"/raw/");
308 alias = xstrcat(alias, myhostname);
309 alias = xstrcat(alias, (char *)"/coolers/w1/");
310 alias = xstrcat(alias, tmp1->master);
311 alias = xstrcat(alias, (char *)"/");
312 alias = xstrcat(alias, tmp1->name);
313 alias = xstrcat(alias, (char *)"/temperature");
314
315 /*
316 * Publish the temperature.
317 */
318 sprintf(buf, "%.1f", tmp1->lastval / 1000.0);
319 if ((rc = mosquitto_publish(mymosq, &mid_sent, alias, strlen(buf), buf, qos, 1))) {
320 if (rc == MOSQ_ERR_NO_CONN)
321 mosquitto_reconnect(mymosq);
322 else
323 syslog(LOG_NOTICE, "mainloop: error %d from mosquitto_publish", rc);
324 }
325 tmp1->update = FALSE;
326 lcdupdate = TRUE;
327
328 free(alias);
329 alias = NULL;
330 }
331 }
332
333 if (my_shutdown) {
334 /*
335 * Final publish 0 to clients/<hostname>/coolers/state
336 */
337 sprintf(buf, "0");
338 mosquitto_publish(mymosq, &mid_sent, state, strlen(buf), buf, qos, true);
339 last_mid = mid_sent;
340 status = STATUS_WAITING;
341 mb_lcdClear(lcdHandle);
342 // lcdPosition(lcdHandle, 0, 0);
343 mb_lcdPuts(lcdHandle, "Shuting down ...");
344 }
345
346 } else if (status == STATUS_WAITING) {
347 if (debug)
348 fprintf(stdout, (char *)"Waiting\n");
349 if (last_mid_sent == last_mid && disconnect_sent == false) {
350 mosquitto_disconnect(mymosq);
351 disconnect_sent = true;
352 }
353 }
354 rc = MOSQ_ERR_SUCCESS;
355
356 return (rc == MOSQ_ERR_SUCCESS && connected);
357 }
358
359
360
361 void my_mosquitto_exit(void)
362 {
363 mosquitto_loop_stop(mymosq, false);
364 mosquitto_destroy(mymosq);
365 mosquitto_lib_cleanup();
366 }
367
368
369 #endif

mercurial