From 40e7e1b418353fbda2068a78a03b539c1d0f5180 Mon Sep 17 00:00:00 2001 From: MichaelDvP <59284019+MichaelDvP@users.noreply.github.com> Date: Wed, 30 Jun 2021 13:13:18 +0200 Subject: [PATCH] always register subscriptions (#79) --- src/mqtt.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/mqtt.cpp b/src/mqtt.cpp index df8687fb5..49800ab16 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -57,9 +57,6 @@ uuid::log::Logger Mqtt::logger_{F_(mqtt), uuid::log::Facility::DAEMON}; // subscribe to an MQTT topic, and store the associated callback function // only if it already hasn't been added void Mqtt::subscribe(const uint8_t device_type, const std::string & topic, mqtt_subfunction_p cb) { - if (!enabled()) { - return; - } // check if we already have the topic subscribed, if so don't add it again if (!mqtt_subfunctions_.empty()) { @@ -74,18 +71,18 @@ void Mqtt::subscribe(const uint8_t device_type, const std::string & topic, mqtt_ } } - LOG_DEBUG(F("Subscribing MQTT topic %s for device type %s"), topic.c_str(), EMSdevice::device_type_2_device_name(device_type).c_str()); - - // add to MQTT queue as a subscribe operation - auto message = queue_subscribe_message(topic); - - if (message == nullptr) { - return; - } - // register in our libary with the callback function. // We store the original topic without base mqtt_subfunctions_.emplace_back(device_type, std::move(topic), std::move(cb)); + + if (!enabled()) { + return; + } + + LOG_DEBUG(F("Subscribing MQTT topic %s for device type %s"), topic.c_str(), EMSdevice::device_type_2_device_name(device_type).c_str()); + + // add to MQTT queue as a subscribe operation + queue_subscribe_message(topic); } // subscribe to the command topic if it doesn't exist yet @@ -107,6 +104,10 @@ void Mqtt::register_command(const uint8_t device_type, const __FlashStringHelper LOG_DEBUG(F("Registering MQTT cmd %s with topic %s"), uuid::read_flash_string(cmd).c_str(), EMSdevice::device_type_2_device_name(device_type).c_str()); } + if (!enabled()) { + return; + } + // register the individual commands too (e.g. ems-esp/boiler/wwonetime) // https://github.com/emsesp/EMS-ESP32/issues/31 std::string topic(MQTT_TOPIC_MAX_SIZE, '\0');