add json config

This commit is contained in:
2018-11-18 19:29:46 +01:00
parent f168f24736
commit e08ac6eb6f
3 changed files with 59 additions and 14 deletions

48
src/MqttConfig.h Normal file
View 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

View File

@@ -2,7 +2,12 @@
MqttPlugin::MqttPlugin(MqttConfig cfg) 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) void MqttPlugin::activate(Scheduler *scheduler)
@@ -35,7 +40,7 @@ void MqttPlugin::connect()
if (client->connect(mqttConfig.clientName)) if (client->connect(mqttConfig.clientName))
{ {
// bind handlers to all local subscriptions // bind handlers to all local subscriptions
if (!locallySubscribed) if (!subscribed)
{ {
for (auto const &localSub : mediator->subscriptions) for (auto const &localSub : mediator->subscriptions)
{ {
@@ -46,7 +51,7 @@ void MqttPlugin::connect()
(String(mqttConfig.inTopicRoot) + String(localSub.first.c_str())) (String(mqttConfig.inTopicRoot) + String(localSub.first.c_str()))
.c_str()); .c_str());
} }
locallySubscribed = true; subscribed = true;
} }
PRINT_MSG(Serial, "MQTT", "connected"); PRINT_MSG(Serial, "MQTT", "connected");
} }

View File

@@ -8,24 +8,16 @@
#include <PubSubClient.h> #include <PubSubClient.h>
#include <Plugin.h> #include <Plugin.h>
#include "utils/print.h" #include "utils/print.h"
#include "MqttConfig.h"
using namespace std; using namespace std;
using namespace std::placeholders; using namespace std::placeholders;
struct MqttConfig
{
const char *clientName;
const char *brokerHost;
int brokerPort;
const char *inTopicRoot;
const char *outTopicRoot;
};
class MqttPlugin : public Plugin class MqttPlugin : public Plugin
{ {
public: public:
PubSubClient *client; PubSubClient *client;
MqttConfig mqttConfig; MqttConfigJson mqttConfig;
MqttPlugin(MqttConfig cfg); MqttPlugin(MqttConfig cfg);
@@ -39,7 +31,7 @@ class MqttPlugin : public Plugin
WiFiClient wifiClient; WiFiClient wifiClient;
Task connectTask; Task connectTask;
Task processTask; Task processTask;
bool locallySubscribed = false; bool subscribed = false;
void enableConnectTask(Scheduler *scheduler); void enableConnectTask(Scheduler *scheduler);
void enableProcessTask(Scheduler *scheduler); void enableProcessTask(Scheduler *scheduler);