Versie 0.6.4. Aan de global setup de MQTT instellingen toegevoegd. Die zijn er ook als MQTT niet op het systeem aanwezig is. Indien het de global setup wijzigd, wordt de MQTT verbinding opnieuw gemaakt.

Sat, 14 Jul 2018 21:15:20 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 14 Jul 2018 21:15:20 +0200
changeset 534
92b546d4a839
parent 533
49580ca85ab7
child 535
3c4c6d5e4b8d

Versie 0.6.4. Aan de global setup de MQTT instellingen toegevoegd. Die zijn er ook als MQTT niet op het systeem aanwezig is. Indien het de global setup wijzigd, wordt de MQTT verbinding opnieuw gemaakt.

configure file | annotate | diff | comparison | revisions
configure.ac file | annotate | diff | comparison | revisions
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.h file | annotate | diff | comparison | revisions
www-thermferm/global.php file | annotate | diff | comparison | revisions
--- a/configure	Sat Jul 14 17:21:25 2018 +0200
+++ b/configure	Sat Jul 14 21:15:20 2018 +0200
@@ -2035,7 +2035,7 @@
 
 
 PACKAGE="mbsePi-apps"
-VERSION="0.6.3"
+VERSION="0.6.4"
 COPYRIGHT="Copyright (C) 2014-2018 Michiel Broek, All Rights Reserved"
 CYEARS="2014-2018"
 
--- a/configure.ac	Sat Jul 14 17:21:25 2018 +0200
+++ b/configure.ac	Sat Jul 14 21:15:20 2018 +0200
@@ -8,7 +8,7 @@
 dnl General settings
 dnl After changeing the version number, run autoconf!
 PACKAGE="mbsePi-apps"
-VERSION="0.6.3"
+VERSION="0.6.4"
 COPYRIGHT="Copyright (C) 2014-2018 Michiel Broek, All Rights Reserved"
 CYEARS="2014-2018"
 AC_SUBST(PACKAGE)
--- a/thermferm/mqtt.c	Sat Jul 14 17:21:25 2018 +0200
+++ b/thermferm/mqtt.c	Sat Jul 14 21:15:20 2018 +0200
@@ -718,6 +718,14 @@
         mosquitto_destroy(mosq);
         mosquitto_lib_cleanup();
 	mqtt_use = FALSE;
+	mqtt_status = STATUS_CONNECTING;
+	mqtt_mid_sent = 0;
+	mqtt_last_mid = -1;
+	mqtt_last_mid_sent = -1;
+	mqtt_connected = TRUE;
+	mqtt_disconnect_sent = FALSE;
+	mqtt_connect_lost = FALSE;
+	mqtt_my_shutdown = FALSE;
 	syslog(LOG_NOTICE, "MQTT disconnected");
     }
 #endif
--- a/thermferm/rdconfig.c	Sat Jul 14 17:21:25 2018 +0200
+++ b/thermferm/rdconfig.c	Sat Jul 14 21:15:20 2018 +0200
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * Copyright (C) 2014-2017
+ * Copyright (C) 2014-2018
  *   
  * Michiel Broek <mbroek at mbse dot eu>
  *
@@ -65,12 +65,16 @@
     Config.temp_value = 20000;
     Config.temp_state = Config.hum_state = 1;	// missing
     Config.hum_value = 50000;
-#ifdef HAVE_MOSQUITTO_H
     if (Config.mqtt_host)
 	free(Config.mqtt_host);
     Config.mqtt_host = NULL;
     Config.mqtt_port = 1883;
-#endif
+    if (Config.mqtt_username)
+	free(Config.mqtt_username);
+    Config.mqtt_username = NULL;
+    if (Config.mqtt_password)
+	free(Config.mqtt_password);
+    Config.mqtt_password = NULL;
  
     for (tmp2 = Config.units; tmp2; tmp2 = tmp2->next) {
 	if (tmp2->uuid)
@@ -255,7 +259,6 @@
 	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;
@@ -264,7 +267,16 @@
 	syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
 	return 1;
     }
-#endif
+    if (Config.mqtt_username && Config.mqtt_password) {
+    	if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MQTT_USER", "%s", Config.mqtt_username)) < 0) {
+	    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+	    return 1;
+	}
+    	if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MQTT_PASS", "%s", Config.mqtt_password)) < 0) {
+	    syslog(LOG_NOTICE, "wrconfig: error at xmlTextWriterWriteFormatElement");
+	    return 1;
+	}
+    }
 
     /* 
      * Start an element named "LCDS" as child of THERMFERM.
@@ -2131,10 +2143,8 @@
     }
     syslog(LOG_NOTICE, "rdconfig: using %s", mypath);
 
-#ifdef HAVE_MOSQUITTO_H
     Config.mqtt_host = xstrcpy((char *)"localhost");
     Config.mqtt_port = 1883;
-#endif
     Config.next_unit = 1;
 
     if ((cur = xmlDocGetRootElement(doc)) == NULL) {
@@ -2194,7 +2204,6 @@
 		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);
 	}
@@ -2204,7 +2213,12 @@
 		Config.mqtt_port = ival;
 	    xmlFree(key);
 	}
-#endif
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"MQTT_USER"))) {
+	    Config.mqtt_username = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	}
+	if ((!xmlStrcmp(cur->name, (const xmlChar *)"MQTT_PASS"))) {
+	    Config.mqtt_password = (char *)xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
+	}
 	if ((!xmlStrcmp(cur->name, (const xmlChar *)"LCDS"))) {
 	    parseLCDs(doc, cur);
 	}
--- a/thermferm/server.c	Sat Jul 14 17:21:25 2018 +0200
+++ b/thermferm/server.c	Sat Jul 14 21:15:20 2018 +0200
@@ -1007,15 +1007,16 @@
 	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 *)"MQTT_USER,%s", Config.mqtt_username);
+	srv_send((char *)"MQTT_PASS,%s", Config.mqtt_password);
 	srv_send((char *)".");
 	return 0;
     }
 
     if (strcmp(opt, (char *)"PUT") == 0) {
+	bool mqtt_reconnect = false;
 	while (1) {
 	    rlen = srv_recv(ibuf);
 	    if (rlen == -1) {
@@ -1024,6 +1025,8 @@
 	    if (strlen(ibuf)) {
 		if (strcmp(ibuf, (char *)".") == 0) {
 		    srv_send((char *)"219 Accepted Global record");
+		    if (mqtt_reconnect)
+			mqtt_connect();
 		    return 1;
 		}
 		kwd = strtok(ibuf, ",\0");
@@ -1099,6 +1102,53 @@
 				syslog(LOG_NOTICE, "Global LCD rows %d to %d", Config.lcd_rows, ival);
 			    Config.lcd_rows = ival;
 			}
+		
+		    } else if (strcmp(kwd, (char *)"MQTT_HOST") == 0) {
+			if (val && Config.mqtt_host && (strcmp(val, Config.mqtt_host)))
+			    syslog(LOG_NOTICE, "Global MQTT host `%s' to `%s'", Config.mqtt_host, val);
+			mqtt_disconnect();
+			mqtt_reconnect = true;
+			if (Config.mqtt_host)
+			    free(Config.mqtt_host);
+			if (val)
+			    Config.mqtt_host = xstrcpy(val);
+			else
+			    Config.mqtt_host = NULL;
+
+		    } else if (val && (strcmp(kwd, (char *)"MQTT_PORT") == 0)) {
+			if (sscanf(val, "%d", &ival) == 1) {
+			    if (Config.mqtt_port != ival) {
+				syslog(LOG_NOTICE, "Global MQTT port %d to %d", Config.mqtt_port, ival);
+				mqtt_disconnect();
+				mqtt_reconnect = true;
+			        Config.mqtt_port = ival;
+			    }
+			}
+
+		    } else if (strcmp(kwd, (char *)"MQTT_USER") == 0) {
+			if (val && Config.mqtt_username && (strcmp(val, Config.mqtt_username)))
+			    syslog(LOG_NOTICE, "Global MQTT username `%s' to `%s'", Config.mqtt_username, val);
+			mqtt_disconnect();
+			mqtt_reconnect = true;
+			if (Config.mqtt_username)
+			    free(Config.mqtt_username);
+			if (val)
+			    Config.mqtt_username = xstrcpy(val);
+			else
+			    Config.mqtt_username = NULL;
+
+		    } else if (strcmp(kwd, (char *)"MQTT_PASS") == 0) {
+			if (val && Config.mqtt_password && (strcmp(val, Config.mqtt_password)))
+			    syslog(LOG_NOTICE, "Global MQTT password `%s' to `%s'", Config.mqtt_password, val);
+			mqtt_disconnect();
+			mqtt_reconnect = true;
+			if (Config.mqtt_password)
+			    free(Config.mqtt_password);
+			if (val)
+			    Config.mqtt_password = xstrcpy(val);
+			else
+			    Config.mqtt_password = NULL;
+
 		    }
 		}
 	    }
--- a/thermferm/thermferm.h	Sat Jul 14 17:21:25 2018 +0200
+++ b/thermferm/thermferm.h	Sat Jul 14 21:15:20 2018 +0200
@@ -362,10 +362,10 @@
 #ifdef USE_SIMULATOR
     simulator_list	*simulators;		/* Simulators			*/
 #endif
-#ifdef HAVE_MOSQUITTO_H
     char		*mqtt_host;		/* MQTT host to connect to	*/
     int			mqtt_port;		/* MQTT port to connect to	*/
-#endif
+    char		*mqtt_username;		/* MQTT username		*/
+    char		*mqtt_password;		/* MQTT password		*/
 } sys_config;
 
 
--- a/www-thermferm/global.php	Sat Jul 14 17:21:25 2018 +0200
+++ b/www-thermferm/global.php	Sat Jul 14 21:15:20 2018 +0200
@@ -1,6 +1,6 @@
 <?php
 /*****************************************************************************
- * Copyright (C) 2014-2017
+ * Copyright (C) 2014-2018
  *   
  * Michiel Broek <mbroek at mbse dot eu>
  *
@@ -52,7 +52,11 @@
 	if (isset($_POST['LCDcols']))
 	    $cmd[] = "LCD_COLS,".$_POST['LCDcols'];
 	if (isset($_POST['LCDrows']))
-	    $cmd[] = "LCD_ROWS,".$_POST['LCDrows'];
+		$cmd[] = "LCD_ROWS,".$_POST['LCDrows'];
+	$cmd[] = "MQTT_HOST,".$_POST['MQTThost'];
+	$cmd[] = "MQTT_POST,".$_POST['MQTTport'];
+	$cmd[] = "MQTT_USER,".$_POST['MQTTuser'];
+	$cmd[] = "MQTT_PASS,".$_POST['MQTTpass'];
 	$cmd[] = ".";
 	send_array($cmd);
     }
@@ -64,6 +68,10 @@
     unset($_POST['TempHumIdx']);
     unset($_POST['LCDcols']);
     unset($_POST['LCDrows']);
+    unset($_POST['MQTThost']);
+    unset($_POST['MQTTport']);
+    unset($_POST['MQTTuser']);
+    unset($_POST['MQTTpass']);
     unset($_POST['key']);
     load('maintenance.php');
 }
@@ -74,7 +82,9 @@
 
     if (isset($_POST['Name']) && isset($_POST['Port']) &&
 	isset($_POST['TempAddress']) && isset($_POST['HumAddress']) &&
-	isset($_POST['TempHumIdx']) && isset($_POST['key'])) {
+	isset($_POST['TempHumIdx']) && isset($_POST['key']) &&
+	isset($_POST['MQTThost']) && isset($_POST['MQTTport']) &&
+	isset($_POST['MQTTuser']) && isset($_POST['MQTTpass'])) {
 
 	if ($_POST['key'] == 'Cancel')
 	    return 99;
@@ -207,7 +217,7 @@
 	    }
 	    if ($f[0] == "TEMP_HUM_IDX") {
 		$outstr .= '       <tr class="editor">'.PHP_EOL;
-		$outstr .= '        <td class="editname">Temp/Humidity domoticz index</td>'.PHP_EOL;
+		$outstr .= '        <td class="editname">Domoticz TH index</td>'.PHP_EOL;
 		$outstr .= '        <td class="editfield"><input type="text" name="TempHumIdx" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
 		$outstr .= '       </tr>'.PHP_EOL;
 	    }
@@ -223,6 +233,36 @@
 		$outstr .= '        <td class="editfield"><input type="text" name="LCDrows" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
 		$outstr .= '       </tr>'.PHP_EOL;
 	    }
+	    if ($f[0] == "MQTT_HOST") {
+		$outstr .= '       <tr class="editor">'.PHP_EOL;
+		$outstr .= '        <td class="editname">MQTT host</td>'.PHP_EOL;
+		$outstr .= '        <td class="editfield"><input type="text" name="MQTThost" size="50" value="'.$f[1].'"></td>'.PHP_EOL;
+		$outstr .= '       </tr>'.PHP_EOL;
+	    }
+	    if ($f[0] == "MQTT_PORT") {
+		$outstr .= '       <tr class="editor">'.PHP_EOL;
+		$outstr .= '        <td class="editname">MQTT port</td>'.PHP_EOL;
+		$outstr .= '        <td class="editfield"><input type="text" name="MQTTport" size="5" value="'.$f[1].'"></td>'.PHP_EOL;
+		$outstr .= '       </tr>'.PHP_EOL;
+	    }
+	    if ($f[0] == "MQTT_USER") {
+		$outstr .= '       <tr class="editor">'.PHP_EOL;
+		$outstr .= '        <td class="editname">MQTT username</td>'.PHP_EOL;
+		if (strcmp($f[1], "(null)") == 0) {
+		    $f[1] = "";
+		}
+		$outstr .= '        <td class="editfield"><input type="text" name="MQTTuser" size="50" value="'.$f[1].'"></td>'.PHP_EOL;
+		$outstr .= '       </tr>'.PHP_EOL;
+	    }
+	    if ($f[0] == "MQTT_PASS") {
+		$outstr .= '       <tr class="editor">'.PHP_EOL;
+		$outstr .= '        <td class="editname">MQTT password</td>'.PHP_EOL;
+		if (strcmp($f[1], "(null)") == 0) {
+		    $f[1] = "";
+		}
+		$outstr .= '        <td class="editfield"><input type="text" name="MQTTpass" size="50" value="'.$f[1].'"></td>'.PHP_EOL;
+		$outstr .= '       </tr>'.PHP_EOL;
+	    }
 	    $i++;
 	}
     }

mercurial