mirror of
https://gitlab.com/wirelos/sprocket-plugin-mqtt.git
synced 2025-12-14 13:51:27 +01:00
add json config
This commit is contained in:
48
src/MqttConfig.h
Normal file
48
src/MqttConfig.h
Normal file
@@ -0,0 +1,48 @@
|
||||
#ifndef __MQTT_CONFIG__
|
||||
#define __MQTT_CONFIG__
|
||||
|
||||
#include <JsonStruct.h>
|
||||
|
||||
#define JSON_MQTT_CLIENT_NAME "mqttClientName"
|
||||
#define JSON_MQTT_BROKER_HOST "mqttBrokerHost"
|
||||
#define JSON_MQTT_BROKER_PORT "mqttBrokerPort"
|
||||
#define JSON_MQTT_IN_TOPIC_ROOT "mqttInTopicRoot"
|
||||
#define JSON_MQTT_OUT_TOPIC_ROOT "mqttOutTopicRoot"
|
||||
|
||||
struct MqttConfig
|
||||
{
|
||||
const char *clientName;
|
||||
const char *brokerHost;
|
||||
int brokerPort;
|
||||
const char *inTopicRoot;
|
||||
const char *outTopicRoot;
|
||||
};
|
||||
|
||||
struct MqttConfigJson : public MqttConfig, public JsonStruct
|
||||
{
|
||||
void mapJsonObject(JsonObject &root)
|
||||
{
|
||||
root[JSON_MQTT_CLIENT_NAME] = clientName;
|
||||
root[JSON_MQTT_BROKER_HOST] = brokerHost;
|
||||
root[JSON_MQTT_BROKER_PORT] = brokerPort;
|
||||
root[JSON_MQTT_IN_TOPIC_ROOT] = inTopicRoot;
|
||||
root[JSON_MQTT_OUT_TOPIC_ROOT] = outTopicRoot;
|
||||
}
|
||||
void fromJsonObject(JsonObject &json)
|
||||
{
|
||||
if (!verifyJsonObject(json))
|
||||
{
|
||||
Serial.println("ERROR: cannot parse JSON object");
|
||||
valid = 0;
|
||||
return;
|
||||
}
|
||||
clientName = getAttr(json, JSON_MQTT_CLIENT_NAME);
|
||||
brokerHost = getAttr(json, JSON_MQTT_BROKER_HOST);
|
||||
brokerPort = getIntAttrFromJson(json, JSON_MQTT_BROKER_PORT);
|
||||
inTopicRoot = getAttr(json, JSON_MQTT_IN_TOPIC_ROOT);
|
||||
outTopicRoot = getAttr(json, JSON_MQTT_OUT_TOPIC_ROOT);
|
||||
valid = 1;
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -2,7 +2,12 @@
|
||||
|
||||
MqttPlugin::MqttPlugin(MqttConfig cfg)
|
||||
{
|
||||
mqttConfig = cfg;
|
||||
mqttConfig.brokerHost = cfg.brokerHost;
|
||||
mqttConfig.brokerPort = cfg.brokerPort;
|
||||
mqttConfig.clientName = cfg.clientName;
|
||||
mqttConfig.inTopicRoot = cfg.inTopicRoot;
|
||||
mqttConfig.outTopicRoot = cfg.outTopicRoot;
|
||||
mqttConfig.fromFile("/mqttConfig.json");
|
||||
}
|
||||
|
||||
void MqttPlugin::activate(Scheduler *scheduler)
|
||||
@@ -35,7 +40,7 @@ void MqttPlugin::connect()
|
||||
if (client->connect(mqttConfig.clientName))
|
||||
{
|
||||
// bind handlers to all local subscriptions
|
||||
if (!locallySubscribed)
|
||||
if (!subscribed)
|
||||
{
|
||||
for (auto const &localSub : mediator->subscriptions)
|
||||
{
|
||||
@@ -46,7 +51,7 @@ void MqttPlugin::connect()
|
||||
(String(mqttConfig.inTopicRoot) + String(localSub.first.c_str()))
|
||||
.c_str());
|
||||
}
|
||||
locallySubscribed = true;
|
||||
subscribed = true;
|
||||
}
|
||||
PRINT_MSG(Serial, "MQTT", "connected");
|
||||
}
|
||||
|
||||
@@ -8,24 +8,16 @@
|
||||
#include <PubSubClient.h>
|
||||
#include <Plugin.h>
|
||||
#include "utils/print.h"
|
||||
#include "MqttConfig.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;
|
||||
MqttConfig mqttConfig;
|
||||
MqttConfigJson mqttConfig;
|
||||
|
||||
MqttPlugin(MqttConfig cfg);
|
||||
|
||||
@@ -39,7 +31,7 @@ class MqttPlugin : public Plugin
|
||||
WiFiClient wifiClient;
|
||||
Task connectTask;
|
||||
Task processTask;
|
||||
bool locallySubscribed = false;
|
||||
bool subscribed = false;
|
||||
|
||||
void enableConnectTask(Scheduler *scheduler);
|
||||
void enableProcessTask(Scheduler *scheduler);
|
||||
|
||||
Reference in New Issue
Block a user