coolers/mosquitto.c

changeset 26
9322c619c525
child 27
4703cc10b99a
equal deleted inserted replaced
25:5e0695f6add5 26:9322c619c525
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 "coolers.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 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 (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 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 if ((rc = mosquitto_connect(mymosq, Config.mosq_host, Config.mosq_port, keepalive))) {
173 if (rc == MOSQ_ERR_ERRNO) {
174 strerror_r(errno, err, 1024);
175 syslog(LOG_NOTICE, "mosquitto_connect: error: %s", err);
176 } else {
177 syslog(LOG_NOTICE, "mosquitto_connect: unable to connect (%d)", rc);
178 }
179 mosquitto_lib_cleanup();
180 return rc;
181 }
182 syslog(LOG_NOTICE, "Connected with %s:%d", Config.mosq_host, Config.mosq_port);
183
184 /*
185 * Initialise is complete, report our presence state
186 */
187 mosquitto_loop_start(mymosq);
188 sprintf(buf, "1");
189 rc = mosquitto_publish(mymosq, &mid_sent, state, strlen(buf), buf, qos, 1);
190
191 /*
192 * Report alias names
193 */
194 for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) {
195 old1 = tmp1->next;
196
197 alias = xstrcpy((char *)"/raw/");
198 alias = xstrcat(alias, myhostname);
199 alias = xstrcat(alias, (char *)"/coolers/w1/");
200 alias = xstrcat(alias, tmp1->master);
201 alias = xstrcat(alias, (char *)"/");
202 alias = xstrcat(alias, tmp1->name);
203 alias = xstrcat(alias, (char *)"/alias");
204
205 sprintf(buf, "%s", tmp1->alias);
206 if ((rc = mosquitto_publish(mymosq, &mid_sent, alias, strlen(buf), buf, qos, 1))) {
207 if (rc == MOSQ_ERR_NO_CONN)
208 mosquitto_reconnect(mymosq);
209 else
210 syslog(LOG_NOTICE, "mainloop: error %d from mosquitto_publish", rc);
211 }
212
213 free(alias);
214 alias = NULL;
215 }
216
217 for (tmp2 = Config.rcswitch; tmp2; tmp2 = old2) {
218 old2 = tmp2->next;
219
220 alias = xstrcpy((char *)"/raw/");
221 alias = xstrcat(alias, myhostname);
222 alias = xstrcat(alias, (char *)"/coolers/rcswitch/");
223 alias = xstrcat(alias, tmp2->address);
224 alias = xstrcat(alias, (char *)"/alias");
225
226 sprintf(buf, "%s", tmp2->alias);
227 if ((rc = mosquitto_publish(mymosq, &mid_sent, alias, strlen(buf), buf, qos, 1))) {
228 if (rc == MOSQ_ERR_NO_CONN)
229 mosquitto_reconnect(mymosq);
230 else
231 syslog(LOG_NOTICE, "mainloop: error %d from mosquitto_publish", rc);
232 }
233
234 free(alias);
235 alias = NULL;
236 }
237
238 return 0;
239 }
240
241
242
243 int my_mosquitto_loop(void)
244 {
245 w1_therm *tmp1, *old1;
246 char buf[1024], *alias, *state = NULL;
247 int rc;
248
249 if (status == STATUS_CONNACK_RECVD) {
250 /*
251 * Here send our 1-wire sensors values
252 */
253 for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) {
254 old1 = tmp1->next;
255
256 /*
257 * Build path and alias topic
258 */
259 alias = xstrcpy((char *)"/raw/");
260 alias = xstrcat(alias, myhostname);
261 alias = xstrcat(alias, (char *)"/coolers/w1/");
262 alias = xstrcat(alias, tmp1->master);
263 alias = xstrcat(alias, (char *)"/");
264 alias = xstrcat(alias, tmp1->name);
265 alias = xstrcat(alias, (char *)"/temperature");
266
267 if (tmp1->update) {
268 /*
269 * Temperature is changed and valid, update and publish this.
270 */
271 sprintf(buf, "%.1f", tmp1->lastval / 1000.0);
272 if ((rc = mosquitto_publish(mymosq, &mid_sent, alias, strlen(buf), buf, qos, 1))) {
273 if (rc == MOSQ_ERR_NO_CONN)
274 mosquitto_reconnect(mymosq);
275 else
276 syslog(LOG_NOTICE, "mainloop: error %d from mosquitto_publish", rc);
277 }
278 tmp1->update = FALSE;
279 }
280
281 free(alias);
282 alias = NULL;
283 }
284
285 if (shutdown) {
286 /*
287 * Final publish 0 to clients/<hostname>/coolers/state
288 */
289 sprintf(buf, "0");
290 mosquitto_publish(mymosq, &mid_sent, state, strlen(buf), buf, qos, true);
291 last_mid = mid_sent;
292 status = STATUS_WAITING;
293 lcdClear(lcdHandle);
294 lcdPosition(lcdHandle, 0, 0);
295 lcdPuts(lcdHandle, "Shuting down ...");
296 }
297
298 usleep(100000);
299
300 } else if (status == STATUS_WAITING) {
301 if (debug)
302 fprintf(stdout, (char *)"Waiting\n");
303 if (last_mid_sent == last_mid && disconnect_sent == false) {
304 mosquitto_disconnect(mymosq);
305 disconnect_sent = true;
306 }
307 usleep(100000);
308 }
309 rc = MOSQ_ERR_SUCCESS;
310
311 return (rc == MOSQ_ERR_SUCCESS && connected);
312 }
313
314
315
316 void my_mosquitto_exit(void)
317 {
318 mosquitto_loop_stop(mymosq, false);
319 mosquitto_destroy(mymosq);
320 mosquitto_lib_cleanup();
321 }
322
323
324 #endif

mercurial