Units now have an unique alias name so that the MQTT messages are more friendly.

Mon, 09 May 2016 21:35:55 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 09 May 2016 21:35:55 +0200
changeset 500
5aa914eb644e
parent 499
602d9968960f
child 501
52bb55ba0b01

Units now have an unique alias name so that the MQTT messages are more friendly.

thermferm/mqtt.c file | annotate | diff | comparison | revisions
thermferm/rdconfig.c file | annotate | diff | comparison | revisions
thermferm/server.c file | annotate | diff | comparison | revisions
thermferm/thermferm.c file | annotate | diff | comparison | revisions
thermferm/thermferm.h file | annotate | diff | comparison | revisions
--- a/thermferm/mqtt.c	Mon May 02 16:15:37 2016 +0200
+++ b/thermferm/mqtt.c	Mon May 09 21:35:55 2016 +0200
@@ -67,13 +67,13 @@
 void my_disconnect_callback(struct mosquitto *my_mosq, void *obj, int rc)
 {
     if (mqtt_my_shutdown) {
-       syslog(LOG_NOTICE, "MQTT: acknowledged DISCONNECT from %s", Config.mosq_host);
+       syslog(LOG_NOTICE, "MQTT: acknowledged DISCONNECT from %s", Config.mqtt_host);
        mqtt_connected = FALSE;
     } else {
        /*
         * The remote server was brought down. We must keep running
         */
-       syslog(LOG_NOTICE, "MQTT: received DISCONNECT from %s, connection lost", Config.mosq_host);
+       syslog(LOG_NOTICE, "MQTT: received DISCONNECT from %s, connection lost", Config.mqtt_host);
        mqtt_connect_lost = TRUE;
     }
 }
@@ -161,7 +161,7 @@
     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 = mosquitto_connect(mosq, Config.mqtt_host, Config.mqtt_port, keepalive))) {
         if (rc == MOSQ_ERR_ERRNO) {
             strerror_r(errno, err, 1024);
             syslog(LOG_NOTICE, "MQTT: mosquitto_connect: error: %s", err);
@@ -172,7 +172,7 @@
 	syslog(LOG_NOTICE, "MQTT: will run without an MQTT broker.");
     } else {
         mqtt_use = TRUE;
-        syslog(LOG_NOTICE, "MQTT: connected with %s:%d", Config.mosq_host, Config.mosq_port);
+        syslog(LOG_NOTICE, "MQTT: connected with %s:%d", Config.mqtt_host, Config.mqtt_port);
 
         /*
          * Initialise is complete, report our presence state
--- a/thermferm/rdconfig.c	Mon May 02 16:15:37 2016 +0200
+++ b/thermferm/rdconfig.c	Mon May 09 21:35:55 2016 +0200
@@ -65,10 +65,10 @@
     Config.temp_state = Config.hum_state = 1;	// missing
     Config.hum_value = 50000;
 #ifdef HAVE_MOSQUITTO_H
-    if (Config.mosq_host)
-	free(Config.mosq_host);
-    Config.mosq_host = NULL;
-    Config.mosq_port = 1883;
+    if (Config.mqtt_host)
+	free(Config.mqtt_host);
+    Config.mqtt_host = NULL;
+    Config.mqtt_port = 1883;
 #endif
  
     for (tmp2 = Config.units; tmp2; tmp2 = tmp2->next) {
@@ -244,6 +244,20 @@
 	syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
 	return 1;
     }
+    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "NEXT_UNIT", "%d", Config.next_unit)) < 0) {
+	syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+	return 1;
+    }
+#ifdef HAVE_MOSQUITTO_H
+    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MQTT_HOST", "%s", Config.mqtt_host)) < 0) {
+	syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+	return 1;
+    }
+    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MQTT_PORT", "%d", Config.mqtt_port)) < 0) {
+	syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+	return 1;
+    }
+#endif
 
     /* 
      * Start an element named "LCDS" as child of THERMFERM.
@@ -320,6 +334,10 @@
 		syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
 		return 1;
 	    }
+	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ALIAS", "%s", tmp3->alias)) < 0) {
+		syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+		return 1;
+	    }
 	    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "VOLUME", "%.1f", tmp3->volume)) < 0) {
 		syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
 		return 1;
@@ -1004,7 +1022,7 @@
 /*
  * Parse a fermenter unit
  */
-int parseUnit(xmlDocPtr doc, xmlNodePtr cur)
+int parseUnit(xmlDocPtr doc, xmlNodePtr cur, int number)
 {
     xmlChar     *key;
     int         i, ival;
@@ -1014,7 +1032,7 @@
     unit = (units_list *)malloc(sizeof(units_list));
     unit->next = NULL;
     unit->version = 1;
-    unit->uuid = unit->name = unit->air_address = unit->beer_address = unit->heater_address = \
+    unit->uuid = unit->name = unit->alias = unit->air_address = unit->beer_address = unit->heater_address = \
 		 unit->cooler_address = unit->fan_address = unit->door_address = \
 		 unit->light_address = unit->psu_address = unit->profile = NULL;
     unit->volume = unit->prof_peak_abs = unit->prof_peak_rel = 0.0;
@@ -1052,6 +1070,9 @@
 	if ((!xmlStrcmp(cur->name, (const xmlChar *)"NAME"))) {
 	    unit->name = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
 	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"ALIAS"))) {
+	    unit->alias = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	}
 	if ((!xmlStrcmp(cur->name, (const xmlChar *)"VOLUME"))) {
 	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
 	    if (sscanf((const char *)key, "%f", &val) == 1)
@@ -1392,6 +1413,15 @@
 	cur = cur->next;
     }
 
+    /*
+     * If there is no alias name, create it.
+     */
+    if (unit->alias == NULL) {
+	char	an[128];
+	sprintf(an, "unit%d", number);
+	unit->alias = xstrcpy(an);
+    }
+
     if (Config.units == NULL) {
 	Config.units = unit;
     } else {
@@ -1413,7 +1443,8 @@
     cur = cur->xmlChildrenNode;
     while (cur != NULL) {
         if ((!xmlStrcmp(cur->name, (const xmlChar *)"UNIT"))) {
-            parseUnit(doc, cur);
+            parseUnit(doc, cur, Config.next_unit);
+	    Config.next_unit++;
         }
         cur = cur->next;
     }
@@ -1983,9 +2014,10 @@
     syslog(LOG_NOTICE, "rdconfig: using %s", mypath);
 
 #ifdef HAVE_MOSQUITTO_H
-    Config.mosq_host = xstrcpy((char *)"localhost");
-    Config.mosq_port = 1883;
+    Config.mqtt_host = xstrcpy((char *)"localhost");
+    Config.mqtt_port = 1883;
 #endif
+    Config.next_unit = 1;
 
     if ((cur = xmlDocGetRootElement(doc)) == NULL) {
 	syslog(LOG_NOTICE, "XML file %s empty.", mypath);
@@ -2032,6 +2064,23 @@
 	if ((!xmlStrcmp(cur->name, (const xmlChar *)"HUM_ADDRESS"))) {
 	    Config.hum_address = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
 	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"NEXT_UNIT"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%d", &ival) == 1)
+		Config.next_unit = ival;
+	    xmlFree(key);
+	}
+#ifdef HAVE_MOSQUITTO_H
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"MQTT_HOST"))) {
+	    Config.mqtt_host = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"MQTT_PORT"))) {
+	    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	    if (sscanf((const char *)key, "%d", &ival) == 1)
+		Config.mqtt_port = ival;
+	    xmlFree(key);
+	}
+#endif
 	if ((!xmlStrcmp(cur->name, (const xmlChar *)"LCDS"))) {
 	    parseLCDs(doc, cur);
 	}
--- a/thermferm/server.c	Mon May 02 16:15:37 2016 +0200
+++ b/thermferm/server.c	Mon May 09 21:35:55 2016 +0200
@@ -1005,6 +1005,11 @@
 	srv_send((char *)"HUM_VALUE,%.0f", Config.hum_value / 1000.0);
 	srv_send((char *)"LCD_COLS,%d", Config.lcd_cols);
 	srv_send((char *)"LCD_ROWS,%d", Config.lcd_rows);
+	srv_send((char *)"NEXT_UNIT,%d", Config.next_unit);
+#ifdef HAVE_MOSQUITTO_H
+	srv_send((char *)"MQTT_HOST,%s", Config.mqtt_host);
+	srv_send((char *)"MQTT_PORT,%d", Config.mqtt_port);
+#endif
 	srv_send((char *)".");
 	return 0;
     }
@@ -1984,6 +1989,10 @@
     }
 
     if (strcmp(opt, (char *)"ADD") == 0) {
+	char an[128];
+
+	sprintf(an, "unit%d", Config.next_unit);
+	Config.next_unit++;
 	unit = (units_list *)malloc(sizeof(units_list));
 	unit->next = NULL;
 	unit->version = 1;
@@ -1991,6 +2000,7 @@
 	uuid_generate(uu);
 	uuid_unparse(uu, unit->uuid);
 	unit->name = xstrcpy(param);
+	unit->alias = xstrcpy(an);
 	unit->air_address = unit->beer_address = unit->heater_address = unit->cooler_address = \
 			    unit->fan_address = unit->door_address = unit->light_address = \
 			    unit->psu_address = unit->profile = NULL;
@@ -2072,6 +2082,7 @@
 		srv_send((char *)"213 Unit listing follows:");
 		srv_send((char *)"NAME,%s", unit->name);
 		srv_send((char *)"UUID,%s", unit->uuid);
+		srv_send((char *)"ALIAS,%s", unit->alias);
 		srv_send((char *)"VOLUME,%2f", unit->volume);
 		srv_send((char *)"AIR_ADDRESS,%s", unit->air_address);
 		srv_send((char *)"AIR_STATE,%s", TEMPSTATE[unit->air_state]);
--- a/thermferm/thermferm.c	Mon May 02 16:15:37 2016 +0200
+++ b/thermferm/thermferm.c	Mon May 09 21:35:55 2016 +0200
@@ -1068,15 +1068,15 @@
 	unit->mqtt_flag = MQTT_FLAG_MODE;
 	unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = unit->light_state = 0;
 	unit->heater_wait = unit->cooler_wait = unit->fan_wait = unit->light_wait = 0;
-	mqtt_publish_int(unit->uuid, (char *)"state", (unit->mode != UNITMODE_OFF) ? 1 : 0);
+	mqtt_publish_int(unit->alias, (char *)"state", (unit->mode != UNITMODE_OFF) ? 1 : 0);
 	if (unit->name)
-	    mqtt_publish_str(unit->uuid, (char *)"name", unit->name);
+	    mqtt_publish_str(unit->alias, (char *)"name", unit->name);
 	if (unit->heater_address)
-	    mqtt_publish_int(unit->uuid, (char *)"heater", 0);
+	    mqtt_publish_int(unit->alias, (char *)"heater", 0);
 	if (unit->cooler_address)
-	    mqtt_publish_int(unit->uuid, (char *)"cooler", 0);
+	    mqtt_publish_int(unit->alias, (char *)"cooler", 0);
 	if (unit->fan_address)
-	    mqtt_publish_int(unit->uuid, (char *)"fan", 0);
+	    mqtt_publish_int(unit->alias, (char *)"fan", 0);
 	if (unit->air_address)
 	    unit->mqtt_flag |= MQTT_FLAG_AIR;
 	if (unit->beer_address)
@@ -1290,13 +1290,13 @@
 			    if (unit->door_state == 0) {
 			    	syslog(LOG_NOTICE, "Unit `%s' door closed", unit->name);
 			    	unit->door_state = 1;
-				mqtt_publish_str(unit->uuid, (char *)"door", (char *)"closed");
+				mqtt_publish_str(unit->alias, (char *)"door", (char *)"closed");
 			    }
 			} else {
 			    if (unit->door_state) {
 			    	syslog(LOG_NOTICE, "Unit `%s' door opened", unit->name);
 			    	unit->door_state = 0;
-				mqtt_publish_str(unit->uuid, (char *)"door", (char *)"open");
+				mqtt_publish_str(unit->alias, (char *)"door", (char *)"open");
 			    }
 			}
 		    }
@@ -1313,13 +1313,13 @@
 			    if (unit->psu_state == 0) {
 				syslog(LOG_NOTICE, "Unit `%s' PSU (12 volt) is on", unit->name);
 				unit->psu_state = 1;
-				mqtt_publish_str(unit->uuid, (char *)"12volt", (char *)"on");
+				mqtt_publish_str(unit->alias, (char *)"12volt", (char *)"on");
 			    }
 			} else {
 			    if (unit->psu_state) {
 				syslog(LOG_NOTICE, "Unit `%s' PSU (12 volt) is off", unit->name);
 				unit->psu_state = 0;
-				mqtt_publish_str(unit->uuid, (char *)"12volt", (char *)"off");
+				mqtt_publish_str(unit->alias, (char *)"12volt", (char *)"off");
 			    }
 			}
 		    }
@@ -1653,7 +1653,7 @@
 				    syslog(LOG_NOTICE, "Unit `%s' heater %d%% => %d%%", unit->name, unit->heater_state, power);
 				    unit->heater_state = power;
 				    if (unit->heater_address)
-					mqtt_publish_int(unit->uuid, (char *)"heater", unit->heater_state);
+					mqtt_publish_int(unit->alias, (char *)"heater", unit->heater_state);
 				}
 			    }
 			} else {
@@ -1664,7 +1664,7 @@
 				    syslog(LOG_NOTICE, "Unit `%s' heater On => Off", unit->name);
 				    unit->heater_state = 0;
 				    if (unit->heater_address)
-					mqtt_publish_int(unit->uuid, (char *)"heater", 0);
+					mqtt_publish_int(unit->alias, (char *)"heater", 0);
 				}
 			    }
 			}
@@ -1684,7 +1684,7 @@
 				    syslog(LOG_NOTICE, "Unit `%s' cooler %d%% => %d%%", unit->name, unit->cooler_state, power);
 				    unit->cooler_state = power;
 				    if (unit->cooler_address)
-					mqtt_publish_int(unit->uuid, (char *)"cooler", unit->cooler_state);
+					mqtt_publish_int(unit->alias, (char *)"cooler", unit->cooler_state);
 				}
 			    }
 			} else {
@@ -1695,7 +1695,7 @@
 				    syslog(LOG_NOTICE, "Unit `%s' cooler On => Off", unit->name);
 				    unit->cooler_state = 0;
 				    if (unit->cooler_address)
-					mqtt_publish_int(unit->uuid, (char *)"cooler", 0);
+					mqtt_publish_int(unit->alias, (char *)"cooler", 0);
 				}
 			    }
 			}
@@ -1721,7 +1721,7 @@
 				    syslog(LOG_NOTICE, "Unit `%s' Fan Off => On", unit->name);
 				    unit->fan_state = 100;
 				    if (unit->fan_address)
-					mqtt_publish_int(unit->uuid, (char *)"fan", 100);
+					mqtt_publish_int(unit->alias, (char *)"fan", 100);
 			    	}
 			    }
 			} else {
@@ -1732,7 +1732,7 @@
 				    syslog(LOG_NOTICE, "Unit `%s' Fan On => Off", unit->name);
 			    	    unit->fan_state = 0;
 				    if (unit->fan_address)
-					mqtt_publish_int(unit->uuid, (char *)"fan", 0);
+					mqtt_publish_int(unit->alias, (char *)"fan", 0);
 				}
 			    }
 			}
@@ -1793,24 +1793,24 @@
 		 * Publish MQTT messages set in flag
 		 */
 		if (unit->mqtt_flag & MQTT_FLAG_SP) {
-		    mqtt_publish_float(unit->uuid, (char *)"setpoint/high", LCDspH, 1);
-		    mqtt_publish_float(unit->uuid, (char *)"setpoint/low", LCDspL, 1);
+		    mqtt_publish_float(unit->alias, (char *)"setpoint/high", LCDspH, 1);
+		    mqtt_publish_float(unit->alias, (char *)"setpoint/low", LCDspL, 1);
 		}
 		if (unit->mqtt_flag & MQTT_FLAG_AIR) {
-		    mqtt_publish_float(unit->uuid, (char *)"air/temperature", unit->air_temperature / 1000.0, 3);
+		    mqtt_publish_float(unit->alias, (char *)"air/temperature", unit->air_temperature / 1000.0, 3);
 		}
 		if (unit->mqtt_flag & MQTT_FLAG_BEER) {
-		    mqtt_publish_float(unit->uuid, (char *)"beer/temperature", unit->beer_temperature / 1000.0, 3);
+		    mqtt_publish_float(unit->alias, (char *)"beer/temperature", unit->beer_temperature / 1000.0, 3);
 		}
 		if (unit->mqtt_flag & MQTT_FLAG_MODE) {
-		    mqtt_publish_str(unit->uuid, (char *)"mode", (char *)UNITMODE[unit->mode]);
+		    mqtt_publish_str(unit->alias, (char *)"mode", (char *)UNITMODE[unit->mode]);
 		}
 		if (unit->mqtt_flag & MQTT_FLAG_PROFILE) {
-		    mqtt_publish_str(unit->uuid, (char *)"profile/uuid", unit->profile);
-		    mqtt_publish_str(unit->uuid, (char *)"profile/state", (char *)PROFSTATE[unit->prof_state]);
+		    mqtt_publish_str(unit->alias, (char *)"profile/uuid", unit->profile);
+		    mqtt_publish_str(unit->alias, (char *)"profile/state", (char *)PROFSTATE[unit->prof_state]);
 		}
 		if (unit->mqtt_flag & MQTT_FLAG_PERCENT) {
-		    mqtt_publish_int(unit->uuid, (char *)"profile/percent", unit->prof_percent);
+		    mqtt_publish_int(unit->alias, (char *)"profile/percent", unit->prof_percent);
 		}
 		unit->mqtt_flag = 0;
 	    } /* for units */
@@ -1928,12 +1928,12 @@
 
         if (unit->mode != UNITMODE_OFF) {
 	    if (unit->heater_address)
-		mqtt_publish_int(unit->uuid, (char *)"heater", 0);
+		mqtt_publish_int(unit->alias, (char *)"heater", 0);
 	    if (unit->cooler_address)
-		mqtt_publish_int(unit->uuid, (char *)"cooler", 0);
+		mqtt_publish_int(unit->alias, (char *)"cooler", 0);
 	    if (unit->fan_address)
-		mqtt_publish_int(unit->uuid, (char *)"fan", 0);
-	    mqtt_publish_int(unit->uuid, (char *)"state", 0);
+		mqtt_publish_int(unit->alias, (char *)"fan", 0);
+	    mqtt_publish_int(unit->alias, (char *)"state", 0);
 	}
 
 	/*
--- a/thermferm/thermferm.h	Mon May 02 16:15:37 2016 +0200
+++ b/thermferm/thermferm.h	Mon May 09 21:35:55 2016 +0200
@@ -131,6 +131,7 @@
     int			version;		/* Record version		*/
     char		*uuid;			/* uid code			*/
     char		*name;			/* friendly name		*/
+    char		*alias;			/* alias name 'unit1'		*/
     float		volume;			/* Volume of this unit		*/
     char		*air_address;		/* DS18B20 address		*/
     int			air_state;		/* 0=ok, 1=missing, 2=error	*/
@@ -331,6 +332,7 @@
     int			lcd_cols;		/* LCD display columns		*/
     int			lcd_rows;		/* LCD display rows		*/
     int			lcd_address;		/* LCD display i2c address	*/
+    int			next_unit;		/* Next unit alias name		*/
     units_list		*units;			/* Fermenter units		*/
     profiles_list	*profiles;		/* Ferment profiles		*/
     devices_list	*devices;		/* Sensors and switches		*/
@@ -338,8 +340,8 @@
     simulator_list	*simulators;		/* Simulators			*/
 #endif
 #ifdef HAVE_MOSQUITTO_H
-    char		*mosq_host;		/* MQTT host to connect to	*/
-    int			mosq_port;		/* MQTT port to connect to	*/
+    char		*mqtt_host;		/* MQTT host to connect to	*/
+    int			mqtt_port;		/* MQTT port to connect to	*/
 #endif
 } sys_config;
 

mercurial