ESPMegaPRO-v3-SDK/Template Project/lib/ESPMegaPRO/ESPMegaIoT.cpp

35 lines
1.4 KiB
C++
Raw Normal View History

2023-12-28 04:41:20 +00:00
#include <ESPMegaIoT.hpp>
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';
// Remove the base topic from the topic
char *topic_without_base = topic + strlen(base_topic) + 1;
// Call the respective card's mqtt callback
// Note that after the base topic, there should be the card id
// /base_topic/card_id/...
// First, get the card id in integer form
char *card_id_str = strtok(topic_without_base, "/");
uint8_t card_id = atoi(card_id_str);
// Check if the card is registered
if (expansionCards[card_id] == NULL) {
return;
}
// Get the card type
uint8_t card_type = expansionCards[card_id]->getType();
// Cast the card to the respective type
switch(card_type) {
case CARD_TYPE_ANALOG_INPUT:
((AnalogIoT *)expansionCards[card_id])->mqttCallback(topic_without_base + strlen(card_id_str) + 1, payload_buffer);
break;
case CARD_TYPE_DIGITAL_INPUT:
((DigitalInputIoT *)expansionCards[card_id])->mqttCallback(topic_without_base + strlen(card_id_str) + 1, payload_buffer);
break;
case CARD_TYPE_DIGITAL_OUTPUT:
((DigitalOutputIoT *)expansionCards[card_id])->mqttCallback(topic_without_base + strlen(card_id_str) + 1, payload_buffer);
break;
}
}