2023-11-07 00:11:12 +00:00
|
|
|
#include "webserver.hpp"
|
|
|
|
|
|
|
|
AsyncWebServer server(80);
|
2023-11-08 14:27:22 +00:00
|
|
|
AsyncEventSource events("/events");
|
2023-11-08 19:29:06 +00:00
|
|
|
TaskHandle_t eventSourceTaskHandle;
|
2023-11-07 00:11:12 +00:00
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
void setupWebserver()
|
|
|
|
{
|
|
|
|
events.onConnect([](AsyncEventSourceClient *client)
|
|
|
|
{ client->send("welcome", NULL, millis(), 1000); });
|
2023-11-30 21:38:01 +00:00
|
|
|
server.addHandler(&events);
|
2023-11-08 14:27:22 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
// server.serveStatic("/css", LittleFS, "/css/");
|
2024-03-10 11:35:20 +00:00
|
|
|
server.serveStatic("/fonts", LittleFS, "/fonts/");
|
2023-11-30 21:38:01 +00:00
|
|
|
server.serveStatic("/build", LittleFS, "/build");
|
|
|
|
server.serveStatic("/swagger.json", LittleFS, "/swagger.json");
|
|
|
|
server.serveStatic("/api.html", LittleFS, "/api.html");
|
2024-05-18 23:32:45 +00:00
|
|
|
server.serveStatic("/fs_hash.txt", LittleFS, "/fs_hash.txt");
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
server.on("/", HTTP_GET, onIndex);
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2023-11-30 21:38:01 +00: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 15:12:44 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
server.on("/api/full_refresh", HTTP_GET, onApiFullRefresh);
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2024-01-31 22:45:26 +00:00
|
|
|
server.on("/api/stop_datasources", HTTP_GET, onApiStopDataSources);
|
|
|
|
server.on("/api/restart_datasources", HTTP_GET, onApiRestartDataSources);
|
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
server.on("/api/action/pause", HTTP_GET, onApiActionPause);
|
|
|
|
server.on("/api/action/timer_restart", HTTP_GET, onApiActionTimerRestart);
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
server.on("/api/settings", HTTP_GET, onApiSettingsGet);
|
|
|
|
server.on("/api/settings", HTTP_POST, onApiSettingsPost);
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
server.on("/api/show/screen", HTTP_GET, onApiShowScreen);
|
|
|
|
server.on("/api/show/text", HTTP_GET, onApiShowText);
|
2023-11-17 18:28:40 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncCallbackJsonWebHandler *settingsPatchHandler =
|
|
|
|
new AsyncCallbackJsonWebHandler("/api/json/settings", onApiSettingsPatch);
|
|
|
|
server.addHandler(settingsPatchHandler);
|
2023-11-17 18:28:40 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncCallbackJsonWebHandler *handler = new AsyncCallbackJsonWebHandler(
|
|
|
|
"/api/show/custom", onApiShowTextAdvanced);
|
|
|
|
server.addHandler(handler);
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncCallbackJsonWebHandler *lightsJsonHandler =
|
|
|
|
new AsyncCallbackJsonWebHandler("/api/lights", onApiLightsSetJson);
|
|
|
|
server.addHandler(lightsJsonHandler);
|
2023-11-18 13:03:47 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
server.on("/api/lights/off", HTTP_GET, onApiLightsOff);
|
|
|
|
server.on("/api/lights/color", HTTP_GET, onApiLightsSetColor);
|
|
|
|
server.on("/api/lights", HTTP_GET, onApiLightsStatus);
|
2024-05-18 23:32:45 +00:00
|
|
|
server.on("/api/identify", HTTP_GET, onApiIdentify);
|
2023-11-08 11:18:59 +00:00
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
#ifdef HAS_FRONTLIGHT
|
2024-04-28 14:47:57 +00:00
|
|
|
server.on("/api/frontlight/on", HTTP_GET, onApiFrontlightOn);
|
2024-06-07 23:00:52 +00:00
|
|
|
server.on("/api/frontlight/flash", HTTP_GET, onApiFrontlightFlash);
|
|
|
|
server.on("/api/frontlight/brightness", HTTP_GET, onApiFrontlightSetBrightness);
|
2024-04-28 14:47:57 +00:00
|
|
|
server.on("/api/frontlight/off", HTTP_GET, onApiFrontlightOff);
|
2024-06-07 23:00:52 +00:00
|
|
|
|
|
|
|
server.addRewrite(
|
|
|
|
new OneParamRewrite("/api/frontlight/brightness/{b}", "/api/frontlight/brightness?b={b}"));
|
2024-05-18 23:32:45 +00:00
|
|
|
#endif
|
2024-04-28 14:47:57 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
// server.on("^\\/api\\/lights\\/([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$", HTTP_GET,
|
|
|
|
// onApiLightsSetColor);
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
server.on("/api/restart", HTTP_GET, onApiRestart);
|
|
|
|
server.addRewrite(new OneParamRewrite("/api/lights/{color}",
|
|
|
|
"/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 00:11:12 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
server.onNotFound(onNotFound);
|
2023-11-07 00:11:12 +00:00
|
|
|
|
2023-11-30 21:38:01 +00: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-08 14:27:22 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
server.begin();
|
2023-11-12 23:33:48 +00:00
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
if (preferences.getBool("mdnsEnabled", true))
|
|
|
|
{
|
|
|
|
if (!MDNS.begin(getMyHostname()))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
Serial.println(F("Error setting up MDNS responder!"));
|
2024-05-18 23:32:45 +00:00
|
|
|
while (1)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
delay(1000);
|
|
|
|
}
|
2023-11-12 23:33:48 +00:00
|
|
|
}
|
2023-11-30 21:38:01 +00: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 21:00:08 +00:00
|
|
|
MDNS.addServiceTxt("http", "tcp", "hw_rev", getHwRev());
|
2023-11-30 21:38:01 +00:00
|
|
|
}
|
2023-11-08 19:29:06 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
xTaskCreate(eventSourceTask, "eventSourceTask", 4096, NULL, tskIDLE_PRIORITY,
|
|
|
|
&eventSourceTaskHandle);
|
2023-11-07 00:11:12 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
void stopWebServer() { server.end(); }
|
2023-11-10 22:18:14 +00:00
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
JsonDocument getStatusObject()
|
|
|
|
{
|
2024-03-10 19:24:55 +00:00
|
|
|
JsonDocument root;
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
root["currentScreen"] = getCurrentScreen();
|
|
|
|
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 20:26:15 +00:00
|
|
|
|
2024-03-10 19:24:55 +00:00
|
|
|
JsonObject conStatus = root["connectionStatus"].to<JsonObject>();
|
2023-11-30 21:38:01 +00:00
|
|
|
conStatus["price"] = isPriceNotifyConnected();
|
|
|
|
conStatus["blocks"] = isBlockNotifyConnected();
|
2023-11-08 11:18:59 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
root["rssi"] = WiFi.RSSI();
|
2023-11-21 15:12:44 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
return root;
|
2023-11-08 14:27:22 +00:00
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
JsonDocument getLedStatusObject()
|
|
|
|
{
|
2024-03-10 19:24:55 +00:00
|
|
|
JsonDocument root;
|
|
|
|
JsonArray colors = root["data"].to<JsonArray>();
|
2023-11-30 21:38:01 +00:00
|
|
|
// Adafruit_NeoPixel pix = getPixels();
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
for (uint i = 0; i < pixels.numPixels(); i++)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
uint32_t pixColor = pixels.getPixelColor(pixels.numPixels() - i - 1);
|
|
|
|
uint alpha = (pixColor >> 24) & 0xFF;
|
|
|
|
uint red = (pixColor >> 16) & 0xFF;
|
|
|
|
uint green = (pixColor >> 8) & 0xFF;
|
|
|
|
uint blue = pixColor & 0xFF;
|
|
|
|
char hexColor[8];
|
|
|
|
sprintf(hexColor, "#%02X%02X%02X", red, green, blue);
|
|
|
|
|
2024-03-10 19:24:55 +00:00
|
|
|
JsonObject object = colors.add<JsonObject>();
|
2023-11-30 21:38:01 +00:00
|
|
|
object["red"] = red;
|
|
|
|
object["green"] = green;
|
|
|
|
object["blue"] = blue;
|
|
|
|
object["hex"] = hexColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
return root;
|
2023-11-17 21:09:28 +00:00
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
void eventSourceUpdate()
|
|
|
|
{
|
|
|
|
if (!events.count())
|
|
|
|
return;
|
2024-03-10 19:24:55 +00:00
|
|
|
JsonDocument root = getStatusObject();
|
|
|
|
JsonArray data = root["data"].to<JsonArray>();
|
2023-11-17 21:09:28 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
root["leds"] = getLedStatusObject()["data"];
|
2023-11-17 21:09:28 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
String epdContent[NUM_SCREENS];
|
|
|
|
std::array<String, NUM_SCREENS> retEpdContent = getCurrentEpdContent();
|
|
|
|
std::copy(std::begin(retEpdContent), std::end(retEpdContent), epdContent);
|
2023-11-08 14:27:22 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
copyArray(epdContent, data);
|
2023-11-08 14:27:22 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
String bufString;
|
|
|
|
serializeJson(root, bufString);
|
2023-11-08 14:27:22 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
events.send(bufString.c_str(), "status");
|
2023-11-08 14:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Api
|
|
|
|
* @Path("/api/status")
|
|
|
|
*/
|
2024-05-18 23:32:45 +00:00
|
|
|
void onApiStatus(AsyncWebServerRequest *request)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncResponseStream *response =
|
|
|
|
request->beginResponseStream("application/json");
|
2023-11-08 14:27:22 +00:00
|
|
|
|
2024-03-10 19:24:55 +00:00
|
|
|
JsonDocument root = getStatusObject();
|
|
|
|
JsonArray data = root["data"].to<JsonArray>();
|
|
|
|
JsonArray rendered = root["rendered"].to<JsonArray>();
|
2023-11-30 21:38:01 +00:00
|
|
|
String epdContent[NUM_SCREENS];
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
root["leds"] = getLedStatusObject()["data"];
|
2023-11-19 01:23:47 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
std::array<String, NUM_SCREENS> retEpdContent = getCurrentEpdContent();
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
std::copy(std::begin(retEpdContent), std::end(retEpdContent), epdContent);
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
copyArray(epdContent, data);
|
|
|
|
copyArray(epdContent, rendered);
|
|
|
|
serializeJson(root, *response);
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
request->send(response);
|
2023-11-07 20:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Api
|
|
|
|
* @Path("/api/action/pause")
|
|
|
|
*/
|
2024-05-18 23:32:45 +00:00
|
|
|
void onApiActionPause(AsyncWebServerRequest *request)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
setTimerActive(false);
|
|
|
|
request->send(200);
|
2023-11-07 20:26:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Api
|
|
|
|
* @Path("/api/action/timer_restart")
|
|
|
|
*/
|
2024-05-18 23:32:45 +00:00
|
|
|
void onApiActionTimerRestart(AsyncWebServerRequest *request)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
setTimerActive(true);
|
|
|
|
request->send(200);
|
2023-11-07 20:26:15 +00:00
|
|
|
}
|
|
|
|
|
2023-11-12 23:33:48 +00:00
|
|
|
/**
|
|
|
|
* @Api
|
|
|
|
* @Path("/api/full_refresh")
|
|
|
|
*/
|
2024-05-18 23:32:45 +00:00
|
|
|
void onApiFullRefresh(AsyncWebServerRequest *request)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
forceFullRefresh();
|
|
|
|
std::array<String, NUM_SCREENS> newEpdContent = getCurrentEpdContent();
|
2023-11-12 23:33:48 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
setEpdContent(newEpdContent, true);
|
2023-11-12 23:33:48 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
request->send(200);
|
2023-11-12 23:33:48 +00:00
|
|
|
}
|
|
|
|
|
2023-11-14 22:09:23 +00:00
|
|
|
/**
|
|
|
|
* @Api
|
|
|
|
* @Path("/api/show/screen")
|
|
|
|
*/
|
2024-05-18 23:32:45 +00:00
|
|
|
void onApiShowScreen(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
if (request->hasParam("s"))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncWebParameter *p = request->getParam("s");
|
|
|
|
uint currentScreen = p->value().toInt();
|
|
|
|
setCurrentScreen(currentScreen);
|
|
|
|
}
|
|
|
|
request->send(200);
|
2023-11-07 20:26:15 +00:00
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
void onApiShowText(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
if (request->hasParam("t"))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncWebParameter *p = request->getParam("t");
|
|
|
|
String t = p->value();
|
2024-05-18 23:32:45 +00:00
|
|
|
t.toUpperCase(); // This is needed as long as lowercase letters are glitchy
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
std::array<String, NUM_SCREENS> textEpdContent;
|
2024-05-18 23:32:45 +00:00
|
|
|
for (uint i = 0; i < NUM_SCREENS; i++)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
textEpdContent[i] = t[i];
|
2023-11-14 18:46:29 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
setEpdContent(textEpdContent);
|
|
|
|
}
|
|
|
|
setCurrentScreen(SCREEN_CUSTOM);
|
|
|
|
request->send(200);
|
2023-11-14 18:46:29 +00:00
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
void onApiShowTextAdvanced(AsyncWebServerRequest *request, JsonVariant &json)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
JsonArray screens = json.as<JsonArray>();
|
2023-11-22 15:26:57 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
std::array<String, NUM_SCREENS> epdContent;
|
|
|
|
int i = 0;
|
2024-05-18 23:32:45 +00:00
|
|
|
for (JsonVariant s : screens)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
epdContent[i] = s.as<String>();
|
|
|
|
i++;
|
|
|
|
}
|
2023-11-22 15:26:57 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
setEpdContent(epdContent);
|
2023-11-17 18:28:40 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
setCurrentScreen(SCREEN_CUSTOM);
|
|
|
|
request->send(200);
|
|
|
|
}
|
2023-11-17 18:28:40 +00:00
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
void onApiSettingsPatch(AsyncWebServerRequest *request, JsonVariant &json)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
JsonObject settings = json.as<JsonObject>();
|
|
|
|
|
|
|
|
bool settingsChanged = true;
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
if (settings.containsKey("fgColor"))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
String fgColor = settings["fgColor"].as<String>();
|
|
|
|
preferences.putUInt("fgColor", strtol(fgColor.c_str(), NULL, 16));
|
|
|
|
setFgColor(int(strtol(fgColor.c_str(), NULL, 16)));
|
|
|
|
Serial.print(F("Setting foreground color to "));
|
|
|
|
Serial.println(strtol(fgColor.c_str(), NULL, 16));
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
2024-05-18 23:32:45 +00:00
|
|
|
if (settings.containsKey("bgColor"))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
String bgColor = settings["bgColor"].as<String>();
|
|
|
|
|
|
|
|
preferences.putUInt("bgColor", strtol(bgColor.c_str(), NULL, 16));
|
|
|
|
setBgColor(int(strtol(bgColor.c_str(), NULL, 16)));
|
|
|
|
Serial.print(F("Setting background color to "));
|
|
|
|
Serial.println(bgColor.c_str());
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
if (settings.containsKey("timePerScreen"))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
preferences.putUInt("timerSeconds",
|
|
|
|
settings["timePerScreen"].as<uint>() * 60);
|
|
|
|
}
|
|
|
|
|
|
|
|
String strSettings[] = {"hostnamePrefix", "mempoolInstance"};
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
for (String setting : strSettings)
|
|
|
|
{
|
|
|
|
if (settings.containsKey(setting))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
preferences.putString(setting.c_str(), settings[setting].as<String>());
|
|
|
|
Serial.printf("Setting %s to %s\r\n", setting.c_str(),
|
|
|
|
settings[setting].as<String>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-07 23:00:52 +00:00
|
|
|
String uintSettings[] = {"minSecPriceUpd", "fullRefreshMin", "ledBrightness", "flMaxBrightness", "flEffectDelay"};
|
2023-11-30 21:38:01 +00:00
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
for (String setting : uintSettings)
|
|
|
|
{
|
|
|
|
if (settings.containsKey(setting))
|
|
|
|
{
|
2023-11-30 21:38:01 +00: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-05-18 23:32:45 +00:00
|
|
|
if (settings.containsKey("tzOffset"))
|
|
|
|
{
|
2023-11-30 21:38:01 +00: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
String boolSettings[] = {"fetchEurPrice", "ledTestOnPower", "ledFlashOnUpd",
|
2024-05-18 23:32:45 +00:00
|
|
|
"mdnsEnabled", "otaEnabled", "stealFocus",
|
2024-03-11 20:21:15 +00:00
|
|
|
"mcapBigChar", "useSatsSymbol", "useBlkCountdown",
|
2024-06-07 23:00:52 +00:00
|
|
|
"suffixPrice", "disableLeds", "ownDataSource", "flAlwaysOn", "flFlashOnUpd"};
|
2023-11-30 21:38:01 +00:00
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
for (String setting : boolSettings)
|
|
|
|
{
|
|
|
|
if (settings.containsKey(setting))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
preferences.putBool(setting.c_str(), settings[setting].as<boolean>());
|
|
|
|
Serial.printf("Setting %s to %d\r\n", setting.c_str(),
|
|
|
|
settings[setting].as<boolean>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
if (settings.containsKey("screens"))
|
|
|
|
{
|
|
|
|
for (JsonVariant screen : settings["screens"].as<JsonArray>())
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
JsonObject s = screen.as<JsonObject>();
|
|
|
|
uint id = s["id"].as<uint>();
|
|
|
|
String key = "screen[" + String(id) + "]";
|
|
|
|
String prefKey = "screen" + String(id) + "Visible";
|
|
|
|
bool visible = s["enabled"].as<boolean>();
|
|
|
|
preferences.putBool(prefKey.c_str(), visible);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
if (settings.containsKey("txPower"))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
int txPower = settings["txPower"].as<int>();
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
if (txPower == 80)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
preferences.remove("txPower");
|
2024-05-18 23:32:45 +00:00
|
|
|
if (WiFi.getTxPower() != 80)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
ESP.restart();
|
|
|
|
}
|
2024-05-18 23:32:45 +00:00
|
|
|
}
|
|
|
|
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 21:38:01 +00:00
|
|
|
// is valid value
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
if (WiFi.setTxPower(static_cast<wifi_power_t>(txPower)))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
Serial.printf("Set WiFi Tx power to: %d\n", txPower);
|
|
|
|
preferences.putInt("txPower", txPower);
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
2023-11-21 15:12:44 +00:00
|
|
|
}
|
2023-11-30 21:38:01 +00:00
|
|
|
}
|
2023-11-21 15:12:44 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
request->send(200);
|
2024-05-18 23:32:45 +00:00
|
|
|
if (settingsChanged)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
queueLedEffect(LED_FLASH_SUCCESS);
|
|
|
|
}
|
2023-11-17 18:28:40 +00:00
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
void onApiRestart(AsyncWebServerRequest *request)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
request->send(200);
|
2023-11-12 23:33:48 +00:00
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
if (events.count())
|
|
|
|
events.send("closing");
|
2023-11-12 23:33:48 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
delay(500);
|
2023-11-12 23:33:48 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
esp_restart();
|
2023-11-07 20:26:15 +00:00
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
void onApiIdentify(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
queueLedEffect(LED_FLASH_IDENTIFY);
|
|
|
|
|
|
|
|
request->send(200);
|
|
|
|
}
|
|
|
|
|
2023-11-07 20:26:15 +00:00
|
|
|
/**
|
|
|
|
* @Api
|
|
|
|
* @Method GET
|
|
|
|
* @Path("/api/settings")
|
|
|
|
*/
|
2024-05-18 23:32:45 +00:00
|
|
|
void onApiSettingsGet(AsyncWebServerRequest *request)
|
|
|
|
{
|
2024-03-10 19:24:55 +00:00
|
|
|
JsonDocument root;
|
2023-11-30 21:38:01 +00: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", 600);
|
|
|
|
root["tzOffset"] = preferences.getInt("gmtOffset", TIME_OFFSET_SECONDS) / 60;
|
|
|
|
root["useBitcoinNode"] = preferences.getBool("useNode", false);
|
|
|
|
root["mempoolInstance"] =
|
|
|
|
preferences.getString("mempoolInstance", DEFAULT_MEMPOOL_INSTANCE);
|
|
|
|
root["ledTestOnPower"] = preferences.getBool("ledTestOnPower", true);
|
|
|
|
root["ledFlashOnUpd"] = preferences.getBool("ledFlashOnUpd", false);
|
|
|
|
root["ledBrightness"] = preferences.getUInt("ledBrightness", 128);
|
2024-03-10 11:35:20 +00:00
|
|
|
root["stealFocus"] = preferences.getBool("stealFocus", false);
|
2023-11-30 21:38:01 +00:00
|
|
|
root["mcapBigChar"] = preferences.getBool("mcapBigChar", true);
|
|
|
|
root["mdnsEnabled"] = preferences.getBool("mdnsEnabled", true);
|
|
|
|
root["otaEnabled"] = preferences.getBool("otaEnabled", true);
|
|
|
|
root["fetchEurPrice"] = preferences.getBool("fetchEurPrice", false);
|
2024-03-10 11:35:20 +00:00
|
|
|
root["useSatsSymbol"] = preferences.getBool("useSatsSymbol", false);
|
|
|
|
root["useBlkCountdown"] = preferences.getBool("useBlkCountdown", false);
|
2024-03-11 20:21:15 +00:00
|
|
|
root["suffixPrice"] = preferences.getBool("suffixPrice", false);
|
|
|
|
root["disableLeds"] = preferences.getBool("disableLeds", false);
|
2024-03-10 11:35:20 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
root["hostnamePrefix"] = preferences.getString("hostnamePrefix", "btclock");
|
|
|
|
root["hostname"] = getMyHostname();
|
|
|
|
root["ip"] = WiFi.localIP();
|
|
|
|
root["txPower"] = WiFi.getTxPower();
|
2024-05-08 23:02:40 +00:00
|
|
|
root["ownDataSource"] = preferences.getBool("ownDataSource", true);
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
#ifdef HAS_FRONTLIGHT
|
2024-04-27 14:48:06 +00:00
|
|
|
root["hasFrontlight"] = true;
|
2024-06-07 23:00:52 +00:00
|
|
|
root["flMaxBrightness"] = preferences.getUInt("flMaxBrightness", 2048);
|
2024-05-08 21:54:17 +00:00
|
|
|
root["flAlwaysOn"] = preferences.getBool("flAlwaysOn", false);
|
2024-06-07 23:00:52 +00:00
|
|
|
root["flEffectDelay"] = preferences.getUInt("flEffectDelay");
|
|
|
|
root["flFlashOnUpd"] = preferences.getBool("flFlashOnUpd", false);
|
2024-05-08 21:54:17 +00:00
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
#else
|
2024-04-27 14:48:06 +00:00
|
|
|
root["hasFrontlight"] = false;
|
2024-05-18 23:32:45 +00:00
|
|
|
#endif
|
2024-04-27 14:48:06 +00:00
|
|
|
|
2023-11-07 20:26:15 +00:00
|
|
|
#ifdef GIT_REV
|
2023-11-30 21:38:01 +00:00
|
|
|
root["gitRev"] = String(GIT_REV);
|
2023-11-07 20:26:15 +00:00
|
|
|
#endif
|
|
|
|
#ifdef LAST_BUILD_TIME
|
2023-11-30 21:38:01 +00:00
|
|
|
root["lastBuildTime"] = String(LAST_BUILD_TIME);
|
2023-11-07 20:26:15 +00:00
|
|
|
#endif
|
2024-03-10 19:24:55 +00:00
|
|
|
JsonArray screens = root["screens"].to<JsonArray>();
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
std::vector<std::string> screenNameMap = getScreenNameMap();
|
2023-11-08 11:18:59 +00:00
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
for (int i = 0; i < screenNameMap.size(); i++)
|
|
|
|
{
|
2024-03-10 19:24:55 +00:00
|
|
|
JsonObject o = screens.add<JsonObject>();
|
2023-11-30 21:38:01 +00:00
|
|
|
String key = "screen" + String(i) + "Visible";
|
|
|
|
o["id"] = i;
|
|
|
|
o["name"] = screenNameMap[i];
|
|
|
|
o["enabled"] = preferences.getBool(key.c_str(), true);
|
|
|
|
}
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncResponseStream *response =
|
|
|
|
request->beginResponseStream("application/json");
|
|
|
|
serializeJson(root, *response);
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
request->send(response);
|
2023-11-07 20:26:15 +00:00
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
bool processEpdColorSettings(AsyncWebServerRequest *request)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
bool settingsChanged = false;
|
2024-05-18 23:32:45 +00:00
|
|
|
if (request->hasParam("fgColor", true))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncWebParameter *fgColor = request->getParam("fgColor", true);
|
|
|
|
preferences.putUInt("fgColor", strtol(fgColor->value().c_str(), NULL, 16));
|
|
|
|
setFgColor(int(strtol(fgColor->value().c_str(), NULL, 16)));
|
|
|
|
// Serial.print(F("Setting foreground color to "));
|
|
|
|
// Serial.println(fgColor->value().c_str());
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
2024-05-18 23:32:45 +00:00
|
|
|
if (request->hasParam("bgColor", true))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncWebParameter *bgColor = request->getParam("bgColor", true);
|
|
|
|
|
|
|
|
preferences.putUInt("bgColor", strtol(bgColor->value().c_str(), NULL, 16));
|
|
|
|
setBgColor(int(strtol(bgColor->value().c_str(), NULL, 16)));
|
|
|
|
// Serial.print(F("Setting background color to "));
|
|
|
|
// Serial.println(bgColor->value().c_str());
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return settingsChanged;
|
2023-11-07 20:26:15 +00:00
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
void onApiSettingsPost(AsyncWebServerRequest *request)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
bool settingsChanged = false;
|
|
|
|
|
|
|
|
settingsChanged = processEpdColorSettings(request);
|
|
|
|
|
|
|
|
int headers = request->headers();
|
|
|
|
int i;
|
2024-05-18 23:32:45 +00:00
|
|
|
for (i = 0; i < headers; i++)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncWebHeader *h = request->getHeader(i);
|
|
|
|
Serial.printf("HEADER[%s]: %s\n", h->name().c_str(), h->value().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
int params = request->params();
|
2024-05-18 23:32:45 +00:00
|
|
|
for (int i = 0; i < params; i++)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncWebParameter *p = request->getParam(i);
|
2024-05-18 23:32:45 +00:00
|
|
|
if (p->isFile())
|
|
|
|
{ // p->isPost() is also true
|
2023-11-30 21:38:01 +00:00
|
|
|
Serial.printf("FILE[%s]: %s, size: %u\n", p->name().c_str(),
|
|
|
|
p->value().c_str(), p->size());
|
2024-05-18 23:32:45 +00:00
|
|
|
}
|
|
|
|
else if (p->isPost())
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
Serial.printf("POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
|
2024-05-18 23:32:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
Serial.printf("GET[%s]: %s\n", p->name().c_str(), p->value().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
if (request->hasParam("fetchEurPrice", true))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncWebParameter *fetchEurPrice = request->getParam("fetchEurPrice", true);
|
|
|
|
|
|
|
|
preferences.putBool("fetchEurPrice", fetchEurPrice->value().toInt());
|
|
|
|
settingsChanged = true;
|
2024-05-18 23:32:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
preferences.putBool("fetchEurPrice", 0);
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
if (request->hasParam("ledTestOnPower", true))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncWebParameter *ledTestOnPower =
|
|
|
|
request->getParam("ledTestOnPower", true);
|
|
|
|
|
|
|
|
preferences.putBool("ledTestOnPower", ledTestOnPower->value().toInt());
|
|
|
|
settingsChanged = true;
|
2024-05-18 23:32:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
preferences.putBool("ledTestOnPower", 0);
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
if (request->hasParam("ledFlashOnUpd", true))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncWebParameter *ledFlashOnUpdate =
|
|
|
|
request->getParam("ledFlashOnUpd", true);
|
|
|
|
|
|
|
|
preferences.putBool("ledFlashOnUpd", ledFlashOnUpdate->value().toInt());
|
|
|
|
settingsChanged = true;
|
2024-05-18 23:32:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
preferences.putBool("ledFlashOnUpd", 0);
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
if (request->hasParam("mdnsEnabled", true))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncWebParameter *mdnsEnabled = request->getParam("mdnsEnabled", true);
|
|
|
|
|
|
|
|
preferences.putBool("mdnsEnabled", mdnsEnabled->value().toInt());
|
|
|
|
settingsChanged = true;
|
2024-05-18 23:32:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
preferences.putBool("mdnsEnabled", 0);
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
if (request->hasParam("otaEnabled", true))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncWebParameter *otaEnabled = request->getParam("otaEnabled", true);
|
|
|
|
|
|
|
|
preferences.putBool("otaEnabled", otaEnabled->value().toInt());
|
|
|
|
settingsChanged = true;
|
2024-05-18 23:32:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
preferences.putBool("otaEnabled", 0);
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
if (request->hasParam("stealFocusOnBlock", true))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncWebParameter *stealFocusOnBlock =
|
|
|
|
request->getParam("stealFocusOnBlock", true);
|
|
|
|
|
|
|
|
preferences.putBool("stealFocus", stealFocusOnBlock->value().toInt());
|
|
|
|
settingsChanged = true;
|
2024-05-18 23:32:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
preferences.putBool("stealFocus", 0);
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
if (request->hasParam("mcapBigChar", true))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncWebParameter *mcapBigChar = request->getParam("mcapBigChar", true);
|
|
|
|
|
|
|
|
preferences.putBool("mcapBigChar", mcapBigChar->value().toInt());
|
|
|
|
settingsChanged = true;
|
2024-05-18 23:32:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
preferences.putBool("mcapBigChar", 0);
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
if (request->hasParam("mempoolInstance", true))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncWebParameter *mempoolInstance =
|
|
|
|
request->getParam("mempoolInstance", true);
|
|
|
|
|
|
|
|
preferences.putString("mempoolInstance", mempoolInstance->value().c_str());
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
if (request->hasParam("hostnamePrefix", true))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncWebParameter *hostnamePrefix =
|
|
|
|
request->getParam("hostnamePrefix", true);
|
|
|
|
|
|
|
|
preferences.putString("hostnamePrefix", hostnamePrefix->value().c_str());
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
if (request->hasParam("ledBrightness", true))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncWebParameter *ledBrightness = request->getParam("ledBrightness", true);
|
|
|
|
|
|
|
|
preferences.putUInt("ledBrightness", ledBrightness->value().toInt());
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
if (request->hasParam("fullRefreshMin", true))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncWebParameter *fullRefreshMin =
|
|
|
|
request->getParam("fullRefreshMin", true);
|
|
|
|
|
|
|
|
preferences.putUInt("fullRefreshMin", fullRefreshMin->value().toInt());
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
if (request->hasParam("wpTimeout", true))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncWebParameter *wpTimeout = request->getParam("wpTimeout", true);
|
|
|
|
|
|
|
|
preferences.putUInt("wpTimeout", wpTimeout->value().toInt());
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> screenNameMap = getScreenNameMap();
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
if (request->hasParam("screens"))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncWebParameter *screenParam = request->getParam("screens", true);
|
|
|
|
|
|
|
|
Serial.printf(screenParam->value().c_str());
|
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
for (int i = 0; i < screenNameMap.size(); i++)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
String key = "screen[" + String(i) + "]";
|
|
|
|
String prefKey = "screen" + String(i) + "Visible";
|
|
|
|
bool visible = false;
|
2024-05-18 23:32:45 +00:00
|
|
|
if (request->hasParam(key, true))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncWebParameter *screenParam = request->getParam(key, true);
|
|
|
|
visible = screenParam->value().toInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
preferences.putBool(prefKey.c_str(), visible);
|
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
if (request->hasParam("tzOffset", true))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncWebParameter *p = request->getParam("tzOffset", true);
|
|
|
|
int tzOffsetSeconds = p->value().toInt() * 60;
|
|
|
|
preferences.putInt("gmtOffset", tzOffsetSeconds);
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
if (request->hasParam("minSecPriceUpd", true))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncWebParameter *p = request->getParam("minSecPriceUpd", true);
|
|
|
|
int minSecPriceUpd = p->value().toInt();
|
|
|
|
preferences.putUInt("minSecPriceUpd", minSecPriceUpd);
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
if (request->hasParam("timePerScreen", true))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncWebParameter *p = request->getParam("timePerScreen", true);
|
|
|
|
uint timerSeconds = p->value().toInt() * 60;
|
|
|
|
preferences.putUInt("timerSeconds", timerSeconds);
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
request->send(200);
|
2024-05-18 23:32:45 +00:00
|
|
|
if (settingsChanged)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
queueLedEffect(LED_FLASH_SUCCESS);
|
|
|
|
}
|
|
|
|
}
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
void onApiSystemStatus(AsyncWebServerRequest *request)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncResponseStream *response =
|
|
|
|
request->beginResponseStream("application/json");
|
2023-11-08 11:18:59 +00:00
|
|
|
|
2024-03-10 19:24:55 +00:00
|
|
|
JsonDocument root;
|
2023-11-17 18:28:40 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
root["espFreeHeap"] = ESP.getFreeHeap();
|
|
|
|
root["espHeapSize"] = ESP.getHeapSize();
|
|
|
|
root["espFreePsram"] = ESP.getFreePsram();
|
|
|
|
root["espPsramSize"] = ESP.getPsramSize();
|
|
|
|
root["rssi"] = WiFi.RSSI();
|
|
|
|
root["txPower"] = WiFi.getTxPower();
|
2023-11-17 18:28:40 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
serializeJson(root, *response);
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
request->send(response);
|
|
|
|
}
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
#define STRINGIFY(x) #x
|
|
|
|
#define ENUM_TO_STRING(x) STRINGIFY(x)
|
2023-11-08 14:27:22 +00:00
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
void onApiSetWifiTxPower(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
if (request->hasParam("txPower"))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncWebParameter *txPowerParam = request->getParam("txPower");
|
|
|
|
int txPower = txPowerParam->value().toInt();
|
|
|
|
if (static_cast<int>(wifi_power_t::WIFI_POWER_MINUS_1dBm) <= txPower &&
|
2024-05-18 23:32:45 +00:00
|
|
|
txPower <= static_cast<int>(wifi_power_t::WIFI_POWER_19_5dBm))
|
|
|
|
{
|
2023-11-30 21:38:01 +00: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);
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
if (WiFi.setTxPower(static_cast<wifi_power_t>(txPower)))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
preferences.putInt("txPower", txPower);
|
|
|
|
request->send(200, "application/json", "{\"setTxPower\": \"ok\"}");
|
|
|
|
return;
|
|
|
|
}
|
2023-11-07 20:26:15 +00:00
|
|
|
}
|
2023-11-30 21:38:01 +00:00
|
|
|
}
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
return request->send(400);
|
2023-11-07 20:26:15 +00:00
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
void onApiLightsStatus(AsyncWebServerRequest *request)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncResponseStream *response =
|
|
|
|
request->beginResponseStream("application/json");
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
serializeJson(getLedStatusObject()["data"], *response);
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
request->send(response);
|
|
|
|
}
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
void onApiStopDataSources(AsyncWebServerRequest *request)
|
|
|
|
{
|
2024-01-31 22:45:26 +00:00
|
|
|
AsyncResponseStream *response =
|
|
|
|
request->beginResponseStream("application/json");
|
|
|
|
|
|
|
|
stopPriceNotify();
|
|
|
|
stopBlockNotify();
|
|
|
|
|
|
|
|
request->send(response);
|
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
void onApiRestartDataSources(AsyncWebServerRequest *request)
|
|
|
|
{
|
2024-01-31 22:45:26 +00:00
|
|
|
AsyncResponseStream *response =
|
|
|
|
request->beginResponseStream("application/json");
|
|
|
|
|
2024-04-16 13:17:34 +00:00
|
|
|
restartPriceNotify();
|
|
|
|
restartBlockNotify();
|
2024-05-18 23:32:45 +00:00
|
|
|
// setupPriceNotify();
|
|
|
|
// setupBlockNotify();
|
2024-01-31 22:45:26 +00:00
|
|
|
|
|
|
|
request->send(response);
|
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
void onApiLightsOff(AsyncWebServerRequest *request)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
setLights(0, 0, 0);
|
|
|
|
request->send(200);
|
2023-11-07 20:26:15 +00:00
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
void onApiLightsSetColor(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
if (request->hasParam("c"))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
AsyncResponseStream *response =
|
|
|
|
request->beginResponseStream("application/json");
|
2023-11-21 15:12:44 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
String rgbColor = request->getParam("c")->value();
|
2023-11-21 15:12:44 +00:00
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
if (rgbColor.compareTo("off") == 0)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
setLights(0, 0, 0);
|
2024-05-18 23:32:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
uint r, g, b;
|
|
|
|
sscanf(rgbColor.c_str(), "%02x%02x%02x", &r, &g, &b);
|
|
|
|
setLights(r, g, b);
|
2023-11-21 15:12:44 +00:00
|
|
|
}
|
|
|
|
|
2024-03-10 19:24:55 +00:00
|
|
|
JsonDocument doc;
|
2023-11-30 21:38:01 +00:00
|
|
|
doc["result"] = rgbColor;
|
2023-11-17 21:09:28 +00:00
|
|
|
|
2023-11-18 13:03:47 +00:00
|
|
|
serializeJson(getLedStatusObject()["data"], *response);
|
2023-11-17 21:09:28 +00:00
|
|
|
|
|
|
|
request->send(response);
|
2024-05-18 23:32:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
request->send(400);
|
|
|
|
}
|
2023-11-17 21:09:28 +00:00
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
void onApiLightsSetJson(AsyncWebServerRequest *request, JsonVariant &json)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
JsonArray lights = json.as<JsonArray>();
|
2023-11-19 01:23:47 +00:00
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
if (lights.size() != pixels.numPixels())
|
|
|
|
{
|
|
|
|
if (!lights.size())
|
|
|
|
{
|
2024-04-27 14:48:06 +00:00
|
|
|
// if empty, assume off request
|
2024-05-18 23:32:45 +00:00
|
|
|
return onApiLightsOff(request);
|
2024-04-27 14:48:06 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
Serial.printf("Invalid values for LED set %d\n", lights.size());
|
|
|
|
request->send(400);
|
|
|
|
return;
|
|
|
|
}
|
2023-11-19 01:23:47 +00:00
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
for (uint i = 0; i < pixels.numPixels(); i++)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
unsigned int red, green, blue;
|
2023-11-08 11:18:59 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
if (lights[i].containsKey("red") && lights[i].containsKey("green") &&
|
2024-05-18 23:32:45 +00:00
|
|
|
lights[i].containsKey("blue"))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
red = lights[i]["red"].as<uint>();
|
|
|
|
green = lights[i]["green"].as<uint>();
|
|
|
|
blue = lights[i]["blue"].as<uint>();
|
2024-05-18 23:32:45 +00:00
|
|
|
}
|
|
|
|
else if (lights[i].containsKey("hex"))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
if (!sscanf(lights[i]["hex"].as<String>().c_str(), "#%02X%02X%02X", &red,
|
2024-05-18 23:32:45 +00:00
|
|
|
&green, &blue) == 3)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
Serial.printf("Invalid hex for LED %d\n", i);
|
2023-11-18 13:47:45 +00:00
|
|
|
request->send(400);
|
|
|
|
return;
|
2023-11-30 21:38:01 +00:00
|
|
|
}
|
2024-05-18 23:32:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
Serial.printf("No valid color for LED %d\n", i);
|
|
|
|
request->send(400);
|
|
|
|
return;
|
2023-11-18 13:47:45 +00:00
|
|
|
}
|
2023-11-18 13:03:47 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
pixels.setPixelColor((pixels.numPixels() - i - 1),
|
|
|
|
pixels.Color(red, green, blue));
|
|
|
|
}
|
2023-11-18 13:47:45 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
pixels.show();
|
|
|
|
saveLedState();
|
2023-11-18 13:47:45 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
request->send(200);
|
2023-11-18 13:03:47 +00:00
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
void onIndex(AsyncWebServerRequest *request)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
request->send(LittleFS, "/index.html", String(), false);
|
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
void onNotFound(AsyncWebServerRequest *request)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
// Serial.printf("NotFound, URL[%s]\n", request->url());
|
|
|
|
|
|
|
|
// Serial.printf("NotFound, METHOD[%s]\n", request->methodToString());
|
|
|
|
|
|
|
|
// int headers = request->headers();
|
|
|
|
// int i;
|
|
|
|
// for (i = 0; i < headers; i++)
|
|
|
|
// {
|
|
|
|
// AsyncWebHeader *h = request->getHeader(i);
|
|
|
|
// Serial.printf("NotFound HEADER[%s]: %s\n", h->name().c_str(),
|
|
|
|
// h->value().c_str());
|
|
|
|
// }
|
|
|
|
|
|
|
|
// int params = request->params();
|
|
|
|
// for (int i = 0; i < params; i++)
|
|
|
|
// {
|
|
|
|
// AsyncWebParameter *p = request->getParam(i);
|
|
|
|
// if (p->isFile())
|
|
|
|
// { // p->isPost() is also true
|
|
|
|
// Serial.printf("NotFound FILE[%s]: %s, size: %u\n",
|
|
|
|
// p->name().c_str(), p->value().c_str(), p->size());
|
|
|
|
// }
|
|
|
|
// else if (p->isPost())
|
|
|
|
// {
|
|
|
|
// Serial.printf("NotFound POST[%s]: %s\n", p->name().c_str(),
|
|
|
|
// p->value().c_str());
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// {
|
|
|
|
// Serial.printf("NotFound GET[%s]: %s\n", p->name().c_str(),
|
|
|
|
// p->value().c_str());
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Access-Control-Request-Method == POST might be better
|
|
|
|
|
|
|
|
if (request->method() == HTTP_OPTIONS ||
|
2024-05-18 23:32:45 +00:00
|
|
|
request->hasHeader("Sec-Fetch-Mode"))
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
// Serial.printf("NotFound, Return[%d]\n", 200);
|
|
|
|
|
|
|
|
request->send(200);
|
2024-05-18 23:32:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
// Serial.printf("NotFound, Return[%d]\n", 404);
|
|
|
|
request->send(404);
|
|
|
|
}
|
2023-11-07 00:11:12 +00:00
|
|
|
};
|
2023-11-08 19:29:06 +00:00
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
void eventSourceTask(void *pvParameters)
|
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
2023-11-30 21:38:01 +00:00
|
|
|
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
|
|
|
|
eventSourceUpdate();
|
|
|
|
}
|
2024-04-28 14:47:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAS_FRONTLIGHT
|
2024-05-18 23:32:45 +00:00
|
|
|
void onApiFrontlightOn(AsyncWebServerRequest *request)
|
|
|
|
{
|
2024-04-28 14:47:57 +00:00
|
|
|
frontlightFadeInAll();
|
|
|
|
|
|
|
|
request->send(200);
|
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
void onApiFrontlightStatus(AsyncWebServerRequest *request)
|
|
|
|
{
|
2024-04-28 14:47:57 +00:00
|
|
|
AsyncResponseStream *response =
|
|
|
|
request->beginResponseStream("application/json");
|
|
|
|
|
|
|
|
JsonDocument root;
|
|
|
|
JsonArray ledStates = root["data"].to<JsonArray>();
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
for (int ledPin = 0; ledPin < NUM_SCREENS; ledPin++)
|
|
|
|
{
|
|
|
|
uint16_t onTime, offTime;
|
|
|
|
flArray.getPWM(ledPin, &onTime, &offTime);
|
2024-04-28 14:47:57 +00:00
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
ledStates.add(onTime);
|
2024-04-28 14:47:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
serializeJson(ledStates, *response);
|
|
|
|
|
|
|
|
request->send(response);
|
|
|
|
}
|
|
|
|
|
2024-06-07 23:00:52 +00:00
|
|
|
void onApiFrontlightFlash(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
frontlightFlash(preferences.getUInt("flEffectDelay"));
|
|
|
|
|
|
|
|
request->send(200);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onApiFrontlightSetBrightness(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
if (request->hasParam("b"))
|
|
|
|
{
|
|
|
|
frontlightSetBrightness(request->getParam("b")->value().toInt());
|
|
|
|
request->send(200);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
request->send(400);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-18 23:32:45 +00:00
|
|
|
void onApiFrontlightOff(AsyncWebServerRequest *request)
|
|
|
|
{
|
2024-04-28 14:47:57 +00:00
|
|
|
frontlightFadeOutAll();
|
|
|
|
|
|
|
|
request->send(200);
|
|
|
|
}
|
|
|
|
#endif
|