Implemented public getPrefix method to get the mqtt topic prefix

This commit is contained in:
Thomas Basler 2022-07-18 19:08:38 +02:00
parent 3d1a56a61c
commit bb52d24c25
2 changed files with 9 additions and 2 deletions

View File

@ -15,6 +15,8 @@ public:
bool getConnected();
void publish(String subtopic, String payload);
String getPrefix();
private:
void WiFiEvent(WiFiEvent_t event);

View File

@ -51,7 +51,7 @@ void MqttSettingsClass::performConnect()
mqttClient.setServer(config.Mqtt_Hostname, config.Mqtt_Port);
mqttClient.setCredentials(config.Mqtt_Username, config.Mqtt_Password);
willTopic = String(config.Mqtt_Topic) + config.Mqtt_LwtTopic;
willTopic = getPrefix() + config.Mqtt_LwtTopic;
mqttClient.setWill(willTopic.c_str(), 2, config.Mqtt_Retain, config.Mqtt_LwtValue_Offline);
clientId = WiFiSettings.getApName();
@ -81,9 +81,14 @@ bool MqttSettingsClass::getConnected()
return mqttClient.connected();
}
String MqttSettingsClass::getPrefix()
{
return Configuration.get().Mqtt_Topic;
}
void MqttSettingsClass::publish(String subtopic, String payload)
{
String topic = Configuration.get().Mqtt_Topic;
String topic = getPrefix();
topic += subtopic;
mqttClient.publish(topic.c_str(), 0, Configuration.get().Mqtt_Retain, payload.c_str());
}