This repository was archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathwebServer.cpp
More file actions
203 lines (159 loc) · 6.67 KB
/
webServer.cpp
File metadata and controls
203 lines (159 loc) · 6.67 KB
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
#include "webServer.h"
#include "ArduinoJson.h"
#include "LittleFS.h"
// Include the header file we create with webpack
#include "generated/html.h"
//Access to other classes for GUI functions
#include "WiFiManager.h"
#include "configManager.h"
#include "updater.h"
#include "dashboard.h"
void webServer::begin()
{
//to enable testing and debugging of the interface
DefaultHeaders::Instance().addHeader(PSTR("Access-Control-Allow-Origin"), PSTR("*"));
server.addHandler(&ws);
server.begin();
server.serveStatic("/download", LittleFS, "/");
server.onNotFound(requestHandler);
//handle uploads
server.on(PSTR("/upload"), HTTP_POST, [](AsyncWebServerRequest *request) {}, handleFileUpload);
bindAll();
}
void webServer::bindAll()
{
//Restart the ESP
server.on(PSTR("/api/restart"), HTTP_POST, [](AsyncWebServerRequest *request) {
request->send(200, PSTR("text/html"), ""); //respond first because of restart
ESP.restart();
});
//update WiFi details
server.on(PSTR("/api/wifi/set"), HTTP_POST, [](AsyncWebServerRequest *request) {
request->send(200, PSTR("text/html"), ""); //respond first because of wifi change
WiFiManager.setNewWifi(request->arg("ssid"), request->arg("pass"));
});
//update WiFi details with static IP
server.on(PSTR("/api/wifi/setStatic"), HTTP_POST, [](AsyncWebServerRequest *request) {
request->send(200, PSTR("text/html"), ""); //respond first because of wifi change
WiFiManager.setNewWifi(request->arg("ssid"), request->arg("pass"), request->arg("ip"), request->arg("sub"), request->arg("gw"), request->arg("dns"));
});
//update WiFi details
server.on(PSTR("/api/wifi/forget"), HTTP_POST, [](AsyncWebServerRequest *request) {
request->send(200, PSTR("text/html"), ""); //respond first because of wifi change
WiFiManager.forget();
});
//get WiFi details
server.on(PSTR("/api/wifi/get"), HTTP_GET, [](AsyncWebServerRequest *request) {
String JSON;
StaticJsonDocument<200> jsonBuffer;
jsonBuffer["captivePortal"] = WiFiManager.isCaptivePortal();
jsonBuffer["ssid"] = WiFiManager.SSID();
serializeJson(jsonBuffer, JSON);
request->send(200, PSTR("text/html"), JSON);
});
//get file listing
server.on(PSTR("/api/files/get"), HTTP_GET, [](AsyncWebServerRequest *request) {
String JSON;
StaticJsonDocument<1000> jsonBuffer;
JsonArray files = jsonBuffer.createNestedArray("files");
//get file listing
Dir dir = LittleFS.openDir("");
while (dir.next())
files.add(dir.fileName());
//get used and total data
FSInfo fs_info;
LittleFS.info(fs_info);
jsonBuffer["used"] = String(fs_info.usedBytes);
jsonBuffer["max"] = String(fs_info.totalBytes);
serializeJson(jsonBuffer, JSON);
request->send(200, PSTR("text/html"), JSON);
});
//remove file
server.on(PSTR("/api/files/remove"), HTTP_POST, [](AsyncWebServerRequest *request) {
LittleFS.remove("/" + request->arg("filename"));
request->send(200, PSTR("text/html"), "");
});
//update from LittleFS
server.on(PSTR("/api/update"), HTTP_POST, [](AsyncWebServerRequest *request) {
updater.requestStart("/" + request->arg("filename"));
request->send(200, PSTR("text/html"), "");
});
//update status
server.on(PSTR("/api/update-status"), HTTP_GET, [](AsyncWebServerRequest *request) {
String JSON;
StaticJsonDocument<200> jsonBuffer;
jsonBuffer["status"] = updater.getStatus();
serializeJson(jsonBuffer, JSON);
request->send(200, PSTR("text/html"), JSON);
});
//send binary configuration data
server.on(PSTR("/api/config/get"), HTTP_GET, [](AsyncWebServerRequest *request) {
AsyncResponseStream *response = request->beginResponseStream(PSTR("application/octet-stream"));
response->write(reinterpret_cast<char*>(&configManager.data), sizeof(configManager.data));
request->send(response);
});
//receive binary configuration data from body
server.on(
PSTR("/api/config/set"), HTTP_POST,
[this](AsyncWebServerRequest *request) {},
[](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {},
[this](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) {
static uint8_t buffer[sizeof(configManager.data)];
static uint32_t bufferIndex = 0;
for (size_t i = 0; i < len; i++)
{
buffer[bufferIndex] = data[i];
bufferIndex++;
}
if (index + len == total)
{
bufferIndex = 0;
configManager.saveRaw(buffer);
request->send(200, PSTR("text/html"), "");
}
});
//receive binary configuration data from body
server.on(
PSTR("/api/dash/set"), HTTP_POST,
[this](AsyncWebServerRequest *request) {},
[](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {},
[this](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) {
memcpy(reinterpret_cast<uint8_t *>(&(dash.data)) + (request->arg("start")).toInt(), data, (request->arg("length")).toInt());
request->send(200, PSTR("text/html"), "");
});
}
// Callback for the html
void webServer::serveProgmem(AsyncWebServerRequest *request)
{
// Dump the byte array in PROGMEM with a 200 HTTP code (OK)
AsyncWebServerResponse *response = request->beginResponse_P(200, PSTR("text/html"), html, html_len);
// Tell the browswer the content is Gzipped
response->addHeader(PSTR("Content-Encoding"), PSTR("gzip"));
request->send(response);
}
void webServer::handleFileUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final)
{
static File fsUploadFile;
if (!index)
{
Serial.println(PSTR("Start file upload"));
Serial.println(filename);
if (!filename.startsWith("/"))
filename = "/" + filename;
fsUploadFile = LittleFS.open(filename, "w");
}
for (size_t i = 0; i < len; i++)
{
fsUploadFile.write(data[i]);
}
if (final)
{
String JSON;
StaticJsonDocument<100> jsonBuffer;
jsonBuffer["success"] = fsUploadFile.isFile();
serializeJson(jsonBuffer, JSON);
request->send(200, PSTR("text/html"), JSON);
fsUploadFile.close();
}
}
webServer GUI;