From 557ba659d56615b3134c9ebb0aacd8f107951e60 Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Fri, 22 Dec 2023 19:47:00 +0100 Subject: [PATCH] Make sure MQTT config is updated for current connection if changed --- lib/AmsMqttHandler/include/AmsMqttHandler.h | 1 + lib/AmsMqttHandler/src/AmsMqttHandler.cpp | 13 +++++++++++++ src/AmsToMqttBridge.cpp | 20 ++++++++++++++++---- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/AmsMqttHandler/include/AmsMqttHandler.h b/lib/AmsMqttHandler/include/AmsMqttHandler.h index 0c9a87fd..811c15ca 100644 --- a/lib/AmsMqttHandler/include/AmsMqttHandler.h +++ b/lib/AmsMqttHandler/include/AmsMqttHandler.h @@ -23,6 +23,7 @@ class AmsMqttHandler { }; void setCaVerification(bool); + void setConfig(MqttConfig& mqttConfig); bool connect(); void disconnect(); diff --git a/lib/AmsMqttHandler/src/AmsMqttHandler.cpp b/lib/AmsMqttHandler/src/AmsMqttHandler.cpp index c489aa5f..4126fd4f 100644 --- a/lib/AmsMqttHandler/src/AmsMqttHandler.cpp +++ b/lib/AmsMqttHandler/src/AmsMqttHandler.cpp @@ -7,6 +7,10 @@ void AmsMqttHandler::setCaVerification(bool caVerification) { this->caVerification = caVerification; } +void AmsMqttHandler::setConfig(MqttConfig& mqttConfig) { + this->mqttConfig = mqttConfig; +} + bool AmsMqttHandler::connect() { if(millis() - lastMqttRetry < 10000) { yield(); @@ -93,10 +97,19 @@ bool AmsMqttHandler::connect() { if(debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("CA verification disabled\n")); mqttSecureClient->setInsecure(); } + if(mqttClient != NULL) { + mqttClient->stop(); + delete mqttClient; + } mqttClient = mqttSecureClient; if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("MQTT SSL setup complete (%dkb free heap)\n"), ESP.getFreeHeap()); } + } else if(mqttSecureClient != NULL) { + mqttSecureClient->stop(); + delete mqttSecureClient; + mqttSecureClient = NULL; + mqttClient = NULL; } if(mqttClient == NULL) { diff --git a/src/AmsToMqttBridge.cpp b/src/AmsToMqttBridge.cpp index 432908d3..449c90da 100644 --- a/src/AmsToMqttBridge.cpp +++ b/src/AmsToMqttBridge.cpp @@ -557,8 +557,15 @@ void loop() { if (mqttEnabled || config.isMqttChanged()) { if(mqttHandler == NULL || !mqttHandler->connected() || config.isMqttChanged()) { + if(mqttHandler != NULL && config.isMqttChanged()) { + MqttConfig mqttConfig; + if(config.getMqttConfig(mqttConfig)) { + mqttHandler->disconnect(); + mqttHandler->setConfig(mqttConfig); + config.ackMqttChange(); + } + } MQTT_connect(); - config.ackMqttChange(); } } else if(mqttHandler != NULL) { mqttHandler->disconnect(); @@ -1859,9 +1866,14 @@ void MQTT_connect() { mqttEnabled = true; ws.setMqttEnabled(true); - if(mqttHandler != NULL && mqttHandler->getFormat() != mqttConfig.payloadFormat) { - delete mqttHandler; - mqttHandler = NULL; + if(mqttHandler != NULL) { + mqttHandler->disconnect(); + if(mqttHandler->getFormat() != mqttConfig.payloadFormat) { + delete mqttHandler; + mqttHandler = NULL; + } else if(config.isMqttChanged()) { + mqttHandler->setConfig(mqttConfig); + } } if(mqttHandler == NULL) {