mirror of
https://github.com/s00500/ESPUI.git
synced 2024-11-22 14:20:53 +00:00
ixing filelist functions for esp8266
This commit is contained in:
parent
7e4dbd7e03
commit
5f7f8dd4e5
@ -1,5 +1,5 @@
|
|||||||
name=ESPUI
|
name=ESPUI
|
||||||
version=1.4.4
|
version=1.4.5
|
||||||
author=Lukas Bachschwell
|
author=Lukas Bachschwell
|
||||||
maintainer=Lukas Bachschwell <lukas@lbsfilm.at>
|
maintainer=Lukas Bachschwell <lukas@lbsfilm.at>
|
||||||
sentence=ESP32 and ESP8266 Web Interface Library
|
sentence=ESP32 and ESP8266 Web Interface Library
|
||||||
|
@ -2,33 +2,35 @@
|
|||||||
|
|
||||||
#include "uploadDataIndex.h"
|
#include "uploadDataIndex.h"
|
||||||
|
|
||||||
|
|
||||||
#include "uploadDataStyle.h"
|
|
||||||
#include "uploadDataNormalize.h"
|
#include "uploadDataNormalize.h"
|
||||||
|
#include "uploadDataStyle.h"
|
||||||
|
|
||||||
#include "uploadDataControls.h"
|
#include "uploadDataControls.h"
|
||||||
#include "uploadDataZepto.h"
|
|
||||||
#include "uploadDataSlider.h"
|
#include "uploadDataSlider.h"
|
||||||
|
#include "uploadDataZepto.h"
|
||||||
|
|
||||||
#include <ESPAsyncWebServer.h>
|
#include <ESPAsyncWebServer.h>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
// ################# Spiffs functions
|
// ################# Spiffs functions
|
||||||
|
#if defined(ESP32)
|
||||||
void listDir(const char *dirname, uint8_t levels) {
|
void listDir(const char *dirname, uint8_t levels) {
|
||||||
Serial.printf("Listing directory: %s\n", dirname);
|
Serial.printf("Listing directory: %s\n", dirname);
|
||||||
|
|
||||||
File root = SPIFFS.open(dirname);
|
File root = SPIFFS.open(dirname);
|
||||||
|
|
||||||
if (!root) {
|
if (!root) {
|
||||||
Serial.println("Failed to open directory");
|
Serial.println("Failed to open directory");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!root.isDirectory()) {
|
if (!root.isDirectory()) {
|
||||||
Serial.println("Not a directory");
|
Serial.println("Not a directory");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
File file = root.openNextFile();
|
File file = root.openNextFile();
|
||||||
|
|
||||||
while (file) {
|
while (file) {
|
||||||
if (file.isDirectory()) {
|
if (file.isDirectory()) {
|
||||||
Serial.print(" DIR : ");
|
Serial.print(" DIR : ");
|
||||||
@ -42,9 +44,45 @@ void listDir(const char * dirname, uint8_t levels){
|
|||||||
Serial.print(" SIZE: ");
|
Serial.print(" SIZE: ");
|
||||||
Serial.println(file.size());
|
Serial.println(file.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
file = root.openNextFile();
|
file = root.openNextFile();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
|
||||||
|
void listDir(const char *dirname, uint8_t levels) {
|
||||||
|
// ignoring levels for esp8266
|
||||||
|
Serial.printf("Listing directory: %s\n", dirname);
|
||||||
|
|
||||||
|
String str = "";
|
||||||
|
Dir dir = SPIFFS.openDir("/");
|
||||||
|
while (dir.next()) {
|
||||||
|
Serial.print(" FILE: ");
|
||||||
|
Serial.print(dir.fileName());
|
||||||
|
Serial.print(" SIZE: ");
|
||||||
|
Serial.println(dir.fileSize());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void ESPUIClass::list() {
|
||||||
|
if (!SPIFFS.begin()) {
|
||||||
|
Serial.println("SPIFFS Mount Failed");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
listDir("/", 1);
|
||||||
|
#if defined(ESP32)
|
||||||
|
Serial.println(SPIFFS.totalBytes());
|
||||||
|
Serial.println(SPIFFS.usedBytes());
|
||||||
|
#else
|
||||||
|
FSInfo fs_info;
|
||||||
|
SPIFFS.info(fs_info);
|
||||||
|
|
||||||
|
Serial.println(fs_info.totalBytes);
|
||||||
|
Serial.println(fs_info.usedBytes);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
void deleteFile(const char *path) {
|
void deleteFile(const char *path) {
|
||||||
Serial.print(SPIFFS.exists(path));
|
Serial.print(SPIFFS.exists(path));
|
||||||
@ -91,6 +129,7 @@ Serial.println("About to prepare filesystem...");
|
|||||||
Serial.println("SPIFFS Mount Failed");
|
Serial.println("SPIFFS Mount Failed");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
listDir("/", 1);
|
||||||
Serial.println("SPIFFS Mount ESP32 Done");
|
Serial.println("SPIFFS Mount ESP32 Done");
|
||||||
#else
|
#else
|
||||||
SPIFFS.format();
|
SPIFFS.format();
|
||||||
@ -98,9 +137,8 @@ Serial.println("About to prepare filesystem...");
|
|||||||
Serial.println("SPIFFS Mount ESP8266 Done");
|
Serial.println("SPIFFS Mount ESP8266 Done");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
listDir("/", 1);
|
// TODO: This is a workaround, have to find out why SPIFFS on ESP32 behaves
|
||||||
|
// incredibly strangely, see issue #6
|
||||||
//TODO: This is a workaround, have to find out why SPIFFS on ESP32 behaves incredibly strangely, see issue #6
|
|
||||||
/*
|
/*
|
||||||
deleteFile("/index.htm");
|
deleteFile("/index.htm");
|
||||||
|
|
||||||
@ -125,22 +163,14 @@ writeFile("/js/controls.js", JS_CONTROLS);
|
|||||||
writeFile("/js/slider.js", JS_SLIDER);
|
writeFile("/js/slider.js", JS_SLIDER);
|
||||||
|
|
||||||
Serial.println("Done Initializing filesystem :-)");
|
Serial.println("Done Initializing filesystem :-)");
|
||||||
|
|
||||||
|
#if defined(ESP32)
|
||||||
listDir("/", 1);
|
listDir("/", 1);
|
||||||
|
#endif
|
||||||
|
|
||||||
SPIFFS.end();
|
SPIFFS.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ESPUIClass::list() {
|
|
||||||
if(!SPIFFS.begin()) {
|
|
||||||
Serial.println("SPIFFS Mount Failed");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
listDir("/", 1);
|
|
||||||
|
|
||||||
Serial.println(SPIFFS.totalBytes());
|
|
||||||
Serial.println(SPIFFS.usedBytes());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Handle Websockets Communication
|
// Handle Websockets Communication
|
||||||
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
|
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
|
||||||
AwsEventType type, void *arg, uint8_t *data, size_t len) {
|
AwsEventType type, void *arg, uint8_t *data, size_t len) {
|
||||||
@ -159,8 +189,7 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
|
|||||||
if (debug) {
|
if (debug) {
|
||||||
Serial.println("JSON Data Sent to Client!");
|
Serial.println("JSON Data Sent to Client!");
|
||||||
}
|
}
|
||||||
}
|
} break;
|
||||||
break;
|
|
||||||
case WS_EVT_DATA:
|
case WS_EVT_DATA:
|
||||||
String msg = "";
|
String msg = "";
|
||||||
for (size_t i = 0; i < len; i++) {
|
for (size_t i = 0; i < len; i++) {
|
||||||
@ -168,10 +197,12 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
|
|||||||
}
|
}
|
||||||
int id = msg.substring(msg.lastIndexOf(':') + 1).toInt();
|
int id = msg.substring(msg.lastIndexOf(':') + 1).toInt();
|
||||||
if (id >= ESPUI.cIndex) {
|
if (id >= ESPUI.cIndex) {
|
||||||
if(debug) Serial.println("Maleformated id in websocket message");
|
if (debug)
|
||||||
|
Serial.println("Maleformated id in websocket message");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Control *c = ESPUI.controls[msg.substring(msg.lastIndexOf(':') + 1).toInt()];
|
Control *c =
|
||||||
|
ESPUI.controls[msg.substring(msg.lastIndexOf(':') + 1).toInt()];
|
||||||
|
|
||||||
if (msg.startsWith("bdown:")) {
|
if (msg.startsWith("bdown:")) {
|
||||||
c->callback(*c, B_DOWN);
|
c->callback(*c, B_DOWN);
|
||||||
@ -204,7 +235,8 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
|
|||||||
ESPUI.updateSwitcher(c->id, false);
|
ESPUI.updateSwitcher(c->id, false);
|
||||||
c->callback(*c, S_INACTIVE);
|
c->callback(*c, S_INACTIVE);
|
||||||
} else if (msg.startsWith("slvalue:")) {
|
} else if (msg.startsWith("slvalue:")) {
|
||||||
int value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')).toInt();
|
int value =
|
||||||
|
msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')).toInt();
|
||||||
ESPUI.updateSlider(c->id, value, client->id());
|
ESPUI.updateSlider(c->id, value, client->id());
|
||||||
c->callback(*c, SL_VALUE);
|
c->callback(*c, SL_VALUE);
|
||||||
}
|
}
|
||||||
@ -235,7 +267,8 @@ void ESPUIClass::label(const char *label, int color, String value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: this still needs a range setting
|
// TODO: this still needs a range setting
|
||||||
void ESPUIClass::slider(const char *label, void (*callBack)(Control, int), int color, String value) {
|
void ESPUIClass::slider(const char *label, void (*callBack)(Control, int),
|
||||||
|
int color, String value) {
|
||||||
if (labelExists(label)) {
|
if (labelExists(label)) {
|
||||||
if (debug)
|
if (debug)
|
||||||
Serial.println("UI ERROR: Element " + String(label) +
|
Serial.println("UI ERROR: Element " + String(label) +
|
||||||
@ -382,8 +415,7 @@ void ESPUIClass::updateSlider(int id, int nValue, int clientId ) {
|
|||||||
textThem(json, clientId);
|
textThem(json, clientId);
|
||||||
} else {
|
} else {
|
||||||
if (debug)
|
if (debug)
|
||||||
Serial.println(String("Error: ") + String(id) +
|
Serial.println(String("Error: ") + String(id) + String(" is no slider"));
|
||||||
String(" is no slider"));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -397,7 +429,8 @@ void ESPUIClass::updateSwitcher(String label, bool nValue, int clientId) {
|
|||||||
updateSwitcher(getIdByLabel(label), nValue, clientId);
|
updateSwitcher(getIdByLabel(label), nValue, clientId);
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is a hacky workaround because ESPAsyncWebServer does not have a function like this and it's clients array is private
|
// This is a hacky workaround because ESPAsyncWebServer does not have a function
|
||||||
|
// like this and it's clients array is private
|
||||||
void ESPUIClass::textThem(String text, int clientId) {
|
void ESPUIClass::textThem(String text, int clientId) {
|
||||||
int tryId = 0;
|
int tryId = 0;
|
||||||
for (int count = 0; count < this->ws->count();) {
|
for (int count = 0; count < this->ws->count();) {
|
||||||
@ -455,13 +488,15 @@ void ESPUIClass::begin(const char *_title) {
|
|||||||
ws = new AsyncWebSocket("/ws");
|
ws = new AsyncWebSocket("/ws");
|
||||||
|
|
||||||
if (!SPIFFS.begin()) {
|
if (!SPIFFS.begin()) {
|
||||||
Serial.println("SPIFFS Mount Failed, PLEASE CHECK THE README ON HOW TO PREPARE YOUR ESP!!!!!!!");
|
Serial.println("SPIFFS Mount Failed, PLEASE CHECK THE README ON HOW TO "
|
||||||
|
"PREPARE YOUR ESP!!!!!!!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
listDir("/", 1);
|
listDir("/", 1);
|
||||||
|
|
||||||
if (!SPIFFS.exists("/index.htm")) {
|
if (!SPIFFS.exists("/index.htm")) {
|
||||||
Serial.println("Please read the README!!!!!!!, Make sure to ESPUI.prepareFileSystem() once in an empty sketch");
|
Serial.println("Please read the README!!!!!!!, Make sure to "
|
||||||
|
"ESPUI.prepareFileSystem() once in an empty sketch");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -475,9 +510,7 @@ void ESPUIClass::begin(const char *_title) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
server->onNotFound(
|
server->onNotFound(
|
||||||
[](AsyncWebServerRequest *request) {
|
[](AsyncWebServerRequest *request) { request->send(404); });
|
||||||
request->send(404);
|
|
||||||
});
|
|
||||||
|
|
||||||
server->begin();
|
server->begin();
|
||||||
if (debug)
|
if (debug)
|
||||||
|
Loading…
Reference in New Issue
Block a user