2023-12-28 12:11:12 +00:00
|
|
|
#include <DigitalInputIoT.hpp>
|
|
|
|
|
|
|
|
|
|
|
|
bool DigitalInputIoT::begin(uint8_t card_id, DigitalInputCard *card, PubSubClient *mqtt, char *base_topic) {
|
|
|
|
this->card = card;
|
|
|
|
this->card_id = card_id;
|
|
|
|
this->mqtt = mqtt;
|
|
|
|
this->base_topic = base_topic;
|
|
|
|
this->card->registerCallback(std::bind(&DigitalInputIoT::handleValueChange, this, std::placeholders::_1, std::placeholders::_2));
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
2023-12-28 13:20:49 +00:00
|
|
|
|
|
|
|
void DigitalInputIoT::subscribe() {
|
|
|
|
char topic[64];
|
|
|
|
sprintf(topic, "%s/%d/%s", this->base_topic, this->card_id, PUBLISH_ENABLE_TOPIC);
|
|
|
|
this->subscribeRelative(topic);
|
|
|
|
}
|
|
|
|
|
2023-12-28 12:11:12 +00:00
|
|
|
void DigitalInputIoT::handleMqttMessage(char *topic, char *payload) {
|
|
|
|
// payload is char '0' or '1'
|
|
|
|
if (!strcmp(topic, PUBLISH_ENABLE_TOPIC)) {
|
|
|
|
if (payload[0] == '1') {
|
|
|
|
this->setDigitalInputsPublishEnabled(true);
|
|
|
|
} else {
|
|
|
|
this->setDigitalInputsPublishEnabled(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void DigitalInputIoT::publishDigitalInputs() {
|
|
|
|
if (!this->digital_inputs_publish_enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < 16; i++) {
|
|
|
|
this->publishDigitalInput(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void DigitalInputIoT::setDigitalInputsPublishEnabled(bool enabled) {
|
|
|
|
this->digital_inputs_publish_enabled = enabled;
|
|
|
|
if (enabled) {
|
|
|
|
this->publishDigitalInputs();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void DigitalInputIoT::handleValueChange(uint8_t pin, uint8_t value) {
|
|
|
|
if (this->digital_inputs_publish_enabled) {
|
|
|
|
this->publishDigitalInput(pin);
|
|
|
|
}
|
|
|
|
if (this->change_callback != NULL) {
|
|
|
|
this->change_callback(pin, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
void DigitalInputIoT::registerChangeCallback(std::function<void(uint8_t, uint8_t)> callback) {
|
|
|
|
this->change_callback = callback;
|
|
|
|
|
|
|
|
}
|
|
|
|
void DigitalInputIoT::publishReport() {
|
|
|
|
this->publishDigitalInputs();
|
|
|
|
}
|
|
|
|
uint8_t DigitalInputIoT::getType() {
|
|
|
|
return CARD_TYPE_DIGITAL_INPUT;
|
2023-12-28 13:20:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DigitalInputIoT::publishDigitalInput(uint8_t pin) {
|
|
|
|
char topic[64];
|
|
|
|
char payload[2];
|
|
|
|
sprintf(topic, "%s/%d/%d", this->base_topic, this->card_id, pin);
|
|
|
|
sprintf(payload, "%d", this->card->digitalRead(pin, false));
|
|
|
|
this->publishRelative(topic, payload);
|
2023-12-28 12:11:12 +00:00
|
|
|
}
|