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