Skip to content

Commit

Permalink
Feature: Added config option to change MQTT CleanSession Flag
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed Sep 1, 2023
1 parent b95236c commit c5f9f46
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions include/defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
2 changes: 2 additions & 0 deletions src/MqttSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ void MqttSettingsClass::performConnect()
}
static_cast<espMqttClientSecure*>(mqttClient)->setWill(willTopic.c_str(), 2, config.Mqtt_Retain, config.Mqtt_LwtValue_Offline);
static_cast<espMqttClientSecure*>(mqttClient)->setClientId(clientId.c_str());
static_cast<espMqttClientSecure*>(mqttClient)->setCleanSession(config.Mqtt_CleanSession);
static_cast<espMqttClientSecure*>(mqttClient)->onConnect(std::bind(&MqttSettingsClass::onMqttConnect, this, _1));
static_cast<espMqttClientSecure*>(mqttClient)->onDisconnect(std::bind(&MqttSettingsClass::onMqttDisconnect, this, _1));
static_cast<espMqttClientSecure*>(mqttClient)->onMessage(std::bind(&MqttSettingsClass::onMqttMessage, this, _1, _2, _3, _4, _5, _6));
Expand All @@ -135,6 +136,7 @@ void MqttSettingsClass::performConnect()
static_cast<espMqttClient*>(mqttClient)->setCredentials(config.Mqtt_Username, config.Mqtt_Password);
static_cast<espMqttClient*>(mqttClient)->setWill(willTopic.c_str(), 2, config.Mqtt_Retain, config.Mqtt_LwtValue_Offline);
static_cast<espMqttClient*>(mqttClient)->setClientId(clientId.c_str());
static_cast<espMqttClient*>(mqttClient)->setCleanSession(config.Mqtt_CleanSession);
static_cast<espMqttClient*>(mqttClient)->onConnect(std::bind(&MqttSettingsClass::onMqttConnect, this, _1));
static_cast<espMqttClient*>(mqttClient)->onDisconnect(std::bind(&MqttSettingsClass::onMqttDisconnect, this, _1));
static_cast<espMqttClient*>(mqttClient)->onMessage(std::bind(&MqttSettingsClass::onMqttMessage, this, _1, _2, _3, _4, _5, _6));
Expand Down
4 changes: 4 additions & 0 deletions src/WebApi_mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -313,6 +316,7 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request)
strlcpy(config.Mqtt_LwtValue_Online, root["mqtt_lwt_online"].as<String>().c_str(), sizeof(config.Mqtt_LwtValue_Online));
strlcpy(config.Mqtt_LwtValue_Offline, root["mqtt_lwt_offline"].as<String>().c_str(), sizeof(config.Mqtt_LwtValue_Offline));
config.Mqtt_PublishInterval = root["mqtt_publish_interval"].as<uint32_t>();
config.Mqtt_CleanSession = root["mqtt_clean_session"].as<bool>();
config.Mqtt_Hass_Enabled = root["mqtt_hass_enabled"].as<bool>();
config.Mqtt_Hass_Expire = root["mqtt_hass_expire"].as<bool>();
config.Mqtt_Hass_Retain = root["mqtt_hass_retain"].as<bool>();
Expand Down
2 changes: 2 additions & 0 deletions webapp/src/locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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):",
Expand Down
2 changes: 2 additions & 0 deletions webapp/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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):",
Expand Down
2 changes: 2 additions & 0 deletions webapp/src/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)",
Expand Down
1 change: 1 addition & 0 deletions webapp/src/types/MqttConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions webapp/src/types/MqttStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions webapp/src/views/MqttAdminView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
type="number" min="5" max="86400"
:postfix="$t('mqttadmin.Seconds')"/>

<InputElement :label="$t('mqttadmin.CleanSession')"
v-model="mqttConfigList.mqtt_clean_session"
type="checkbox"/>

<InputElement :label="$t('mqttadmin.EnableRetain')"
v-model="mqttConfigList.mqtt_retain"
type="checkbox"/>
Expand Down
6 changes: 6 additions & 0 deletions webapp/src/views/MqttInfoView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
<th>{{ $t('mqttinfo.PublishInterval') }}</th>
<td>{{ $t('mqttinfo.Seconds', { sec: mqttDataList.mqtt_publish_interval }) }}</td>
</tr>
<tr>
<th>{{ $t('mqttinfo.CleanSession') }}</th>
<td>
<StatusBadge :status="mqttDataList.mqtt_clean_session" true_text="mqttinfo.Enabled" false_text="mqttinfo.Disabled" />
</td>
</tr>
<tr>
<th>{{ $t('mqttinfo.Retain') }}</th>
<td>
Expand Down

0 comments on commit c5f9f46

Please sign in to comment.