diff --git a/src/MqttPlugin.cpp b/src/MqttPlugin.cpp index bf81f5d..3a926a6 100644 --- a/src/MqttPlugin.cpp +++ b/src/MqttPlugin.cpp @@ -70,10 +70,15 @@ void MqttPlugin::connect() } subscribed = true; } + connectTries = 0; publish("mqtt/connect", clientName); PRINT_MSG(Serial, "MQTT", "connected"); } else { PRINT_MSG(Serial, "MQTT", "connect failed"); + connectTries++; + if(connectTries == maxConnectTries){ + connectTask.disable(); + } } } } diff --git a/src/MqttPlugin.h b/src/MqttPlugin.h index 37373d2..b2fbcbc 100644 --- a/src/MqttPlugin.h +++ b/src/MqttPlugin.h @@ -15,52 +15,54 @@ using namespace std::placeholders; class MqttPlugin : public Plugin { - public: - PubSubClient *client; +public: + PubSubClient *client; - MqttPlugin(MqttConfig cfg, bool bindUp = true, bool bindDown = true); + MqttPlugin(MqttConfig cfg, bool bindUp = true, bool bindDown = true); - /** + /** * Connects to queue and triggers connect and process task. * Binds downstream handler to MQTT client. */ - void activate(Scheduler *scheduler); + void activate(Scheduler *scheduler); - private: - WiFiClient wifiClient; - Task connectTask; - Task processTask; - bool subscribed = false; - bool bindUpstream = false; - bool bindDownstream = false; - String brokerHost; - int brokerPort; - String clientName; - String topicRoot; +private: + WiFiClient wifiClient; + Task connectTask; + Task processTask; + bool subscribed = false; + bool bindUpstream = false; + bool bindDownstream = false; + String brokerHost; + int brokerPort; + String clientName; + String topicRoot; + int connectTries = 0; + int maxConnectTries = 5; // TODO add to config - void applyConfig(MqttConfig cfg); - void applyConfigFromFile(const char* fileName); - void enableConnectTask(Scheduler *scheduler); - void enableProcessTask(Scheduler *scheduler); - /** + void applyConfig(MqttConfig cfg); + void applyConfigFromFile(const char *fileName); + void enableConnectTask(Scheduler *scheduler); + void enableProcessTask(Scheduler *scheduler); + /** * Connects to MQTT server and subscribes all topics available in the mediator * to the corresponding topic on the remote queue by prefixing the topic with inRootTopic. * Creates shadow subscriptions of every local topic to propagate messages to the remote queue via upstreamHandler. */ - virtual void connect(); + virtual void connect(); - /** + /** * The upstream handler is bound to every local subscription. * It passes the message to the remote queue by prefixing the outRootTopic. - */ - virtual void upstreamHandler(String topic, String msg); + */ + virtual void upstreamHandler(String topic, String msg); - /** + /** * The downstream handler is bound to the remote counterpart of local subscriptions, * prefixed with inRootTopic. * Everything after the prefix is used as local topic and dispatched to local subscriptions. - */ - virtual void downstreamHandler(char *topic, uint8_t *payload, unsigned int length); + */ + virtual void downstreamHandler(char *topic, uint8_t *payload, unsigned int length); }; #endif \ No newline at end of file