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;
}
connectTries = 0;
publish("mqtt/connect", clientName);
PRINT_MSG(Serial, "MQTT", "connected");
} else {
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
{
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