1
0
mirror of https://github.com/s00500/ESPUI.git synced 2024-11-21 22:50:55 +00:00

Add Port Option

Signed-off-by: Lukas Bachschwell <lukas@lbsfilm.at>
This commit is contained in:
Lukas Bachschwell 2021-10-29 19:20:20 +02:00
parent 6b3056ab31
commit aa03c1683d
Signed by: lbsadmin
GPG Key ID: CCC6AA87CC8DF425
2 changed files with 58 additions and 64 deletions

View File

@ -27,12 +27,11 @@ void listDir(const char* dirname, uint8_t levels)
#endif #endif
#if defined(ESP32) #if defined(ESP32)
File root = SPIFFS.open(dirname); File root = SPIFFS.open(dirname);
#else #else
File root = LittleFS.open(dirname); File root = LittleFS.open(dirname);
#endif #endif
if (!root) if (!root)
{ {
#if defined(DEBUG_ESPUI) #if defined(DEBUG_ESPUI)
@ -116,24 +115,24 @@ void listDir(const char* dirname, uint8_t levels)
void ESPUIClass::list() void ESPUIClass::list()
{ {
#if defined(ESP32) #if defined(ESP32)
if (!SPIFFS.begin()) if (!SPIFFS.begin())
{ {
Serial.println(F("SPIFFS Mount Failed")); Serial.println(F("SPIFFS Mount Failed"));
return; return;
} }
#else #else
if (!LittleFS.begin()) if (!LittleFS.begin())
{ {
Serial.println(F("LittleFS Mount Failed")); Serial.println(F("LittleFS Mount Failed"));
return; return;
} }
#endif #endif
listDir("/", 1); listDir("/", 1);
#if defined(ESP32) #if defined(ESP32)
Serial.println(SPIFFS.totalBytes()); Serial.println(SPIFFS.totalBytes());
Serial.println(SPIFFS.usedBytes()); Serial.println(SPIFFS.usedBytes());
#else #else
FSInfo fs_info; FSInfo fs_info;
@ -147,10 +146,10 @@ if (!LittleFS.begin())
void deleteFile(const char* path) void deleteFile(const char* path)
{ {
#if defined(ESP32) #if defined(ESP32)
bool exists = SPIFFS.exists(path); bool exists = SPIFFS.exists(path);
#else #else
bool exists = LittleFS.exists(path); bool exists = LittleFS.exists(path);
#endif #endif
if (!exists) if (!exists)
@ -173,12 +172,12 @@ void deleteFile(const char* path)
#endif #endif
#if defined(ESP32) #if defined(ESP32)
bool didRemove = SPIFFS.remove(path); bool didRemove = SPIFFS.remove(path);
#else #else
bool didRemove = LittleFS.remove(path); bool didRemove = LittleFS.remove(path);
#endif #endif
if (didRemove) if (didRemove)
{ {
#if defined(DEBUG_ESPUI) #if defined(DEBUG_ESPUI)
if (ESPUI.verbosity) if (ESPUI.verbosity)
{ {
@ -207,13 +206,13 @@ void writeFile(const char* path, const char* data)
#endif #endif
#if defined(ESP32) #if defined(ESP32)
File file = SPIFFS.open(path, FILE_WRITE); File file = SPIFFS.open(path, FILE_WRITE);
#else #else
File file = LittleFS.open(path, FILE_WRITE); File file = LittleFS.open(path, FILE_WRITE);
#endif #endif
if (!file) if (!file)
{ {
#if defined(DEBUG_ESPUI) #if defined(DEBUG_ESPUI)
if (ESPUI.verbosity) if (ESPUI.verbosity)
{ {
@ -369,9 +368,9 @@ void ESPUIClass::prepareFileSystem()
#endif #endif
#if defined(ESP32) #if defined(ESP32)
SPIFFS.end(); SPIFFS.end();
#else #else
LittleFS.end(); LittleFS.end();
#endif #endif
} }
@ -381,8 +380,7 @@ void onWsEvent(
{ {
switch (type) switch (type)
{ {
case WS_EVT_DISCONNECT: case WS_EVT_DISCONNECT: {
{
#if defined(DEBUG_ESPUI) #if defined(DEBUG_ESPUI)
if (ESPUI.verbosity) if (ESPUI.verbosity)
{ {
@ -393,8 +391,7 @@ void onWsEvent(
break; break;
} }
case WS_EVT_PONG: case WS_EVT_PONG: {
{
#if defined(DEBUG_ESPUI) #if defined(DEBUG_ESPUI)
if (ESPUI.verbosity) if (ESPUI.verbosity)
{ {
@ -405,8 +402,7 @@ void onWsEvent(
break; break;
} }
case WS_EVT_ERROR: case WS_EVT_ERROR: {
{
#if defined(DEBUG_ESPUI) #if defined(DEBUG_ESPUI)
if (ESPUI.verbosity) if (ESPUI.verbosity)
{ {
@ -417,8 +413,7 @@ void onWsEvent(
break; break;
} }
case WS_EVT_CONNECT: case WS_EVT_CONNECT: {
{
#if defined(DEBUG_ESPUI) #if defined(DEBUG_ESPUI)
if (ESPUI.verbosity) if (ESPUI.verbosity)
{ {
@ -438,8 +433,7 @@ void onWsEvent(
} }
break; break;
case WS_EVT_DATA: case WS_EVT_DATA: {
{
String msg = ""; String msg = "";
msg.reserve(len + 1); msg.reserve(len + 1);
@ -569,7 +563,7 @@ void onWsEvent(
else if (msg.startsWith("tabvalue:")) else if (msg.startsWith("tabvalue:"))
{ {
c->callback(c, client->id()); c->callback(c, client->id());
} }
else if (msg.startsWith(F("svalue:"))) else if (msg.startsWith(F("svalue:")))
{ {
c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')); c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':'));
@ -889,7 +883,7 @@ void ESPUIClass::updateGauge(uint16_t id, int number, int clientId)
updateControlValue(id, String(number), clientId); updateControlValue(id, String(number), clientId);
} }
void ESPUIClass::clearGraph(uint16_t id, int clientId) {} void ESPUIClass::clearGraph(uint16_t id, int clientId) { }
void ESPUIClass::addGraphPoint(uint16_t id, int nValue, int clientId) void ESPUIClass::addGraphPoint(uint16_t id, int nValue, int clientId)
{ {
@ -1037,7 +1031,7 @@ void ESPUIClass::jsonReload()
this->ws->textAll(json); this->ws->textAll(json);
} }
void ESPUIClass::beginSPIFFS(const char* _title, const char* username, const char* password) void ESPUIClass::beginSPIFFS(const char* _title, const char* username, const char* password, uint16_t port)
{ {
ui_title = _title; ui_title = _title;
this->basicAuthUsername = username; this->basicAuthUsername = username;
@ -1052,16 +1046,16 @@ void ESPUIClass::beginSPIFFS(const char* _title, const char* username, const cha
basicAuth = true; basicAuth = true;
} }
server = new AsyncWebServer(80); server = new AsyncWebServer(port);
ws = new AsyncWebSocket("/ws"); ws = new AsyncWebSocket("/ws");
#if defined(ESP32) #if defined(ESP32)
bool fsBegin = SPIFFS.begin(); bool fsBegin = SPIFFS.begin();
#else #else
bool fsBegin = LittleFS.begin(); bool fsBegin = LittleFS.begin();
#endif #endif
if (!fsBegin) if (!fsBegin)
{ {
#if defined(DEBUG_ESPUI) #if defined(DEBUG_ESPUI)
if (ESPUI.verbosity) if (ESPUI.verbosity)
{ {
@ -1081,12 +1075,12 @@ void ESPUIClass::beginSPIFFS(const char* _title, const char* username, const cha
#endif #endif
#if defined(ESP32) #if defined(ESP32)
bool indexExists = SPIFFS.exists("/index.htm"); bool indexExists = SPIFFS.exists("/index.htm");
#else #else
bool indexExists = LittleFS.exists("/index.htm"); bool indexExists = LittleFS.exists("/index.htm");
#endif #endif
if (!indexExists) if (!indexExists)
{ {
#if defined(DEBUG_ESPUI) #if defined(DEBUG_ESPUI)
if (ESPUI.verbosity) if (ESPUI.verbosity)
{ {
@ -1108,17 +1102,17 @@ void ESPUIClass::beginSPIFFS(const char* _title, const char* username, const cha
ws->setAuthentication(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword); ws->setAuthentication(ESPUI.basicAuthUsername, ESPUI.basicAuthPassword);
} }
#if defined(ESP32) #if defined(ESP32)
server->serveStatic("/", SPIFFS, "/").setDefaultFile("index.htm").setAuthentication(username, password); server->serveStatic("/", SPIFFS, "/").setDefaultFile("index.htm").setAuthentication(username, password);
#else #else
server->serveStatic("/", LittleFS, "/").setDefaultFile("index.htm").setAuthentication(username, password); server->serveStatic("/", LittleFS, "/").setDefaultFile("index.htm").setAuthentication(username, password);
#endif #endif
} }
else else
{ {
#if defined(ESP32) #if defined(ESP32)
server->serveStatic("/", SPIFFS, "/").setDefaultFile("index.htm"); server->serveStatic("/", SPIFFS, "/").setDefaultFile("index.htm");
#else #else
server->serveStatic("/", LittleFS, "/").setDefaultFile("index.htm"); server->serveStatic("/", LittleFS, "/").setDefaultFile("index.htm");
#endif #endif
} }
@ -1144,7 +1138,7 @@ void ESPUIClass::beginSPIFFS(const char* _title, const char* username, const cha
#endif #endif
} }
void ESPUIClass::begin(const char* _title, const char* username, const char* password) void ESPUIClass::begin(const char* _title, const char* username, const char* password, uint16_t port)
{ {
basicAuthUsername = username; basicAuthUsername = username;
basicAuthPassword = password; basicAuthPassword = password;
@ -1160,7 +1154,7 @@ void ESPUIClass::begin(const char* _title, const char* username, const char* pas
ui_title = _title; ui_title = _title;
server = new AsyncWebServer(80); server = new AsyncWebServer(port);
ws = new AsyncWebSocket("/ws"); ws = new AsyncWebSocket("/ws");
ws->onEvent(onWsEvent); ws->onEvent(onWsEvent);

View File

@ -215,10 +215,10 @@ public:
bool sliderContinuous; bool sliderContinuous;
void setVerbosity(Verbosity verbosity); void setVerbosity(Verbosity verbosity);
void begin(const char* _title, const char* username = nullptr, void begin(const char* _title, const char* username = nullptr, const char* password = nullptr,
const char* password = nullptr); // Setup server and page in Memorymode uint16_t port = 80); // Setup server and page in Memorymode
void beginSPIFFS(const char* _title, const char* username = nullptr, void beginSPIFFS(const char* _title, const char* username = nullptr, const char* password = nullptr,
const char* password = nullptr); // Setup server and page in SPIFFSmode uint16_t port = 80); // Setup server and page in SPIFFSmode
void prepareFileSystem(); // Initially preps the filesystem and loads a lot of void prepareFileSystem(); // Initially preps the filesystem and loads a lot of
// stuff into SPIFFS // stuff into SPIFFS