mirror of
https://gitlab.com/wirelos/sprocket-plugin-mqtt.git
synced 2025-12-15 22:18:20 +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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user