#include "boomcast_web.h"
#include "boomcast_audio.h"
#include "boomcast_config.h"
#include "boomcast_network.h"
#include "boomcast_queue.h"
#include "boomcast_state.h"
BoomWeb boomWeb;
static String webJsonEscape(String value) {
value.replace("\\", "\\\\");
value.replace("\"", "\\\"");
value.replace("\r", "\\r");
value.replace("\n", "\\n");
return value;
}
static const char MANAGEMENT_HTML[] PROGMEM = R"BOOMHTML(
BoomCast
✦
BoomCast
Firework sensor · local management
Loading…
Today—
Queued—
Microphone—
Storage—
Live microphone shape
A low-resolution level preview. This graph is generated locally from the current sample block; it is not an audio stream.
Detection sensitivity
Higher means easier to trigger. This changes BoomCast's software thresholds, not microphone gain.
Device
Open this page again from the address shown on the e-paper status screen.
Listening for bangs, not conversations.
BoomCast keeps a rolling RAM buffer and writes a short clip to SD only around a detected event.
IP: —
Wi-Fi: —
Upload queue
Files stay on the SD card until the server returns a successful response.
Wi-Fi setup
Scan nearby networks, save credentials, and reboot into station mode.
)BOOMHTML";
bool BoomWeb::authorise() {
String password = boomState.prefs().getString("mgmt_pass", "");
if (password.length() && server_.authenticate(MANAGEMENT_USER, password.c_str())) return true;
server_.requestAuthentication(BASIC_AUTH, "BoomCast");
return false;
}
void BoomWeb::addNoCache() {
server_.sendHeader("Cache-Control", "no-store, max-age=0");
server_.sendHeader("Pragma", "no-cache");
}
void BoomWeb::sendJson(int code, const String& json) {
addNoCache();
server_.send(code, "application/json; charset=utf-8", json);
}
void BoomWeb::begin() {
server_.on("/", HTTP_GET, [this]() { handleRoot(); });
server_.on("/api/status", HTTP_GET, [this]() { handleStatus(); });
server_.on("/api/events", HTTP_GET, [this]() { handleEvents(); });
server_.on("/api/wifi/scan", HTTP_GET, [this]() { handleWifiScan(); });
server_.on("/api/wifi", HTTP_POST, [this]() { handleWifiSave(); });
server_.on("/api/wifi/forget", HTTP_POST, [this]() { handleWifiForget(); });
server_.on("/api/sensitivity", HTTP_POST, [this]() { handleSensitivity(); });
server_.on("/api/sync", HTTP_POST, [this]() { handleSync(); });
server_.on("/api/reboot", HTTP_POST, [this]() { handleReboot(); });
server_.onNotFound([this]() {
if (!authorise()) return;
sendJson(404, "{\"error\":\"Not found\"}");
});
server_.begin();
Serial.println("[Web] management interface listening on port 80");
}
void BoomWeb::handle() {
server_.handleClient();
}
void BoomWeb::handleRoot() {
if (!authorise()) return;
addNoCache();
server_.send_P(200, "text/html; charset=utf-8", MANAGEMENT_HTML);
}
void BoomWeb::handleStatus() {
if (!authorise()) return;
BoomStateSnapshot s = boomState.snapshot();
String json;
json.reserve(900);
json += "{\"version\":\"" + String(BOOMCAST_FIRMWARE_VERSION) + "\"";
json += ",\"wifi\":" + String(s.wifiConnected ? "true" : "false");
json += ",\"ap\":" + String(s.apMode ? "true" : "false");
json += ",\"sd\":" + String(s.sdReady ? "true" : "false");
json += ",\"capturing\":" + String(s.capturing ? "true" : "false");
json += ",\"syncing\":" + String(s.syncing ? "true" : "false");
json += ",\"ip\":\"" + webJsonEscape(s.ip) + "\"";
json += ",\"ssid\":\"" + webJsonEscape(s.ssid) + "\"";
json += ",\"queue\":" + String(s.queueCount);
json += ",\"today\":" + String(s.todayEvents);
json += ",\"sensitivity\":" + String(s.sensitivity);
json += ",\"syncMessage\":\"" + webJsonEscape(s.lastSyncMessage) + "\"";
json += ",\"waveform\":[";
for (size_t i = 0; i < BOOMCAST_WAVEFORM_POINTS; ++i) {
if (i) json += ',';
json += String((int)s.waveform[i]);
}
json += "]}";
sendJson(200, json);
}
void BoomWeb::handleEvents() {
if (!authorise()) return;
sendJson(200, boomQueue.listJson(MAX_QUEUE_ITEMS_SHOWN));
}
void BoomWeb::handleWifiScan() {
if (!authorise()) return;
sendJson(200, boomNetwork.scanJson());
}
void BoomWeb::handleWifiSave() {
if (!authorise()) return;
String ssid = server_.arg("ssid");
String password = server_.arg("password");
ssid.trim();
if (!ssid.length()) {
sendJson(400, "{\"error\":\"SSID is required\"}");
return;
}
if (!boomNetwork.saveCredentials(ssid, password)) {
sendJson(500, "{\"error\":\"Could not save Wi-Fi credentials\"}");
return;
}
sendJson(200, "{\"ok\":true,\"rebooting\":true}");
boomState.requestReboot(1200);
}
void BoomWeb::handleWifiForget() {
if (!authorise()) return;
boomNetwork.clearCredentials();
sendJson(200, "{\"ok\":true,\"rebooting\":true}");
boomState.requestReboot(1200);
}
void BoomWeb::handleSensitivity() {
if (!authorise()) return;
if (!server_.hasArg("value")) {
sendJson(400, "{\"error\":\"value is required\"}");
return;
}
int value = constrain(server_.arg("value").toInt(), 1, 100);
boomState.setSensitivity((uint8_t)value, true);
boomAudio.setSensitivity((uint8_t)value);
sendJson(200, "{\"ok\":true,\"value\":" + String(value) + "}");
}
void BoomWeb::handleSync() {
if (!authorise()) return;
if (!boomNetwork.connected()) {
sendJson(409, "{\"error\":\"No station Wi-Fi connection; upload unavailable in setup-AP-only mode\"}");
return;
}
String file = server_.arg("file");
if (file.length() && !boomQueue.exists(file)) {
sendJson(404, "{\"error\":\"Queued file not found\"}");
return;
}
boomState.requestSync(file);
sendJson(202, "{\"ok\":true,\"queued\":true}");
}
void BoomWeb::handleReboot() {
if (!authorise()) return;
sendJson(200, "{\"ok\":true,\"rebooting\":true}");
boomState.requestReboot(900);
}