-
Notifications
You must be signed in to change notification settings - Fork 2
/
webHandlers.ino
225 lines (199 loc) · 6.96 KB
/
webHandlers.ino
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
/*
* Intit Web handlders for different paths/URLs
*/
File fsUploadFile;
// Setup Web handlers
int initWebHandlers(){
//Handle root + index.htm
webServer.on("/", [](){
if( !handleFileRead("/") )
webServer.send(404, "text/plain", "FileNotFound");
});
webServer.on("/api", HTTP_GET, handleAPI);
webServer.on("/sys", HTTP_GET, handleSys);
webServer.on("/reset", handleReset);
//Setup FS Browser handlers
webServer.on("/list", HTTP_GET, handleFileList); //list directory
webServer.on("/edit", HTTP_GET, [](){ //load editor
if(!handleFileRead("/edit.htm")) webServer.send(404, "text/plain", "FileNotFound");
});
webServer.on("/edit", HTTP_PUT, handleFileCreate); //create file
webServer.on("/edit", HTTP_DELETE, handleFileDelete); //delete file
//first callback is called after the request has ended with all parsed arguments
//second callback handles file uploads at that location
webServer.on("/edit", HTTP_POST, [](){ webServer.send(200, "text/plain", ""); }, handleFileUpload);
//called when the url is not defined here
//use it to load content from SPIFFS
webServer.onNotFound([](){
if(!handleFileRead(webServer.uri()))
webServer.send(404, "text/plain", "FileNotFound");
});
webServer.begin(); //TODO:<-- Check success
return true;
}
/* DEBUG WEBSERVER
for ( uint8_t i = 0; i < webServer.args(); i++ ) {
page += " " + webServer.argName ( i ) + ": " + webServer.arg ( i ) + "\n";
}
*/
/*
* Handler functions
*/
// //get heap status, analog input value and all GPIO statuses in one json call
void handleSys() {
char buffer[100];
StaticJsonBuffer<100> jsonBuffer;
JsonObject &root = jsonBuffer.createObject();
root["heap"] = ESP.getFreeHeap();
root["analog"] = analogRead(A0);
root["gpio"] = (uint32_t)(((GPI | GPO) & 0xFFFF) | ((GP16I & 0x01) << 16));
root.printTo(buffer, 100);
webServer.send(200, "text/json", buffer );
}
// Webservice API handlder
void handleAPI() {
char buffer[500];
StaticJsonBuffer<500> jsonBuffer;
JsonObject &json = prepareAPI(jsonBuffer);
json.printTo(buffer, 500);
webServer.send(200, "text/json", buffer );
}
// Webserver reset handlder
void handleReset() {
String page = FPSTR(HTTP_HEAD);
page.replace("{v}", "Info");
page += FPSTR(HTTP_SCRIPT);
page += FPSTR(HTTP_STYLE);
//page += _customHeadElement;
page += FPSTR(HTTP_HEAD_END);
page += F("Module will reset in a few seconds.");
page += FPSTR(HTTP_END);
webServer.send(200, "text/html", page);
delay(5000);
//reset the ESP and reconfigure wifi
initWifi(RESET_WIFI);
ESP.reset();
delay(2000);
}
// SPIFF Browser functions
bool handleFileRead(String path){
//DEBUGPORT.println("*WEB handleFileRead: " + path);
if(path.endsWith("/")) path += "index.htm";
String contentType = getContentType(path);
String pathWithGz = path + ".gz";
if(SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)){
if(SPIFFS.exists(pathWithGz))
path += ".gz";
File file = SPIFFS.open(path, "r");
size_t sent = webServer.streamFile(file, contentType);
file.close();
return true;
}
return false;
}
// Upload handler
// for file in `ls -A1`; do curl -F "file=@$PWD/$file" esp8266fs.local/edit; done
void handleFileUpload(){
if(webServer.uri() != "/edit") return;
HTTPUpload& upload = webServer.upload();
if(upload.status == UPLOAD_FILE_START){
String filename = upload.filename;
if(!filename.startsWith("/")) filename = "/"+filename;
DEBUGPORT.print("*WEB handleFileUpload Name: "); DEBUGPORT.println(filename);
fsUploadFile = SPIFFS.open(filename, "w");
filename = String();
} else if(upload.status == UPLOAD_FILE_WRITE){
if(fsUploadFile)
fsUploadFile.write(upload.buf, upload.currentSize);
} else if(upload.status == UPLOAD_FILE_END){
if(fsUploadFile)
fsUploadFile.close();
DEBUGPORT.print("*WEB handleFileUpload Size: "); DEBUGPORT.println(upload.totalSize);
}
}
// Delete a file from SPIFF
void handleFileDelete(){
if(webServer.args() == 0)
return webServer.send(500, "text/plain", "BAD ARGS");
String path = webServer.arg(0);
DEBUGPORT.println("*WEB handleFileDelete: " + path);
if(path == "/")
return webServer.send(500, "text/plain", "BAD PATH");
if(!SPIFFS.exists(path))
return webServer.send(404, "text/plain", "FileNotFound");
SPIFFS.remove(path);
webServer.send(200, "text/plain", "");
path = String();
}
// Create a new file if it does not exist
void handleFileCreate(){
if(webServer.args() == 0)
return webServer.send(500, "text/plain", "BAD ARGS");
String path = webServer.arg(0);
DEBUGPORT.println("*WEB handleFileCreate: " + path);
if(path == "/")
return webServer.send(500, "text/plain", "BAD PATH");
if(SPIFFS.exists(path))
return webServer.send(500, "text/plain", "FILE EXISTS");
File file = SPIFFS.open(path, "w");
if(file)
file.close();
else
return webServer.send(500, "text/plain", "CREATE FAILED");
webServer.send(200, "text/plain", "");
path = String();
}
// Directory of files in SPIFF
void handleFileList() {
if(!webServer.hasArg("dir")) {webServer.send(500, "text/plain", "BAD ARGS"); return;}
String path = webServer.arg("dir");
DEBUGPORT.println("*WEB handleFileList: " + path);
Dir dir = SPIFFS.openDir(path);
path = String();
String output = "[";
while(dir.next()){
File entry = dir.openFile("r");
if (output != "[") output += ',';
bool isDir = false;
output += "{\"type\":\"";
output += (isDir)?"dir":"file";
output += "\",\"name\":\"";
output += String(entry.name()).substring(1);
output += "\"}";
entry.close();
}
output += "]";
webServer.send(200, "text/json", output);
}
/*
* Helper functions
*/
// Format size string
String formatBytes(size_t bytes){
if (bytes < 1024){
return String(bytes)+"B";
} else if(bytes < (1024 * 1024)){
return String(bytes/1024.0)+"KB";
} else if(bytes < (1024 * 1024 * 1024)){
return String(bytes/1024.0/1024.0)+"MB";
} else {
return String(bytes/1024.0/1024.0/1024.0)+"GB";
}
}
// Return type of content string from filename
String getContentType(String filename){
if(webServer.hasArg("download")) return "application/octet-stream";
else if(filename.endsWith(".htm")) return "text/html";
else if(filename.endsWith(".html")) return "text/html";
else if(filename.endsWith(".css")) return "text/css";
else if(filename.endsWith(".js")) return "application/javascript";
else if(filename.endsWith(".png")) return "image/png";
else if(filename.endsWith(".gif")) return "image/gif";
else if(filename.endsWith(".jpg")) return "image/jpeg";
else if(filename.endsWith(".ico")) return "image/x-icon";
else if(filename.endsWith(".xml")) return "text/xml";
else if(filename.endsWith(".pdf")) return "application/x-pdf";
else if(filename.endsWith(".zip")) return "application/x-zip";
else if(filename.endsWith(".gz")) return "application/x-gzip";
return "text/plain";
}