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
|
|
|
|
|
|
|
void setupWebserver()
|
|
|
|
{
|
2023-11-17 21:09:28 +00:00
|
|
|
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2023-11-08 14:27:22 +00:00
|
|
|
events.onConnect([](AsyncEventSourceClient *client)
|
2023-11-12 11:38:28 +00:00
|
|
|
{ client->send("welcome", NULL, millis(), 1000); });
|
2023-11-08 14:27:22 +00:00
|
|
|
server.addHandler(&events);
|
|
|
|
|
2023-11-17 18:28:40 +00:00
|
|
|
// server.serveStatic("/css", LittleFS, "/css/");
|
|
|
|
// server.serveStatic("/js", LittleFS, "/js/");
|
|
|
|
server.serveStatic("/build", LittleFS, "/build");
|
2023-11-17 21:09:28 +00:00
|
|
|
server.serveStatic("/swagger.json", LittleFS, "/swagger.json");
|
2023-11-14 22:09:23 +00:00
|
|
|
server.serveStatic("/api.html", LittleFS, "/api.html");
|
2023-11-07 20:26:15 +00:00
|
|
|
|
|
|
|
server.on("/", HTTP_GET, onIndex);
|
|
|
|
|
|
|
|
server.on("/api/status", HTTP_GET, onApiStatus);
|
|
|
|
server.on("/api/system_status", HTTP_GET, onApiSystemStatus);
|
2023-11-12 23:33:48 +00:00
|
|
|
server.on("/api/full_refresh", HTTP_GET, onApiFullRefresh);
|
2023-11-07 20:26:15 +00:00
|
|
|
|
|
|
|
server.on("/api/action/pause", HTTP_GET, onApiActionPause);
|
|
|
|
server.on("/api/action/timer_restart", HTTP_GET, onApiActionTimerRestart);
|
|
|
|
|
|
|
|
server.on("/api/settings", HTTP_GET, onApiSettingsGet);
|
|
|
|
server.on("/api/settings", HTTP_POST, onApiSettingsPost);
|
|
|
|
|
|
|
|
server.on("/api/show/screen", HTTP_GET, onApiShowScreen);
|
|
|
|
server.on("/api/show/text", HTTP_GET, onApiShowText);
|
2023-11-17 18:28:40 +00:00
|
|
|
|
|
|
|
AsyncCallbackJsonWebHandler *settingsPatchHandler = new AsyncCallbackJsonWebHandler("/api/json/settings", onApiSettingsPatch);
|
|
|
|
server.addHandler(settingsPatchHandler);
|
|
|
|
|
2023-11-14 18:46:29 +00:00
|
|
|
AsyncCallbackJsonWebHandler *handler = new AsyncCallbackJsonWebHandler("/api/show/custom", onApiShowTextAdvanced);
|
|
|
|
server.addHandler(handler);
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2023-11-08 11:18:59 +00:00
|
|
|
server.on("/api/lights/off", HTTP_GET, onApiLightsOff);
|
|
|
|
server.on("/api/lights/color", HTTP_GET, onApiLightsSetColor);
|
2023-11-17 21:09:28 +00:00
|
|
|
server.on("/api/lights", HTTP_GET, onApiLightsStatus);
|
2023-11-08 11:18:59 +00:00
|
|
|
|
2023-11-08 14:27:22 +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-08 14:27:22 +00:00
|
|
|
server.on("/api/restart", HTTP_GET, onApiRestart);
|
|
|
|
server.addRewrite(new OneParamRewrite("/api/lights/{color}", "/api/lights/color?c={color}"));
|
2023-11-07 20:26:15 +00:00
|
|
|
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
|
|
|
|
|
|
|
server.onNotFound(onNotFound);
|
|
|
|
|
2023-11-08 14:27:22 +00:00
|
|
|
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", "*");
|
2023-11-17 18:28:40 +00:00
|
|
|
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Methods", "GET, PATCH, POST, OPTIONS");
|
2023-11-14 22:09:23 +00:00
|
|
|
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Headers", "*");
|
2023-11-08 14:27:22 +00:00
|
|
|
|
2023-11-07 00:11:12 +00:00
|
|
|
server.begin();
|
2023-11-12 23:33:48 +00:00
|
|
|
|
|
|
|
if (preferences.getBool("mdnsEnabled", true))
|
|
|
|
{
|
|
|
|
if (!MDNS.begin(getMyHostname()))
|
|
|
|
{
|
|
|
|
Serial.println(F("Error setting up MDNS responder!"));
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
delay(1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
2023-11-08 19:29:06 +00:00
|
|
|
|
|
|
|
xTaskCreate(eventSourceTask, "eventSourceTask", 4096, NULL, tskIDLE_PRIORITY, &eventSourceTaskHandle);
|
2023-11-07 00:11:12 +00:00
|
|
|
}
|
|
|
|
|
2023-11-12 11:38:28 +00:00
|
|
|
void stopWebServer()
|
|
|
|
{
|
2023-11-10 22:18:14 +00:00
|
|
|
server.end();
|
|
|
|
}
|
|
|
|
|
2023-11-08 14:27:22 +00:00
|
|
|
StaticJsonDocument<768> getStatusObject()
|
2023-11-07 20:26:15 +00:00
|
|
|
{
|
2023-11-08 14:27:22 +00:00
|
|
|
StaticJsonDocument<768> root;
|
2023-11-07 20:26:15 +00:00
|
|
|
|
|
|
|
root["currentScreen"] = getCurrentScreen();
|
|
|
|
root["numScreens"] = NUM_SCREENS;
|
2023-11-08 14:27:22 +00:00
|
|
|
root["timerRunning"] = isTimerActive();
|
2023-11-07 20:26:15 +00:00
|
|
|
root["espUptime"] = esp_timer_get_time() / 1000000;
|
2023-11-08 19:29:06 +00:00
|
|
|
// root["currentPrice"] = getPrice();
|
|
|
|
// root["currentBlockHeight"] = getBlockHeight();
|
2023-11-07 20:26:15 +00:00
|
|
|
root["espFreeHeap"] = ESP.getFreeHeap();
|
|
|
|
root["espHeapSize"] = ESP.getHeapSize();
|
2023-11-08 19:29:06 +00:00
|
|
|
// root["espFreePsram"] = ESP.getFreePsram();
|
|
|
|
// root["espPsramSize"] = ESP.getPsramSize();
|
2023-11-07 20:26:15 +00:00
|
|
|
|
2023-11-08 11:18:59 +00:00
|
|
|
JsonObject conStatus = root.createNestedObject("connectionStatus");
|
|
|
|
conStatus["price"] = isPriceNotifyConnected();
|
|
|
|
conStatus["blocks"] = isBlockNotifyConnected();
|
|
|
|
|
2023-11-08 14:27:22 +00:00
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2023-11-17 21:09:28 +00:00
|
|
|
StaticJsonDocument<512> getLedStatusObject()
|
|
|
|
{
|
|
|
|
StaticJsonDocument<512> root;
|
|
|
|
JsonArray colors = root.createNestedArray("data");
|
|
|
|
// Adafruit_NeoPixel pix = getPixels();
|
|
|
|
|
|
|
|
for (uint i = 0; i < pixels.numPixels(); i++) {
|
|
|
|
uint32_t pixColor = pixels.getPixelColor(i);
|
|
|
|
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);
|
|
|
|
|
|
|
|
JsonObject object = colors.createNestedObject();
|
|
|
|
object["red"] = red;
|
|
|
|
object["green"] = green;
|
|
|
|
object["blue"] = blue;
|
|
|
|
object["hex"] = hexColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2023-11-08 19:29:06 +00:00
|
|
|
void eventSourceUpdate()
|
2023-11-08 14:27:22 +00:00
|
|
|
{
|
2023-11-10 19:59:08 +00:00
|
|
|
if (!events.count())
|
|
|
|
return;
|
2023-11-08 14:27:22 +00:00
|
|
|
StaticJsonDocument<768> root = getStatusObject();
|
|
|
|
JsonArray data = root.createNestedArray("data");
|
2023-11-17 21:09:28 +00:00
|
|
|
|
|
|
|
root["leds"] = getLedStatusObject()["data"];
|
|
|
|
|
2023-11-08 14:27:22 +00:00
|
|
|
String epdContent[NUM_SCREENS];
|
|
|
|
std::array<String, NUM_SCREENS> retEpdContent = getCurrentEpdContent();
|
|
|
|
std::copy(std::begin(retEpdContent), std::end(retEpdContent), epdContent);
|
|
|
|
|
|
|
|
copyArray(epdContent, data);
|
|
|
|
|
|
|
|
String bufString;
|
|
|
|
serializeJson(root, bufString);
|
|
|
|
|
|
|
|
events.send(bufString.c_str(), "status");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Api
|
|
|
|
* @Path("/api/status")
|
|
|
|
*/
|
|
|
|
void onApiStatus(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
AsyncResponseStream *response = request->beginResponseStream("application/json");
|
|
|
|
|
|
|
|
StaticJsonDocument<768> root = getStatusObject();
|
2023-11-07 20:26:15 +00:00
|
|
|
JsonArray data = root.createNestedArray("data");
|
|
|
|
JsonArray rendered = root.createNestedArray("rendered");
|
|
|
|
String epdContent[NUM_SCREENS];
|
|
|
|
|
|
|
|
std::array<String, NUM_SCREENS> retEpdContent = getCurrentEpdContent();
|
|
|
|
|
|
|
|
std::copy(std::begin(retEpdContent), std::end(retEpdContent), epdContent);
|
|
|
|
|
|
|
|
copyArray(epdContent, data);
|
|
|
|
copyArray(epdContent, rendered);
|
|
|
|
serializeJson(root, *response);
|
|
|
|
|
|
|
|
request->send(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Api
|
|
|
|
* @Path("/api/action/pause")
|
|
|
|
*/
|
|
|
|
void onApiActionPause(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
setTimerActive(false);
|
|
|
|
request->send(200);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Api
|
|
|
|
* @Path("/api/action/timer_restart")
|
|
|
|
*/
|
|
|
|
void onApiActionTimerRestart(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
setTimerActive(true);
|
|
|
|
request->send(200);
|
|
|
|
}
|
|
|
|
|
2023-11-12 23:33:48 +00:00
|
|
|
/**
|
|
|
|
* @Api
|
|
|
|
* @Path("/api/full_refresh")
|
|
|
|
*/
|
|
|
|
void onApiFullRefresh(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
forceFullRefresh();
|
|
|
|
std::array<String, NUM_SCREENS> newEpdContent = getCurrentEpdContent();
|
|
|
|
|
|
|
|
setEpdContent(newEpdContent, true);
|
|
|
|
|
|
|
|
request->send(200);
|
|
|
|
}
|
|
|
|
|
2023-11-14 22:09:23 +00:00
|
|
|
/**
|
|
|
|
* @Api
|
|
|
|
* @Path("/api/show/screen")
|
|
|
|
*/
|
2023-11-07 20:26:15 +00:00
|
|
|
void onApiShowScreen(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
if (request->hasParam("s"))
|
|
|
|
{
|
|
|
|
AsyncWebParameter *p = request->getParam("s");
|
|
|
|
uint currentScreen = p->value().toInt();
|
|
|
|
setCurrentScreen(currentScreen);
|
|
|
|
}
|
|
|
|
request->send(200);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onApiShowText(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
if (request->hasParam("t"))
|
|
|
|
{
|
|
|
|
AsyncWebParameter *p = request->getParam("t");
|
|
|
|
String t = p->value();
|
|
|
|
t.toUpperCase(); // This is needed as long as lowercase letters are glitchy
|
|
|
|
|
|
|
|
std::array<String, NUM_SCREENS> textEpdContent;
|
2023-11-08 14:27:22 +00:00
|
|
|
for (uint i = 0; i < NUM_SCREENS; i++)
|
|
|
|
{
|
2023-11-07 20:26:15 +00:00
|
|
|
textEpdContent[i] = t[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
setEpdContent(textEpdContent);
|
|
|
|
}
|
2023-11-10 18:52:06 +00:00
|
|
|
setCurrentScreen(SCREEN_CUSTOM);
|
2023-11-07 20:26:15 +00:00
|
|
|
request->send(200);
|
|
|
|
}
|
|
|
|
|
2023-11-14 18:46:29 +00:00
|
|
|
void onApiShowTextAdvanced(AsyncWebServerRequest *request, JsonVariant &json)
|
|
|
|
{
|
|
|
|
JsonArray screens = json.as<JsonArray>();
|
|
|
|
|
|
|
|
std::array<String, NUM_SCREENS> epdContent;
|
|
|
|
int i = 0;
|
|
|
|
for (JsonVariant s : screens)
|
|
|
|
{
|
|
|
|
epdContent[i] = s.as<String>();
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
setEpdContent(epdContent);
|
|
|
|
|
|
|
|
setCurrentScreen(SCREEN_CUSTOM);
|
|
|
|
request->send(200);
|
|
|
|
}
|
|
|
|
|
2023-11-17 18:28:40 +00:00
|
|
|
void onApiSettingsPatch(AsyncWebServerRequest *request, JsonVariant &json)
|
|
|
|
{
|
|
|
|
JsonObject settings = json.as<JsonObject>();
|
|
|
|
|
|
|
|
bool settingsChanged = true;
|
|
|
|
|
|
|
|
if (settings.containsKey("fgColor"))
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (settings.containsKey("bgColor"))
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settings.containsKey("timePerScreen"))
|
|
|
|
{
|
|
|
|
preferences.putUInt("timerSeconds", settings["timePerScreen"].as<uint>() * 60);
|
|
|
|
}
|
|
|
|
|
|
|
|
String strSettings[] = {"hostnamePrefix", "mempoolInstance"};
|
|
|
|
|
|
|
|
for (String setting : strSettings)
|
|
|
|
{
|
|
|
|
if (settings.containsKey(setting))
|
|
|
|
{
|
|
|
|
preferences.putString(setting.c_str(), settings[setting].as<String>());
|
|
|
|
Serial.printf("Setting %s to %s\r\n", setting.c_str(), settings[setting].as<String>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String uintSettings[] = {"minSecPriceUpd", "fullRefreshMin", "gmtOffset", "ledBrightness", "mcapBigChar"};
|
|
|
|
|
|
|
|
for (String setting : uintSettings)
|
|
|
|
{
|
|
|
|
if (settings.containsKey(setting))
|
|
|
|
{
|
|
|
|
preferences.putUInt(setting.c_str(), settings[setting].as<uint>());
|
|
|
|
Serial.printf("Setting %s to %d\r\n", setting.c_str(), settings[setting].as<uint>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String boolSettings[] = {"fetchEurPrice", "ledTestOnPower", "ledFlashOnUpd", "mdnsEnabled", "otaEnabled", "stealFocus", "mcapBigChar"};
|
|
|
|
|
|
|
|
for (String setting : boolSettings)
|
|
|
|
{
|
|
|
|
if (settings.containsKey(setting))
|
|
|
|
{
|
|
|
|
preferences.putBool(setting.c_str(), settings[setting].as<boolean>());
|
|
|
|
Serial.printf("Setting %s to %d\r\n", setting.c_str(), settings[setting].as<boolean>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settings.containsKey("screens"))
|
|
|
|
{
|
|
|
|
for (JsonVariant screen : settings["screens"].as<JsonArray>())
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
request->send(200);
|
|
|
|
if (settingsChanged)
|
|
|
|
{
|
|
|
|
queueLedEffect(LED_FLASH_SUCCESS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-07 20:26:15 +00:00
|
|
|
void onApiRestart(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
request->send(200);
|
2023-11-12 23:33:48 +00:00
|
|
|
|
|
|
|
if (events.count())
|
|
|
|
events.send("closing");
|
|
|
|
|
|
|
|
delay(500);
|
|
|
|
|
2023-11-07 20:26:15 +00:00
|
|
|
esp_restart();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Api
|
|
|
|
* @Method GET
|
|
|
|
* @Path("/api/settings")
|
|
|
|
*/
|
|
|
|
void onApiSettingsGet(AsyncWebServerRequest *request)
|
|
|
|
{
|
2023-11-10 18:52:06 +00:00
|
|
|
StaticJsonDocument<1536> root;
|
2023-11-07 20:26:15 +00:00
|
|
|
root["numScreens"] = NUM_SCREENS;
|
|
|
|
root["fgColor"] = getFgColor();
|
|
|
|
root["bgColor"] = getBgColor();
|
|
|
|
root["timerSeconds"] = getTimerSeconds();
|
2023-11-08 14:27:22 +00:00
|
|
|
root["timerRunning"] = isTimerActive();
|
|
|
|
root["minSecPriceUpd"] = preferences.getUInt("minSecPriceUpd", DEFAULT_SECONDS_BETWEEN_PRICE_UPDATE);
|
2023-11-14 18:46:29 +00:00
|
|
|
root["fullRefreshMin"] = preferences.getUInt("fullRefreshMin", DEFAULT_MINUTES_FULL_REFRESH);
|
2023-11-07 20:26:15 +00:00
|
|
|
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);
|
2023-11-10 18:52:06 +00:00
|
|
|
root["ledTestOnPower"] = preferences.getBool("ledTestOnPower", true);
|
2023-11-17 18:28:40 +00:00
|
|
|
root["ledFlashOnUpd"] = preferences.getBool("ledFlashOnUpd", false);
|
2023-11-07 20:26:15 +00:00
|
|
|
root["ledBrightness"] = preferences.getUInt("ledBrightness", 128);
|
2023-11-17 18:28:40 +00:00
|
|
|
root["stealFocus"] = preferences.getBool("stealFocus", true);
|
2023-11-10 19:59:08 +00:00
|
|
|
root["mcapBigChar"] = preferences.getBool("mcapBigChar", true);
|
2023-11-12 23:33:48 +00:00
|
|
|
root["mdnsEnabled"] = preferences.getBool("mdnsEnabled", true);
|
|
|
|
root["otaEnabled"] = preferences.getBool("otaEnabled", true);
|
2023-11-13 16:14:11 +00:00
|
|
|
root["fetchEurPrice"] = preferences.getBool("fetchEurPrice", false);
|
2023-11-15 23:08:46 +00:00
|
|
|
root["hostnamePrefix"] = preferences.getString("hostnamePrefix", "btclock");
|
2023-11-12 23:33:48 +00:00
|
|
|
root["hostname"] = getMyHostname();
|
|
|
|
root["ip"] = WiFi.localIP();
|
2023-11-07 20:26:15 +00:00
|
|
|
|
|
|
|
#ifdef GIT_REV
|
|
|
|
root["gitRev"] = String(GIT_REV);
|
|
|
|
#endif
|
|
|
|
#ifdef LAST_BUILD_TIME
|
|
|
|
root["lastBuildTime"] = String(LAST_BUILD_TIME);
|
|
|
|
#endif
|
|
|
|
JsonArray screens = root.createNestedArray("screens");
|
|
|
|
|
2023-11-08 19:29:06 +00:00
|
|
|
std::vector<std::string> screenNameMap = getScreenNameMap();
|
2023-11-08 11:18:59 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < screenNameMap.size(); i++)
|
|
|
|
{
|
|
|
|
JsonObject o = screens.createNestedObject();
|
|
|
|
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
|
|
|
|
|
|
|
AsyncResponseStream *response = request->beginResponseStream("application/json");
|
|
|
|
serializeJson(root, *response);
|
|
|
|
|
|
|
|
request->send(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool processEpdColorSettings(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
bool settingsChanged = false;
|
|
|
|
if (request->hasParam("fgColor", true))
|
|
|
|
{
|
|
|
|
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)));
|
2023-11-13 11:27:34 +00:00
|
|
|
// Serial.print(F("Setting foreground color to "));
|
|
|
|
// Serial.println(fgColor->value().c_str());
|
2023-11-07 20:26:15 +00:00
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
if (request->hasParam("bgColor", true))
|
|
|
|
{
|
|
|
|
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)));
|
2023-11-13 11:27:34 +00:00
|
|
|
// Serial.print(F("Setting background color to "));
|
|
|
|
// Serial.println(bgColor->value().c_str());
|
2023-11-07 20:26:15 +00:00
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return settingsChanged;
|
|
|
|
}
|
|
|
|
|
|
|
|
void onApiSettingsPost(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
bool settingsChanged = false;
|
|
|
|
|
|
|
|
settingsChanged = processEpdColorSettings(request);
|
|
|
|
|
2023-11-17 18:28:40 +00:00
|
|
|
int headers = request->headers();
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < headers; i++)
|
|
|
|
{
|
|
|
|
AsyncWebHeader *h = request->getHeader(i);
|
|
|
|
Serial.printf("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("FILE[%s]: %s, size: %u\n", p->name().c_str(), p->value().c_str(), p->size());
|
|
|
|
}
|
|
|
|
else if (p->isPost())
|
|
|
|
{
|
|
|
|
Serial.printf("POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Serial.printf("GET[%s]: %s\n", p->name().c_str(), p->value().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-13 16:14:11 +00:00
|
|
|
if (request->hasParam("fetchEurPrice", true))
|
|
|
|
{
|
|
|
|
AsyncWebParameter *fetchEurPrice = request->getParam("fetchEurPrice", true);
|
|
|
|
|
|
|
|
preferences.putBool("fetchEurPrice", fetchEurPrice->value().toInt());
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
preferences.putBool("fetchEurPrice", 0);
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
2023-11-12 23:33:48 +00:00
|
|
|
if (request->hasParam("ledTestOnPower", true))
|
|
|
|
{
|
|
|
|
AsyncWebParameter *ledTestOnPower = request->getParam("ledTestOnPower", true);
|
|
|
|
|
|
|
|
preferences.putBool("ledTestOnPower", ledTestOnPower->value().toInt());
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
preferences.putBool("ledTestOnPower", 0);
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
2023-11-07 20:26:15 +00:00
|
|
|
if (request->hasParam("ledFlashOnUpd", true))
|
|
|
|
{
|
|
|
|
AsyncWebParameter *ledFlashOnUpdate = request->getParam("ledFlashOnUpd", true);
|
|
|
|
|
|
|
|
preferences.putBool("ledFlashOnUpd", ledFlashOnUpdate->value().toInt());
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
preferences.putBool("ledFlashOnUpd", 0);
|
2023-11-12 23:33:48 +00:00
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request->hasParam("mdnsEnabled", true))
|
|
|
|
{
|
|
|
|
AsyncWebParameter *mdnsEnabled = request->getParam("mdnsEnabled", true);
|
|
|
|
|
|
|
|
preferences.putBool("mdnsEnabled", mdnsEnabled->value().toInt());
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
preferences.putBool("mdnsEnabled", 0);
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request->hasParam("otaEnabled", true))
|
|
|
|
{
|
|
|
|
AsyncWebParameter *otaEnabled = request->getParam("otaEnabled", true);
|
|
|
|
|
|
|
|
preferences.putBool("otaEnabled", otaEnabled->value().toInt());
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
preferences.putBool("otaEnabled", 0);
|
2023-11-07 20:26:15 +00:00
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
2023-11-10 19:59:08 +00:00
|
|
|
if (request->hasParam("stealFocusOnBlock", true))
|
|
|
|
{
|
|
|
|
AsyncWebParameter *stealFocusOnBlock = request->getParam("stealFocusOnBlock", true);
|
|
|
|
|
|
|
|
preferences.putBool("stealFocus", stealFocusOnBlock->value().toInt());
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
preferences.putBool("stealFocus", 0);
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request->hasParam("mcapBigChar", true))
|
|
|
|
{
|
|
|
|
AsyncWebParameter *mcapBigChar = request->getParam("mcapBigChar", true);
|
|
|
|
|
|
|
|
preferences.putBool("mcapBigChar", mcapBigChar->value().toInt());
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
preferences.putBool("mcapBigChar", 0);
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
2023-11-07 20:26:15 +00:00
|
|
|
if (request->hasParam("mempoolInstance", true))
|
|
|
|
{
|
|
|
|
AsyncWebParameter *mempoolInstance = request->getParam("mempoolInstance", true);
|
|
|
|
|
|
|
|
preferences.putString("mempoolInstance", mempoolInstance->value().c_str());
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
2023-11-15 23:08:46 +00:00
|
|
|
if (request->hasParam("hostnamePrefix", true))
|
|
|
|
{
|
|
|
|
AsyncWebParameter *hostnamePrefix = request->getParam("hostnamePrefix", true);
|
|
|
|
|
|
|
|
preferences.putString("hostnamePrefix", hostnamePrefix->value().c_str());
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
2023-11-07 20:26:15 +00:00
|
|
|
if (request->hasParam("ledBrightness", true))
|
|
|
|
{
|
|
|
|
AsyncWebParameter *ledBrightness = request->getParam("ledBrightness", true);
|
|
|
|
|
|
|
|
preferences.putUInt("ledBrightness", ledBrightness->value().toInt());
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request->hasParam("fullRefreshMin", true))
|
|
|
|
{
|
|
|
|
AsyncWebParameter *fullRefreshMin = request->getParam("fullRefreshMin", true);
|
|
|
|
|
|
|
|
preferences.putUInt("fullRefreshMin", fullRefreshMin->value().toInt());
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request->hasParam("wpTimeout", true))
|
|
|
|
{
|
|
|
|
AsyncWebParameter *wpTimeout = request->getParam("wpTimeout", true);
|
|
|
|
|
|
|
|
preferences.putUInt("wpTimeout", wpTimeout->value().toInt());
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
2023-11-08 19:29:06 +00:00
|
|
|
std::vector<std::string> screenNameMap = getScreenNameMap();
|
2023-11-08 11:18:59 +00:00
|
|
|
|
2023-11-17 18:28:40 +00:00
|
|
|
if (request->hasParam("screens"))
|
|
|
|
{
|
|
|
|
AsyncWebParameter *screenParam = request->getParam("screens", true);
|
|
|
|
|
|
|
|
Serial.printf(screenParam->value().c_str());
|
|
|
|
}
|
|
|
|
|
2023-11-08 11:18:59 +00:00
|
|
|
for (int i = 0; i < screenNameMap.size(); i++)
|
|
|
|
{
|
|
|
|
String key = "screen[" + String(i) + "]";
|
|
|
|
String prefKey = "screen" + String(i) + "Visible";
|
|
|
|
bool visible = false;
|
|
|
|
if (request->hasParam(key, true))
|
|
|
|
{
|
|
|
|
AsyncWebParameter *screenParam = request->getParam(key, true);
|
|
|
|
visible = screenParam->value().toInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
preferences.putBool(prefKey.c_str(), visible);
|
|
|
|
}
|
2023-11-07 20:26:15 +00:00
|
|
|
|
|
|
|
if (request->hasParam("tzOffset", true))
|
|
|
|
{
|
|
|
|
AsyncWebParameter *p = request->getParam("tzOffset", true);
|
|
|
|
int tzOffsetSeconds = p->value().toInt() * 60;
|
|
|
|
preferences.putInt("gmtOffset", tzOffsetSeconds);
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
2023-11-10 19:59:08 +00:00
|
|
|
if (request->hasParam("minSecPriceUpd", true))
|
2023-11-08 14:27:22 +00:00
|
|
|
{
|
|
|
|
AsyncWebParameter *p = request->getParam("minSecPriceUpd", true);
|
2023-11-08 19:29:06 +00:00
|
|
|
int minSecPriceUpd = p->value().toInt();
|
|
|
|
preferences.putUInt("minSecPriceUpd", minSecPriceUpd);
|
2023-11-08 14:27:22 +00:00
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
2023-11-07 20:26:15 +00:00
|
|
|
if (request->hasParam("timePerScreen", true))
|
|
|
|
{
|
|
|
|
AsyncWebParameter *p = request->getParam("timePerScreen", true);
|
|
|
|
uint timerSeconds = p->value().toInt() * 60;
|
|
|
|
preferences.putUInt("timerSeconds", timerSeconds);
|
|
|
|
settingsChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
request->send(200);
|
|
|
|
if (settingsChanged)
|
|
|
|
{
|
2023-11-08 11:18:59 +00:00
|
|
|
queueLedEffect(LED_FLASH_SUCCESS);
|
2023-11-07 20:26:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onApiSystemStatus(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
AsyncResponseStream *response = request->beginResponseStream("application/json");
|
|
|
|
|
|
|
|
StaticJsonDocument<128> root;
|
|
|
|
|
|
|
|
root["espFreeHeap"] = ESP.getFreeHeap();
|
|
|
|
root["espHeapSize"] = ESP.getHeapSize();
|
|
|
|
root["espFreePsram"] = ESP.getFreePsram();
|
|
|
|
root["espPsramSize"] = ESP.getPsramSize();
|
|
|
|
|
|
|
|
serializeJson(root, *response);
|
|
|
|
|
|
|
|
request->send(response);
|
|
|
|
}
|
|
|
|
|
2023-11-17 21:09:28 +00:00
|
|
|
void onApiLightsStatus(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
AsyncResponseStream *response = request->beginResponseStream("application/json");
|
|
|
|
|
|
|
|
serializeJson(getLedStatusObject(), *response);
|
|
|
|
|
|
|
|
request->send(response);
|
|
|
|
}
|
|
|
|
|
2023-11-08 11:18:59 +00:00
|
|
|
void onApiLightsOff(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
setLights(0, 0, 0);
|
|
|
|
request->send(200);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onApiLightsSetColor(AsyncWebServerRequest *request)
|
|
|
|
{
|
2023-11-08 14:27:22 +00:00
|
|
|
if (request->hasParam("c"))
|
|
|
|
{
|
|
|
|
String rgbColor = request->getParam("c")->value();
|
2023-11-12 23:33:48 +00:00
|
|
|
|
2023-11-14 22:09:23 +00:00
|
|
|
if (rgbColor.compareTo("off") == 0)
|
|
|
|
{
|
2023-11-12 23:33:48 +00:00
|
|
|
setLights(0, 0, 0);
|
2023-11-14 22:09:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-11-12 23:33:48 +00:00
|
|
|
uint r, g, b;
|
|
|
|
sscanf(rgbColor.c_str(), "%02x%02x%02x", &r, &g, &b);
|
|
|
|
setLights(r, g, b);
|
|
|
|
}
|
2023-11-08 14:27:22 +00:00
|
|
|
request->send(200, "text/plain", rgbColor);
|
|
|
|
}
|
2023-11-08 11:18:59 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 20:26:15 +00:00
|
|
|
void onIndex(AsyncWebServerRequest *request) { request->send(LittleFS, "/index.html", String(), false); }
|
|
|
|
|
2023-11-07 00:11:12 +00:00
|
|
|
void onNotFound(AsyncWebServerRequest *request)
|
|
|
|
{
|
2023-11-14 22:09:23 +00:00
|
|
|
// Access-Control-Request-Method == POST might be better
|
|
|
|
|
|
|
|
if (request->method() == HTTP_OPTIONS || request->hasHeader("Sec-Fetch-Mode"))
|
2023-11-07 00:11:12 +00:00
|
|
|
{
|
|
|
|
request->send(200);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-11-17 18:28:40 +00:00
|
|
|
request->send(200);
|
2023-11-07 00:11:12 +00:00
|
|
|
}
|
|
|
|
};
|
2023-11-08 19:29:06 +00:00
|
|
|
|
2023-11-10 19:59:08 +00:00
|
|
|
void eventSourceTask(void *pvParameters)
|
|
|
|
{
|
2023-11-08 19:29:06 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
|
|
|
|
eventSourceUpdate();
|
|
|
|
}
|
|
|
|
}
|