initializer changes

This commit is contained in:
Siwat Sirichai 2023-12-28 13:34:51 +07:00
parent 9fa8cbc4b1
commit 17160e586b
7 changed files with 39 additions and 6 deletions

View File

@ -5,7 +5,7 @@ class AnalogIoT : public IoTComponent {
public:
AnalogIoT();
~AnalogIoT();
bool begin(AnalogCard *card);
bool begin(AnalogCard *card, void (*publishRelative)(uint8_t, char *, char *), PubSubClient *mqtt, char *base_topic);
void handleMqttMessage(char *topic, char *payload);
void publishADCs();
void setADCsPublishInterval(uint32_t interval);

View File

@ -5,7 +5,7 @@
class DigitalInputIoT : public IoTComponent {
public:
bool begin(DigitalInputCard *card);
bool begin(DigitalInputCard *card, void (*publishRelative)(uint8_t, char *, char *), PubSubClient *mqtt, char *base_topic);
void handleMqttMessage(char *topic, char *payload);
void publishDigitalInputs();
void setDigitalInputsPublishEnabled(bool enabled);
@ -15,6 +15,5 @@ class DigitalInputIoT : public IoTComponent {
uint8_t getType();
private:
bool digital_inputs_publish_enabled = false;
DigitalInputCard *card;
};

View File

@ -10,7 +10,10 @@ DigitalOutputIoT::~DigitalOutputIoT() {
delete[] this->value_report_topic;
}
bool DigitalOutputIoT::begin(ExpansionCard *card) {
bool DigitalOutputIoT::begin(ExpansionCard *card, void (*publishRelative)(uint8_t, char *, char *), PubSubClient *mqtt, char *base_topic) {
this->mqtt = mqtt;
this->base_topic = base_topic;
this->publishRelative = publishRelative;
this->card = (DigitalOutputCard *) card;
this->set_state_length = strlen(SET_STATE_TOPIC);
this->set_value_length = strlen(SET_VALUE_TOPIC);

View File

@ -7,7 +7,7 @@ class DigitalOutputIoT : public IoTComponent {
public:
DigitalOutputIoT();
~DigitalOutputIoT();
bool begin(ExpansionCard *card);
bool begin(ExpansionCard *card, void (*publishRelative)(uint8_t, char *, char *), PubSubClient *mqtt, char *base_topic);
void handleMqttMessage(char *topic, char *payload);
void publishDigitalOutputs();
void publishDigitalOutput(uint8_t pin);

View File

@ -19,3 +19,28 @@ void ESPMegaIoT::mqttCallback(char *topic, byte *payload, unsigned int length) {
components[card_id]->handleMqttMessage(topic_without_base + 3, payload_buffer);
}
void ESPMegaIoT::setBaseTopic(char *base_topic) {
strcpy(this->base_topic, base_topic);
base_topic_length = strlen(base_topic);
}
void ESPMegaIoT::begin() {
}
void ESPMegaIoT::loop();
void ESPMegaIoT::registerCard(uint8_t card_id);
void ESPMegaIoT::deregisterCard(uint8_t card_id);
void ESPMegaIoT::publishCard(uint8_t card_id);
void ESPMegaIoT::subscribeToTopic(char *topic);
void ESPMegaIoT::unsubscribeFromTopic(char *topic);
void ESPMegaIoT::connectToEthernet();
bool ESPMegaIoT::ethernetConnected();
void ESPMegaIoT::connectToWifi(char *ssid, char *password);
void ESPMegaIoT::connectToWifi(char *ssid);
void ESPMegaIoT::disconnectFromWifi();
bool ESPMegaIoT::wifiConnected();
void ESPMegaIoT::connectToMqtt(char *mqtt_server, uint16_t mqtt_port, char *mqtt_user, char *mqtt_password);
void ESPMegaIoT::connectToMqtt(char *mqtt_server, uint16_t mqtt_port);
void ESPMegaIoT::disconnectFromMqtt();
void ESPMegaIoT::publishToTopic(char *topic, char *payload);
void ESPMegaIoT::registerMqttCallback(void (*callback)(char *, char *));

View File

@ -30,15 +30,19 @@ class ESPMegaIoT
void disconnectFromMqtt();
void publishToTopic(char *topic, char *payload);
void registerMqttCallback(void (*callback)(char *, char *));
void setBaseTopic(char *base_topic);
private:
void sessionKeepAlive();
void mqttReconnect();
void wifiReconnect();
void mqttCallback(char *topic, byte *payload, unsigned int length);
void publishRelative(char *topic, char *payload);
PubSubClient mqtt;
IoTComponent *components[255];
bool card_publish_enabled[255];
char payload_buffer[200];
char base_topic[100];
uint8_t base_topic_length;
};

View File

@ -3,11 +3,13 @@
#include <PubSubClient.h>
class IoTComponent {
public:
virtual bool begin(ExpansionCard *card);
virtual bool begin(ExpansionCard *card, void (*publishRelative)(uint8_t, char *, char *), PubSubClient *mqtt, char *base_topic);
virtual void handleMqttMessage(char *topic, char *payload);
void setMqttClient(PubSubClient *mqtt);
virtual void publishReport();
virtual uint8_t getType();
protected:
char *base_topic;
void (*publishRelative)(uint8_t, char *, char *);
PubSubClient *mqtt;
};