From 531f7e05a746ee90b5476f4c4ae428ce8037ce81 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Wed, 14 Feb 2024 02:01:39 +0700 Subject: [PATCH] fix IoT Callback issue when the topic is base-1 --- ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp | 13 +++++++++---- ESPMegaPRO-OS-SDK/src/main.cpp | 12 +++++++----- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp index 9362e10..b6f2a8f 100644 --- a/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp +++ b/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/ESPMegaIoT.cpp @@ -41,16 +41,21 @@ void ESPMegaIoT::mqttCallback(char *topic, byte *payload, unsigned int length) // Create a null terminated string from the payload memcpy(payload_buffer, payload, length); payload_buffer[length] = '\0'; + // If the topic is not appended with the base topic, call only the absolute callbacks + if (strncmp(topic, this->mqtt_config.base_topic, base_topic_length) != 0) + { + for (const auto &callback : mqtt_callbacks) + { + callback.second(topic, payload_buffer); + } + return; + } // Remove the base topic from the topic char *topic_without_base = topic + strlen(this->mqtt_config.base_topic) + 1; for (const auto &callback : mqtt_relative_callbacks) { callback.second(topic_without_base, payload_buffer); } - for (const auto &callback : mqtt_callbacks) - { - callback.second(topic, payload_buffer); - } // Call the respective card's mqtt callback // Note that after the base topic, there should be the card id // /base_topic/card_id/... diff --git a/ESPMegaPRO-OS-SDK/src/main.cpp b/ESPMegaPRO-OS-SDK/src/main.cpp index 957ec89..c154233 100644 --- a/ESPMegaPRO-OS-SDK/src/main.cpp +++ b/ESPMegaPRO-OS-SDK/src/main.cpp @@ -9,9 +9,9 @@ // #define FRAM_DEBUG // #define MQTT_DEBUG -#define WRITE_DEFAULT_NETCONF -#define CLIMATE_CARD_ENABLE -#define MQTT_CARD_REGISTER +// #define WRITE_DEFAULT_NETCONF +//#define CLIMATE_CARD_ENABLE +//#define MQTT_CARD_REGISTER #define DISPLAY_ENABLE #define WEB_SERVER_ENABLE #define LCD_OTA_ENABLE @@ -199,8 +199,10 @@ void setup() ESP_LOGI("Initializer", "Enabling internal display"); espmega.enableInternalDisplay(&Serial); ESP_LOGI("Initializer", "Binding climate card to internal display"); +#ifdef CLIMATE_CARD_ENABLE espmega.display->bindClimateCard(&climateCard); #endif +#endif #ifdef WEB_SERVER_ENABLE ESP_LOGI("Initializer", "Enabling web server"); espmega.enableWebServer(80); @@ -213,8 +215,8 @@ void setup() #endif #ifdef REMOTE_VARIABLE_ENABLE ESP_LOGI("Initializer", "Initializing testvar"); - testVar.begin(32, "/testvar", espmega.iot, true,"/testvar/request"); - testVar.enableSetValue("/testvar/set"); + testVar.begin(32, "/xm/fan_speed", espmega.iot, true,"/pm/request_fan_speed"); + testVar.enableSetValue("/pm/request_switch_state"); #endif #ifdef CT_ENABLE ESP_LOGI("Initializer", "Initializing analog card");