thermferm/server.c

changeset 534
92b546d4a839
parent 533
49580ca85ab7
child 536
e833bbd5e733
--- 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;
+
 		    }
 		}
 	    }

mercurial