ESPMegaPRO-v3-SDK/ESPMegaPRO-OS-SDK/lib/ESPMegaPRO/DigitalInputIoT.cpp

125 lines
3.8 KiB
C++
Raw Normal View History

2023-12-28 12:11:12 +00:00
#include <DigitalInputIoT.hpp>
2023-12-31 18:38:25 +00:00
/**
* @brief Initializes the DigitalInputIoT object.
*
* This function sets the necessary parameters for the DigitalInputIoT object, such as the card ID, expansion card, MQTT client, and base topic.
* It also enables the publishing of digital input values and registers a callback function for handling value changes.
*
* @note Although this function can be called in the main program, it is recommended to use ESPMegaIoT::registerCard() to automatically manage the instantiation and initialization of this component.
*
* @param card_id The ID of the card.
* @param card Pointer to the DigitalInputCard object.
* @param mqtt Pointer to the PubSubClient object.
* @param base_topic The base topic for MQTT communication.
* @return True if the initialization is successful, false otherwise.
*/
2023-12-28 16:28:21 +00:00
bool DigitalInputIoT::begin(uint8_t card_id, ExpansionCard *card, PubSubClient *mqtt, char *base_topic) {
this->card = (DigitalInputCard *)card;
2023-12-28 12:11:12 +00:00
this->card_id = card_id;
this->mqtt = mqtt;
this->base_topic = base_topic;
2023-12-28 16:28:21 +00:00
this->setDigitalInputsPublishEnabled(true);
2023-12-28 12:11:12 +00:00
this->card->registerCallback(std::bind(&DigitalInputIoT::handleValueChange, this, std::placeholders::_1, std::placeholders::_2));
return true;
}
2023-12-28 13:20:49 +00:00
2023-12-31 18:38:25 +00:00
/**
* @brief Subscribes to the MQTT topics for the DigitalInputIoT component.
*/
2023-12-28 13:20:49 +00:00
void DigitalInputIoT::subscribe() {
2023-12-31 16:29:40 +00:00
this->subscribeRelative(PUBLISH_ENABLE_TOPIC);
2024-05-17 14:29:50 +00:00
this->subscribeRelative(INPUT_REQUEST_STATE_TOPIC);
2023-12-28 13:20:49 +00:00
}
2023-12-31 18:38:25 +00:00
/**
* @brief Handles MQTT messages for the DigitalInputIoT component.
*
* @param topic The trimmed topic of the MQTT message.
* @param payload The null-terminated payload of the MQTT message.
*/
2023-12-28 12:11:12 +00:00
void DigitalInputIoT::handleMqttMessage(char *topic, char *payload) {
2024-05-17 14:29:50 +00:00
if (!strcmp(topic, INPUT_REQUEST_STATE_TOPIC)) {
this->publishDigitalInputs();
}
2023-12-28 12:11:12 +00:00
// payload is char '0' or '1'
2024-05-17 14:29:50 +00:00
else if (!strcmp(topic, PUBLISH_ENABLE_TOPIC)) {
2023-12-28 12:11:12 +00:00
if (payload[0] == '1') {
this->setDigitalInputsPublishEnabled(true);
} else {
this->setDigitalInputsPublishEnabled(false);
}
}
2024-05-17 14:29:50 +00:00
2023-12-28 12:11:12 +00:00
}
2023-12-31 18:38:25 +00:00
/**
* @brief Publish all digital inputs to the MQTT broker.
*/
2023-12-28 12:11:12 +00:00
void DigitalInputIoT::publishDigitalInputs() {
if (!this->digital_inputs_publish_enabled) {
return;
}
for (int i = 0; i < 16; i++) {
this->publishDigitalInput(i);
}
}
2023-12-31 18:38:25 +00:00
/**
* @brief Set if the digital inputs should be published to the MQTT broker.
*
* @param enabled True if the digital inputs should be published, false otherwise.
*/
2023-12-28 12:11:12 +00:00
void DigitalInputIoT::setDigitalInputsPublishEnabled(bool enabled) {
this->digital_inputs_publish_enabled = enabled;
if (enabled) {
this->publishDigitalInputs();
}
}
2023-12-31 18:38:25 +00:00
/**
* @brief Handles a value change for a digital input.
*
* @note This function is registered as a callback function for the DigitalInputCard object.
*
* @param pin The pin that changed.
* @param value The new value of the pin.
*/
2023-12-28 12:11:12 +00:00
void DigitalInputIoT::handleValueChange(uint8_t pin, uint8_t value) {
if (this->digital_inputs_publish_enabled) {
this->publishDigitalInput(pin);
}
}
2023-12-31 18:38:25 +00:00
/**
* @brief Publish all inputs to the MQTT Broker
*
* @note This function is overriden from the IoTComponent class and is called by ESPMegaIoT.
*
* @param pin The pin to publish.
*/
2023-12-28 12:11:12 +00:00
void DigitalInputIoT::publishReport() {
this->publishDigitalInputs();
}
uint8_t DigitalInputIoT::getType() {
return CARD_TYPE_DIGITAL_INPUT;
2023-12-28 13:20:49 +00:00
}
2023-12-31 18:38:25 +00:00
/**
* @brief Publish a digital input to the MQTT broker.
*
* @param pin The pin to publish.
*/
2023-12-28 13:20:49 +00:00
void DigitalInputIoT::publishDigitalInput(uint8_t pin) {
2023-12-28 16:28:21 +00:00
char topic[20] = {0};
char payload[20] = {0};
topic[0] = pin/10 + '0';
2023-12-28 16:28:21 +00:00
topic[1] = pin%10 + '0';
topic[2] = '\0';
payload[0] = this->card->digitalRead(pin, false) + '0';
payload[1] = '\0';
2023-12-28 13:20:49 +00:00
this->publishRelative(topic, payload);
2023-12-28 12:11:12 +00:00
}