forked from rjwats/esp8266-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFactoryResetService.cpp
39 lines (35 loc) · 1.2 KB
/
FactoryResetService.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <FactoryResetService.h>
using namespace std::placeholders;
FactoryResetService::FactoryResetService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : fs(fs) {
server->on(FACTORY_RESET_SERVICE_PATH,
HTTP_POST,
securityManager->wrapRequest(std::bind(&FactoryResetService::handleRequest, this, _1),
AuthenticationPredicates::IS_ADMIN));
}
void FactoryResetService::handleRequest(AsyncWebServerRequest* request) {
request->onDisconnect(std::bind(&FactoryResetService::factoryReset, this));
request->send(200);
}
/**
* Delete function assumes that all files are stored flat, within the config directory.
*/
void FactoryResetService::factoryReset() {
#ifdef ESP32
File root = fs->open(FS_CONFIG_DIRECTORY);
File file;
while (file = root.openNextFile()) {
String path = file.path();
file.close();
fs->remove(path);
}
#elif defined(ESP8266)
Dir configDirectory = fs->openDir(FS_CONFIG_DIRECTORY);
while (configDirectory.next()) {
String path = FS_CONFIG_DIRECTORY;
path.concat("/");
path.concat(configDirectory.fileName());
fs->remove(path);
}
#endif
RestartService::restartNow();
}