This commit is contained in:
2018-11-15 12:50:59 +01:00
parent 4c27a0fe7b
commit 99d72b06a0
3 changed files with 119 additions and 103 deletions

View File

@@ -1,43 +1,11 @@
#ifndef __MQTT_PLUGIN__ #include "MqttPlugin.h"
#define __MQTT_PLUGIN__
#define _TASK_SLEEP_ON_IDLE_RUN MqttPlugin::MqttPlugin(MqttConfig cfg)
#define _TASK_STD_FUNCTION
#include <WiFiClient.h>
#include <PubSubClient.h>
#include <Plugin.h>
#include "utils_print.h"
using namespace std;
using namespace std::placeholders;
struct MqttConfig
{
const char *clientName;
const char *brokerHost;
int brokerPort;
const char *inTopicRoot;
const char *outTopicRoot;
};
class MqttPlugin : public Plugin
{
public:
PubSubClient *client;
WiFiClient wifiClient;
Task connectTask;
Task processTask;
MqttConfig mqttConfig;
vector<String> topics;
MqttPlugin(MqttConfig cfg)
{ {
mqttConfig = cfg; mqttConfig = cfg;
//subscribe("mqtt/out", bind(&MqttPlugin::mqttPublish, this, _1));
} }
void activate(Scheduler *scheduler) void MqttPlugin::activate(Scheduler *scheduler)
{ {
client = new PubSubClient(mqttConfig.brokerHost, mqttConfig.brokerPort, bind(&MqttPlugin::downstreamHandler, this, _1, _2, _3), wifiClient); client = new PubSubClient(mqttConfig.brokerHost, mqttConfig.brokerPort, bind(&MqttPlugin::downstreamHandler, this, _1, _2, _3), wifiClient);
enableConnectTask(scheduler); enableConnectTask(scheduler);
@@ -45,27 +13,26 @@ class MqttPlugin : public Plugin
PRINT_MSG(Serial, "MQTT", "plugin activated"); PRINT_MSG(Serial, "MQTT", "plugin activated");
} }
private: void MqttPlugin::enableConnectTask(Scheduler *scheduler)
void enableConnectTask(Scheduler *scheduler)
{ {
connectTask.set(TASK_SECOND * 5, TASK_FOREVER, bind(&MqttPlugin::connect, this)); connectTask.set(TASK_SECOND * 5, TASK_FOREVER, bind(&MqttPlugin::connect, this));
scheduler->addTask(connectTask); scheduler->addTask(connectTask);
connectTask.enable(); connectTask.enable();
} }
void enableProcessTask(Scheduler *scheduler) void MqttPlugin::enableProcessTask(Scheduler *scheduler)
{ {
processTask.set(TASK_MILLISECOND * 5, TASK_FOREVER, bind(&MqttPlugin::process, this)); processTask.set(TASK_MILLISECOND * 5, TASK_FOREVER, bind(&MqttPlugin::process, this));
scheduler->addTask(processTask); scheduler->addTask(processTask);
processTask.enable(); processTask.enable();
} }
void process() void MqttPlugin::process()
{ {
client->loop(); client->loop();
} }
void connect() void MqttPlugin::connect()
{ {
if (!client->connected()) if (!client->connected())
{ {
@@ -87,19 +54,19 @@ class MqttPlugin : public Plugin
} }
} }
void mqttPublish(String msg) void MqttPlugin::mqttPublish(String msg)
{ {
client->publish(mqttConfig.outTopicRoot, msg.c_str()); client->publish(mqttConfig.outTopicRoot, msg.c_str());
} }
void upstreamHandler(String topic, String msg) void MqttPlugin::upstreamHandler(String topic, String msg)
{ {
// publish message on remote queue // publish message on remote queue
PRINT_MSG(Serial, "MQTT", String("upstream: " + msg).c_str()); PRINT_MSG(Serial, "MQTT", String("upstream: " + msg).c_str());
client->publish((String(mqttConfig.outTopicRoot) + topic).c_str(), msg.c_str()); client->publish((String(mqttConfig.outTopicRoot) + topic).c_str(), msg.c_str());
} }
void downstreamHandler(char *topic, uint8_t *payload, unsigned int length) void MqttPlugin::downstreamHandler(char *topic, uint8_t *payload, unsigned int length)
{ {
char *cleanPayload = (char *)malloc(length + 1); char *cleanPayload = (char *)malloc(length + 1);
payload[length] = '\0'; payload[length] = '\0';
@@ -114,8 +81,4 @@ class MqttPlugin : public Plugin
PRINT_MSG(Serial, "MQTT", (String(topic) + " " + msg).c_str()); PRINT_MSG(Serial, "MQTT", (String(topic) + " " + msg).c_str());
PRINT_MSG(Serial, "MQTT", (String("publish local: ") + localTopic).c_str()); PRINT_MSG(Serial, "MQTT", (String("publish local: ") + localTopic).c_str());
} }
};
#endif

53
src/MqttPlugin.h Normal file
View File

@@ -0,0 +1,53 @@
#ifndef __MQTT_PLUGIN__
#define __MQTT_PLUGIN__
#define _TASK_SLEEP_ON_IDLE_RUN
#define _TASK_STD_FUNCTION
#include <WiFiClient.h>
#include <PubSubClient.h>
#include <Plugin.h>
#include "utils_print.h"
using namespace std;
using namespace std::placeholders;
struct MqttConfig
{
const char *clientName;
const char *brokerHost;
int brokerPort;
const char *inTopicRoot;
const char *outTopicRoot;
};
class MqttPlugin : public Plugin
{
public:
PubSubClient *client;
WiFiClient wifiClient;
Task connectTask;
Task processTask;
MqttConfig mqttConfig;
vector<String> topics;
MqttPlugin(MqttConfig cfg);
void activate(Scheduler *scheduler);
private:
void enableConnectTask(Scheduler *scheduler);
void enableProcessTask(Scheduler *scheduler);
void process();
void connect();
void mqttPublish(String msg);
void upstreamHandler(String topic, String msg);
void downstreamHandler(char *topic, uint8_t *payload, unsigned int length);
};
#endif

View File

@@ -1,7 +1,7 @@
#include "config.h" #include "config.h"
#include "WiFiNet.h" #include "WiFiNet.h"
#include "Sprocket.h" #include "Sprocket.h"
#include "MqttPlugin.cpp" #include "MqttPlugin.h"
WiFiNet *network; WiFiNet *network;
Sprocket *sprocket; Sprocket *sprocket;