fix IoT Callback issue when the topic is base-1

This commit is contained in:
Siwat Sirichai 2024-02-14 02:01:39 +07:00
parent 1b9163d468
commit 531f7e05a7
2 changed files with 16 additions and 9 deletions

View File

@ -41,16 +41,21 @@ void ESPMegaIoT::mqttCallback(char *topic, byte *payload, unsigned int length)
// Create a null terminated string from the payload // Create a null terminated string from the payload
memcpy(payload_buffer, payload, length); memcpy(payload_buffer, payload, length);
payload_buffer[length] = '\0'; 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 // Remove the base topic from the topic
char *topic_without_base = topic + strlen(this->mqtt_config.base_topic) + 1; char *topic_without_base = topic + strlen(this->mqtt_config.base_topic) + 1;
for (const auto &callback : mqtt_relative_callbacks) for (const auto &callback : mqtt_relative_callbacks)
{ {
callback.second(topic_without_base, payload_buffer); 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 // Call the respective card's mqtt callback
// Note that after the base topic, there should be the card id // Note that after the base topic, there should be the card id
// /base_topic/card_id/... // /base_topic/card_id/...

View File

@ -9,9 +9,9 @@
// #define FRAM_DEBUG // #define FRAM_DEBUG
// #define MQTT_DEBUG // #define MQTT_DEBUG
#define WRITE_DEFAULT_NETCONF // #define WRITE_DEFAULT_NETCONF
#define CLIMATE_CARD_ENABLE //#define CLIMATE_CARD_ENABLE
#define MQTT_CARD_REGISTER //#define MQTT_CARD_REGISTER
#define DISPLAY_ENABLE #define DISPLAY_ENABLE
#define WEB_SERVER_ENABLE #define WEB_SERVER_ENABLE
#define LCD_OTA_ENABLE #define LCD_OTA_ENABLE
@ -199,8 +199,10 @@ void setup()
ESP_LOGI("Initializer", "Enabling internal display"); ESP_LOGI("Initializer", "Enabling internal display");
espmega.enableInternalDisplay(&Serial); espmega.enableInternalDisplay(&Serial);
ESP_LOGI("Initializer", "Binding climate card to internal display"); ESP_LOGI("Initializer", "Binding climate card to internal display");
#ifdef CLIMATE_CARD_ENABLE
espmega.display->bindClimateCard(&climateCard); espmega.display->bindClimateCard(&climateCard);
#endif #endif
#endif
#ifdef WEB_SERVER_ENABLE #ifdef WEB_SERVER_ENABLE
ESP_LOGI("Initializer", "Enabling web server"); ESP_LOGI("Initializer", "Enabling web server");
espmega.enableWebServer(80); espmega.enableWebServer(80);
@ -213,8 +215,8 @@ void setup()
#endif #endif
#ifdef REMOTE_VARIABLE_ENABLE #ifdef REMOTE_VARIABLE_ENABLE
ESP_LOGI("Initializer", "Initializing testvar"); ESP_LOGI("Initializer", "Initializing testvar");
testVar.begin(32, "/testvar", espmega.iot, true,"/testvar/request"); testVar.begin(32, "/xm/fan_speed", espmega.iot, true,"/pm/request_fan_speed");
testVar.enableSetValue("/testvar/set"); testVar.enableSetValue("/pm/request_switch_state");
#endif #endif
#ifdef CT_ENABLE #ifdef CT_ENABLE
ESP_LOGI("Initializer", "Initializing analog card"); ESP_LOGI("Initializer", "Initializing analog card");