Feature: Allow configuration of LWT QoS

This commit is contained in:
Thomas Basler 2023-11-19 16:17:10 +01:00
parent b85c53f476
commit ee4811bbe7
11 changed files with 52 additions and 1 deletions

View File

@ -98,6 +98,7 @@ struct CONFIG_T {
char Topic[MQTT_MAX_TOPIC_STRLEN + 1];
char Value_Online[MQTT_MAX_LWTVALUE_STRLEN + 1];
char Value_Offline[MQTT_MAX_LWTVALUE_STRLEN + 1];
uint8_t Qos;
} Lwt;
struct {

View File

@ -56,6 +56,7 @@ enum WebApiError {
MqttPublishInterval,
MqttHassTopicLength,
MqttHassTopicCharacter,
MqttLwtQos,
NetworkBase = 8000,
NetworkIpInvalid,

View File

@ -75,6 +75,7 @@
#define MQTT_LWT_TOPIC "dtu/status"
#define MQTT_LWT_ONLINE "online"
#define MQTT_LWT_OFFLINE "offline"
#define MQTT_LWT_QOS 2U
#define MQTT_PUBLISH_INTERVAL 5U
#define MQTT_CLEAN_SESSION true

View File

@ -67,6 +67,7 @@ bool ConfigurationClass::write()
mqtt_lwt["topic"] = config.Mqtt.Lwt.Topic;
mqtt_lwt["value_online"] = config.Mqtt.Lwt.Value_Online;
mqtt_lwt["value_offline"] = config.Mqtt.Lwt.Value_Offline;
mqtt_lwt["qos"] = config.Mqtt.Lwt.Qos;
JsonObject mqtt_tls = mqtt.createNestedObject("tls");
mqtt_tls["enabled"] = config.Mqtt.Tls.Enabled;
@ -220,6 +221,7 @@ bool ConfigurationClass::read()
strlcpy(config.Mqtt.Lwt.Topic, mqtt_lwt["topic"] | MQTT_LWT_TOPIC, sizeof(config.Mqtt.Lwt.Topic));
strlcpy(config.Mqtt.Lwt.Value_Online, mqtt_lwt["value_online"] | MQTT_LWT_ONLINE, sizeof(config.Mqtt.Lwt.Value_Online));
strlcpy(config.Mqtt.Lwt.Value_Offline, mqtt_lwt["value_offline"] | MQTT_LWT_OFFLINE, sizeof(config.Mqtt.Lwt.Value_Offline));
config.Mqtt.Lwt.Qos = mqtt_lwt["qos"] | MQTT_LWT_QOS;
JsonObject mqtt_tls = mqtt["tls"];
config.Mqtt.Tls.Enabled = mqtt_tls["enabled"] | MQTT_TLS;

View File

@ -134,7 +134,7 @@ void MqttSettingsClass::performConnect()
} else {
static_cast<espMqttClient*>(mqttClient)->setServer(config.Mqtt.Hostname, config.Mqtt.Port);
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.Lwt.Value_Offline);
static_cast<espMqttClient*>(mqttClient)->setWill(willTopic.c_str(), config.Mqtt.Lwt.Qos, config.Mqtt.Retain, config.Mqtt.Lwt.Value_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));

View File

@ -85,6 +85,7 @@ void WebApiMqttClass::onMqttAdminGet(AsyncWebServerRequest* request)
root["mqtt_lwt_topic"] = config.Mqtt.Lwt.Topic;
root["mqtt_lwt_online"] = config.Mqtt.CleanSession;
root["mqtt_lwt_offline"] = config.Mqtt.Lwt.Value_Offline;
root["mqtt_lwt_qos"] = config.Mqtt.Lwt.Qos;
root["mqtt_publish_interval"] = config.Mqtt.PublishInterval;
root["mqtt_clean_session"] = config.Mqtt.CleanSession;
root["mqtt_hass_enabled"] = config.Mqtt.Hass.Enabled;
@ -150,6 +151,7 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request)
&& root.containsKey("mqtt_lwt_topic")
&& root.containsKey("mqtt_lwt_online")
&& root.containsKey("mqtt_lwt_offline")
&& root.containsKey("mqtt_lwt_qos")
&& root.containsKey("mqtt_publish_interval")
&& root.containsKey("mqtt_clean_session")
&& root.containsKey("mqtt_hass_enabled")
@ -269,6 +271,15 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request)
return;
}
if (root["mqtt_lwt_qos"].as<uint8_t>() > 2) {
retMsg["message"] = "LWT QoS must not be greater than " STR(2) "!";
retMsg["code"] = WebApiError::MqttLwtQos;
retMsg["param"]["max"] = 2;
response->setLength();
request->send(response);
return;
}
if (root["mqtt_publish_interval"].as<uint32_t>() < 5 || root["mqtt_publish_interval"].as<uint32_t>() > 65535) {
retMsg["message"] = "Publish interval must be a number between 5 and 65535!";
retMsg["code"] = WebApiError::MqttPublishInterval;
@ -315,6 +326,7 @@ void WebApiMqttClass::onMqttAdminPost(AsyncWebServerRequest* request)
strlcpy(config.Mqtt.Lwt.Topic, root["mqtt_lwt_topic"].as<String>().c_str(), sizeof(config.Mqtt.Lwt.Topic));
strlcpy(config.Mqtt.Lwt.Value_Online, root["mqtt_lwt_online"].as<String>().c_str(), sizeof(config.Mqtt.Lwt.Value_Online));
strlcpy(config.Mqtt.Lwt.Value_Offline, root["mqtt_lwt_offline"].as<String>().c_str(), sizeof(config.Mqtt.Lwt.Value_Offline));
config.Mqtt.Lwt.Qos = root["mqtt_lwt_qos"].as<uint8_t>();
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>();

View File

@ -73,6 +73,7 @@
"7013": "Veröffentlichungsintervall muss zwischen {min} und {max} sein!",
"7014": "Hass-Topic darf nicht länger als {max} Zeichen sein!",
"7015": "Hass-Topic darf keine Leerzeichen enthalten!",
"7016": "LWT QOS darf icht größer als {max} sein!",
"8001": "IP-Adresse ist ungültig!",
"8002": "Netzmaske ist ungültig!",
"8003": "Standardgateway ist ungültig!",
@ -437,6 +438,10 @@
"LwtOnlineHint": "Nachricht, die im LWT-Topic veröffentlicht wird, wenn OpenDTU online ist",
"LwtOffline": "LWT-Offline-Nachricht:",
"LwtOfflineHint": "Nachricht, die im LWT-Topic veröffentlicht wird, wenn OpenDTU offline ist",
"LwtQos": "QoS (Quality of Service):",
"QOS0": "0 (Höchstens einmal)",
"QOS1": "1 (Mindestens einmal)",
"QOS2": "2 (Exakt einmal)",
"HassParameters": "Home Assistant MQTT-Auto-Discovery-Parameter",
"HassPrefixTopic": "Präfix Topic:",
"HassPrefixTopicHint": "The prefix for the discovery topic",

View File

@ -73,6 +73,7 @@
"7013": "Publish interval must be a number between {min} and {max}!",
"7014": "Hass topic must not longer then {max} characters!",
"7015": "Hass topic must not contain space characters!",
"7016": "LWT QOS must not greater then {max}!",
"8001": "IP address is invalid!",
"8002": "Netmask is invalid!",
"8003": "Gateway is invalid!",
@ -437,6 +438,10 @@
"LwtOnlineHint": "Message that will be published to LWT topic when online",
"LwtOffline": "LWT Offline message:",
"LwtOfflineHint": "Message that will be published to LWT topic when offline",
"LwtQos": "QoS (Quality of Service):",
"QOS0": "0 (At most once)",
"QOS1": "1 (At least once)",
"QOS2": "2 (Exactly once)",
"HassParameters": "Home Assistant MQTT Auto Discovery Parameters",
"HassPrefixTopic": "Prefix Topic:",
"HassPrefixTopicHint": "The prefix for the discovery topic",

View File

@ -73,6 +73,7 @@
"7013": "L'intervalle de publication doit être un nombre compris entre {min} et {max} !",
"7014": "Le sujet Hass ne doit pas dépasser {max} caractères !",
"7015": "Le sujet Hass ne doit pas contenir d'espace !",
"7016": "LWT QOS ne doit pas être supérieur à {max}!",
"8001": "L'adresse IP n'est pas valide !",
"8002": "Le masque de réseau n'est pas valide !",
"8003": "La passerelle n'est pas valide !",
@ -437,6 +438,10 @@
"LwtOnlineHint": "Message qui sera publié sur le sujet LWT lorsqu'il sera en ligne",
"LwtOffline": "Message hors ligne de LWT",
"LwtOfflineHint": "Message qui sera publié sur le sujet LWT lorsqu'il sera hors ligne",
"LwtQos": "QoS (Quality of Service):",
"QOS0": "0 (Au maximum une fois)",
"QOS1": "1 (Au moins une fois)",
"QOS2": "2 (Exactement une fois)",
"HassParameters": "Paramètres de découverte automatique MQTT de Home Assistant",
"HassPrefixTopic": "Préfixe du sujet",
"HassPrefixTopicHint": "Le préfixe de découverte du sujet",

View File

@ -16,6 +16,7 @@ export interface MqttConfig {
mqtt_lwt_topic: string;
mqtt_lwt_online: string;
mqtt_lwt_offline: string;
mqtt_lwt_qos: number;
mqtt_hass_enabled: boolean;
mqtt_hass_expire: boolean;
mqtt_hass_retain: boolean;

View File

@ -98,6 +98,19 @@
v-model="mqttConfigList.mqtt_lwt_offline"
type="text" maxlength="20"
:placeholder="$t('mqttadmin.LwtOfflineHint')"/>
<div class="row mb-3">
<label class="col-sm-2 col-form-label">
{{ $t('mqttadmin.LwtQos') }}
</label>
<div class="col-sm-10">
<select class="form-select" v-model="mqttConfigList.mqtt_lwt_qos">
<option v-for="qostype in qosTypeList" :key="qostype.key" :value="qostype.key">
{{ $t(`mqttadmin.` + qostype.value) }}
</option>
</select>
</div>
</div>
</CardElement>
<CardElement :text="$t('mqttadmin.HassParameters')" textVariant="text-bg-primary" add-space
@ -149,6 +162,11 @@ export default defineComponent({
alertMessage: "",
alertType: "info",
showAlert: false,
qosTypeList: [
{ key: 0, value: 'QOS0' },
{ key: 1, value: 'QOS1' },
{ key: 2, value: 'QOS2' },
],
};
},
created() {