From 0d18e841412d8768beeccb7e20799e17ab32d8dc Mon Sep 17 00:00:00 2001 From: proddy Date: Thu, 13 Aug 2020 17:50:48 +0200 Subject: [PATCH] minor changes --- src/mqtt.cpp | 29 ++++++++++++++--------------- src/mqtt.h | 4 ++-- src/system.h | 2 -- src/test/test.cpp | 3 --- 4 files changed, 16 insertions(+), 22 deletions(-) diff --git a/src/mqtt.cpp b/src/mqtt.cpp index acef4ff51..11a703603 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -78,6 +78,8 @@ void Mqtt::add_command(const uint8_t device_type, const uint8_t device_id, const Mqtt::subscribe(device_type, cmd_topic, nullptr); // use an empty function handler to signal this is a command function } + LOG_DEBUG(F("Registering MQTT cmd %s with topic %s"), uuid::read_flash_string(cmd).c_str(), EMSdevice::device_type_topic_name(device_type).c_str()); + mqtt_cmdfunctions_.emplace_back(device_type, device_id, cmd, cb); } @@ -135,11 +137,16 @@ void Mqtt::show_mqtt(uuid::console::Shell & shell) { // show subscriptions shell.printfln(F("MQTT subscriptions:")); for (const auto & mqtt_subfunction : mqtt_subfunctions_) { - shell.printf(F(" topic: %s, [cmd]:"), mqtt_subfunction.full_topic_.c_str()); - // show the commands associated with this subscription - for (const auto & mqtt_cmdfunction : mqtt_cmdfunctions_) { - if (EMSdevice::device_type_topic_name(mqtt_cmdfunction.device_type_) == mqtt_subfunction.topic_) { - shell.printf(F(" %s"), uuid::read_flash_string(mqtt_cmdfunction.cmd_).c_str()); + // don't show commands if its homeassistant + if ((strncmp(mqtt_subfunction.full_topic_.c_str(), "homeassistant/", 13) == 0)) { + shell.printf(F(" topic: %s"), mqtt_subfunction.full_topic_.c_str()); + } else { + // show the commands associated with this subscription + shell.printf(F(" topic: %s, [cmd]:"), mqtt_subfunction.full_topic_.c_str()); + for (const auto & mqtt_cmdfunction : mqtt_cmdfunctions_) { + if (EMSdevice::device_type_topic_name(mqtt_cmdfunction.device_type_) == mqtt_subfunction.topic_) { + shell.printf(F(" %s"), uuid::read_flash_string(mqtt_cmdfunction.cmd_).c_str()); + } } } shell.println(); @@ -365,11 +372,6 @@ void Mqtt::start() { on_publish(packetId); }); -#if defined(EMSESP_STANDALONE) - // simulate an MQTT connection - on_connect(); -#endif - // create space for command buffer, to avoid heap memory fragmentation mqtt_cmdfunctions_.reserve(40); // current count with boiler+thermostat is 37 mqtt_subfunctions_.reserve(10); @@ -400,12 +402,9 @@ void Mqtt::on_connect() { resubscribe(); // in case this is a reconnect, re-subscribe again to all MQTT topics - // add the system MQTT subscriptions, only if its a fresh start with no previous subscriptions // these commands respond to the topic "system_cmd" and take a payload like {cmd:"", data:"", id:""} - if (mqtt_subfunctions_.empty()) { - add_command(EMSdevice::DeviceType::SERVICEKEY, bus_id_, F("pin"), System::mqtt_command_pin); - add_command(EMSdevice::DeviceType::SERVICEKEY, bus_id_, F("send"), System::mqtt_command_send); - } + add_command(EMSdevice::DeviceType::SERVICEKEY, bus_id_, F("pin"), System::mqtt_command_pin); + add_command(EMSdevice::DeviceType::SERVICEKEY, bus_id_, F("send"), System::mqtt_command_send); LOG_INFO(F("MQTT connected")); } diff --git a/src/mqtt.h b/src/mqtt.h index 6c700137c..8c9e36faf 100644 --- a/src/mqtt.h +++ b/src/mqtt.h @@ -171,8 +171,8 @@ class Mqtt { // function handlers for MQTT subscriptions struct MQTTSubFunction { - uint8_t device_type_; // which device type, from DeviceType:: - const std::string topic_; + uint8_t device_type_; // which device type, from DeviceType:: + const std::string topic_; // short topic name const std::string full_topic_; // the fully qualified topic name, usually with the hostname prefixed mqtt_subfunction_p mqtt_subfunction_; // can be empty diff --git a/src/system.h b/src/system.h index 65ed7fd98..9cb62f9ab 100644 --- a/src/system.h +++ b/src/system.h @@ -35,8 +35,6 @@ using uuid::console::Shell; -#define MQTT_SYSTEM_CMD "system_cmd" - namespace emsesp { class System { diff --git a/src/test/test.cpp b/src/test/test.cpp index e6a92e59d..6b951ba93 100644 --- a/src/test/test.cpp +++ b/src/test/test.cpp @@ -537,9 +537,6 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & command) { if (command == "mqtt") { shell.printfln(F("Testing MQTT...")); - // simulate an on connect - Mqtt::on_connect(); - // add a boiler // question: do we need to set the mask? std::string version("1.2.3");