Skip to content

Commit 8a15a14

Browse files
author
burnsed
committed
Update for ArduinoJson v6
1 parent 1f8f84c commit 8a15a14

File tree

2 files changed

+28
-115
lines changed

2 files changed

+28
-115
lines changed

src/ConfigTool.cpp

Lines changed: 7 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@
77
#include <FS.h>
88
#include <ArduinoJson.h>
99
#include <SPIFFS.h>
10-
11-
#ifdef ESP8266
12-
#include <ESP8266WebServer.h>
13-
#endif
14-
1510
#include <map>
1611

1712
void ConfigTool::load() {
@@ -23,90 +18,27 @@ void ConfigTool::load() {
2318
return;
2419
}
2520

26-
DynamicJsonBuffer jsonBuffer;
27-
JsonObject& root = jsonBuffer.parseObject(f.readStringUntil('\n'));
21+
DynamicJsonDocument root(ConfigSize);
22+
deserializeJson(root, f.readStringUntil('\n'));
2823
for (auto item : variables_) {
29-
item.second->deserialize(&root);
24+
item.second->deserialize(root);
3025
}
3126

3227
f.close();
3328
}
3429

3530
void ConfigTool::save() {
36-
DynamicJsonBuffer jsonBuffer;
37-
JsonObject& root = jsonBuffer.createObject();
31+
DynamicJsonDocument root(ConfigSize);
3832

3933
for (auto item : variables_) {
40-
item.second->serialize(&root);
34+
item.second->serialize(root);
4135
}
4236

4337
SPIFFS.begin();
4438
File f = SPIFFS.open("/config.json", "w");
45-
46-
root.printTo(f);
47-
48-
f.close();
39+
serializeJson(root, f);
4940
}
5041

51-
#ifdef ESP8266
52-
String ConfigTool::createWebPage(bool updated) {
53-
const String beginHtml = "<html><head><title>Config Tool</title><link rel=\"stylesheet\"href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css\"integrity=\"sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4\"crossorigin=\"anonymous\"><script src=\"https://code.jquery.com/jquery-3.3.1.slim.min.js\"integrity=\"sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo\"crossorigin=\"anonymous\"></script><script src=\"https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js\"integrity=\"sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ\"crossorigin=\"anonymous\"></script><script src=\"https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js\"integrity=\"sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm\"crossorigin=\"anonymous\"></script></head><body><div class=\"container\"><div class=\"jumbotron\"style=\"width:100%\"><h1>ConfigTool Web Interface</h1><p>Edit the config variables here and click save.</p></div>";
54-
const String continueHtml = "<form method=\"POST\" action=\"\">";
55-
const String savedAlert = "<div class=\"alert alert-success\" role=\"alert\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\"><span aria-hidden=\"true\">&times;</span></button>The config has been saved.</div>";
56-
57-
const String endHtml = "<div class=\"form-group row\"><div class=\"col-sm-1\"><button class=\"btn btn-primary\" type=\"submit\">Save</button></div><div class=\"col-sm-1 offset-sm-0\"><button type=\"button\" class=\"btn btn-danger\" onclick=\"reset()\">Reset</button></div></div></form></div></body><script>function reset(){var url=window.location.href;if(url.indexOf('?')>0){url=url.substring(0,url.indexOf('?'));}url+='?reset=true';window.location.replace(url);}</script></html>";
58-
59-
String result = beginHtml;
60-
61-
if (updated) {
62-
result += savedAlert;
63-
}
64-
65-
result += continueHtml;
66-
67-
for (auto item : variables_) {
68-
result += "<div class=\"form-group row\"><div class=\"col-2\"><label>" + item.first + "</label></div><div class=\"col-10\"><input name=\"" + item.first + "\" class=\"form-control\" type=\"text\" value=\"" + item.second->toString() + "\" /></div></div>";
69-
}
70-
71-
result += endHtml;
72-
73-
return result;
74-
}
75-
76-
std::function<void()> ConfigTool::getWebHandler(ESP8266WebServer* server) {
77-
return [this, server]() {
78-
bool updated = false;
79-
80-
if (server->args() == 1 && server->hasArg("reset") && server->arg("reset") == "true") {
81-
Serial.println("Reset is true.");
82-
for (auto item : variables_) {
83-
item.second->reset();
84-
}
85-
updated = true;
86-
}
87-
else {
88-
Serial.write("Args hit: ");
89-
for (int i = 0; i < server->args(); i++) {
90-
String name = server->argName(i);
91-
auto item = variables_.find(name);
92-
Serial.print(name + " | ");
93-
if (item == variables_.end()) {
94-
continue;
95-
}
96-
updated = true;
97-
item->second->fromString(server->arg(name));
98-
}
99-
Serial.println();
100-
}
101-
save();
102-
103-
String html = createWebPage(updated);
104-
server->send(200, "text/html", html);
105-
return;
106-
};
107-
}
108-
#endif
109-
11042
void ConfigTool::reset() {
11143
SPIFFS.begin(true);
11244
SPIFFS.remove("/config.json");
@@ -115,3 +47,4 @@ void ConfigTool::reset() {
11547
item.second->reset();
11648
}
11749
}
50+

src/ConfigTool.h

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,18 @@
22
Name: ConfigTool.h
33
Author: Tvde1
44
*/
5+
#ifndef _ConfigTool_h
6+
#define _ConfigTool_h
57

68
#include <Arduino.h>
79
#include <map>
810
#include <ArduinoJson.h>
911

10-
#ifdef ESP8266
11-
#include <ESP8266WebServer.h>
12-
#endif
13-
14-
#include <string>
15-
16-
#ifndef _ConfigTool_h
17-
#define _ConfigTool_h
1812

1913
struct BaseVar {
2014
String name;
21-
virtual void serialize(JsonObject*) = 0;
22-
virtual void deserialize(JsonObject*) = 0;
15+
virtual void serialize(JsonDocument) = 0;
16+
virtual void deserialize(JsonDocument) = 0;
2317
virtual void reset() = 0;
2418
virtual String toString() = 0;
2519
virtual void fromString(String) = 0;
@@ -29,9 +23,9 @@ template <typename T>
2923
struct ConfigVar : BaseVar {
3024
ConfigVar(String n, T* p) {};
3125

32-
void deserialize(JsonObject *json) {};
26+
void deserialize(JsonDocument json) {};
3327

34-
void serialize(JsonObject* json) {};
28+
void serialize(JsonDocument json) {};
3529

3630
void reset() {};
3731

@@ -50,16 +44,12 @@ struct ConfigVar<String> : BaseVar {
5044
defaultValue = *p;
5145
};
5246

53-
void deserialize(JsonObject *json) {
54-
if (json->containsKey(name) && json->is<char*>(name)) {
55-
56-
*pointer = String{ json->get<char *>(name) };
57-
json->remove(name);
58-
}
47+
void deserialize(JsonDocument json) {
48+
*pointer = String{ json[name] | defaultValue };
5949
}
6050

61-
void serialize(JsonObject* json) {
62-
json->set(name, *pointer);
51+
void serialize(JsonDocument json) {
52+
json[name] = *pointer;
6353
}
6454

6555
void reset() {
@@ -85,16 +75,14 @@ struct ConfigVar<bool> : BaseVar {
8575
defaultValue = *p;
8676
};
8777

88-
void deserialize(JsonObject *json) {
89-
if (json->containsKey(name) && json->is<bool>(name)) {
90-
91-
*pointer = json->get<bool>(name);
92-
json->remove(name);
78+
void deserialize(JsonDocument json) {
79+
if (!json[name].isNull()) {
80+
*pointer = json[name];
9381
}
9482
}
9583

96-
void serialize(JsonObject* json) {
97-
json->set(name, *pointer);
84+
void serialize(JsonDocument json) {
85+
json[name] = *pointer;
9886
}
9987

10088
void reset() {
@@ -120,15 +108,14 @@ struct ConfigVar<int> : BaseVar {
120108
defaultValue = *p;
121109
};
122110

123-
void deserialize(JsonObject *json) {
124-
if (json->containsKey(name) && json->is<int>(name)) {
125-
*pointer = json->get<int>(name);
126-
json->remove(name);
111+
void deserialize(JsonDocument json) {
112+
if (!json[name].isNull()) {
113+
*pointer = json[name];
127114
}
128115
}
129116

130-
void serialize(JsonObject* json) {
131-
json->set(name, *pointer);
117+
void serialize(JsonDocument json) {
118+
json[name] = *pointer;
132119
}
133120

134121
void reset() {
@@ -152,19 +139,12 @@ struct ConfigTool {
152139
};
153140
void load();
154141
void save();
155-
156-
#ifdef ESP8266
157-
std::function<void()> getWebHandler(ESP8266WebServer*);
158-
#endif
142+
int ConfigSize = 1024;
159143

160144
void reset();
161145
private:
162146
std::map<String, BaseVar*> variables_;
163147

164-
#ifdef ESP8266
165-
String createWebPage(bool);
166-
#endif
167-
168148
};
169149

170150
#endif

0 commit comments

Comments
 (0)