--- a/coolers/mosquitto.c Tue May 06 13:24:25 2014 +0200 +++ b/coolers/mosquitto.c Tue May 06 21:37:06 2014 +0200 @@ -21,7 +21,7 @@ *****************************************************************************/ #include "../lib/mbselib.h" -#include "coolers.h" +#include "mosquitto.h" #ifdef HAVE_WIRINGPI_H @@ -128,7 +128,7 @@ } mymosq = mosquitto_new(id, true, NULL); - if(!mymosq) { + if (!mymosq) { switch(errno) { case ENOMEM: syslog(LOG_NOTICE, "mosquitto_new: Out of memory"); @@ -228,11 +228,12 @@ if (rc == MOSQ_ERR_NO_CONN) mosquitto_reconnect(mymosq); else - syslog(LOG_NOTICE, "mainloop: error %d from mosquitto_publish", rc); + syslog(LOG_NOTICE, "my_mosquitto_init: error %d from mosquitto_publish", rc); } free(alias); alias = NULL; + my_mosquitto_switch(tmp2->address, 0); } return 0; @@ -240,6 +241,44 @@ +int my_mosquitto_switch(char *address, int state) +{ + char *cmd = NULL, buf[10]; + int rc; + + cmd = xstrcpy(address); + if (state) + cmd = xstrcat(cmd, (char *)",1"); + else + cmd = xstrcat(cmd, (char *)",0"); + rc = toggleSwitch(cmd); + if (debug) + fprintf(stdout, "Switch %s rc=%d\n", cmd, rc); + syslog(LOG_NOTICE, "Switch %s rc=%d", cmd, rc); + free(cmd); + + cmd = xstrcpy((char *)"/raw/"); + cmd = xstrcat(cmd, myhostname); + cmd = xstrcat(cmd, (char *)"/coolers/rcswitch/"); + cmd = xstrcat(cmd, address); + cmd = xstrcat(cmd, (char *)"/state"); + sprintf(buf, "%d", state); + + if ((rc = mosquitto_publish(mymosq, &mid_sent, cmd, strlen(buf), buf, qos, 1))) { + if (rc == MOSQ_ERR_NO_CONN) + mosquitto_reconnect(mymosq); + else + syslog(LOG_NOTICE, "my_mosquitto_switch: error %d from mosquitto_publish", rc); + } + + free(cmd); + cmd = NULL; + + return rc; +} + + + int my_mosquitto_loop(void) { w1_therm *tmp1, *old1; @@ -253,20 +292,20 @@ for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) { old1 = tmp1->next; - /* - * Build path and alias topic - */ - alias = xstrcpy((char *)"/raw/"); - alias = xstrcat(alias, myhostname); - alias = xstrcat(alias, (char *)"/coolers/w1/"); - alias = xstrcat(alias, tmp1->master); - alias = xstrcat(alias, (char *)"/"); - alias = xstrcat(alias, tmp1->name); - alias = xstrcat(alias, (char *)"/temperature"); - if (tmp1->update) { /* - * Temperature is changed and valid, update and publish this. + * Build path and alias topic + */ + alias = xstrcpy((char *)"/raw/"); + alias = xstrcat(alias, myhostname); + alias = xstrcat(alias, (char *)"/coolers/w1/"); + alias = xstrcat(alias, tmp1->master); + alias = xstrcat(alias, (char *)"/"); + alias = xstrcat(alias, tmp1->name); + alias = xstrcat(alias, (char *)"/temperature"); + + /* + * Publish the temperature. */ sprintf(buf, "%.1f", tmp1->lastval / 1000.0); if ((rc = mosquitto_publish(mymosq, &mid_sent, alias, strlen(buf), buf, qos, 1))) { @@ -277,10 +316,10 @@ } tmp1->update = FALSE; lcdupdate = TRUE; - } - free(alias); - alias = NULL; + free(alias); + alias = NULL; + } } if (shutdown) { @@ -296,8 +335,6 @@ lcdPuts(lcdHandle, "Shuting down ..."); } - usleep(100000); - } else if (status == STATUS_WAITING) { if (debug) fprintf(stdout, (char *)"Waiting\n"); @@ -305,7 +342,6 @@ mosquitto_disconnect(mymosq); disconnect_sent = true; } - usleep(100000); } rc = MOSQ_ERR_SUCCESS;