max tries

This commit is contained in:
2018-11-29 06:49:07 +01:00
parent 031894a63e
commit b6df54ba06
2 changed files with 35 additions and 28 deletions

View File

@@ -70,10 +70,15 @@ void MqttPlugin::connect()
} }
subscribed = true; subscribed = true;
} }
connectTries = 0;
publish("mqtt/connect", clientName); publish("mqtt/connect", clientName);
PRINT_MSG(Serial, "MQTT", "connected"); PRINT_MSG(Serial, "MQTT", "connected");
} else { } else {
PRINT_MSG(Serial, "MQTT", "connect failed"); PRINT_MSG(Serial, "MQTT", "connect failed");
connectTries++;
if(connectTries == maxConnectTries){
connectTask.disable();
}
} }
} }
} }

View File

@@ -15,52 +15,54 @@ using namespace std::placeholders;
class MqttPlugin : public Plugin class MqttPlugin : public Plugin
{ {
public: public:
PubSubClient *client; 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. * Connects to queue and triggers connect and process task.
* Binds downstream handler to MQTT client. * Binds downstream handler to MQTT client.
*/ */
void activate(Scheduler *scheduler); void activate(Scheduler *scheduler);
private: private:
WiFiClient wifiClient; WiFiClient wifiClient;
Task connectTask; Task connectTask;
Task processTask; Task processTask;
bool subscribed = false; bool subscribed = false;
bool bindUpstream = false; bool bindUpstream = false;
bool bindDownstream = false; bool bindDownstream = false;
String brokerHost; String brokerHost;
int brokerPort; int brokerPort;
String clientName; String clientName;
String topicRoot; String topicRoot;
int connectTries = 0;
int maxConnectTries = 5; // TODO add to config
void applyConfig(MqttConfig cfg); void applyConfig(MqttConfig cfg);
void applyConfigFromFile(const char* fileName); void applyConfigFromFile(const char *fileName);
void enableConnectTask(Scheduler *scheduler); void enableConnectTask(Scheduler *scheduler);
void enableProcessTask(Scheduler *scheduler); void enableProcessTask(Scheduler *scheduler);
/** /**
* Connects to MQTT server and subscribes all topics available in the mediator * 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. * 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. * 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. * The upstream handler is bound to every local subscription.
* It passes the message to the remote queue by prefixing the outRootTopic. * 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, * The downstream handler is bound to the remote counterpart of local subscriptions,
* prefixed with inRootTopic. * prefixed with inRootTopic.
* Everything after the prefix is used as local topic and dispatched to local subscriptions. * 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 #endif