Skip to content

Commit a9951d0

Browse files
author
Bjarne Loft
committed
New handing of config
1 parent 0b4ca35 commit a9951d0

File tree

10 files changed

+178
-203
lines changed

10 files changed

+178
-203
lines changed

src/Config.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#include "Config.h"
2+
#include <cstring>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include "FS.h"
6+
7+
Config::Config() {
8+
SPIFFS.begin();
9+
}
10+
11+
void Config::load() {
12+
File f = SPIFFS.open("/config.cfg", "r");
13+
if (f) {
14+
while(f.available()) {
15+
String line = f.readStringUntil('\n');
16+
int pos = line.indexOf("=");
17+
String key = line.substring(0, pos);
18+
String value = line.substring(pos+1, line.length()-1);
19+
set(key.c_str(), value.c_str());
20+
}
21+
f.close();
22+
}
23+
}
24+
25+
void Config::save() {
26+
File f = SPIFFS.open("/config.cfg", "w");
27+
if (f) {
28+
each([&](char *key, char *value) {
29+
f.print(key);
30+
f.print("=");
31+
f.println(value);
32+
});
33+
f.close();
34+
}
35+
}
36+
37+
void Config::reset() {
38+
SPIFFS.remove("/config.cfg");
39+
ESP.restart();
40+
}
41+
42+
void Config::each(std::function<void(char*, char*)> f) {
43+
struct element * ele = elements;
44+
while(ele) {
45+
f(ele->key, ele->value);
46+
ele = ele->next;
47+
}
48+
}
49+
50+
int Config::getInt(const char *key, int defaultValue) {
51+
char defaultStr[16];
52+
sprintf(defaultStr, "%i", defaultValue);
53+
return atoi(get(key, defaultStr));
54+
}
55+
56+
double Config::getDouble(const char *key, double defaultValue) {
57+
char defaultStr[16];
58+
sprintf(defaultStr, "%f", defaultValue);
59+
return atof(get(key, defaultStr));
60+
}
61+
62+
long Config::getLong(const char *key, long defaultValue) {
63+
char defaultStr[16];
64+
sprintf(defaultStr, "%ld", defaultValue);
65+
return atol(get(key, defaultStr));
66+
}
67+
68+
bool Config::getBool(const char *key, bool defaultValue) {
69+
return strcmp(get(key, defaultValue ? "true" : "false"), "true") == 0;
70+
}
71+
72+
char * Config::get(const char *key, const char *defaultValue) {
73+
struct element * ele = elements;
74+
while(ele) {
75+
if(strcmp(key, ele->key) == 0) {
76+
return ele->value;
77+
}
78+
ele = ele->next;
79+
}
80+
return set(key, defaultValue);
81+
}
82+
83+
char * Config::set(const char *key, const char *value){
84+
struct element * ele = elements;
85+
while(ele) {
86+
if(strcmp(key, ele->key) == 0) {
87+
free(ele->value);
88+
elements->value = (char*)malloc(strlen(value)+1);
89+
strcpy(elements->value, value);
90+
return ele->value;
91+
}
92+
ele = ele->next;
93+
}
94+
ele = elements;
95+
elements = new element();
96+
elements->key = (char*)malloc(strlen(key)+1);
97+
strcpy(elements->key, key);
98+
elements->value = (char*)malloc(strlen(value)+1);
99+
strcpy(elements->value, value);
100+
elements->next = ele;
101+
return elements->value;
102+
}

src/Config.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#pragma once
2+
3+
#include <functional>
4+
5+
class Config {
6+
private:
7+
struct element {
8+
char * key;
9+
char * value;
10+
struct element * next;
11+
};
12+
struct element * elements = nullptr;
13+
14+
public:
15+
Config();
16+
void load();
17+
void save();
18+
void reset();
19+
20+
void each(std::function<void(char*, char*)> f);
21+
22+
int getInt(const char *key, int defaultValue);
23+
double getDouble(const char *key, double defaultValue);
24+
long getLong(const char *key, long defaultValue);
25+
bool getBool(const char *key, bool defaultValue);
26+
char * get(const char *key, const char *defaultValue);
27+
char * set(const char *key, const char *value);
28+
};

src/ConfigEntry.cpp

Lines changed: 0 additions & 107 deletions
This file was deleted.

src/ConfigEntry.h

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/EasyMqtt.cpp

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <ESP8266WiFi.h>
44
#include <PubSubClient.h>
55
#include "Entry.h"
6-
#include "ConfigEntry.h"
6+
#include "Config.h"
77
#include "WebPortal.h"
88
#include <ArduinoOTA.h>
99

@@ -97,25 +97,22 @@ EasyMqtt::EasyMqtt() : Entry("easyMqtt") {
9797
#endif
9898

9999
// Add config entry
100-
configEntry = new ConfigEntry();
101-
addChild(configEntry);
102-
103-
configEntry->load();
104-
105-
deviceId = configEntry->getString("device.name", String(ESP.getChipId()).c_str());
100+
cfg = new Config();
101+
cfg->load();
106102

103+
// Add time(NTP)
107104
ntpClient = new NTPClient();
108105

109-
setInterval(60, 10);
110-
106+
deviceId = config().get("device.name", String(ESP.getChipId()).c_str());
111107

112-
char password[32];
113-
configEntry->getCString("password", "", password);
108+
char * password = config().get("password", "");
114109
if(strlen(password) > 0) {
115110
ArduinoOTA.setPassword(password);
116111
}
117112
ArduinoOTA.setHostname(deviceId.c_str());
118113
ArduinoOTA.begin();
114+
115+
setInterval(60, 10);
119116

120117
get("$system").setInterval(300); // every 5 min
121118
get("$system")["deviceId"] << [this]() {
@@ -153,18 +150,15 @@ EasyMqtt::EasyMqtt() : Entry("easyMqtt") {
153150
};
154151

155152
get("$system")["restart"] >> [this](String value) {
156-
if(value == "restart") {
153+
if(value == config().get("password", "")) {
157154
debug("Restart");
158-
disconnectWiFi();
159155
ESP.restart();
160156
}
161157
};
162158

163159
get("$system")["reset"] >> [this](String value) {
164-
if(value == "reset") {
165-
debug("Factory Reset");
160+
if(value == config().get("password", "")) {
166161
config().reset();
167-
ESP.restart();
168162
}
169163
};
170164
}
@@ -177,8 +171,8 @@ String EasyMqtt::getTopic() {
177171
return String("easyMqtt/") + deviceId;
178172
}
179173

180-
ConfigEntry & EasyMqtt::config() {
181-
return *configEntry;
174+
Config & EasyMqtt::config() {
175+
return *cfg;
182176
}
183177

184178
NTPClient & EasyMqtt::ntp() {
@@ -190,8 +184,8 @@ NTPClient & EasyMqtt::ntp() {
190184
Deprecated
191185
*/
192186
void EasyMqtt::wifi(const char* ssid, const char* password) {
193-
config().getString("wifi.ssid", ssid);
194-
config().getString("wifi.password", password);
187+
config().get("wifi.ssid", ssid);
188+
config().get("wifi.password", password);
195189
wifi_ssid = ssid;
196190
wifi_password = password;
197191
}
@@ -201,10 +195,10 @@ void EasyMqtt::wifi(const char* ssid, const char* password) {
201195
Deprecated
202196
*/
203197
void EasyMqtt::mqtt(const char* host, int port, const char* username, const char* password) {
204-
config().getString("mqtt.host", host);
205-
config().getString("mqtt.port", String(port).c_str());
206-
config().getString("mqtt.username", username);
207-
config().getString("mqtt.password", password);
198+
config().get("mqtt.host", host);
199+
config().get("mqtt.port", String(port).c_str());
200+
config().get("mqtt.username", username);
201+
config().get("mqtt.password", password);
208202
mqtt_host = host;
209203
mqtt_port = port;
210204
mqtt_username = username;

src/EasyMqtt.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <ESP8266WiFi.h>
44
#include <PubSubClient.h>
55
#include "Entry.h"
6-
#include "ConfigEntry.h"
6+
#include "Config.h"
77
#include "WebPortal.h"
88
#include "NTPClient.h"
99

@@ -13,7 +13,7 @@ class EasyMqtt : public Entry {
1313
PubSubClient mqttClient;
1414
WebPortal webPortal;
1515

16-
ConfigEntry* configEntry;
16+
Config* cfg;
1717

1818
NTPClient* ntpClient;
1919

@@ -47,7 +47,7 @@ class EasyMqtt : public Entry {
4747

4848
virtual String getTopic();
4949

50-
ConfigEntry & config();
50+
Config & config();
5151

5252
NTPClient & ntp();
5353

0 commit comments

Comments
 (0)