btclock_v3/src/lib/webserver.cpp

1062 lines
31 KiB
C++
Raw Normal View History

2023-11-07 01:11:12 +01:00
#include "webserver.hpp"
2024-12-21 00:01:27 +01:00
static const char* JSON_CONTENT = "application/json";
static const char *const PROGMEM strSettings[] = {
"hostnamePrefix", "mempoolInstance", "nostrPubKey", "nostrRelay", "bitaxeHostname", "miningPoolName", "miningPoolUser", "nostrZapPubkey", "httpAuthUser", "httpAuthPass", "gitReleaseUrl", "poolLogosUrl"};
static const char *const PROGMEM uintSettings[] = {"minSecPriceUpd", "fullRefreshMin", "ledBrightness", "flMaxBrightness", "flEffectDelay", "luxLightToggle", "wpTimeout", "srcV2Currency"};
static const char *const PROGMEM boolSettings[] = {"fetchEurPrice", "ledTestOnPower", "ledFlashOnUpd",
"mdnsEnabled", "otaEnabled", "stealFocus",
"mcapBigChar", "useSatsSymbol", "useBlkCountdown",
"suffixPrice", "disableLeds", "ownDataSource",
"mowMode", "suffixShareDot", "flOffWhenDark",
"flAlwaysOn", "flDisable", "flFlashOnUpd",
"mempoolSecure", "useNostr", "bitaxeEnabled",
"miningPoolStats", "verticalDesc",
"nostrZapNotify", "stagingSource", "httpAuthEnabled"};
2023-11-07 01:11:12 +01:00
AsyncWebServer server(80);
AsyncEventSource events("/events");
TaskHandle_t eventSourceTaskHandle;
2023-11-07 01:11:12 +01:00
2024-12-21 00:01:27 +01:00
#define HTTP_OK 200
#define HTTP_BAD_REQUEST 400
void setupWebserver()
{
events.onConnect([](AsyncEventSourceClient *client)
{ client->send("welcome", NULL, millis(), 1000); });
2023-11-30 22:38:01 +01:00
server.addHandler(&events);
2024-09-03 01:36:44 +02:00
AsyncStaticWebHandler &staticHandler = server.serveStatic("/", LittleFS, "/").setDefaultFile("index.html");
server.rewrite("/convert", "/");
server.rewrite("/api", "/");
2024-09-03 01:36:44 +02:00
if (preferences.getBool("httpAuthEnabled", DEFAULT_HTTP_AUTH_ENABLED))
{
staticHandler.setAuthentication(
preferences.getString("httpAuthUser", DEFAULT_HTTP_AUTH_USERNAME),
preferences.getString("httpAuthPass", DEFAULT_HTTP_AUTH_PASSWORD));
}
// server.on("/", HTTP_GET, onIndex);
2023-11-30 22:38:01 +01:00
server.on("/api/status", HTTP_GET, onApiStatus);
server.on("/api/system_status", HTTP_GET, onApiSystemStatus);
server.on("/api/wifi_set_tx_power", HTTP_GET, onApiSetWifiTxPower);
2023-11-21 16:12:44 +01:00
2023-11-30 22:38:01 +01:00
server.on("/api/full_refresh", HTTP_GET, onApiFullRefresh);
2023-11-07 21:26:15 +01:00
2024-01-31 23:45:26 +01:00
server.on("/api/stop_datasources", HTTP_GET, onApiStopDataSources);
server.on("/api/restart_datasources", HTTP_GET, onApiRestartDataSources);
2023-11-30 22:38:01 +01:00
server.on("/api/action/pause", HTTP_GET, onApiActionPause);
server.on("/api/action/timer_restart", HTTP_GET, onApiActionTimerRestart);
2023-11-07 21:26:15 +01:00
2023-11-30 22:38:01 +01:00
server.on("/api/settings", HTTP_GET, onApiSettingsGet);
2023-11-07 21:26:15 +01:00
2023-11-30 22:38:01 +01:00
server.on("/api/show/screen", HTTP_GET, onApiShowScreen);
server.on("/api/show/currency", HTTP_GET, onApiShowCurrency);
2023-11-30 22:38:01 +01:00
server.on("/api/show/text", HTTP_GET, onApiShowText);
2023-11-17 19:28:40 +01:00
2024-12-21 00:01:27 +01:00
server.on("/api/screen/next", HTTP_GET, onApiScreenControl);
server.on("/api/screen/previous", HTTP_GET, onApiScreenControl);
2024-09-02 22:44:23 +02:00
2023-11-30 22:38:01 +01:00
AsyncCallbackJsonWebHandler *settingsPatchHandler =
new AsyncCallbackJsonWebHandler("/api/json/settings", onApiSettingsPatch);
server.addHandler(settingsPatchHandler);
2023-11-17 19:28:40 +01:00
2023-11-30 22:38:01 +01:00
AsyncCallbackJsonWebHandler *handler = new AsyncCallbackJsonWebHandler(
"/api/show/custom", onApiShowTextAdvanced);
server.addHandler(handler);
2023-11-07 21:26:15 +01:00
2023-11-30 22:38:01 +01:00
AsyncCallbackJsonWebHandler *lightsJsonHandler =
new AsyncCallbackJsonWebHandler("/api/lights/set", onApiLightsSetJson);
2023-11-30 22:38:01 +01:00
server.addHandler(lightsJsonHandler);
2023-11-30 22:38:01 +01:00
server.on("/api/lights/off", HTTP_GET, onApiLightsOff);
server.on("/api/lights/color", HTTP_GET, onApiLightsSetColor);
server.on("/api/lights", HTTP_GET, onApiLightsStatus);
server.on("/api/identify", HTTP_GET, onApiIdentify);
2023-11-08 12:18:59 +01:00
#ifdef HAS_FRONTLIGHT
2024-04-28 16:47:57 +02:00
server.on("/api/frontlight/on", HTTP_GET, onApiFrontlightOn);
server.on("/api/frontlight/flash", HTTP_GET, onApiFrontlightFlash);
2024-06-29 02:19:25 +02:00
server.on("/api/frontlight/status", HTTP_GET, onApiFrontlightStatus);
server.on("/api/frontlight/brightness", HTTP_GET, onApiFrontlightSetBrightness);
2024-04-28 16:47:57 +02:00
server.on("/api/frontlight/off", HTTP_GET, onApiFrontlightOff);
server.addRewrite(
new OneParamRewrite("/api/frontlight/brightness/{b}", "/api/frontlight/brightness?b={b}"));
#endif
2024-04-28 16:47:57 +02:00
2023-11-30 22:38:01 +01:00
// server.on("^\\/api\\/lights\\/([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$", HTTP_GET,
// onApiLightsSetColor);
2023-11-07 21:26:15 +01:00
if (preferences.getBool("otaEnabled", DEFAULT_OTA_ENABLED))
2024-06-29 02:19:25 +02:00
{
2024-06-09 00:45:46 +02:00
server.on("/upload/firmware", HTTP_POST, onFirmwareUpdate, asyncFirmwareUpdateHandler);
server.on("/upload/webui", HTTP_POST, onFirmwareUpdate, asyncWebuiUpdateHandler);
server.on("/api/firmware/auto_update", HTTP_GET, onAutoUpdateFirmware);
2024-06-09 00:45:46 +02:00
}
2023-11-30 22:38:01 +01:00
server.on("/api/restart", HTTP_GET, onApiRestart);
server.addRewrite(
new OneParamRewrite("/api/show/currency/{c}", "/api/show/currency?c={c}"));
server.addRewrite(new OneParamRewrite("/api/lights/color/{color}",
2023-11-30 22:38:01 +01:00
"/api/lights/color?c={color}"));
server.addRewrite(
new OneParamRewrite("/api/show/screen/{s}", "/api/show/screen?s={s}"));
server.addRewrite(
new OneParamRewrite("/api/show/text/{text}", "/api/show/text?t={text}"));
server.addRewrite(new OneParamRewrite("/api/show/number/{number}",
"/api/show/text?t={text}"));
2023-11-07 01:11:12 +01:00
2023-11-30 22:38:01 +01:00
server.onNotFound(onNotFound);
2023-11-07 01:11:12 +01:00
2023-11-30 22:38:01 +01:00
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", "*");
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Methods",
"GET, PATCH, POST, OPTIONS");
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Headers", "*");
2023-11-30 22:38:01 +01:00
server.begin();
if (preferences.getBool("mdnsEnabled", DEFAULT_MDNS_ENABLED))
{
if (!MDNS.begin(getMyHostname()))
{
2023-11-30 22:38:01 +01:00
Serial.println(F("Error setting up MDNS responder!"));
while (1)
{
2023-11-30 22:38:01 +01:00
delay(1000);
}
}
2023-11-30 22:38:01 +01:00
MDNS.addService("http", "tcp", 80);
MDNS.addServiceTxt("http", "tcp", "model", "BTClock");
MDNS.addServiceTxt("http", "tcp", "version", "3.0");
MDNS.addServiceTxt("http", "tcp", "rev", GIT_REV);
2024-05-18 23:00:08 +02:00
MDNS.addServiceTxt("http", "tcp", "hw_rev", getHwRev());
2023-11-30 22:38:01 +01:00
}
2023-11-30 22:38:01 +01:00
xTaskCreate(eventSourceTask, "eventSourceTask", 4096, NULL, tskIDLE_PRIORITY,
&eventSourceTaskHandle);
2023-11-07 01:11:12 +01:00
}
2023-11-30 22:38:01 +01:00
void stopWebServer() { server.end(); }
void onFirmwareUpdate(AsyncWebServerRequest *request)
{
bool shouldReboot = !Update.hasError();
AsyncWebServerResponse *response = request->beginResponse(200, "text/plain", shouldReboot ? "OK" : "FAIL");
response->addHeader("Connection", "close");
request->send(response);
}
void onAutoUpdateFirmware(AsyncWebServerRequest *request)
2024-09-11 17:40:44 +02:00
{
UpdateMessage msg = {UPDATE_ALL};
2024-09-11 17:40:44 +02:00
if (xQueueSend(otaQueue, &msg, 0) == pdTRUE)
{
request->send(200, "application/json", "{\"msg\":\"Firmware update triggered\"}");
2024-09-11 17:40:44 +02:00
}
else
{
request->send(503,"application/json", "{\"msg\":\"Update already in progress\"}");
2024-09-11 17:40:44 +02:00
}
}
void asyncWebuiUpdateHandler(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final)
{
asyncFileUpdateHandler(request, filename, index, data, len, final, U_SPIFFS);
}
void asyncFileUpdateHandler(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final, int command)
{
2024-06-09 00:45:46 +02:00
if (!index)
{
Serial.printf("Update Start: %s\n", filename.c_str());
2024-06-09 00:45:46 +02:00
if (command == U_FLASH)
{
2024-06-09 00:45:46 +02:00
// Update.runAsync(true);
if (!Update.begin((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000), command)
{
Update.printError(Serial);
return;
}
}
else if (command == U_SPIFFS)
{
size_t fsSize = UPDATE_SIZE_UNKNOWN; // or specify the size of your filesystem partition
if (!Update.begin(fsSize, U_SPIFFS)) // or U_FS for LittleFS
{
Update.printError(Serial);
return;
}
}
}
if (!Update.hasError())
{
if (Update.write(data, len) != len)
{
Update.printError(Serial);
}
}
if (final)
{
if (Update.end(true))
{
Serial.printf("Update Success: %uB\n", index + len);
onApiRestart(request);
}
else
{
Update.printError(Serial);
}
}
}
void asyncFirmwareUpdateHandler(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final)
{
2024-06-09 00:45:46 +02:00
asyncFileUpdateHandler(request, filename, index, data, len, final, U_FLASH);
}
JsonDocument getStatusObject()
{
JsonDocument root;
2023-11-07 21:26:15 +01:00
root["currentScreen"] = ScreenHandler::getCurrentScreen();
2023-11-30 22:38:01 +01:00
root["numScreens"] = NUM_SCREENS;
root["timerRunning"] = isTimerActive();
root["espUptime"] = esp_timer_get_time() / 1000000;
// root["currentPrice"] = getPrice();
// root["currentBlockHeight"] = getBlockHeight();
root["espFreeHeap"] = ESP.getFreeHeap();
root["espHeapSize"] = ESP.getHeapSize();
// root["espFreePsram"] = ESP.getFreePsram();
// root["espPsramSize"] = ESP.getPsramSize();
2023-11-07 21:26:15 +01:00
JsonObject conStatus = root["connectionStatus"].to<JsonObject>();
2024-12-21 00:01:27 +01:00
2023-11-30 22:38:01 +01:00
conStatus["price"] = isPriceNotifyConnected();
conStatus["blocks"] = isBlockNotifyConnected();
conStatus["V2"] = V2Notify::isV2NotifyConnected();
2024-07-15 16:36:51 +02:00
conStatus["nostr"] = nostrConnected();
2023-11-08 12:18:59 +01:00
2023-11-30 22:38:01 +01:00
root["rssi"] = WiFi.RSSI();
root["currency"] = getCurrencyCode(ScreenHandler::getCurrentCurrency());
2024-06-29 02:19:25 +02:00
#ifdef HAS_FRONTLIGHT
std::vector<uint16_t> statuses = frontlightGetStatus();
uint16_t arr[NUM_SCREENS];
std::copy(statuses.begin(), statuses.end(), arr);
JsonArray data = root["flStatus"].to<JsonArray>();
copyArray(arr, data);
if (hasLightLevel())
{
root["lightLevel"] = getLightLevel();
}
#endif
2023-11-30 22:38:01 +01:00
return root;
}
JsonDocument getLedStatusObject()
{
JsonDocument root;
JsonArray colors = root["data"].to<JsonArray>();
2023-11-30 22:38:01 +01:00
// Adafruit_NeoPixel pix = getPixels();
for (uint i = 0; i < pixels.numPixels(); i++)
{
2023-11-30 22:38:01 +01:00
uint32_t pixColor = pixels.getPixelColor(pixels.numPixels() - i - 1);
uint red = (pixColor >> 16) & 0xFF;
uint green = (pixColor >> 8) & 0xFF;
uint blue = pixColor & 0xFF;
char hexColor[8];
snprintf(hexColor, sizeof(hexColor), "#%02X%02X%02X", red, green, blue);
2023-11-30 22:38:01 +01:00
JsonObject object = colors.add<JsonObject>();
2023-11-30 22:38:01 +01:00
object["red"] = red;
object["green"] = green;
object["blue"] = blue;
object["hex"] = hexColor;
}
return root;
}
2024-12-21 00:01:27 +01:00
void eventSourceUpdate() {
if (!events.count()) return;
JsonDocument doc = getStatusObject();
doc["leds"] = getLedStatusObject()["data"];
// Get current EPD content directly as array
std::array<String, NUM_SCREENS> epdContent = getCurrentEpdContent();
// Add EPD content arrays
JsonArray data = doc["data"].to<JsonArray>();
// Copy array elements directly
for(const auto& content : epdContent) {
data.add(content);
}
2024-12-21 00:01:27 +01:00
String buffer;
serializeJson(doc, buffer);
events.send(buffer.c_str(), "status");
}
/**
* @Api
* @Path("/api/status")
*/
void onApiStatus(AsyncWebServerRequest *request)
{
2023-11-30 22:38:01 +01:00
AsyncResponseStream *response =
2024-12-21 00:01:27 +01:00
request->beginResponseStream(JSON_CONTENT);
JsonDocument root = getStatusObject();
2024-12-21 00:01:27 +01:00
// Get current EPD content directly as array
std::array<String, NUM_SCREENS> epdContent = getCurrentEpdContent();
// Add EPD content arrays
JsonArray data = root["data"].to<JsonArray>();
2024-12-21 00:01:27 +01:00
// Copy array elements directly
for(const auto& content : epdContent) {
data.add(content);
}
2023-11-07 21:26:15 +01:00
2023-11-30 22:38:01 +01:00
root["leds"] = getLedStatusObject()["data"];
serializeJson(root, *response);
2023-11-07 21:26:15 +01:00
2023-11-30 22:38:01 +01:00
request->send(response);
2023-11-07 21:26:15 +01:00
}
/**
* @Api
* @Path("/api/action/pause")
*/
void onApiActionPause(AsyncWebServerRequest *request)
{
2023-11-30 22:38:01 +01:00
setTimerActive(false);
2024-12-21 00:01:27 +01:00
request->send(HTTP_OK);
2023-11-07 21:26:15 +01:00
};
/**
* @Api
* @Path("/api/action/timer_restart")
*/
void onApiActionTimerRestart(AsyncWebServerRequest *request)
{
2023-11-30 22:38:01 +01:00
setTimerActive(true);
2024-12-21 00:01:27 +01:00
request->send(HTTP_OK);
2023-11-07 21:26:15 +01:00
}
/**
* @Api
* @Path("/api/full_refresh")
*/
void onApiFullRefresh(AsyncWebServerRequest *request)
{
2023-11-30 22:38:01 +01:00
forceFullRefresh();
std::array<String, NUM_SCREENS> newEpdContent = getCurrentEpdContent();
2023-11-30 22:38:01 +01:00
setEpdContent(newEpdContent, true);
2024-12-21 00:01:27 +01:00
request->send(HTTP_OK);
}
2023-11-14 23:09:23 +01:00
/**
* @Api
* @Path("/api/show/screen")
*/
void onApiShowScreen(AsyncWebServerRequest *request)
{
if (request->hasParam("s"))
{
2024-06-28 17:56:08 +02:00
const AsyncWebParameter *p = request->getParam("s");
2023-11-30 22:38:01 +01:00
uint currentScreen = p->value().toInt();
ScreenHandler::setCurrentScreen(currentScreen);
2023-11-30 22:38:01 +01:00
}
2024-12-21 00:01:27 +01:00
request->send(HTTP_OK);
2023-11-07 21:26:15 +01:00
}
2024-09-02 22:44:23 +02:00
/**
* @Api
* @Path("/api/screen/next")
*/
2024-12-21 00:01:27 +01:00
void onApiScreenControl(AsyncWebServerRequest *request) {
const String& action = request->url();
if (action.endsWith("/next")) {
ScreenHandler::nextScreen();
2024-12-21 00:01:27 +01:00
} else if (action.endsWith("/previous")) {
ScreenHandler::previousScreen();
2024-12-21 00:01:27 +01:00
}
request->send(HTTP_OK);
2024-09-02 22:44:23 +02:00
}
void onApiShowText(AsyncWebServerRequest *request)
{
if (request->hasParam("t"))
{
2024-06-28 17:56:08 +02:00
const AsyncWebParameter *p = request->getParam("t");
2023-11-30 22:38:01 +01:00
String t = p->value();
t.toUpperCase(); // This is needed as long as lowercase letters are glitchy
2023-11-07 21:26:15 +01:00
2023-11-30 22:38:01 +01:00
std::array<String, NUM_SCREENS> textEpdContent;
for (uint i = 0; i < NUM_SCREENS; i++)
{
2023-11-30 22:38:01 +01:00
textEpdContent[i] = t[i];
2023-11-14 19:46:29 +01:00
}
2023-11-30 22:38:01 +01:00
setEpdContent(textEpdContent);
}
ScreenHandler::setCurrentScreen(SCREEN_CUSTOM);
2024-12-21 00:01:27 +01:00
request->send(HTTP_OK);
2023-11-14 19:46:29 +01:00
}
void onApiShowTextAdvanced(AsyncWebServerRequest *request, JsonVariant &json)
{
2023-11-30 22:38:01 +01:00
JsonArray screens = json.as<JsonArray>();
2023-11-22 16:26:57 +01:00
2023-11-30 22:38:01 +01:00
std::array<String, NUM_SCREENS> epdContent;
int i = 0;
for (JsonVariant s : screens)
{
2023-11-30 22:38:01 +01:00
epdContent[i] = s.as<String>();
i++;
}
2023-11-22 16:26:57 +01:00
2023-11-30 22:38:01 +01:00
setEpdContent(epdContent);
2023-11-17 19:28:40 +01:00
ScreenHandler::setCurrentScreen(SCREEN_CUSTOM);
2024-12-21 00:01:27 +01:00
request->send(HTTP_OK);
2023-11-30 22:38:01 +01:00
}
2023-11-17 19:28:40 +01:00
void onApiSettingsPatch(AsyncWebServerRequest *request, JsonVariant &json)
{
2024-09-03 01:36:44 +02:00
if (
preferences.getBool("httpAuthEnabled", DEFAULT_HTTP_AUTH_ENABLED) &&
!request->authenticate(
preferences.getString("httpAuthUser", DEFAULT_HTTP_AUTH_USERNAME).c_str(),
preferences.getString("httpAuthPass", DEFAULT_HTTP_AUTH_PASSWORD).c_str()))
{
return request->requestAuthentication();
}
2023-11-30 22:38:01 +01:00
JsonObject settings = json.as<JsonObject>();
bool settingsChanged = true;
2024-12-21 00:15:08 +01:00
if (settings["fgColor"].is<String>())
{
2023-11-30 22:38:01 +01:00
String fgColor = settings["fgColor"].as<String>();
2024-12-21 00:01:27 +01:00
uint32_t color = strtol(fgColor.c_str(), NULL, 16);
preferences.putUInt("fgColor", color);
setFgColor(color);
2023-11-30 22:38:01 +01:00
Serial.print(F("Setting foreground color to "));
2024-12-21 00:01:27 +01:00
Serial.println(color);
2023-11-30 22:38:01 +01:00
settingsChanged = true;
}
2024-12-21 00:15:08 +01:00
if (settings["bgColor"].is<String>())
{
2023-11-30 22:38:01 +01:00
String bgColor = settings["bgColor"].as<String>();
2024-12-21 00:01:27 +01:00
uint32_t color = strtol(bgColor.c_str(), NULL, 16);
preferences.putUInt("bgColor", color);
setBgColor(color);
2023-11-30 22:38:01 +01:00
Serial.print(F("Setting background color to "));
Serial.println(bgColor.c_str());
settingsChanged = true;
}
2024-12-21 00:15:08 +01:00
if (settings["timePerScreen"].is<uint>())
{
2023-11-30 22:38:01 +01:00
preferences.putUInt("timerSeconds",
settings["timePerScreen"].as<uint>() * 60);
}
for (String setting : strSettings)
{
2024-12-21 00:15:08 +01:00
if (settings[setting].is<String>())
{
2023-11-30 22:38:01 +01:00
preferences.putString(setting.c_str(), settings[setting].as<String>());
Serial.printf("Setting %s to %s\r\n", setting.c_str(),
2024-12-21 00:15:08 +01:00
settings[setting].as<String>().c_str());
2023-11-30 22:38:01 +01:00
}
}
2024-12-21 00:01:27 +01:00
2023-11-30 22:38:01 +01:00
for (String setting : uintSettings)
{
2024-12-21 00:15:08 +01:00
if (settings[setting].is<uint>())
{
2023-11-30 22:38:01 +01:00
preferences.putUInt(setting.c_str(), settings[setting].as<uint>());
Serial.printf("Setting %s to %d\r\n", setting.c_str(),
settings[setting].as<uint>());
}
}
2024-12-21 00:15:08 +01:00
if (settings["tzOffset"].is<int>())
{
2023-11-30 22:38:01 +01:00
int gmtOffset = settings["tzOffset"].as<int>() * 60;
size_t written = preferences.putInt("gmtOffset", gmtOffset);
Serial.printf("Setting %s to %d (%d minutes, written %d)\r\n", "gmtOffset",
gmtOffset, settings["tzOffset"].as<int>(), written);
}
for (String setting : boolSettings)
{
2024-12-21 00:15:08 +01:00
if (settings[setting].is<bool>())
{
2024-12-21 00:15:08 +01:00
preferences.putBool(setting.c_str(), settings[setting].as<bool>());
2023-11-30 22:38:01 +01:00
Serial.printf("Setting %s to %d\r\n", setting.c_str(),
2024-12-21 00:15:08 +01:00
settings[setting].as<bool>());
2023-11-30 22:38:01 +01:00
}
}
2024-12-21 00:15:08 +01:00
if (settings["screens"].is<JsonArray>())
{
for (JsonVariant screen : settings["screens"].as<JsonArray>())
{
2023-11-30 22:38:01 +01:00
JsonObject s = screen.as<JsonObject>();
uint id = s["id"].as<uint>();
String key = "screen[" + String(id) + "]";
String prefKey = "screen" + String(id) + "Visible";
2024-12-21 00:15:08 +01:00
bool visible = s["enabled"].as<bool>();
2023-11-30 22:38:01 +01:00
preferences.putBool(prefKey.c_str(), visible);
}
}
2024-12-21 00:15:08 +01:00
if (settings["actCurrencies"].is<JsonArray>())
{
String actCurrencies;
for (JsonVariant cur : settings["actCurrencies"].as<JsonArray>())
{
if (!actCurrencies.isEmpty())
{
actCurrencies += ",";
}
actCurrencies += cur.as<String>();
}
preferences.putString("actCurrencies", actCurrencies.c_str());
2024-12-21 00:15:08 +01:00
Serial.printf("Set actCurrencies: %s\n", actCurrencies.c_str());
}
2024-12-21 00:15:08 +01:00
if (settings["txPower"].is<int>())
{
2023-11-30 22:38:01 +01:00
int txPower = settings["txPower"].as<int>();
if (txPower == 80)
{
2023-11-30 22:38:01 +01:00
preferences.remove("txPower");
if (WiFi.getTxPower() != 80)
{
2023-11-30 22:38:01 +01:00
ESP.restart();
}
}
else if (static_cast<int>(wifi_power_t::WIFI_POWER_MINUS_1dBm) <=
txPower &&
txPower <= static_cast<int>(wifi_power_t::WIFI_POWER_19_5dBm))
{
2023-11-30 22:38:01 +01:00
// is valid value
if (WiFi.setTxPower(static_cast<wifi_power_t>(txPower)))
{
2023-11-30 22:38:01 +01:00
Serial.printf("Set WiFi Tx power to: %d\n", txPower);
preferences.putInt("txPower", txPower);
settingsChanged = true;
}
2023-11-21 16:12:44 +01:00
}
2023-11-30 22:38:01 +01:00
}
2023-11-21 16:12:44 +01:00
2024-12-21 00:01:27 +01:00
request->send(HTTP_OK);
if (settingsChanged)
{
2023-11-30 22:38:01 +01:00
queueLedEffect(LED_FLASH_SUCCESS);
}
2023-11-17 19:28:40 +01:00
}
void onApiRestart(AsyncWebServerRequest *request)
{
2024-12-21 00:01:27 +01:00
request->send(HTTP_OK);
if (events.count())
events.send("closing");
2023-11-30 22:38:01 +01:00
delay(500);
2023-11-30 22:38:01 +01:00
esp_restart();
2023-11-07 21:26:15 +01:00
}
void onApiIdentify(AsyncWebServerRequest *request)
{
queueLedEffect(LED_FLASH_IDENTIFY);
2024-12-21 00:01:27 +01:00
request->send(HTTP_OK);
}
2023-11-07 21:26:15 +01:00
/**
* @Api
* @Method GET
* @Path("/api/settings")
*/
void onApiSettingsGet(AsyncWebServerRequest *request)
{
2024-09-03 01:36:44 +02:00
if (
preferences.getBool("httpAuthEnabled", DEFAULT_HTTP_AUTH_ENABLED) &&
!request->authenticate(
preferences.getString("httpAuthUser", DEFAULT_HTTP_AUTH_USERNAME).c_str(),
preferences.getString("httpAuthPass", DEFAULT_HTTP_AUTH_PASSWORD).c_str()))
{
return request->requestAuthentication();
}
JsonDocument root;
2023-11-30 22:38:01 +01:00
root["numScreens"] = NUM_SCREENS;
root["fgColor"] = getFgColor();
root["bgColor"] = getBgColor();
root["timerSeconds"] = getTimerSeconds();
root["timerRunning"] = isTimerActive();
root["minSecPriceUpd"] = preferences.getUInt(
"minSecPriceUpd", DEFAULT_SECONDS_BETWEEN_PRICE_UPDATE);
root["fullRefreshMin"] =
preferences.getUInt("fullRefreshMin", DEFAULT_MINUTES_FULL_REFRESH);
root["wpTimeout"] = preferences.getUInt("wpTimeout", DEFAULT_WP_TIMEOUT);
root["tzOffset"] = preferences.getInt("gmtOffset", DEFAULT_TIME_OFFSET_SECONDS) / 60;
2023-11-30 22:38:01 +01:00
root["mempoolInstance"] =
preferences.getString("mempoolInstance", DEFAULT_MEMPOOL_INSTANCE);
root["mempoolSecure"] = preferences.getBool("mempoolSecure", DEFAULT_MEMPOOL_SECURE);
2024-07-11 22:08:42 +02:00
root["useNostr"] = preferences.getBool("useNostr", DEFAULT_USE_NOSTR);
root["ledTestOnPower"] = preferences.getBool("ledTestOnPower", DEFAULT_LED_TEST_ON_POWER);
root["ledFlashOnUpd"] = preferences.getBool("ledFlashOnUpd", DEFAULT_LED_FLASH_ON_UPD);
root["ledBrightness"] = preferences.getUInt("ledBrightness", DEFAULT_LED_BRIGHTNESS);
root["stealFocus"] = preferences.getBool("stealFocus", DEFAULT_STEAL_FOCUS);
root["mcapBigChar"] = preferences.getBool("mcapBigChar", DEFAULT_MCAP_BIG_CHAR);
root["mdnsEnabled"] = preferences.getBool("mdnsEnabled", DEFAULT_MDNS_ENABLED);
root["otaEnabled"] = preferences.getBool("otaEnabled", DEFAULT_OTA_ENABLED);
// root["fetchEurPrice"] = preferences.getBool("fetchEurPrice", DEFAULT_FETCH_EUR_PRICE);
root["useSatsSymbol"] = preferences.getBool("useSatsSymbol", DEFAULT_USE_SATS_SYMBOL);
root["useBlkCountdown"] = preferences.getBool("useBlkCountdown", DEFAULT_USE_BLOCK_COUNTDOWN);
root["suffixPrice"] = preferences.getBool("suffixPrice", DEFAULT_SUFFIX_PRICE);
root["disableLeds"] = preferences.getBool("disableLeds", DEFAULT_DISABLE_LEDS);
2024-11-27 11:33:12 +01:00
root["mowMode"] = preferences.getBool("mowMode", DEFAULT_MOW_MODE);
2024-12-18 00:50:20 +01:00
root["verticalDesc"] = preferences.getBool("verticalDesc", DEFAULT_VERTICAL_DESC);
root["suffixShareDot"] = preferences.getBool("suffixShareDot", DEFAULT_SUFFIX_SHARE_DOT);
root["hostnamePrefix"] = preferences.getString("hostnamePrefix", DEFAULT_HOSTNAME_PREFIX);
2023-11-30 22:38:01 +01:00
root["hostname"] = getMyHostname();
root["ip"] = WiFi.localIP();
root["txPower"] = WiFi.getTxPower();
root["ownDataSource"] = preferences.getBool("ownDataSource", DEFAULT_OWN_DATA_SOURCE);
root["stagingSource"] = preferences.getBool("stagingSource", DEFAULT_STAGING_SOURCE);
root["srcV2Currency"] = preferences.getChar("srcV2Currency", DEFAULT_V2_SOURCE_CURRENCY);
2023-11-07 21:26:15 +01:00
2024-07-11 22:08:42 +02:00
root["nostrPubKey"] = preferences.getString("nostrPubKey", DEFAULT_NOSTR_NPUB);
root["nostrRelay"] = preferences.getString("nostrRelay", DEFAULT_NOSTR_RELAY);
2024-08-24 16:27:55 +03:00
root["nostrZapNotify"] = preferences.getBool("nostrZapNotify", DEFAULT_ZAP_NOTIFY_ENABLED);
root["nostrZapPubkey"] = preferences.getString("nostrZapPubkey", DEFAULT_ZAP_NOTIFY_PUBKEY);
root["ledFlashOnZap"] = preferences.getBool("ledFlashOnZap", DEFAULT_LED_FLASH_ON_ZAP);
root["gitReleaseUrl"] = preferences.getString("gitReleaseUrl", DEFAULT_GIT_RELEASE_URL);
2024-08-24 16:27:55 +03:00
2024-07-29 20:49:46 +02:00
root["bitaxeEnabled"] = preferences.getBool("bitaxeEnabled", DEFAULT_BITAXE_ENABLED);
root["bitaxeHostname"] = preferences.getString("bitaxeHostname", DEFAULT_BITAXE_HOSTNAME);
2024-12-18 17:07:44 -06:00
root["miningPoolStats"] = preferences.getBool("miningPoolStats", DEFAULT_MINING_POOL_STATS_ENABLED);
2024-12-17 20:17:21 -06:00
root["miningPoolName"] = preferences.getString("miningPoolName", DEFAULT_MINING_POOL_NAME);
root["miningPoolUser"] = preferences.getString("miningPoolUser", DEFAULT_MINING_POOL_USER);
root["availablePools"] = PoolFactory::getAvailablePools();
2024-09-03 01:36:44 +02:00
root["httpAuthEnabled"] = preferences.getBool("httpAuthEnabled", DEFAULT_HTTP_AUTH_ENABLED);
root["httpAuthUser"] = preferences.getString("httpAuthUser", DEFAULT_HTTP_AUTH_USERNAME);
root["httpAuthPass"] = preferences.getString("httpAuthPass", DEFAULT_HTTP_AUTH_PASSWORD);
#ifdef HAS_FRONTLIGHT
2024-04-27 16:48:06 +02:00
root["hasFrontlight"] = true;
2024-09-16 21:50:28 +02:00
root["flDisable"] = preferences.getBool("flDisable");
root["flMaxBrightness"] = preferences.getUInt("flMaxBrightness", DEFAULT_FL_MAX_BRIGHTNESS);
root["flAlwaysOn"] = preferences.getBool("flAlwaysOn", DEFAULT_FL_ALWAYS_ON);
root["flEffectDelay"] = preferences.getUInt("flEffectDelay", DEFAULT_FL_EFFECT_DELAY);
root["flFlashOnUpd"] = preferences.getBool("flFlashOnUpd", DEFAULT_FL_FLASH_ON_UPDATE);
root["flFlashOnZap"] = preferences.getBool("flFlashOnZap", DEFAULT_FL_FLASH_ON_ZAP);
2024-06-29 02:19:25 +02:00
root["hasLightLevel"] = hasLightLevel();
root["luxLightToggle"] = preferences.getUInt("luxLightToggle", DEFAULT_LUX_LIGHT_TOGGLE);
2024-12-10 15:13:17 +01:00
root["flOffWhenDark"] = preferences.getBool("flOffWhenDark", DEFAULT_FL_OFF_WHEN_DARK);
#else
2024-04-27 16:48:06 +02:00
root["hasFrontlight"] = false;
2024-06-29 02:19:25 +02:00
root["hasLightLevel"] = false;
#endif
2024-04-27 16:48:06 +02:00
2024-06-09 00:45:46 +02:00
root["hwRev"] = getHwRev();
root["fsRev"] = getFsRev();
2023-11-07 21:26:15 +01:00
#ifdef GIT_REV
2023-11-30 22:38:01 +01:00
root["gitRev"] = String(GIT_REV);
2023-11-07 21:26:15 +01:00
#endif
2024-06-09 00:45:46 +02:00
#ifdef GIT_TAG
root["gitTag"] = String(GIT_TAG);
#endif
2023-11-07 21:26:15 +01:00
#ifdef LAST_BUILD_TIME
2023-11-30 22:38:01 +01:00
root["lastBuildTime"] = String(LAST_BUILD_TIME);
2023-11-07 21:26:15 +01:00
#endif
JsonArray screens = root["screens"].to<JsonArray>();
2023-11-07 21:26:15 +01:00
root["actCurrencies"] = getActiveCurrencies();
root["availableCurrencies"] = getAvailableCurrencies();
2024-07-29 20:49:46 +02:00
std::vector<ScreenMapping> screenNameMap = getScreenNameMap();
2023-11-08 12:18:59 +01:00
for (int i = 0; i < screenNameMap.size(); i++)
{
JsonObject o = screens.add<JsonObject>();
2024-07-29 20:49:46 +02:00
String key = "screen" + String(screenNameMap.at(i).value) + "Visible";
o["id"] = screenNameMap.at(i).value;
o["name"] = String(screenNameMap.at(i).name);
2023-11-30 22:38:01 +01:00
o["enabled"] = preferences.getBool(key.c_str(), true);
}
2023-11-07 21:26:15 +01:00
2024-12-20 23:02:54 +01:00
root["poolLogosUrl"] = preferences.getString("poolLogosUrl", DEFAULT_MINING_POOL_LOGOS_URL);
2023-11-30 22:38:01 +01:00
AsyncResponseStream *response =
2024-12-21 00:01:27 +01:00
request->beginResponseStream(JSON_CONTENT);
2023-11-30 22:38:01 +01:00
serializeJson(root, *response);
2023-11-07 21:26:15 +01:00
2023-11-30 22:38:01 +01:00
request->send(response);
2023-11-07 21:26:15 +01:00
}
bool processEpdColorSettings(AsyncWebServerRequest *request)
{
2023-11-30 22:38:01 +01:00
bool settingsChanged = false;
if (request->hasParam("fgColor", true))
{
2024-06-28 17:56:08 +02:00
const AsyncWebParameter *fgColor = request->getParam("fgColor", true);
2024-12-21 00:01:27 +01:00
uint32_t color = strtol(fgColor->value().c_str(), NULL, 16);
preferences.putUInt("fgColor", color);
setFgColor(color);
2023-11-30 22:38:01 +01:00
// Serial.print(F("Setting foreground color to "));
// Serial.println(fgColor->value().c_str());
settingsChanged = true;
}
if (request->hasParam("bgColor", true))
{
2024-06-28 17:56:08 +02:00
const AsyncWebParameter *bgColor = request->getParam("bgColor", true);
2023-11-30 22:38:01 +01:00
2024-12-21 00:01:27 +01:00
uint32_t color = strtol(bgColor->value().c_str(), NULL, 16);
preferences.putUInt("bgColor", color);
setBgColor(color);
2023-11-30 22:38:01 +01:00
// Serial.print(F("Setting background color to "));
// Serial.println(bgColor->value().c_str());
settingsChanged = true;
}
return settingsChanged;
2023-11-07 21:26:15 +01:00
}
void onApiSystemStatus(AsyncWebServerRequest *request)
{
2023-11-30 22:38:01 +01:00
AsyncResponseStream *response =
2024-12-21 00:01:27 +01:00
request->beginResponseStream(JSON_CONTENT);
2023-11-08 12:18:59 +01:00
JsonDocument root;
2023-11-17 19:28:40 +01:00
2023-11-30 22:38:01 +01:00
root["espFreeHeap"] = ESP.getFreeHeap();
root["espHeapSize"] = ESP.getHeapSize();
root["espFreePsram"] = ESP.getFreePsram();
root["espPsramSize"] = ESP.getPsramSize();
2024-12-20 23:02:54 +01:00
root["fsUsedBytes"] = LittleFS.usedBytes();
root["fsTotalBytes"] = LittleFS.totalBytes();
2023-11-30 22:38:01 +01:00
root["rssi"] = WiFi.RSSI();
root["txPower"] = WiFi.getTxPower();
2023-11-17 19:28:40 +01:00
2023-11-30 22:38:01 +01:00
serializeJson(root, *response);
2023-11-07 21:26:15 +01:00
2023-11-30 22:38:01 +01:00
request->send(response);
}
2023-11-07 21:26:15 +01:00
2023-11-30 22:38:01 +01:00
#define STRINGIFY(x) #x
#define ENUM_TO_STRING(x) STRINGIFY(x)
void onApiSetWifiTxPower(AsyncWebServerRequest *request)
{
if (request->hasParam("txPower"))
{
2024-06-28 17:56:08 +02:00
const AsyncWebParameter *txPowerParam = request->getParam("txPower");
2023-11-30 22:38:01 +01:00
int txPower = txPowerParam->value().toInt();
if (static_cast<int>(wifi_power_t::WIFI_POWER_MINUS_1dBm) <= txPower &&
txPower <= static_cast<int>(wifi_power_t::WIFI_POWER_19_5dBm))
{
2023-11-30 22:38:01 +01:00
// is valid value
String txPowerName =
std::to_string(
static_cast<std::underlying_type_t<wifi_power_t>>(txPower))
.c_str();
Serial.printf("Set WiFi Tx power to: %s\n", txPowerName);
if (WiFi.setTxPower(static_cast<wifi_power_t>(txPower)))
{
2023-11-30 22:38:01 +01:00
preferences.putInt("txPower", txPower);
2024-12-21 00:01:27 +01:00
request->send(HTTP_OK, "application/json", "{\"setTxPower\": \"ok\"}");
2023-11-30 22:38:01 +01:00
return;
}
2023-11-07 21:26:15 +01:00
}
2023-11-30 22:38:01 +01:00
}
2023-11-07 21:26:15 +01:00
2024-12-21 00:01:27 +01:00
return request->send(HTTP_BAD_REQUEST);
2023-11-07 21:26:15 +01:00
}
void onApiLightsStatus(AsyncWebServerRequest *request)
{
2023-11-30 22:38:01 +01:00
AsyncResponseStream *response =
2024-12-21 00:01:27 +01:00
request->beginResponseStream(JSON_CONTENT);
2023-11-07 21:26:15 +01:00
2023-11-30 22:38:01 +01:00
serializeJson(getLedStatusObject()["data"], *response);
2023-11-07 21:26:15 +01:00
2023-11-30 22:38:01 +01:00
request->send(response);
}
2023-11-07 21:26:15 +01:00
void onApiStopDataSources(AsyncWebServerRequest *request)
{
2024-01-31 23:45:26 +01:00
AsyncResponseStream *response =
2024-12-21 00:01:27 +01:00
request->beginResponseStream(JSON_CONTENT);
2024-01-31 23:45:26 +01:00
stopPriceNotify();
stopBlockNotify();
request->send(response);
}
void onApiRestartDataSources(AsyncWebServerRequest *request)
{
2024-01-31 23:45:26 +01:00
AsyncResponseStream *response =
2024-12-21 00:01:27 +01:00
request->beginResponseStream(JSON_CONTENT);
2024-01-31 23:45:26 +01:00
restartPriceNotify();
restartBlockNotify();
// setupPriceNotify();
// setupBlockNotify();
2024-01-31 23:45:26 +01:00
request->send(response);
}
void onApiLightsOff(AsyncWebServerRequest *request)
{
2023-11-30 22:38:01 +01:00
setLights(0, 0, 0);
2024-12-21 00:01:27 +01:00
request->send(HTTP_OK);
2023-11-07 21:26:15 +01:00
}
void onApiLightsSetColor(AsyncWebServerRequest *request)
{
if (request->hasParam("c"))
{
2023-11-30 22:38:01 +01:00
AsyncResponseStream *response =
2024-12-21 00:01:27 +01:00
request->beginResponseStream(JSON_CONTENT);
2023-11-21 16:12:44 +01:00
2023-11-30 22:38:01 +01:00
String rgbColor = request->getParam("c")->value();
2023-11-21 16:12:44 +01:00
if (rgbColor.compareTo("off") == 0)
{
2023-11-30 22:38:01 +01:00
setLights(0, 0, 0);
}
else
{
2023-11-30 22:38:01 +01:00
uint r, g, b;
sscanf(rgbColor.c_str(), "%02x%02x%02x", &r, &g, &b);
setLights(r, g, b);
2023-11-21 16:12:44 +01:00
}
JsonDocument doc;
2023-11-30 22:38:01 +01:00
doc["result"] = rgbColor;
serializeJson(getLedStatusObject()["data"], *response);
request->send(response);
}
else
{
2024-12-21 00:01:27 +01:00
request->send(HTTP_BAD_REQUEST);
2023-11-30 22:38:01 +01:00
}
}
void onApiLightsSetJson(AsyncWebServerRequest *request, JsonVariant &json)
{
2023-11-30 22:38:01 +01:00
JsonArray lights = json.as<JsonArray>();
2023-11-19 02:23:47 +01:00
if (lights.size() != pixels.numPixels())
{
if (!lights.size())
{
2024-04-27 16:48:06 +02:00
// if empty, assume off request
return onApiLightsOff(request);
2024-04-27 16:48:06 +02:00
}
2023-11-30 22:38:01 +01:00
Serial.printf("Invalid values for LED set %d\n", lights.size());
2024-12-21 00:01:27 +01:00
request->send(HTTP_BAD_REQUEST);
2023-11-30 22:38:01 +01:00
return;
}
2023-11-19 02:23:47 +01:00
for (uint i = 0; i < pixels.numPixels(); i++)
{
2023-11-30 22:38:01 +01:00
unsigned int red, green, blue;
2023-11-08 12:18:59 +01:00
2024-12-21 00:15:08 +01:00
if (lights[i]["red"].is<uint>() && lights[i]["green"].is<uint>() &&
lights[i]["blue"].is<uint>())
{
2023-11-30 22:38:01 +01:00
red = lights[i]["red"].as<uint>();
green = lights[i]["green"].as<uint>();
blue = lights[i]["blue"].as<uint>();
}
2024-12-21 00:15:08 +01:00
else if (lights[i]["hex"].is<const char*>())
{
2023-11-30 22:38:01 +01:00
if (!sscanf(lights[i]["hex"].as<String>().c_str(), "#%02X%02X%02X", &red,
&green, &blue) == 3)
{
2023-11-30 22:38:01 +01:00
Serial.printf("Invalid hex for LED %d\n", i);
2024-12-21 00:01:27 +01:00
request->send(HTTP_BAD_REQUEST);
2023-11-18 14:47:45 +01:00
return;
2023-11-30 22:38:01 +01:00
}
}
else
{
2023-11-30 22:38:01 +01:00
Serial.printf("No valid color for LED %d\n", i);
2024-12-21 00:01:27 +01:00
request->send(HTTP_BAD_REQUEST);
2023-11-30 22:38:01 +01:00
return;
2023-11-18 14:47:45 +01:00
}
2023-11-30 22:38:01 +01:00
pixels.setPixelColor((pixels.numPixels() - i - 1),
pixels.Color(red, green, blue));
}
2023-11-18 14:47:45 +01:00
2023-11-30 22:38:01 +01:00
pixels.show();
saveLedState();
2023-11-18 14:47:45 +01:00
2024-12-21 00:01:27 +01:00
request->send(HTTP_OK);
}
void onIndex(AsyncWebServerRequest *request)
{
2023-11-30 22:38:01 +01:00
request->send(LittleFS, "/index.html", String(), false);
}
void onNotFound(AsyncWebServerRequest *request)
{
2024-12-21 00:01:27 +01:00
// Access-Control-Request-Method == POST might be better
2023-11-30 22:38:01 +01:00
if (request->method() == HTTP_OPTIONS ||
request->hasHeader("Sec-Fetch-Mode"))
{
2023-11-30 22:38:01 +01:00
// Serial.printf("NotFound, Return[%d]\n", 200);
2024-12-21 00:01:27 +01:00
request->send(HTTP_OK);
}
else
{
2023-11-30 22:38:01 +01:00
// Serial.printf("NotFound, Return[%d]\n", 404);
request->send(404);
}
2023-11-07 01:11:12 +01:00
};
void eventSourceTask(void *pvParameters)
{
for (;;)
{
2023-11-30 22:38:01 +01:00
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
eventSourceUpdate();
}
2024-04-28 16:47:57 +02:00
}
void onApiShowCurrency(AsyncWebServerRequest *request)
{
if (request->hasParam("c"))
{
const AsyncWebParameter *p = request->getParam("c");
std::string currency = p->value().c_str();
if (!isActiveCurrency(currency))
{
request->send(404);
return;
}
char curChar = getCurrencyChar(currency);
ScreenHandler::setCurrentCurrency(curChar);
ScreenHandler::setCurrentScreen(ScreenHandler::getCurrentScreen());
2024-12-21 00:01:27 +01:00
request->send(HTTP_OK);
return;
}
request->send(404);
}
2024-04-28 16:47:57 +02:00
#ifdef HAS_FRONTLIGHT
void onApiFrontlightOn(AsyncWebServerRequest *request)
{
2024-04-28 16:47:57 +02:00
frontlightFadeInAll();
2024-12-21 00:01:27 +01:00
request->send(HTTP_OK);
2024-04-28 16:47:57 +02:00
}
void onApiFrontlightStatus(AsyncWebServerRequest *request)
{
2024-04-28 16:47:57 +02:00
AsyncResponseStream *response =
2024-12-21 00:01:27 +01:00
request->beginResponseStream(JSON_CONTENT);
2024-04-28 16:47:57 +02:00
JsonDocument root;
2024-06-29 02:19:25 +02:00
std::vector<uint16_t> statuses = frontlightGetStatus();
uint16_t arr[NUM_SCREENS];
std::copy(statuses.begin(), statuses.end(), arr);
2024-04-28 16:47:57 +02:00
2024-06-29 02:19:25 +02:00
JsonArray data = root["flStatus"].to<JsonArray>();
copyArray(arr, data);
serializeJson(root, *response);
2024-04-28 16:47:57 +02:00
request->send(response);
}
void onApiFrontlightFlash(AsyncWebServerRequest *request)
{
frontlightFlash(preferences.getUInt("flEffectDelay"));
2024-12-21 00:01:27 +01:00
request->send(HTTP_OK);
}
void onApiFrontlightSetBrightness(AsyncWebServerRequest *request)
{
if (request->hasParam("b"))
{
frontlightSetBrightness(request->getParam("b")->value().toInt());
2024-12-21 00:01:27 +01:00
request->send(HTTP_OK);
}
else
{
2024-12-21 00:01:27 +01:00
request->send(HTTP_BAD_REQUEST);
}
}
void onApiFrontlightOff(AsyncWebServerRequest *request)
{
2024-04-28 16:47:57 +02:00
frontlightFadeOutAll();
2024-12-21 00:01:27 +01:00
request->send(HTTP_OK);
2024-04-28 16:47:57 +02:00
}
#endif