mirror of
https://gitlab.com/wirelos/sprocket-plugin-mqtt.git
synced 2025-12-14 13:51:27 +01:00
apply config from file
This commit is contained in:
@@ -2,17 +2,30 @@
|
||||
|
||||
MqttPlugin::MqttPlugin(MqttConfig cfg)
|
||||
{
|
||||
mqttConfig.brokerHost = cfg.brokerHost;
|
||||
mqttConfig.brokerPort = cfg.brokerPort;
|
||||
mqttConfig.clientName = cfg.clientName;
|
||||
mqttConfig.inTopicRoot = cfg.inTopicRoot;
|
||||
mqttConfig.outTopicRoot = cfg.outTopicRoot;
|
||||
applyConfig(cfg);
|
||||
}
|
||||
|
||||
void MqttPlugin::applyConfig(MqttConfig cfg) {
|
||||
brokerHost = String(cfg.brokerHost);
|
||||
brokerPort = cfg.brokerPort;
|
||||
clientName = String(cfg.clientName);
|
||||
inTopicRoot = String(cfg.inTopicRoot);
|
||||
outTopicRoot = String(cfg.outTopicRoot);
|
||||
}
|
||||
|
||||
void MqttPlugin::applyConfigFromFile(const char* fileName) {
|
||||
MqttConfigJson configFile;
|
||||
configFile.fromFile(fileName);
|
||||
if(configFile.valid){
|
||||
PRINT_MSG(Serial, "MQTT", "apply config from file");
|
||||
applyConfig(configFile);
|
||||
}
|
||||
}
|
||||
|
||||
void MqttPlugin::activate(Scheduler *scheduler)
|
||||
{
|
||||
mqttConfig.fromFile("/mqttConfig.json");
|
||||
client = new PubSubClient(mqttConfig.brokerHost, mqttConfig.brokerPort, bind(&MqttPlugin::downstreamHandler, this, _1, _2, _3), wifiClient);
|
||||
applyConfigFromFile("/mqttConfig.json");
|
||||
client = new PubSubClient(brokerHost.c_str(), brokerPort, bind(&MqttPlugin::downstreamHandler, this, _1, _2, _3), wifiClient);
|
||||
enableConnectTask(scheduler);
|
||||
enableProcessTask(scheduler);
|
||||
PRINT_MSG(Serial, "MQTT", "plugin activated");
|
||||
@@ -37,7 +50,7 @@ void MqttPlugin::connect()
|
||||
if (!client->connected())
|
||||
{
|
||||
PRINT_MSG(Serial, "MQTT", "try connect");
|
||||
if (client->connect(mqttConfig.clientName))
|
||||
if (client->connect(clientName.c_str()))
|
||||
{
|
||||
// bind handlers to all local subscriptions
|
||||
if (!subscribed)
|
||||
@@ -48,7 +61,7 @@ void MqttPlugin::connect()
|
||||
subscribe(localSub.first.c_str(), bind(&MqttPlugin::upstreamHandler, this, localSub.first.c_str(), _1));
|
||||
// subscribe topic on remote queue to dispatch messages to local subscriptions
|
||||
client->subscribe(
|
||||
(String(mqttConfig.inTopicRoot) + String(localSub.first.c_str()))
|
||||
(inTopicRoot + String(localSub.first.c_str()))
|
||||
.c_str());
|
||||
}
|
||||
subscribed = true;
|
||||
@@ -61,9 +74,10 @@ void MqttPlugin::connect()
|
||||
void MqttPlugin::upstreamHandler(String topic, String msg)
|
||||
{
|
||||
// publish message on remote queue
|
||||
String remoteTopic = String(mqttConfig.outTopicRoot) + topic;
|
||||
String remoteTopic = outTopicRoot + topic;
|
||||
Serial.println(remoteTopic);
|
||||
client->publish(remoteTopic.c_str(), msg.c_str());
|
||||
PRINT_MSG(Serial, "MQTT", String("publish remote: " + remoteTopic).c_str());
|
||||
PRINT_MSG(Serial, "MQTT", remoteTopic.c_str());
|
||||
}
|
||||
|
||||
void MqttPlugin::downstreamHandler(char *topic, uint8_t *payload, unsigned int length)
|
||||
@@ -75,7 +89,7 @@ void MqttPlugin::downstreamHandler(char *topic, uint8_t *payload, unsigned int l
|
||||
free(cleanPayload);
|
||||
|
||||
// substract the topic root and publish msg on local topic
|
||||
int topicRootLength = String(mqttConfig.inTopicRoot).length();
|
||||
int topicRootLength = inTopicRoot.length();
|
||||
String localTopic = String(topic).substring(topicRootLength);
|
||||
publish(localTopic, msg);
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@ class MqttPlugin : public Plugin
|
||||
{
|
||||
public:
|
||||
PubSubClient *client;
|
||||
MqttConfigJson mqttConfig;
|
||||
|
||||
MqttPlugin(MqttConfig cfg);
|
||||
|
||||
@@ -32,10 +31,16 @@ class MqttPlugin : public Plugin
|
||||
Task connectTask;
|
||||
Task processTask;
|
||||
bool subscribed = false;
|
||||
String brokerHost;
|
||||
int brokerPort;
|
||||
String clientName;
|
||||
String inTopicRoot;
|
||||
String outTopicRoot;
|
||||
|
||||
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.
|
||||
|
||||
@@ -32,5 +32,7 @@
|
||||
#define WEB_DEFAULT_FILE "index.html"
|
||||
#define WEB_PORT 80
|
||||
|
||||
#define MQTT_CONFIG_FILE "/mqttConfig.json"
|
||||
|
||||
|
||||
#endif
|
||||
@@ -26,7 +26,7 @@ void setup()
|
||||
});
|
||||
sprocket->addTask(publishHeap);
|
||||
sprocket->addPlugin(
|
||||
new MqttPlugin({"sprocket", "broker.lan", 1883, "wirelos/mqttSprocket-in/", "wirelos/mqttSprocket-out/"}));
|
||||
new MqttPlugin({"mqttSprocket", "192.168.1.2", 1883, "wirelos/mqttSprocket-in/", "wirelos/mqttSprocket-out/"}));
|
||||
|
||||
network = new WiFiNet(
|
||||
SPROCKET_MODE,
|
||||
|
||||
Reference in New Issue
Block a user