From 2b2abc3419022b04942b14604f7cfbaa0c9b4da2 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Fri, 29 Dec 2023 00:48:43 +0700 Subject: [PATCH] implement mqtt handler for analog --- Template Project/lib/ESPMegaPRO/AnalogIoT.cpp | 105 +++++++++++++++++- 1 file changed, 99 insertions(+), 6 deletions(-) diff --git a/Template Project/lib/ESPMegaPRO/AnalogIoT.cpp b/Template Project/lib/ESPMegaPRO/AnalogIoT.cpp index 00aad78..3b2e9eb 100644 --- a/Template Project/lib/ESPMegaPRO/AnalogIoT.cpp +++ b/Template Project/lib/ESPMegaPRO/AnalogIoT.cpp @@ -79,22 +79,115 @@ void AnalogIoT::setADCConversionEnabled(uint8_t pin, bool enabled) { adc_publish_enabled[pin] = enabled; } bool AnalogIoT::processADCSetConversionIntervalMessage(char *topic, char *payload, uint8_t topic_length) { - // TODO: publish all DACs and ADCs + // TODO: Process payload matching the criteria + // Topic: adc/<%02d>/set/conversion_interval + // The first 4 characters are "adc/" + // The length of the topic must be 30 characters + // The last 24 characters must be "/set/conversion_interval" + // After all these conditions are met, the topic is valid + // Extract the pin number from the topic + if (topic_length != 30) { + return false; + } + if (strncmp(topic, "adc/", 4)) { + return false; + } + if (strncmp(topic + 26, "/set/conversion_interval", 24)) { + return false; + } + uint8_t pin = (topic[4] - '0') * 10 + (topic[5] - '0'); + // Extract the payload + uint16_t interval = atoi(payload); + // Set the interval + this->setADCConversionInterval(pin, interval); + return true; } bool AnalogIoT::processADCSetConversionEnabledMessage(char *topic, char *payload, uint8_t topic_length) { - // TODO: publish all DACs and ADCs + // Topic: adc/<%02d>/set/conversion_enabled + // The first 4 characters are "adc/" + // The length of the topic must be 29 characters + // The last 23 characters must be ""/set/conversion_enabled + // After all these conditions are met, the topic is valid + // Extract the pin number from the topic + if (topic_length != 29) { + return false; + } + if (strncmp(topic, "adc/", 4)) { + return false; + } + if (strncmp(topic + 25, "/set/conversion_enabled", 23)) { + return false; + } + uint8_t pin = (topic[4] - '0') * 10 + (topic[5] - '0'); + // Extract the payload + bool enabled = atoi(payload); + // Set conversion enabled + this->setADCConversionEnabled(pin, enabled); + return true; } bool AnalogIoT::processDACSetStateMessage(char *topic, char *payload, uint8_t topic_length) { - // TODO: publish all DACs and ADCs + // Topic: dac/<%02d>/set/state + // The first 4 characters are "dac/" + // The length of the topic must be 16 characters + // The last 10 characters must be "/set/state" + // After all these conditions are met, the topic is valid + // Extract the pin number from the topic + if (topic_length != 16) { + return false; + } + if (strncmp(topic, "dac/", 4)) { + return false; + } + if (strncmp(topic + 12, "/set/state", 10)) { + return false; + } + uint8_t pin = (topic[4] - '0') * 10 + (topic[5] - '0'); + // Extract the payload + bool state = atoi(payload); + // Set the state + this->card->setDACState(pin, state); + return true; } bool AnalogIoT::processDACSetValueMessage(char *topic, char *payload, uint8_t topic_length) { - // TODO: publish all DACs and ADCs + // Topic: dac/<%02d>/set/value + // The first 4 characters are "dac/" + // The length of the topic must be 16 characters + // The last 10 characters must be "/set/value" + // After all these conditions are met, the topic is valid + // Extract the pin number from the topic + if (topic_length != 16) { + return false; + } + if (strncmp(topic, "dac/", 4)) { + return false; + } + if (strncmp(topic + 12, "/set/value", 10)) { + return false; + } + uint8_t pin = (topic[4] - '0') * 10 + (topic[5] - '0'); + // Extract the payload + uint16_t value = atoi(payload); + // Set the value + this->card->setDACValue(pin, value); + return true; } bool AnalogIoT::processRequestStateMessage(char *topic, char *payload, uint8_t topic_length) { - // TODO: publish all DACs and ADCs + // Topic: requeststate + // The length of the topic must be 12 characters + // After all these conditions are met, the topic is valid + if (topic_length != 12) { + return false; + } + if (strncmp(topic, REQUEST_STATE_TOPIC, 12)) { + return false; + } + // Publish the state of all DACs + this->publishDACs(); + // Publish the state of all ADCs + this->publishADCs(); + return false; } void AnalogIoT::publishReport() { - // TODO: publish all DACs and ADCs } void AnalogIoT::subscribe() { // There are 4 DACs and 8 ADCs