fix factory reset using LITTLEFS

This commit is contained in:
proddy
2021-03-04 21:40:03 +01:00
parent 63783449e3
commit 5b9e628eaa
4 changed files with 40 additions and 46 deletions

View File

@@ -2,14 +2,12 @@
using namespace std::placeholders; using namespace std::placeholders;
FactoryResetService::FactoryResetService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : fs(fs) { FactoryResetService::FactoryResetService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager)
server->on(FACTORY_RESET_SERVICE_PATH, : fs(fs) {
HTTP_POST, server->on(FACTORY_RESET_SERVICE_PATH, HTTP_POST, securityManager->wrapRequest(std::bind(&FactoryResetService::handleRequest, this, _1), AuthenticationPredicates::IS_ADMIN));
securityManager->wrapRequest(std::bind(&FactoryResetService::handleRequest, this, _1),
AuthenticationPredicates::IS_ADMIN));
} }
void FactoryResetService::handleRequest(AsyncWebServerRequest* request) { void FactoryResetService::handleRequest(AsyncWebServerRequest * request) {
request->onDisconnect(std::bind(&FactoryResetService::factoryReset, this)); request->onDisconnect(std::bind(&FactoryResetService::factoryReset, this));
request->send(200); request->send(200);
} }
@@ -19,10 +17,16 @@ void FactoryResetService::handleRequest(AsyncWebServerRequest* request) {
*/ */
void FactoryResetService::factoryReset() { void FactoryResetService::factoryReset() {
#ifdef ESP32 #ifdef ESP32
/*
* Based on LITTLEFS. Modified by proddy
* Could be replaced with fs.rmdir(FS_CONFIG_DIRECTORY) in IDF 4.2
*/
File root = fs->open(FS_CONFIG_DIRECTORY); File root = fs->open(FS_CONFIG_DIRECTORY);
File file; File file;
while (file = root.openNextFile()) { while (file = root.openNextFile()) {
fs->remove(file.name()); char * pathStr = strdup(file.name());
file.close();
fs->remove(pathStr);
} }
#elif defined(ESP8266) #elif defined(ESP8266)
Dir configDirectory = fs->openDir(FS_CONFIG_DIRECTORY); Dir configDirectory = fs->openDir(FS_CONFIG_DIRECTORY);

View File

@@ -236,24 +236,15 @@ void System::button_OnLongPress(PButton & b) {
// button indefinite press // button indefinite press
void System::button_OnVLongPress(PButton & b) { void System::button_OnVLongPress(PButton & b) {
LOG_DEBUG(F("Button pressed - very long press")); LOG_DEBUG(F("Button pressed - very long press"));
#ifndef EMSESP_STANDALONE
LOG_WARNING(F("Performing factory reset...")); LOG_WARNING(F("Performing factory reset..."));
EMSESP::console_.loop(); EMSESP::console_.loop();
#ifndef EMSESP_STANDALONE #ifdef EMSESP_TEST
// remove all files under config Test::listDir(LITTLEFS, FS_CONFIG_DIRECTORY, 3);
File root = LITTLEFS.open(FS_CONFIG_DIRECTORY); #endif
File file;
Serial.printf("Removing files: ", file.name());
while (file = root.openNextFile()) {
Serial.printf("%s ", file.name());
LITTLEFS.remove(file.name());
}
Serial.println();
// restart EMSESP::esp8266React.factoryReset();
WiFi.disconnect(true);
delay(500);
ESP.restart();
#endif #endif
} }

View File

@@ -978,6 +978,7 @@ void Test::add_device(uint8_t device_id, uint8_t product_id) {
#ifndef EMSESP_STANDALONE #ifndef EMSESP_STANDALONE
void Test::listDir(fs::FS & fs, const char * dirname, uint8_t levels) { void Test::listDir(fs::FS & fs, const char * dirname, uint8_t levels) {
Serial.println();
Serial.printf("Listing directory: %s\r\n", dirname); Serial.printf("Listing directory: %s\r\n", dirname);
File root = fs.open(dirname); File root = fs.open(dirname);

View File

@@ -44,8 +44,6 @@ class Test {
static void uart_telegram_withCRC(const char * rx_data); static void uart_telegram_withCRC(const char * rx_data);
static void add_device(uint8_t device_id, uint8_t product_id); static void add_device(uint8_t device_id, uint8_t product_id);
static void debug(uuid::console::Shell & shell, const std::string & command); static void debug(uuid::console::Shell & shell, const std::string & command);
private:
#ifndef EMSESP_STANDALONE #ifndef EMSESP_STANDALONE
static void listDir(fs::FS & fs, const char * dirname, uint8_t levels); static void listDir(fs::FS & fs, const char * dirname, uint8_t levels);
#endif #endif