7
7
#include < FS.h>
8
8
#include < ArduinoJson.h>
9
9
#include < SPIFFS.h>
10
-
11
- #ifdef ESP8266
12
- #include < ESP8266WebServer.h>
13
- #endif
14
-
15
10
#include < map>
16
11
17
12
void ConfigTool::load () {
@@ -23,90 +18,27 @@ void ConfigTool::load() {
23
18
return ;
24
19
}
25
20
26
- DynamicJsonBuffer jsonBuffer ;
27
- JsonObject& root = jsonBuffer. parseObject ( f.readStringUntil (' \n ' ));
21
+ DynamicJsonDocument root (ConfigSize) ;
22
+ deserializeJson ( root, f.readStringUntil (' \n ' ));
28
23
for (auto item : variables_) {
29
- item.second ->deserialize (& root);
24
+ item.second ->deserialize (root);
30
25
}
31
26
32
27
f.close ();
33
28
}
34
29
35
30
void ConfigTool::save () {
36
- DynamicJsonBuffer jsonBuffer;
37
- JsonObject& root = jsonBuffer.createObject ();
31
+ DynamicJsonDocument root (ConfigSize);
38
32
39
33
for (auto item : variables_) {
40
- item.second ->serialize (& root);
34
+ item.second ->serialize (root);
41
35
}
42
36
43
37
SPIFFS.begin ();
44
38
File f = SPIFFS.open (" /config.json" , " w" );
45
-
46
- root.printTo (f);
47
-
48
- f.close ();
39
+ serializeJson (root, f);
49
40
}
50
41
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\" >×</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
-
110
42
void ConfigTool::reset () {
111
43
SPIFFS.begin (true );
112
44
SPIFFS.remove (" /config.json" );
@@ -115,3 +47,4 @@ void ConfigTool::reset() {
115
47
item.second ->reset ();
116
48
}
117
49
}
50
+
0 commit comments