19 * along with EC-65K; see the file COPYING. If not, write to the Free |
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. |
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
21 *****************************************************************************/ |
21 *****************************************************************************/ |
22 |
22 |
23 #include "../lib/mbselib.h" |
23 #include "../lib/mbselib.h" |
24 #include "coolers.h" |
24 #include "mosquitto.h" |
25 |
25 |
26 #ifdef HAVE_WIRINGPI_H |
26 #ifdef HAVE_WIRINGPI_H |
27 |
27 |
28 |
28 |
29 #define STATUS_CONNECTING 0 |
29 #define STATUS_CONNECTING 0 |
226 sprintf(buf, "%s", tmp2->alias); |
226 sprintf(buf, "%s", tmp2->alias); |
227 if ((rc = mosquitto_publish(mymosq, &mid_sent, alias, strlen(buf), buf, qos, 1))) { |
227 if ((rc = mosquitto_publish(mymosq, &mid_sent, alias, strlen(buf), buf, qos, 1))) { |
228 if (rc == MOSQ_ERR_NO_CONN) |
228 if (rc == MOSQ_ERR_NO_CONN) |
229 mosquitto_reconnect(mymosq); |
229 mosquitto_reconnect(mymosq); |
230 else |
230 else |
231 syslog(LOG_NOTICE, "mainloop: error %d from mosquitto_publish", rc); |
231 syslog(LOG_NOTICE, "my_mosquitto_init: error %d from mosquitto_publish", rc); |
232 } |
232 } |
233 |
233 |
234 free(alias); |
234 free(alias); |
235 alias = NULL; |
235 alias = NULL; |
|
236 my_mosquitto_switch(tmp2->address, 0); |
236 } |
237 } |
237 |
238 |
238 return 0; |
239 return 0; |
|
240 } |
|
241 |
|
242 |
|
243 |
|
244 int my_mosquitto_switch(char *address, int state) |
|
245 { |
|
246 char *cmd = NULL, buf[10]; |
|
247 int rc; |
|
248 |
|
249 cmd = xstrcpy(address); |
|
250 if (state) |
|
251 cmd = xstrcat(cmd, (char *)",1"); |
|
252 else |
|
253 cmd = xstrcat(cmd, (char *)",0"); |
|
254 rc = toggleSwitch(cmd); |
|
255 if (debug) |
|
256 fprintf(stdout, "Switch %s rc=%d\n", cmd, rc); |
|
257 syslog(LOG_NOTICE, "Switch %s rc=%d", cmd, rc); |
|
258 free(cmd); |
|
259 |
|
260 cmd = xstrcpy((char *)"/raw/"); |
|
261 cmd = xstrcat(cmd, myhostname); |
|
262 cmd = xstrcat(cmd, (char *)"/coolers/rcswitch/"); |
|
263 cmd = xstrcat(cmd, address); |
|
264 cmd = xstrcat(cmd, (char *)"/state"); |
|
265 sprintf(buf, "%d", state); |
|
266 |
|
267 if ((rc = mosquitto_publish(mymosq, &mid_sent, cmd, strlen(buf), buf, qos, 1))) { |
|
268 if (rc == MOSQ_ERR_NO_CONN) |
|
269 mosquitto_reconnect(mymosq); |
|
270 else |
|
271 syslog(LOG_NOTICE, "my_mosquitto_switch: error %d from mosquitto_publish", rc); |
|
272 } |
|
273 |
|
274 free(cmd); |
|
275 cmd = NULL; |
|
276 |
|
277 return rc; |
239 } |
278 } |
240 |
279 |
241 |
280 |
242 |
281 |
243 int my_mosquitto_loop(void) |
282 int my_mosquitto_loop(void) |
251 * Here send our 1-wire sensors values |
290 * Here send our 1-wire sensors values |
252 */ |
291 */ |
253 for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) { |
292 for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) { |
254 old1 = tmp1->next; |
293 old1 = tmp1->next; |
255 |
294 |
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) { |
295 if (tmp1->update) { |
268 /* |
296 /* |
269 * Temperature is changed and valid, update and publish this. |
297 * Build path and alias topic |
|
298 */ |
|
299 alias = xstrcpy((char *)"/raw/"); |
|
300 alias = xstrcat(alias, myhostname); |
|
301 alias = xstrcat(alias, (char *)"/coolers/w1/"); |
|
302 alias = xstrcat(alias, tmp1->master); |
|
303 alias = xstrcat(alias, (char *)"/"); |
|
304 alias = xstrcat(alias, tmp1->name); |
|
305 alias = xstrcat(alias, (char *)"/temperature"); |
|
306 |
|
307 /* |
|
308 * Publish the temperature. |
270 */ |
309 */ |
271 sprintf(buf, "%.1f", tmp1->lastval / 1000.0); |
310 sprintf(buf, "%.1f", tmp1->lastval / 1000.0); |
272 if ((rc = mosquitto_publish(mymosq, &mid_sent, alias, strlen(buf), buf, qos, 1))) { |
311 if ((rc = mosquitto_publish(mymosq, &mid_sent, alias, strlen(buf), buf, qos, 1))) { |
273 if (rc == MOSQ_ERR_NO_CONN) |
312 if (rc == MOSQ_ERR_NO_CONN) |
274 mosquitto_reconnect(mymosq); |
313 mosquitto_reconnect(mymosq); |
275 else |
314 else |
276 syslog(LOG_NOTICE, "mainloop: error %d from mosquitto_publish", rc); |
315 syslog(LOG_NOTICE, "mainloop: error %d from mosquitto_publish", rc); |
277 } |
316 } |
278 tmp1->update = FALSE; |
317 tmp1->update = FALSE; |
279 lcdupdate = TRUE; |
318 lcdupdate = TRUE; |
280 } |
319 |
281 |
320 free(alias); |
282 free(alias); |
321 alias = NULL; |
283 alias = NULL; |
322 } |
284 } |
323 } |
285 |
324 |
286 if (shutdown) { |
325 if (shutdown) { |
287 /* |
326 /* |
288 * Final publish 0 to clients/<hostname>/coolers/state |
327 * Final publish 0 to clients/<hostname>/coolers/state |
294 lcdClear(lcdHandle); |
333 lcdClear(lcdHandle); |
295 lcdPosition(lcdHandle, 0, 0); |
334 lcdPosition(lcdHandle, 0, 0); |
296 lcdPuts(lcdHandle, "Shuting down ..."); |
335 lcdPuts(lcdHandle, "Shuting down ..."); |
297 } |
336 } |
298 |
337 |
299 usleep(100000); |
|
300 |
|
301 } else if (status == STATUS_WAITING) { |
338 } else if (status == STATUS_WAITING) { |
302 if (debug) |
339 if (debug) |
303 fprintf(stdout, (char *)"Waiting\n"); |
340 fprintf(stdout, (char *)"Waiting\n"); |
304 if (last_mid_sent == last_mid && disconnect_sent == false) { |
341 if (last_mid_sent == last_mid && disconnect_sent == false) { |
305 mosquitto_disconnect(mymosq); |
342 mosquitto_disconnect(mymosq); |
306 disconnect_sent = true; |
343 disconnect_sent = true; |
307 } |
344 } |
308 usleep(100000); |
|
309 } |
345 } |
310 rc = MOSQ_ERR_SUCCESS; |
346 rc = MOSQ_ERR_SUCCESS; |
311 |
347 |
312 return (rc == MOSQ_ERR_SUCCESS && connected); |
348 return (rc == MOSQ_ERR_SUCCESS && connected); |
313 } |
349 } |