From c5f9f460cd01017b8ee5239ae70f732a7f29de5b Mon Sep 17 00:00:00 2001 From: Thomas Basler Date: Fri, 1 Sep 2023 18:03:30 +0200 Subject: [PATCH] Feature: Added config option to change MQTT CleanSession Flag --- include/Configuration.h | 1 + include/defaults.h | 1 + src/Configuration.cpp | 2 ++ src/MqttSettings.cpp | 2 ++ src/WebApi_mqtt.cpp | 4 ++++ webapp/src/locales/de.json | 2 ++ webapp/src/locales/en.json | 2 ++ webapp/src/locales/fr.json | 2 ++ webapp/src/types/MqttConfig.ts | 1 + webapp/src/types/MqttStatus.ts | 1 + webapp/src/views/MqttAdminView.vue | 4 ++++ webapp/src/views/MqttInfoView.vue | 6 ++++++ 12 files changed, 28 insertions(+) diff --git a/include/Configuration.h b/include/Configuration.h index 425aed548..c233eb5e0 100644 --- a/include/Configuration.h +++ b/include/Configuration.h @@ -81,6 +81,7 @@ struct CONFIG_T { char Mqtt_LwtValue_Online[MQTT_MAX_LWTVALUE_STRLEN + 1]; char Mqtt_LwtValue_Offline[MQTT_MAX_LWTVALUE_STRLEN + 1]; uint32_t Mqtt_PublishInterval; + bool Mqtt_CleanSession; bool Mqtt_Hass_Enabled; bool Mqtt_Hass_Retain; diff --git a/include/defaults.h b/include/defaults.h index 1df7d774b..e702c4ad8 100644 --- a/include/defaults.h +++ b/include/defaults.h @@ -74,6 +74,7 @@ #define MQTT_LWT_ONLINE "online" #define MQTT_LWT_OFFLINE "offline" #define MQTT_PUBLISH_INTERVAL 5U +#define MQTT_CLEAN_SESSION true #define DTU_SERIAL 0x99978563412U #define DTU_POLL_INTERVAL 5U diff --git a/src/Configuration.cpp b/src/Configuration.cpp index 8ebb2fabc..fdd1e1b66 100644 --- a/src/Configuration.cpp +++ b/src/Configuration.cpp @@ -58,6 +58,7 @@ bool ConfigurationClass::write() mqtt["topic"] = config.Mqtt_Topic; mqtt["retain"] = config.Mqtt_Retain; mqtt["publish_interval"] = config.Mqtt_PublishInterval; + mqtt["clean_session"] = config.Mqtt_CleanSession; JsonObject mqtt_lwt = mqtt.createNestedObject("lwt"); mqtt_lwt["topic"] = config.Mqtt_LwtTopic; @@ -204,6 +205,7 @@ bool ConfigurationClass::read() strlcpy(config.Mqtt_Topic, mqtt["topic"] | MQTT_TOPIC, sizeof(config.Mqtt_Topic)); config.Mqtt_Retain = mqtt["retain"] | MQTT_RETAIN; config.Mqtt_PublishInterval = mqtt["publish_interval"] | MQTT_PUBLISH_INTERVAL; + config.Mqtt_CleanSession = mqtt["clean_session"] | MQTT_CLEAN_SESSION; JsonObject mqtt_lwt = mqtt["lwt"]; strlcpy(config.Mqtt_LwtTopic, mqtt_lwt["topic"] | MQTT_LWT_TOPIC, sizeof(config.Mqtt_LwtTopic)); diff --git a/src/MqttSettings.cpp b/src/MqttSettings.cpp index 81624455b..6275bb8f1 100644 --- a/src/MqttSettings.cpp +++ b/src/MqttSettings.cpp @@ -127,6 +127,7 @@ void MqttSettingsClass::performConnect() } static_cast(mqttClient)->setWill(willTopic.c_str(), 2, config.Mqtt_Retain, config.Mqtt_LwtValue_Offline); static_cast(mqttClient)->setClientId(clientId.c_str()); + static_cast(mqttClient)->setCleanSession(config.Mqtt_CleanSession); static_cast(mqttClient)->onConnect(std::bind(&MqttSettingsClass::onMqttConnect, this, _1)); static_cast(mqttClient)->onDisconnect(std::bind(&MqttSettingsClass::onMqttDisconnect, this, _1)); static_cast(mqttClient)->onMessage(std::bind(&MqttSettingsClass::onMqttMessage, this, _1, _2, _3, _4, _5, _6)); @@ -135,6 +136,7 @@ void MqttSettingsClass::performConnect() static_cast(mqttClient)->setCredentials(config.Mqtt_Username, config.Mqtt_Password); static_cast(mqttClient)->setWill(willTopic.c_str(), 2, config.Mqtt_Retain, config.Mqtt_LwtValue_Offline); static_cast(mqttClient)->setClientId(clientId.c_str()); + static_cast(mqttClient)->setCleanSession(config.Mqtt_CleanSession); static_cast(mqttClient)->onConnect(std::bind(&MqttSettingsClass::onMqttConnect, this, _1)); static_cast(mqttClient)->onDisconnect(std::bind(&MqttSettingsClass::onMqttDisconnect, this, _1)); static_cast(mqttClient)->onMessage(std::bind(&MqttSettingsClass::onMqttMessage, this, _1, _2, _3, _4, _5, _6)); diff --git a/src/WebApi_mqtt.cpp b/src/WebApi_mqtt.cpp index 2ab426d18..aedcd51a9 100644 --- a/src/WebApi_mqtt.cpp +++ b/src/WebApi_mqtt.cpp @@ -49,6 +49,7 @@ void WebApiMqttClass::onMqttStatus(AsyncWebServerRequest* request) root["mqtt_client_cert_info"] = getTlsCertInfo(config.Mqtt_ClientCert); root["mqtt_lwt_topic"] = String(config.Mqtt_Topic) + config.Mqtt_LwtTopic; root["mqtt_publish_interval"] = config.Mqtt_PublishInterval; + root["mqtt_clean_session"] = config.Mqtt_CleanSession; root["mqtt_hass_enabled"] = config.Mqtt_Hass_Enabled; root["mqtt_hass_expire"] = config.Mqtt_Hass_Expire; root["mqtt_hass_retain"] = config.Mqtt_Hass_Retain; @@ -85,6 +86,7 @@ void WebApiMqttClass::onMqttAdminGet(AsyncWebServerRequest* request) root["mqtt_lwt_online"] = config.Mqtt_LwtValue_Online; root["mqtt_lwt_offline"] = config.Mqtt_LwtValue_Offline; root["mqtt_publish_interval"] = config.Mqtt_PublishInterval; + root["mqtt_clean_session"] = config.Mqtt_CleanSession; root["mqtt_hass_enabled"] = config.Mqtt_Hass_Enabled; root["mqtt_hass_expire"] = config.Mqtt_Hass_Expire; root["mqtt_hass_retain"] = config.Mqtt_Hass_Retain; @@ -149,6 +151,7 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request) && root.containsKey("mqtt_lwt_online") && root.containsKey("mqtt_lwt_offline") && root.containsKey("mqtt_publish_interval") + && root.containsKey("mqtt_clean_session") && root.containsKey("mqtt_hass_enabled") && root.containsKey("mqtt_hass_expire") && root.containsKey("mqtt_hass_retain") @@ -313,6 +316,7 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request) strlcpy(config.Mqtt_LwtValue_Online, root["mqtt_lwt_online"].as().c_str(), sizeof(config.Mqtt_LwtValue_Online)); strlcpy(config.Mqtt_LwtValue_Offline, root["mqtt_lwt_offline"].as().c_str(), sizeof(config.Mqtt_LwtValue_Offline)); config.Mqtt_PublishInterval = root["mqtt_publish_interval"].as(); + config.Mqtt_CleanSession = root["mqtt_clean_session"].as(); config.Mqtt_Hass_Enabled = root["mqtt_hass_enabled"].as(); config.Mqtt_Hass_Expire = root["mqtt_hass_expire"].as(); config.Mqtt_Hass_Retain = root["mqtt_hass_retain"].as(); diff --git a/webapp/src/locales/de.json b/webapp/src/locales/de.json index 849415f13..0e7e9ba59 100644 --- a/webapp/src/locales/de.json +++ b/webapp/src/locales/de.json @@ -265,6 +265,7 @@ "BaseTopic": "Basis Topic", "PublishInterval": "Veröffentlichungsintervall", "Seconds": "{sec} Sekunden", + "CleanSession": "CleanSession Flag", "Retain": "Retain", "Tls": "TLS", "RootCertifcateInfo": "Root CA-Zertifikat-Informationen", @@ -412,6 +413,7 @@ "BaseTopicHint": "Basis-Topic, wird allen veröffentlichten Themen vorangestellt (z.B. inverter/)", "PublishInterval": "Veröffentlichungsintervall:", "Seconds": "Sekunden", + "CleanSession": "CleanSession Flag aktivieren", "EnableRetain": "Retain Flag aktivieren", "EnableTls": "TLS aktivieren", "RootCa": "CA-Root-Zertifikat (Standard Letsencrypt):", diff --git a/webapp/src/locales/en.json b/webapp/src/locales/en.json index 6a5c856ed..314f57ff9 100644 --- a/webapp/src/locales/en.json +++ b/webapp/src/locales/en.json @@ -265,6 +265,7 @@ "BaseTopic": "Base Topic", "PublishInterval": "Publish Interval", "Seconds": "{sec} seconds", + "CleanSession": "CleanSession flag", "Retain": "Retain", "Tls": "TLS", "RootCertifcateInfo": "Root CA Certifcate Info", @@ -412,6 +413,7 @@ "BaseTopicHint": "Base topic, will be prepend to all published topics (e.g. inverter/)", "PublishInterval": "Publish Interval:", "Seconds": "seconds", + "CleanSession": "Enable CleanSession flag", "EnableRetain": "Enable Retain Flag", "EnableTls": "Enable TLS", "RootCa": "CA-Root-Certificate (default Letsencrypt):", diff --git a/webapp/src/locales/fr.json b/webapp/src/locales/fr.json index 348de6185..cfd2abd33 100644 --- a/webapp/src/locales/fr.json +++ b/webapp/src/locales/fr.json @@ -265,6 +265,7 @@ "BaseTopic": "Sujet de base", "PublishInterval": "Intervalle de publication", "Seconds": "{sec} secondes", + "CleanSession": "CleanSession Flag", "Retain": "Conserver", "Tls": "TLS", "RootCertifcateInfo": "Informations sur le certificat de l'autorité de certification racine", @@ -412,6 +413,7 @@ "BaseTopicHint": "Sujet de base, qui sera ajouté en préambule à tous les sujets publiés (par exemple, inverter/).", "PublishInterval": "Intervalle de publication", "Seconds": "secondes", + "CleanSession": "Enable CleanSession flag", "EnableRetain": "Activation du maintien", "EnableTls": "Activer le TLS", "RootCa": "Certificat CA-Root (par défaut Letsencrypt)", diff --git a/webapp/src/types/MqttConfig.ts b/webapp/src/types/MqttConfig.ts index dc6280ed9..f2d847b8b 100644 --- a/webapp/src/types/MqttConfig.ts +++ b/webapp/src/types/MqttConfig.ts @@ -6,6 +6,7 @@ export interface MqttConfig { mqtt_password: string; mqtt_topic: string; mqtt_publish_interval: number; + mqtt_clean_session: boolean; mqtt_retain: boolean; mqtt_tls: boolean; mqtt_root_ca_cert: string; diff --git a/webapp/src/types/MqttStatus.ts b/webapp/src/types/MqttStatus.ts index 839d4854c..5312ae55c 100644 --- a/webapp/src/types/MqttStatus.ts +++ b/webapp/src/types/MqttStatus.ts @@ -5,6 +5,7 @@ export interface MqttStatus { mqtt_username: string; mqtt_topic: string; mqtt_publish_interval: number; + mqtt_clean_session: boolean; mqtt_retain: boolean; mqtt_tls: boolean; mqtt_root_ca_cert_info: string; diff --git a/webapp/src/views/MqttAdminView.vue b/webapp/src/views/MqttAdminView.vue index 6cd5ef24e..d8c03b7f3 100644 --- a/webapp/src/views/MqttAdminView.vue +++ b/webapp/src/views/MqttAdminView.vue @@ -48,6 +48,10 @@ type="number" min="5" max="86400" :postfix="$t('mqttadmin.Seconds')"/> + + diff --git a/webapp/src/views/MqttInfoView.vue b/webapp/src/views/MqttInfoView.vue index 5f21713c9..abb2ef3a3 100644 --- a/webapp/src/views/MqttInfoView.vue +++ b/webapp/src/views/MqttInfoView.vue @@ -30,6 +30,12 @@ {{ $t('mqttinfo.PublishInterval') }} {{ $t('mqttinfo.Seconds', { sec: mqttDataList.mqtt_publish_interval }) }} + + {{ $t('mqttinfo.CleanSession') }} + + + + {{ $t('mqttinfo.Retain') }}