Skip to content

Commit

Permalink
simpler fsInfo struct (String), hidden option bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
cotestatnt committed Jan 13, 2024
1 parent 4470bd7 commit b0d0925
Show file tree
Hide file tree
Showing 15 changed files with 199 additions and 156 deletions.
2 changes: 1 addition & 1 deletion built-in-webpages/setup/all.htm

Large diffs are not rendered by default.

110 changes: 55 additions & 55 deletions built-in-webpages/setup/all.htm.c

Large diffs are not rendered by default.

Binary file modified built-in-webpages/setup/all.htm.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion built-in-webpages/setup/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ function addOptionsElement(opt) {
lastBox.appendChild(d);

if(key.endsWith('-hidden')) {
el.classList.add('hide');
d.classList.add('hide');
}
});

Expand Down
5 changes: 3 additions & 2 deletions examples/gpio_list/gpio_list.ino
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,9 @@ void setup() {
*/
#ifdef ESP32
server.setFsInfoCallback([](fsInfo_t* fsInfo) {
fsInfo->totalBytes = LittleFS.totalBytes();
fsInfo->usedBytes = LittleFS.usedBytes();
fsInfo->fsName = "LittleFS";
fsInfo->totalBytes = LittleFS.totalBytes();
fsInfo->usedBytes = LittleFS.usedBytes();
});
#endif

Expand Down
6 changes: 3 additions & 3 deletions examples/handleFormData/handleFormData.ino
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ void setup(){
*/
#ifdef ESP32
server.setFsInfoCallback( [](fsInfo_t* fsInfo) {
fsInfo->totalBytes = LittleFS.totalBytes();
fsInfo->usedBytes = LittleFS.usedBytes();
strcpy(fsInfo->fsName, "LittleFS");
fsInfo->fsName = "LittleFS";
fsInfo->totalBytes = LittleFS.totalBytes();
fsInfo->usedBytes = LittleFS.usedBytes();
});
#endif

Expand Down
5 changes: 3 additions & 2 deletions examples/highcharts/highcharts.ino
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ void setup() {
*/
#ifdef ESP32
server.setFsInfoCallback([](fsInfo_t* fsInfo) {
fsInfo->totalBytes = LittleFS.totalBytes();
fsInfo->usedBytes = LittleFS.usedBytes();
fsInfo->fsName = "LittleFS";
fsInfo->totalBytes = LittleFS.totalBytes();
fsInfo->usedBytes = LittleFS.usedBytes();
});
#endif

Expand Down
5 changes: 3 additions & 2 deletions examples/remoteOTA/remoteOTA.ino
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,9 @@ void setup(){
*/
#ifdef ESP32
server.setFsInfoCallback([](fsInfo_t* fsInfo) {
fsInfo->totalBytes = LittleFS.totalBytes();
fsInfo->usedBytes = LittleFS.usedBytes();
fsInfo->fsName = "LittleFS";
fsInfo->totalBytes = LittleFS.totalBytes();
fsInfo->usedBytes = LittleFS.usedBytes();
});
#endif

Expand Down
6 changes: 3 additions & 3 deletions examples/simpleServer/simpleServer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ bool startFilesystem(){
*/
#ifdef ESP32
void getFsInfo(fsInfo_t* fsInfo) {
fsInfo->totalBytes = LittleFS.totalBytes();
fsInfo->usedBytes = LittleFS.usedBytes();
strcpy(fsInfo->fsName, "LittleFS");
fsInfo->fsName = "LittleFS";
fsInfo->totalBytes = LittleFS.totalBytes();
fsInfo->usedBytes = LittleFS.usedBytes();
}
#endif

Expand Down
6 changes: 3 additions & 3 deletions examples/simpleServerCaptive/simpleServerCaptive.ino
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ bool startFilesystem() {
*/
#ifdef ESP32
void getFsInfo(fsInfo_t* fsInfo) {
fsInfo->totalBytes = LittleFS.totalBytes();
fsInfo->usedBytes = LittleFS.usedBytes();
strcpy(fsInfo->fsName, "LittleFS");
fsInfo->fsName = "LittleFS";
fsInfo->totalBytes = LittleFS.totalBytes();
fsInfo->usedBytes = LittleFS.usedBytes();
}
#endif

Expand Down
6 changes: 3 additions & 3 deletions examples/withWebSocket/withWebSocket.ino
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,9 @@ void setup() {
*/
#ifdef ESP32
server.setFsInfoCallback([](fsInfo_t* fsInfo) {
fsInfo->totalBytes = LittleFS.totalBytes();
fsInfo->usedBytes = LittleFS.usedBytes();
strcpy(fsInfo->fsName, "LittleFS");
fsInfo->fsName = "LittleFS";
fsInfo->totalBytes = LittleFS.totalBytes();
fsInfo->usedBytes = LittleFS.usedBytes();
});
#endif

Expand Down
25 changes: 21 additions & 4 deletions src/AsyncFsWebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,13 @@ void AsyncFsWebServer::enableFsCodeEditor() {

bool AsyncFsWebServer::startCaptivePortal(const char* ssid, const char* pass, const char* redirectTargetURL) {

if (! WiFi.softAP(ssid, pass)) {
bool ap = false;
if (strlen(pass))
ap = WiFi.softAP(ssid, pass);
else
ap = WiFi.softAP(ssid);

if (!ap) {
log_error("Captive portal failed to start: WiFi.softAP failed!");
return false;
}
Expand Down Expand Up @@ -332,7 +338,7 @@ void AsyncFsWebServer::handleUpload(AsyncWebServerRequest *request, String filen

// open the file on first call and store the file handle in the request object
request->_tempFile = m_filesystem->open(filename, "w");
log_info("Upload Start.\nWriting file %s\n", filename.c_str());
log_debug("Upload Start: writing file %s", filename.c_str());
}

if (len) {
Expand All @@ -345,7 +351,7 @@ void AsyncFsWebServer::handleUpload(AsyncWebServerRequest *request, String filen
setTaskWdt(8000);
// close the file handle as the upload is now done
request->_tempFile.close();
log_info("\nUpload complete: %s, size: %d \n", filename.c_str(), index + len);
log_debug("Upload complete: %s, size: %d", filename.c_str(), index + len);
}
}

Expand Down Expand Up @@ -645,6 +651,17 @@ IPAddress AsyncFsWebServer::startWiFi(uint32_t timeout, CallbackF fn ) {
}
}
}

// No connection, start AP and then run captive portal
if (!ip) {
char ssid[21];
#ifdef ESP8266
snprintf(ssid, sizeof(ssid), "ESP-%lX", ESP.getChipId());
#elif defined(ESP32)
snprintf(ssid, sizeof(ssid), "ESP-%llX", ESP.getEfuseMac());
#endif
startCaptivePortal((const char*)ssid, "", "/setup");
}
return ip;
}

Expand Down Expand Up @@ -839,7 +856,7 @@ void AsyncFsWebServer::handleFileDelete(AsyncWebServerRequest *request) {
void AsyncFsWebServer::handleFsStatus(AsyncWebServerRequest *request)
{
log_debug("handleStatus");
fsInfo_t info = {1024, 1024, "Unset"};
fsInfo_t info = {1024, 1024, "ESP Filesystem"};
#ifdef ESP8266
FSInfo fs_info;
m_filesystem->info(fs_info);
Expand Down
40 changes: 18 additions & 22 deletions src/AsyncFsWebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@

#define MIN_F -3.4028235E+38
#define MAX_F 3.4028235E+38
#define MAX_APNAME_LEN 16

typedef struct {
size_t totalBytes;
size_t usedBytes;
char fsName[MAX_APNAME_LEN];
String fsName;
} fsInfo_t;


using FsInfoCallbackF = std::function<void(fsInfo_t*)>;
using CallbackF = std::function<void(void)>;

Expand Down Expand Up @@ -91,15 +91,15 @@ class AsyncFsWebServer : public AsyncWebServer
char* m_pageUser = nullptr;
char* m_pagePswd = nullptr;
String m_host = "esphost";
fs::FS* m_filesystem = nullptr;

uint16_t m_port;
uint32_t m_timeout = 10000;
uint8_t numOptions = 0;
size_t m_contentLen = 0;

char m_version[16] = {__TIME__};
bool m_filesystem_ok = false;
char m_apWebpage[MAX_APNAME_LEN] = "/setup";
size_t m_contentLen = 0;


fs::FS* m_filesystem = nullptr;
FsInfoCallbackF getFsInfo = nullptr;

public:
Expand Down Expand Up @@ -161,7 +161,7 @@ class AsyncFsWebServer : public AsyncWebServer
/*
Start WiFi connection, NO AP mode on fail
*/
IPAddress startWiFi(uint32_t timeout, CallbackF fn=nullptr ) ;
IPAddress startWiFi(uint32_t timeout, CallbackF fn=nullptr) ;

/*
* Redirect to captive portal if we got a request for another domain.
Expand Down Expand Up @@ -224,21 +224,15 @@ class AsyncFsWebServer : public AsyncWebServer
*/
const char* getVersion();

/*
* Set /setup webpage title
*/
void setSetupPageTitle(const char* title) {
setup->addOption("name-logo", title);
}


/////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////// BACKWARD COMPATIBILITY ONLY /////////////////////////////////////////
//////////////////////////// SETUP PAGE CONFIGURATION /////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
void addHTML(const char* h, const char* id, bool ow = false) {setup->addHTML(h, id, ow);}
void addCSS(const char* c, const char* id, bool ow = false){setup->addCSS(c, id, ow);}
void addJavascript(const char* s, const char* id, bool ow = false) {setup->addJavascript(s, id, ow);}
void addDropdownList(const char *l, const char** a, size_t size){setup->addDropdownList(l, a, size);}
void setSetupPageTitle(const char* title) { setup->addOption("name-logo", title); }
void addHTML(const char* html, const char* id, bool ow = false) {setup->addHTML(html, id, ow);}
void addCSS(const char* css, const char* id, bool ow = false){setup->addCSS(css, id, ow);}
void addJavascript(const char* script, const char* id, bool ow = false) {setup->addJavascript(script, id, ow);}
void addDropdownList(const char *lbl, const char** a, size_t size){setup->addDropdownList(lbl, a, size);}
void addOptionBox(const char* title) { setup->addOption("param-box", title); }
void setLogoBase64(const char* logo, const char* w = "128", const char* h = "128", bool ow = false) {
setup->setLogoBase64(logo, w, h, ow);
Expand All @@ -248,12 +242,14 @@ class AsyncFsWebServer : public AsyncWebServer
setup->addOption(lbl, val, false, min, max, st);
}
template <typename T>
void addOption(const char *lbl, T val, bool hd = false, double min = MIN_F,
void addOption(const char *lbl, T val, bool hidden = false, double min = MIN_F,
double max = MAX_F, double st = 1.0) {
setup->addOption(lbl, val, hd, min, max, st);
setup->addOption(lbl, val, hidden, min, max, st);
}
template <typename T>
bool getOptionValue(const char *lbl, T &var) { return setup->getOptionValue(lbl, var);}
template <typename T>
bool saveOptionValue(const char *lbl, T val) { return setup->saveOptionValue(lbl, val);}
/////////////////////////////////////////////////////////////////////////////////////////////////

};
Expand Down
27 changes: 27 additions & 0 deletions src/SetupConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,33 @@ class SetupConfigurator
return true;
}

template <typename T>
bool saveOptionValue(const char *label, T val) {
File file = m_filesystem->open(CONFIG_FOLDER CONFIG_FILE, "w");
DynamicJsonDocument doc(file.size() * 1.33);

if (file) {
DeserializationError error = deserializeJson(doc, file);
if (error) {
log_error("Failed to deserialize file, may be corrupted\n %s\n", error.c_str());
file.close();
return false;
}
file.close();
}
else
return false;

if (doc[label]["value"])
doc[label]["value"] = val;
else if (doc[label]["selected"])
doc[label]["selected"] = val;
else
doc[label] = val;
return true;
}


};

#endif
Loading

0 comments on commit b0d0925

Please sign in to comment.