thermometers/thermometers.c

changeset 144
3446371e0bdb
parent 51
a03b6dac5398
child 145
1396350141cf
--- a/thermometers/thermometers.c	Tue Jul 29 20:11:23 2014 +0200
+++ b/thermometers/thermometers.c	Tue Jul 29 20:42:02 2014 +0200
@@ -23,23 +23,10 @@
 #include "thermometers.h"
 
 
-#define STATUS_CONNECTING 0
-#define STATUS_CONNACK_RECVD 1
-#define STATUS_WAITING 2
-
-/* Global variables for use in callbacks. */
-static int		qos = 0;
-static int		status = STATUS_CONNECTING;
-static int		mid_sent = 0;
-static int		last_mid = -1;
-static int		last_mid_sent = -1;
-static bool		connected = true;
-static bool		disconnect_sent = false;
-static bool		connect_lost = false;
-static bool		my_shutdown = false;
+static int		my_shutdown = FALSE;
 static pid_t		pgrp, mypid;
 
-extern bool		debug;
+extern int		debug;
 extern sys_config	Config;
 #ifdef HAVE_WIRINGPI_H
 extern int		lcdHandle;
@@ -72,54 +59,7 @@
 	default:	syslog(LOG_NOTICE, "die() on signal %d", onsig);
     }
 
-    my_shutdown = true;
-}
-
-
-
-void my_connect_callback(struct mosquitto *mosq, void *obj, int result)
-{
-    if (connect_lost) {
-	connect_lost = false;
-	syslog(LOG_NOTICE, "Reconnect: %s", mosquitto_connack_string(result));
-    }
-
-    if (!result) {
-	status = STATUS_CONNACK_RECVD;
-    } else {
-	syslog(LOG_NOTICE, "my_connect_callback: %s\n", mosquitto_connack_string(result));
-    }
-}
-
-
-
-void my_disconnect_callback(struct mosquitto *mosq, void *obj, int rc)
-{
-    if (my_shutdown) {
-	syslog(LOG_NOTICE, "Acknowledged DISCONNECT from %s", Config.mosq_host);
-	connected = false;
-    } else {
-	/*
-	 * The remove server was brought down. We must keep running
-	 */
-	syslog(LOG_NOTICE, "Received DISCONNECT from %s, connection lost", Config.mosq_host);
-	connect_lost = true;
-    }
-}
-
-
-
-void my_publish_callback(struct mosquitto *mosq, void *obj, int mid)
-{
-    last_mid_sent = mid;
-}
-
-
-
-void my_log_callback(struct mosquitto *mosq, void *obj, int level, const char *str)
-{
-    syslog(LOG_NOTICE, "MQTT: %s", str);
-    printf("MQTT: %s\n", str);
+    my_shutdown = TRUE;
 }
 
 
@@ -155,7 +95,7 @@
 	    break;
 
 	switch (c) {
-	    case 'd':	debug = true;
+	    case 'd':	debug = TRUE;
 			break;
 	    case 'h':	help();
 			return 1;
@@ -268,135 +208,33 @@
 
 int server(void)
 {
-    char                *id = NULL, *state = NULL;
-    struct mosquitto    *mosq = NULL;
     char                hostname[256], buf[1024];
-    int                 temp, rc, deviation, keepalive = 60;
+    int                 temp, rc = 0, deviation;
 #ifdef HAVE_WIRINGPI_H
     int			lcdupdate;
 #endif
-    unsigned int        max_inflight = 20;
-    char                err[1024];
     w1_therm		*tmp1, *old1;
     char		*device, *alias, line[60], *p = NULL;
     FILE		*fp;
 
     /*
-     * Initialize mosquitto communication
-     */
-    mosquitto_lib_init();
-
-    /*
      * Build MQTT id
      */
     hostname[0] = '\0';
     gethostname(hostname, 256);
     hostname[255] = '\0';
 
-    id = xstrcpy((char *)"thermometers/");
-    id = xstrcat(id, hostname);
-    if(strlen(id) > MOSQ_MQTT_ID_MAX_LENGTH) {
-	/*
-	 * Enforce maximum client id length of 23 characters
-	 */
-	id[MOSQ_MQTT_ID_MAX_LENGTH] = '\0';
-    }
-
-    mosq = mosquitto_new(id, true, NULL);
-    if(!mosq) {
-	switch(errno) {
-	    case ENOMEM:
-		syslog(LOG_NOTICE, "mosquitto_new: Out of memory");
-		break;
-	    case EINVAL:
-		syslog(LOG_NOTICE, "mosquitto_new: Invalid id");
-		break;
-	}
-	mosquitto_lib_cleanup();
-	return 1;
-    }
-
-    if (debug) {
-	mosquitto_log_callback_set(mosq, my_log_callback);
-    }
-
-    /*
-     * Set our will
-     */
-    state = xstrcpy((char *)"clients/");
-    state = xstrcat(state, hostname);
-    state = xstrcat(state, (char *)"/thermometers/state");
-    sprintf(buf, "0");
-    if ((rc = mosquitto_will_set(mosq, state, strlen(buf), buf, qos, true))) {
-        if (rc == MOSQ_ERR_INVAL) {
-            syslog(LOG_NOTICE, "mosquitto_will_set: input parameters invalid");
-        } else if (rc == MOSQ_ERR_NOMEM) {
-            syslog(LOG_NOTICE, "mosquitto_will_set: Out of Memory");
-	} else if (rc == MOSQ_ERR_PAYLOAD_SIZE) {
-	    syslog(LOG_NOTICE, "mosquitto_will_set: invalid payload size");
-        }
-        mosquitto_lib_cleanup();
-        return rc;
-    }
-
-    mosquitto_max_inflight_messages_set(mosq, max_inflight);
-    mosquitto_connect_callback_set(mosq, my_connect_callback);
-    mosquitto_disconnect_callback_set(mosq, my_disconnect_callback);
-    mosquitto_publish_callback_set(mosq, my_publish_callback);
-
-    if ((rc = mosquitto_connect(mosq, Config.mosq_host, Config.mosq_port, keepalive))) {
-	if (rc == MOSQ_ERR_ERRNO) {
-	    strerror_r(errno, err, 1024);
-	    syslog(LOG_NOTICE, "mosquitto_connect: error: %s", err);
-	} else {
-	    syslog(LOG_NOTICE, "mosquitto_connect: unable to connect (%d)", rc);
-	}
-	mosquitto_lib_cleanup();
-	return rc;
-    }
-    syslog(LOG_NOTICE, "Connected with %s:%d", Config.mosq_host, Config.mosq_port);
-
     /*
      * Initialise is complete, report our presence state
      */
-    mosquitto_loop_start(mosq);
-    sprintf(buf, "1");
-    rc = mosquitto_publish(mosq, &mid_sent, state, strlen(buf), buf, qos, 1);
 #ifdef HAVE_WIRINGPI_H
 //    setBacklight(0);
 #endif
 
-    /*
-     * Report alias names
-     */
-    for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) {
-	old1 = tmp1->next;
-
-	alias = xstrcpy((char *)"/raw/");
-	alias = xstrcat(alias, hostname);
-	alias = xstrcat(alias, (char *)"/thermometers/w1/");
-	alias = xstrcat(alias, tmp1->master);
-	alias = xstrcat(alias, (char *)"/");
-	alias = xstrcat(alias, tmp1->name);
-	alias = xstrcat(alias, (char *)"/alias");
-
-	sprintf(buf, "%s", tmp1->alias);
-	if ((rc = mosquitto_publish(mosq, &mid_sent, alias, strlen(buf), buf, qos, 1))) {
-	    if (rc == MOSQ_ERR_NO_CONN)
-		mosquitto_reconnect(mosq);
-	    else
-		syslog(LOG_NOTICE, "mainloop: error %d from mosquitto_publish", rc);
-	}
-
-	free(alias);
-	alias = NULL;
-    }
-
     if (debug)
-	fprintf(stdout, (char *)"Enter loop, connected %d\n", connected);
+	fprintf(stdout, (char *)"Enter loop\n");
 
     do {
-	if (status == STATUS_CONNACK_RECVD) {
 #ifdef HAVE_WIRINGPI_H
 	    lcdupdate = FALSE;
 #endif
@@ -459,12 +297,6 @@
 			    	 * Temperature is changed and valid, update and publish this.
 			    	 */
 			    	sprintf(buf, "%.1f", temp / 1000.0);
-			    	if ((rc = mosquitto_publish(mosq, &mid_sent, alias, strlen(buf), buf, qos, 1))) {
-			            if (rc == MOSQ_ERR_NO_CONN)
-				    	mosquitto_reconnect(mosq);
-			    	    else
-				    	syslog(LOG_NOTICE, "mainloop: error %d from mosquitto_publish", rc);
-			    	}
 			    } else {
 				syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, tmp1->lastval, temp);
 				if (debug) {
@@ -508,13 +340,6 @@
 #endif
 
 	    if (my_shutdown) {
-		/*
-		 * Final publish 0 to clients/<hostname>/thermometers/state
-		 */
-		sprintf(buf, "0");
-		mosquitto_publish(mosq, &mid_sent, state, strlen(buf), buf, qos, true);
-		last_mid = mid_sent;
-		status = STATUS_WAITING;
 #ifdef HAVE_WIRINGPI_H
 		lcdClear(lcdHandle);
 		lcdPosition(lcdHandle, 0, 0);
@@ -524,26 +349,11 @@
 
 	    usleep(100000);
 
-	} else if (status == STATUS_WAITING) {
-	    if (debug)
-	    	fprintf(stdout, (char *)"Waiting\n");
-	    if (last_mid_sent == last_mid && disconnect_sent == false) {
-		mosquitto_disconnect(mosq);
-		disconnect_sent = true;
-	    }
-	    usleep(100000);
-	}
-	rc = MOSQ_ERR_SUCCESS;
-
-    } while (rc == MOSQ_ERR_SUCCESS && connected);
+    } while (! my_shutdown);
 
     if (debug)
 	fprintf(stdout, (char *)"Out of loop\n");
 
-    mosquitto_loop_stop(mosq, false);
-    mosquitto_destroy(mosq);
-    mosquitto_lib_cleanup();
-
 #ifdef HAVE_WIRINGPI_H
     stopLCD();
 #endif

mercurial