Skip to content

Commit d1b6677

Browse files
committed
kinda did everything
1 parent 2610f42 commit d1b6677

File tree

2 files changed

+143
-19
lines changed

2 files changed

+143
-19
lines changed

src/ConfigTool.cpp

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "FS.h";
88
#include "ArduinoJson.h";
99
#include "ESP8266WebServer.h";
10+
#include <map>;
1011

1112
void ConfigTool::load() {
1213
SPIFFS.begin();
@@ -19,7 +20,7 @@ void ConfigTool::load() {
1920
JsonObject& root = jsonBuffer.parseObject(f.readStringUntil('\n'));
2021

2122
for (auto item : variables_) {
22-
item->deserialize(&root);
23+
item.second->deserialize(&root);
2324
}
2425

2526
f.close();
@@ -30,7 +31,7 @@ void ConfigTool::save() {
3031
JsonObject& root = jsonBuffer.createObject();
3132

3233
for (auto item : variables_) {
33-
item->serialize(&root);
34+
item.second->serialize(&root);
3435
}
3536

3637
SPIFFS.begin();
@@ -42,15 +43,68 @@ void ConfigTool::save() {
4243
f.close();
4344
}
4445

45-
//TODO: This
46+
String ConfigTool::createWebPage(bool updated) {
47+
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>";
48+
const String continueHtml = "<form method=\"POST\" action=\"\">";
49+
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>";
50+
51+
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>";
52+
53+
String result = beginHtml;
54+
55+
if (updated) {
56+
result += savedAlert;
57+
}
58+
59+
result += continueHtml;
60+
61+
for (auto item : variables_) {
62+
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>";
63+
}
64+
65+
result += endHtml;
66+
67+
return result;
68+
}
69+
4670
std::function<void()> ConfigTool::getWebHandler(ESP8266WebServer* server) {
71+
return [this, server]() {
72+
bool updated = false;
73+
74+
if (server->args() == 1 && server->hasArg("reset") && server->arg("reset") == "true") {
75+
Serial.println("Reset is true.");
76+
for (auto item : variables_) {
77+
item.second->reset();
78+
}
79+
updated = true;
80+
}
81+
else {
82+
Serial.write("Args hit: ");
83+
for (int i = 0; i < server->args(); i++) {
84+
String name = server->argName(i);
85+
auto item = variables_.find(name);
86+
Serial.print(name + " | ");
87+
if (item == variables_.end()) {
88+
continue;
89+
}
90+
updated = true;
91+
item->second->fromString(server->arg(name));
92+
}
93+
Serial.println();
94+
}
95+
save();
96+
97+
String html = createWebPage(updated);
98+
server->send(200, "text/html", html);
99+
return;
100+
};
47101
}
48102

49103
void ConfigTool::reset() {
50104
SPIFFS.begin();
51105
SPIFFS.remove("/config.json");
52106

53107
for (auto item : variables_) {
54-
item->reset();
108+
item.second->reset();
55109
}
56-
}
110+
}

src/ConfigTool.h

Lines changed: 84 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
*/
55

66
#include <Arduino.h>;
7-
#include <list>;
7+
#include <map>;
88
#include "ArduinoJson.h"
99
#include "ESP8266WebServer.h";
10+
#include <string>;
1011

1112
#ifndef _ConfigTool_h
1213
#define _ConfigTool_h
@@ -16,21 +17,39 @@ struct BaseVar {
1617
virtual void serialize(JsonObject*) = 0;
1718
virtual void deserialize(JsonObject*) = 0;
1819
virtual void reset() = 0;
20+
virtual String toString() = 0;
21+
virtual void fromString(String) = 0;
1922
};
2023

2124
template <typename T>
2225
struct ConfigVar : BaseVar {
23-
T* pointer;
24-
T defaultValue;
25-
ConfigVar(String n, T* p) {
26+
ConfigVar(String n, T* p) {};
27+
28+
void deserialize(JsonObject *json) {};
29+
30+
void serialize(JsonObject* json) {};
31+
32+
void reset() {};
33+
34+
String toString() { return ""; };
35+
36+
void fromString(String) {};
37+
};
38+
39+
template <>
40+
struct ConfigVar<String> : BaseVar {
41+
String* pointer;
42+
String defaultValue;
43+
ConfigVar(String n, String* p) {
2644
name = n;
2745
pointer = p;
2846
defaultValue = *p;
2947
};
3048

3149
void deserialize(JsonObject *json) {
32-
if (json->containsKey(name) && json->is<T>(name)) {
33-
*pointer = json->get<T>(name);
50+
if (json->containsKey(name) && json->is<char*>(name)) {
51+
52+
*pointer = String{ json->get<char *>(name) };
3453
json->remove(name);
3554
}
3655
}
@@ -42,22 +61,30 @@ struct ConfigVar : BaseVar {
4261
void reset() {
4362
*pointer = defaultValue;
4463
}
64+
65+
String toString() {
66+
return *pointer;
67+
}
68+
69+
void fromString(String value) {
70+
*pointer = value;
71+
}
4572
};
4673

4774
template <>
48-
struct ConfigVar<String> : BaseVar {
49-
String* pointer;
50-
String defaultValue;
51-
ConfigVar(String n, String* p) {
75+
struct ConfigVar<bool> : BaseVar {
76+
bool* pointer;
77+
bool defaultValue;
78+
ConfigVar(String n, bool* p) {
5279
name = n;
5380
pointer = p;
5481
defaultValue = *p;
5582
};
5683

5784
void deserialize(JsonObject *json) {
58-
if (json->containsKey(name) && json->is<char*>(name)) {
85+
if (json->containsKey(name) && json->is<bool>(name)) {
5986

60-
*pointer = String{ json->get<char *>(name) };
87+
*pointer = json->get<bool>(name);
6188
json->remove(name);
6289
}
6390
}
@@ -69,20 +96,63 @@ struct ConfigVar<String> : BaseVar {
6996
void reset() {
7097
*pointer = defaultValue;
7198
}
99+
100+
String toString() {
101+
return *pointer ? "true" : "false";
102+
}
103+
104+
void fromString(String value) {
105+
*pointer = value == "true";
106+
}
107+
};
108+
109+
template <>
110+
struct ConfigVar<int> : BaseVar {
111+
int* pointer;
112+
int defaultValue;
113+
ConfigVar(String n, int* p) {
114+
name = n;
115+
pointer = p;
116+
defaultValue = *p;
117+
};
118+
119+
void deserialize(JsonObject *json) {
120+
if (json->containsKey(name) && json->is<int>(name)) {
121+
*pointer = json->get<int>(name);
122+
json->remove(name);
123+
}
124+
}
125+
126+
void serialize(JsonObject* json) {
127+
json->set(name, *pointer);
128+
}
129+
130+
void reset() {
131+
*pointer = defaultValue;
132+
}
133+
134+
String toString() {
135+
return String(*pointer);
136+
}
137+
138+
void fromString(String value) {
139+
*pointer = value.toInt();
140+
}
72141
};
73142

74143
struct ConfigTool {
75144
public:
76145
template <typename T>
77146
void addVariable(String name, T* pointer) {
78-
variables_.push_back(new ConfigVar<T>(name, pointer));
147+
variables_[name] = (new ConfigVar<T>(name, pointer));
79148
};
80149
void load();
81150
void save();
82151
std::function<void()> getWebHandler(ESP8266WebServer*);
83152
void reset();
84153
private:
85-
std::list<BaseVar*> variables_;
154+
std::map<String, BaseVar*> variables_;
155+
String createWebPage(bool);
86156
};
87157

88158
#endif

0 commit comments

Comments
 (0)