Skip to content

Commit d7589f2

Browse files
committed
Convert much of the configuration to use c strings
1 parent 04b8aa6 commit d7589f2

File tree

6 files changed

+47
-8
lines changed

6 files changed

+47
-8
lines changed

src/ConfigEntry.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,41 @@ void ConfigEntry::reset() {
5656
// ToDo: Remove all child entries (children = NULL;)
5757
}
5858

59+
long ConfigEntry::getLong(const char *key, long defaultValue) {
60+
char cstr[16];
61+
get(key).getCValue(cstr);
62+
if(strlen(cstr) == 0) {
63+
get(key).setValue(String(defaultValue));
64+
return defaultValue;
65+
}
66+
return atol(cstr);
67+
}
68+
5969
int ConfigEntry::getInt(const char *key, int defaultValue) {
6070
char cstr[16];
61-
itoa(defaultValue, cstr, 10);
62-
return getString(key, cstr).toInt();
71+
get(key).getCValue(cstr);
72+
if(strlen(cstr) == 0) {
73+
get(key).setValue(String(defaultValue));
74+
return defaultValue;
75+
}
76+
return atoi(cstr);
77+
}
78+
79+
double ConfigEntry::getDouble(const char *key, double defaultValue) {
80+
char cstr[16];
81+
get(key).getCValue(cstr);
82+
if(strlen(cstr) == 0) {
83+
get(key).setValue(String(defaultValue));
84+
return defaultValue;
85+
}
86+
return atof(cstr);
87+
}
88+
void ConfigEntry::getCString(const char *key, const char *defaultValue, char *destination) {
89+
get(key).getCValue(destination);
90+
if(strlen(destination) == 0) {
91+
get(key).setValue(defaultValue);
92+
strcpy(destination, defaultValue);
93+
}
6394
}
6495

6596
String ConfigEntry::getString(const char *key, const char *defaultValue) {

src/ConfigEntry.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ class ConfigEntry : public Entry {
1414
void load();
1515
void save();
1616
void reset();
17+
18+
void getCString(const char *key, const char *defaultValue, char *destination);
1719
String getString(const char *key, const char *defaultValue);
20+
1821
void setString(const char *key, const char *value);
22+
1923
int getInt(const char *key, int defaultValue);
24+
long getLong(const char *key, long defaultValue);
25+
double getDouble(const char *key, double defaultValue);
2026
};

src/EasyMqtt.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ EasyMqtt::EasyMqtt() : Entry("easyMqtt") {
134134
get("$system")["online"] << []() {
135135
return "ON";
136136
};
137+
137138
get("$system")["time"].setInterval(900); // every 15 min
138139
get("$system")["time"] << [this]() {
139140
ntp().update();

src/Entry.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ String Entry::getValue() {
165165
return lastValue;
166166
}
167167

168-
const char* Entry::getCValue() {
169-
return lastValue.c_str();
168+
void Entry::getCValue(char * destination) {
169+
strcpy(destination, lastValue.c_str());
170170
}
171171

172172
void Entry::setValue(String value) {

src/Entry.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class Entry {
6464
* Get last value
6565
*/
6666
String getValue();
67-
const char* getCValue();
67+
void getCValue(char * destination);
6868
void setValue(String value);
6969

7070
/**

src/WebPortal.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ void WebPortal::handleRoot() {
6565
page += FPSTR(HTML_MAIN2);
6666
page += FPSTR(HTML_CONFIG_HEADER);
6767
page.replace("{title}", "General");
68-
Entry* config = &mqtt->get("$config");
6968
config->each([&](Entry* entry) {
7069
if(entry == config) return;
7170
page += FPSTR(HTML_CONFIG_ENTRY);
@@ -152,7 +151,7 @@ void WebPortal::loop() {
152151
}
153152

154153
String WebPortal::time(long time) {
155-
float utcOffset = 2;
154+
double utcOffset = config->getDouble("time.offset", 2);
156155

157156
long localTime = round(ntp->getTime(time) + 3600 * utcOffset);
158157

@@ -169,7 +168,9 @@ String WebPortal::time(long time) {
169168
}
170169

171170
bool WebPortal::auth() {
172-
if (!webServer->authenticate("admin", "password")) {
171+
char pass[32];
172+
config->getCString("web.password", "password", pass);
173+
if (!webServer->authenticate("admin", pass)) {
173174
webServer->requestAuthentication();
174175
return false;
175176
}

0 commit comments

Comments
 (0)