-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.cpp
More file actions
266 lines (237 loc) · 7.85 KB
/
Copy pathweb.cpp
File metadata and controls
266 lines (237 loc) · 7.85 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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "web.h"
#include "config.h"
#include "credentials.h"
#include "net.h"
#include "storage.h"
#include "photo.h"
#include "settings.h"
#include "battery.h"
#include "notify.h"
#include "palette.h"
#include "web_ui.h"
#include <WebServer.h>
#include <ArduinoJson.h>
namespace {
WebServer server(80);
volatile bool g_busy = false;
bool g_hasPending = false;
String g_pending;
bool g_commitPending = false;
String g_commitName;
String g_upName;
bool g_upOk = false;
void sendJson(int code, const String& body) {
server.send(code, "application/json", body);
}
// Optional HTTP Basic Auth. Off by default; enable by setting WEB_USER/WEB_PASS
// and WEB_REQUIRE_LOGIN=1 (see secrets.h.example).
bool authOK() {
if (!WEB_REQUIRE_LOGIN) return true;
if (strlen(WEB_USER) == 0 && strlen(WEB_PASS) == 0) return true;
if (!server.authenticate(WEB_USER, WEB_PASS)) { server.requestAuthentication(); return false; }
return true;
}
const char* backendName() {
switch (store::backend()) {
case store::BK_SD: return "sd";
case store::BK_FLASH: return "flash";
default: return "none";
}
}
void handleStatus() {
if (!authOK()) return;
JsonDocument d;
d["mode"] = net::isAp() ? "ap" : "sta";
d["ssid"] = net::ssid();
d["ip"] = net::ip();
d["store"] = backendName();
d["busy"] = g_busy;
d["current"] = store::currentId();
d["count"] = store::count();
d["ssEnabled"] = settings::slideshowEnabled();
d["ssInterval"] = settings::slideshowIntervalSec();
d["dither"] = settings::ditherMethod();
d["fit"] = settings::fitMode();
d["showBattery"] = settings::showBattery();
d["ha"] = strlen(HA_WEBHOOK_URL) > 0;
if (battery::present()) d["battery"] = battery::percent();
String out;
serializeJson(d, out);
sendJson(200, out);
}
void handleList() { if (!authOK()) return; sendJson(200, store::listJson()); }
// Stream a stored photo exactly as it looks on the e-paper (24-bit BMP). step=4
// gives a light gallery thumbnail; full=1 streams the full 800x480 for inspection.
void streamPreview(const uint8_t* frame, bool full) {
const int step = full ? 1 : 4;
const int W = PANEL_W / step, H = PANEL_H / step;
const int rowBytes = W * 3;
const int pad = (4 - (rowBytes & 3)) & 3;
const uint32_t dataSize = (uint32_t)(rowBytes + pad) * H;
const uint32_t fileSize = 54 + dataSize;
server.setContentLength(fileSize);
server.sendHeader("Cache-Control", "no-store");
server.send(200, "image/bmp", "");
uint8_t h[54];
memset(h, 0, sizeof(h));
h[0] = 'B'; h[1] = 'M';
h[2] = fileSize & 0xff; h[3] = (fileSize >> 8) & 0xff; h[4] = (fileSize >> 16) & 0xff; h[5] = (fileSize >> 24) & 0xff;
h[10] = 54; h[14] = 40;
h[18] = W & 0xff; h[19] = (W >> 8) & 0xff; h[20] = (W >> 16) & 0xff; h[21] = (W >> 24) & 0xff;
h[22] = H & 0xff; h[23] = (H >> 8) & 0xff; h[24] = (H >> 16) & 0xff; h[25] = (H >> 24) & 0xff;
h[26] = 1; h[28] = 24;
h[34] = dataSize & 0xff; h[35] = (dataSize >> 8) & 0xff; h[36] = (dataSize >> 16) & 0xff; h[37] = (dataSize >> 24) & 0xff;
h[38] = 0x13; h[39] = 0x0B; h[42] = 0x13; h[43] = 0x0B;
server.sendContent((const char*)h, 54);
uint8_t* row = (uint8_t*)malloc(rowBytes + pad);
if (!row) return;
memset(row, 0, rowBytes + pad);
for (int y = H - 1; y >= 0; --y) {
const uint8_t* s = frame + (size_t)(y * step) * PANEL_W;
for (int x = 0; x < W; ++x) {
uint8_t idx = s[x * step];
if (idx >= E6_COUNT) idx = 0;
const Rgb& c = kE6[idx];
row[x * 3] = c.b; row[x * 3 + 1] = c.g; row[x * 3 + 2] = c.r;
}
server.sendContent((const char*)row, rowBytes + pad);
}
free(row);
}
void handleThumb() {
if (!authOK()) return;
String id = server.arg("id");
if (!store::exists(id)) { server.send(404, "text/plain", "no"); return; }
uint8_t* frame = photo::frameFromId(id);
if (!frame) { server.send(500, "text/plain", "err"); return; }
streamPreview(frame, server.arg("full") == "1");
free(frame);
}
void handleUploadData() {
HTTPUpload& up = server.upload();
switch (up.status) {
case UPLOAD_FILE_START:
g_upName = up.filename;
g_upOk = store::beginUpload();
break;
case UPLOAD_FILE_WRITE:
if (g_upOk) g_upOk = store::appendUpload(up.buf, up.currentSize);
break;
case UPLOAD_FILE_END:
break;
default:
store::clearUpload();
g_upOk = false;
break;
}
}
void handleUploadDone() {
if (!authOK()) { store::clearUpload(); return; }
if (!g_upOk) { store::clearUpload(); sendJson(500, "{\"ok\":false}"); return; }
// Defer the heavy commit() to the main loop: the busy-callback pumps this handler
// mid-refresh, and running commit() (big PSRAM allocs) re-entrantly inside a render
// exhausts memory. The uploaded JPEG stays buffered until the loop processes it.
g_commitName = g_upName;
g_commitPending = true;
g_busy = true;
sendJson(200, "{\"ok\":true}");
}
void handleShow() {
if (!authOK()) return;
String id = server.arg("id");
if (!store::exists(id)) { sendJson(404, "{\"ok\":false}"); return; }
web::queueRender(id);
sendJson(200, "{\"ok\":true}");
}
void handleDelete() {
if (!authOK()) return;
store::remove(server.arg("id"));
sendJson(200, "{\"ok\":true}");
}
void handleNext() {
if (!authOK()) return;
String id = store::nextId(store::currentId());
if (id.length()) web::queueRender(id);
sendJson(200, "{\"ok\":true}");
}
void handleSlideshow() {
if (!authOK()) return;
bool en = server.arg("enabled") == "1";
uint32_t iv = (uint32_t)server.arg("interval").toInt();
if (iv < 60) iv = 60;
settings::setSlideshow(en, iv);
sendJson(200, "{\"ok\":true}");
}
void handleClear() {
if (!authOK()) return;
store::clearAll();
sendJson(200, "{\"ok\":true}");
}
void handleTestAlert() {
if (!authOK()) return;
notify::testAlert();
sendJson(200, "{\"ok\":true}");
}
void handleRender() {
if (!authOK()) return;
int dither = server.arg("dither").toInt();
int fit = server.arg("fit").toInt();
if (dither < 0 || dither > 2) dither = 0;
if (fit < 0 || fit > 1) fit = 0;
settings::setRender(dither, fit);
if (server.hasArg("battery")) settings::setBatteryOverlay(server.arg("battery") == "1");
sendJson(200, "{\"ok\":true}");
}
void handleWifi() {
if (!authOK()) return;
String ssid = server.arg("ssid");
String pass = server.arg("pass");
if (!ssid.length()) { sendJson(400, "{\"ok\":false}"); return; }
net::saveCreds(ssid, pass);
sendJson(200, "{\"ok\":true}");
delay(800);
ESP.restart();
}
void handleRoot() { if (!authOK()) return; server.send_P(200, "text/html", INDEX_HTML); }
void handleNotFound() {
if (net::isAp()) { server.send_P(200, "text/html", INDEX_HTML); return; }
server.send(404, "text/plain", "not found");
}
} // namespace
void web::begin() {
server.on("/", HTTP_GET, handleRoot);
server.on("/api/status", HTTP_GET, handleStatus);
server.on("/api/list", HTTP_GET, handleList);
server.on("/api/thumb", HTTP_GET, handleThumb);
server.on("/api/upload", HTTP_POST, handleUploadDone, handleUploadData);
server.on("/api/show", HTTP_POST, handleShow);
server.on("/api/delete", HTTP_POST, handleDelete);
server.on("/api/next", HTTP_POST, handleNext);
server.on("/api/slideshow", HTTP_POST, handleSlideshow);
server.on("/api/clear", HTTP_POST, handleClear);
server.on("/api/testalert", HTTP_POST, handleTestAlert);
server.on("/api/render", HTTP_POST, handleRender);
server.on("/api/wifi", HTTP_POST, handleWifi);
server.onNotFound(handleNotFound);
server.begin();
}
void web::loop() { server.handleClient(); }
bool web::busy() { return g_busy; }
void web::setBusy(bool b) { g_busy = b; }
void web::queueRender(const String& id) {
g_pending = id;
g_hasPending = true;
g_busy = true;
}
bool web::takePending(String& id) {
if (!g_hasPending) return false;
id = g_pending;
g_hasPending = false;
return true;
}
bool web::takePendingCommit(String& name) {
if (!g_commitPending) return false;
name = g_commitName;
g_commitPending = false;
return true;
}