-
Notifications
You must be signed in to change notification settings - Fork 12
/
webinterface.cpp
237 lines (208 loc) · 7.16 KB
/
webinterface.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "webinterface.h"
extern ESP8266WebServer server;
extern Config config;
/***************************************************************************/
static String getContentType(const String& path) {
if (path.endsWith(".html")) return "text/html";
else if (path.endsWith(".htm")) return "text/html";
else if (path.endsWith(".css")) return "text/css";
else if (path.endsWith(".txt")) return "text/plain";
else if (path.endsWith(".js")) return "application/javascript";
else if (path.endsWith(".png")) return "image/png";
else if (path.endsWith(".gif")) return "image/gif";
else if (path.endsWith(".jpg")) return "image/jpeg";
else if (path.endsWith(".jpeg")) return "image/jpeg";
else if (path.endsWith(".ico")) return "image/x-icon";
else if (path.endsWith(".svg")) return "image/svg+xml";
else if (path.endsWith(".xml")) return "text/xml";
else if (path.endsWith(".pdf")) return "application/pdf";
else if (path.endsWith(".zip")) return "application/zip";
else if (path.endsWith(".gz")) return "application/x-gzip";
else if (path.endsWith(".json")) return "application/json";
return "application/octet-stream";
}
/***************************************************************************/
bool defaultConfig() {
Serial.println("defaultConfig");
config.universe = 1;
config.channels = 512;
config.delay = 25;
return true;
}
bool loadConfig() {
Serial.println("loadConfig");
File configFile = SPIFFS.open("/config.json", "r");
if (!configFile) {
Serial.println("Failed to open config file");
return false;
}
size_t size = configFile.size();
if (size > 1024) {
Serial.println("Config file size is too large");
return false;
}
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
configFile.close();
StaticJsonDocument<300> jsonBuffer;
DynamicJsonDocument root(1024);
DeserializationError error = deserializeJson(root, buf.get());
if (error) {
Serial.println("Failed to parse config file");
return false;
}
N_JSON_TO_CONFIG(universe, "universe");
N_JSON_TO_CONFIG(channels, "channels");
N_JSON_TO_CONFIG(delay, "delay");
return true;
}
bool saveConfig() {
Serial.println("saveConfig");
DynamicJsonDocument root(300);
N_CONFIG_TO_JSON(universe, "universe");
N_CONFIG_TO_JSON(channels, "channels");
N_CONFIG_TO_JSON(delay, "delay");
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("Failed to open config file for writing");
return false;
}
else {
Serial.println("Writing to config file");
serializeJson(root, configFile);
configFile.close();
return true;
}
}
void printRequest() {
String message = "HTTP Request\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nHeaders: ";
message += server.headers();
message += "\n";
for (uint8_t i = 0; i < server.headers(); i++ ) {
message += " " + server.headerName(i) + ": " + server.header(i) + "\n";
}
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
Serial.println(message);
}
void handleUpdate1() {
server.sendHeader("Connection", "close");
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
ESP.restart();
}
void handleUpdate2() {
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
Serial.setDebugOutput(true);
WiFiUDP::stopAll();
Serial.printf("Update: %s\n", upload.filename.c_str());
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
if (!Update.begin(maxSketchSpace)) { //start with max available size
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_END) {
if (Update.end(true)) { //true to set the size to the current progress
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
} else {
Update.printError(Serial);
}
Serial.setDebugOutput(false);
}
yield();
}
void handleDirList() {
Serial.println("handleDirList");
String str = "";
Dir dir = SPIFFS.openDir("/");
while (dir.next()) {
str += dir.fileName();
str += " ";
str += dir.fileSize();
str += " bytes\r\n";
}
server.send(200, "text/plain", str);
}
void handleNotFound() {
Serial.print("handleNotFound: ");
Serial.println(server.uri());
if (SPIFFS.exists(server.uri())) {
handleStaticFile(server.uri());
}
else {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
}
void handleRedirect(const char * filename) {
handleRedirect((String)filename);
}
void handleRedirect(String filename) {
Serial.println("handleRedirect: " + filename);
server.sendHeader("Location", filename, true);
server.send(302, "text/plain", "");
}
bool handleStaticFile(const char * path) {
return handleStaticFile((String)path);
}
bool handleStaticFile(String path) {
Serial.println("handleStaticFile: " + path);
String contentType = getContentType(path); // Get the MIME type
if (SPIFFS.exists(path)) { // If the file exists
File file = SPIFFS.open(path, "r"); // Open it
server.streamFile(file, contentType); // And send it to the client
file.close(); // Then close the file again
return true;
}
Serial.println("\tFile Not Found");
return false; // If the file doesn't exist, return false
}
void handleJSON() {
// this gets called in response to either a PUT or a POST
Serial.println("handleJSON");
printRequest();
if (server.hasArg("universe") || server.hasArg("channels") || server.hasArg("delay")) {
// the body is key1=val1&key2=val2&key3=val3 and the ESP8266Webserver has already parsed it
N_KEYVAL_TO_CONFIG(universe, "universe");
N_KEYVAL_TO_CONFIG(channels, "channels");
N_KEYVAL_TO_CONFIG(delay, "delay");
handleStaticFile("/reload_success.html");
}
else if (server.hasArg("plain")) {
// parse the body as JSON object
DynamicJsonDocument root(300);
DeserializationError error = deserializeJson(root, server.arg("plain"));
if (error) {
handleStaticFile("/reload_failure.html");
return;
}
N_JSON_TO_CONFIG(universe, "universe");
N_JSON_TO_CONFIG(channels, "channels");
N_JSON_TO_CONFIG(delay, "delay");
handleStaticFile("/reload_success.html");
}
saveConfig();
}