My Project
Loading...
Searching...
No Matches
loadNetworkConfiguration.h
Go to the documentation of this file.
1#pragma once
2
4#include <string.h>
5
7{
8 // New up a configation object, so we can set its properties.
10
11 // Initialize file system.
12 if (!SPIFFS.begin(true)) {
13 Serial.println("An error has occurred while mounting SPIFFS");
14 return config;
15 }
16
17 // Read JSON network config file.
18 DynamicJsonDocument doc = MCJsonConfig::ReadJsonFile(configFilePath);
19
20 // Read logging configuration.
22
23 JsonObject loggingConfig = doc["logging"];
24 logging->MinLevel = loggingConfig["min_level"] | "info";
25
26 // Read Serial log configuration
27 logging->Serial = new MCLoggingSerialConfiguration();
28 logging->Serial->Enabled = loggingConfig["serial"]["enabled"] | true;
29
30 // Read Syslog configuration.
31 JsonObject syslogConfig = loggingConfig["syslog"];
32 logging->SysLog = new MCLoggingSyslogConfiguration();
33 logging->SysLog->Enabled = syslogConfig["enabled"] | false;
34 if (logging->SysLog->Enabled) {
35 logging->SysLog->ServerAddress = syslogConfig["server"].as<std::string>();
36 logging->SysLog->ServerPort = syslogConfig["port"] | 514;
37 logging->SysLog->AppName = syslogConfig["appname"].as<std::string>();
38
39 const char *minLevel = loggingConfig["min_level"];
40 if (minLevel) {
41 if (strcmp(minLevel, "debug") == 0) {
42 logging->SysLog->mask = LOG_DEBUG;
43 } else if (strcmp(minLevel, "info") == 0) {
44 logging->SysLog->mask = LOG_INFO;
45 } else if (strcmp(minLevel, "warning") == 0) {
46 logging->SysLog->mask = LOG_WARNING;
47 } else if (strcmp(minLevel, "error") == 0) {
48 logging->SysLog->mask = LOG_ERR;
49 } else if (strcmp(minLevel, "fatal") == 0) {
50 logging->SysLog->mask = LOG_CRIT;
51 } else {
52 logging->SysLog->mask = LOG_EMERG;
53 }
54 }
55
56 logging->SysLog->mask = ((1 << ((logging->SysLog->mask) + 1)) - 1);
57 }
58
59 // Attach logging configuration.
60 config->Logging = logging;
61
62 // Read WiFi configuration.
64 JsonObject wifiConfig = doc["wifi"];
65 wifi->SSID = wifiConfig["SSID"].as<std::string>();
66 wifi->password = wifiConfig["password"].as<std::string>();
67 wifi->hostname = wifiConfig["hostname"].as<std::string>();
68 wifi->otaPassword = wifiConfig["otaPassword"].as<std::string>();
69 wifi->DailyBetweenConnectAttempsInMs = wifiConfig["wait"] | 1000;
70
71 // Attach WiFi configuration.
72 config->WiFi = wifi;
73
74 // Read MQTT configuration.
76 JsonObject mqttConfig = doc["mqtt"];
77 mqtt->ServerAddress = mqttConfig["broker"].as<std::string>();
78 mqtt->ServerPort = mqttConfig["port"] | 1883;
79 mqtt->KeepAlive = mqttConfig["keepalive"] | 10;
80 mqtt->Ping = mqttConfig["ping"] | 0;
81 mqtt->Topic = "rocrail/service/command";
82
83 // Attach MQTT configuration.
84 config->MQTT = mqtt;
85
86 // Return MCNetworkConfiguration object.
87 return config;
88}
static DynamicJsonDocument ReadJsonFile(const char *jsonFilePath)
Definition MCJsonConfig.cpp:3
MCNetworkConfiguration * loadNetworkConfiguration(const char *configFilePath)
Definition loadNetworkConfiguration.h:6
Definition MCLoggingConfiguration.h:6
MCLoggingSerialConfiguration * Serial
Definition MCLoggingConfiguration.h:9
MCLoggingSyslogConfiguration * SysLog
Definition MCLoggingConfiguration.h:10
std::string MinLevel
Definition MCLoggingConfiguration.h:8
Definition MCLoggingSerialConfiguration.h:3
bool Enabled
Definition MCLoggingSerialConfiguration.h:5
Definition MCLoggingSyslogConfiguration.h:5
int mask
Definition MCLoggingSyslogConfiguration.h:10
std::string AppName
Definition MCLoggingSyslogConfiguration.h:9
std::string ServerAddress
Definition MCLoggingSyslogConfiguration.h:7
bool Enabled
Definition MCLoggingSyslogConfiguration.h:6
uint16_t ServerPort
Definition MCLoggingSyslogConfiguration.h:8
Definition MCMQTTConfiguration.h:3
const char * Topic
Definition MCMQTTConfiguration.h:10
uint16_t Ping
Definition MCMQTTConfiguration.h:9
std::string ServerAddress
Definition MCMQTTConfiguration.h:6
uint16_t KeepAlive
Definition MCMQTTConfiguration.h:8
uint16_t ServerPort
Definition MCMQTTConfiguration.h:7
Definition MCNetworkConfiguration.h:5
MCWiFiConfiguration * WiFi
Definition MCNetworkConfiguration.h:8
MCMQTTConfiguration * MQTT
Definition MCNetworkConfiguration.h:9
MCLoggingConfiguration * Logging
Definition MCNetworkConfiguration.h:7
Definition MCWiFiConfiguration.h:3
std::string otaPassword
Definition MCWiFiConfiguration.h:7
std::string hostname
Definition MCWiFiConfiguration.h:8
std::string SSID
Definition MCWiFiConfiguration.h:5
uint32_t DailyBetweenConnectAttempsInMs
Definition MCWiFiConfiguration.h:9
std::string password
Definition MCWiFiConfiguration.h:6