mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 00:09:51 +03:00
formatting
This commit is contained in:
@@ -6,93 +6,93 @@
|
|||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
class FSPersistence {
|
class FSPersistence {
|
||||||
public:
|
public:
|
||||||
FSPersistence(JsonStateReader<T> stateReader,
|
FSPersistence(JsonStateReader<T> stateReader,
|
||||||
JsonStateUpdater<T> stateUpdater,
|
JsonStateUpdater<T> stateUpdater,
|
||||||
StatefulService<T> * statefulService,
|
StatefulService<T>* statefulService,
|
||||||
FS * fs,
|
FS* fs,
|
||||||
char const * filePath,
|
char const* filePath,
|
||||||
size_t bufferSize = DEFAULT_BUFFER_SIZE)
|
size_t bufferSize = DEFAULT_BUFFER_SIZE) :
|
||||||
: _stateReader(stateReader)
|
_stateReader(stateReader),
|
||||||
, _stateUpdater(stateUpdater)
|
_stateUpdater(stateUpdater),
|
||||||
, _statefulService(statefulService)
|
_statefulService(statefulService),
|
||||||
, _fs(fs)
|
_fs(fs),
|
||||||
, _filePath(filePath)
|
_filePath(filePath),
|
||||||
, _bufferSize(bufferSize)
|
_bufferSize(bufferSize),
|
||||||
, _updateHandlerId(0) {
|
_updateHandlerId(0) {
|
||||||
enableUpdateHandler();
|
enableUpdateHandler();
|
||||||
}
|
}
|
||||||
|
|
||||||
void readFromFS() {
|
void readFromFS() {
|
||||||
File settingsFile = _fs->open(_filePath, "r");
|
File settingsFile = _fs->open(_filePath, "r");
|
||||||
|
|
||||||
if (settingsFile) {
|
if (settingsFile) {
|
||||||
DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize);
|
DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize);
|
||||||
DeserializationError error = deserializeJson(jsonDocument, settingsFile);
|
DeserializationError error = deserializeJson(jsonDocument, settingsFile);
|
||||||
if (error == DeserializationError::Ok && jsonDocument.is<JsonObject>()) {
|
if (error == DeserializationError::Ok && jsonDocument.is<JsonObject>()) {
|
||||||
JsonObject jsonObject = jsonDocument.as<JsonObject>();
|
JsonObject jsonObject = jsonDocument.as<JsonObject>();
|
||||||
_statefulService->updateWithoutPropagation(jsonObject, _stateUpdater);
|
|
||||||
settingsFile.close();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
settingsFile.close();
|
|
||||||
}
|
|
||||||
// If we reach here we have not been successful in loading the config,
|
|
||||||
// hard-coded emergency defaults are now applied.
|
|
||||||
|
|
||||||
applyDefaults();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool writeToFS() {
|
|
||||||
// create and populate a new json object
|
|
||||||
DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize);
|
|
||||||
JsonObject jsonObject = jsonDocument.to<JsonObject>();
|
|
||||||
_statefulService->read(jsonObject, _stateReader);
|
|
||||||
|
|
||||||
// serialize it to filesystem
|
|
||||||
File settingsFile = _fs->open(_filePath, "w");
|
|
||||||
|
|
||||||
// failed to open file, return false
|
|
||||||
if (!settingsFile) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// serialize the data to the file
|
|
||||||
serializeJson(jsonDocument, settingsFile);
|
|
||||||
settingsFile.close();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void disableUpdateHandler() {
|
|
||||||
if (_updateHandlerId) {
|
|
||||||
_statefulService->removeUpdateHandler(_updateHandlerId);
|
|
||||||
_updateHandlerId = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void enableUpdateHandler() {
|
|
||||||
if (!_updateHandlerId) {
|
|
||||||
_updateHandlerId = _statefulService->addUpdateHandler([&](const String & originId) { writeToFS(); });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
JsonStateReader<T> _stateReader;
|
|
||||||
JsonStateUpdater<T> _stateUpdater;
|
|
||||||
StatefulService<T> * _statefulService;
|
|
||||||
FS * _fs;
|
|
||||||
char const * _filePath;
|
|
||||||
size_t _bufferSize;
|
|
||||||
update_handler_id_t _updateHandlerId;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
// We assume the updater supplies sensible defaults if an empty object
|
|
||||||
// is supplied, this virtual function allows that to be changed.
|
|
||||||
virtual void applyDefaults() {
|
|
||||||
DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize);
|
|
||||||
JsonObject jsonObject = jsonDocument.as<JsonObject>();
|
|
||||||
_statefulService->updateWithoutPropagation(jsonObject, _stateUpdater);
|
_statefulService->updateWithoutPropagation(jsonObject, _stateUpdater);
|
||||||
|
settingsFile.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
settingsFile.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If we reach here we have not been successful in loading the config,
|
||||||
|
// hard-coded emergency defaults are now applied.
|
||||||
|
applyDefaults();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool writeToFS() {
|
||||||
|
// create and populate a new json object
|
||||||
|
DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize);
|
||||||
|
JsonObject jsonObject = jsonDocument.to<JsonObject>();
|
||||||
|
_statefulService->read(jsonObject, _stateReader);
|
||||||
|
|
||||||
|
// serialize it to filesystem
|
||||||
|
File settingsFile = _fs->open(_filePath, "w");
|
||||||
|
|
||||||
|
// failed to open file, return false
|
||||||
|
if (!settingsFile) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// serialize the data to the file
|
||||||
|
serializeJson(jsonDocument, settingsFile);
|
||||||
|
settingsFile.close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void disableUpdateHandler() {
|
||||||
|
if (_updateHandlerId) {
|
||||||
|
_statefulService->removeUpdateHandler(_updateHandlerId);
|
||||||
|
_updateHandlerId = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void enableUpdateHandler() {
|
||||||
|
if (!_updateHandlerId) {
|
||||||
|
_updateHandlerId = _statefulService->addUpdateHandler([&](const String& originId) { writeToFS(); });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
JsonStateReader<T> _stateReader;
|
||||||
|
JsonStateUpdater<T> _stateUpdater;
|
||||||
|
StatefulService<T>* _statefulService;
|
||||||
|
FS* _fs;
|
||||||
|
char const* _filePath;
|
||||||
|
size_t _bufferSize;
|
||||||
|
update_handler_id_t _updateHandlerId;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
// We assume the updater supplies sensible defaults if an empty object
|
||||||
|
// is supplied, this virtual function allows that to be changed.
|
||||||
|
virtual void applyDefaults() {
|
||||||
|
DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize);
|
||||||
|
JsonObject jsonObject = jsonDocument.as<JsonObject>();
|
||||||
|
_statefulService->updateWithoutPropagation(jsonObject, _stateUpdater);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // end FSPersistence
|
#endif // end FSPersistence
|
||||||
|
|||||||
@@ -2,16 +2,16 @@
|
|||||||
|
|
||||||
using namespace std::placeholders;
|
using namespace std::placeholders;
|
||||||
|
|
||||||
FactoryResetService::FactoryResetService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager)
|
FactoryResetService::FactoryResetService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : fs(fs) {
|
||||||
: fs(fs) {
|
server->on(FACTORY_RESET_SERVICE_PATH,
|
||||||
server->on(FACTORY_RESET_SERVICE_PATH,
|
HTTP_POST,
|
||||||
HTTP_POST,
|
securityManager->wrapRequest(std::bind(&FactoryResetService::handleRequest, this, _1),
|
||||||
securityManager->wrapRequest(std::bind(&FactoryResetService::handleRequest, this, _1), AuthenticationPredicates::IS_ADMIN));
|
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,16 +19,16 @@ void FactoryResetService::handleRequest(AsyncWebServerRequest * request) {
|
|||||||
*/
|
*/
|
||||||
void FactoryResetService::factoryReset() {
|
void FactoryResetService::factoryReset() {
|
||||||
#ifdef ESP32
|
#ifdef ESP32
|
||||||
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());
|
fs->remove(file.name());
|
||||||
}
|
}
|
||||||
#elif defined(ESP8266)
|
#elif defined(ESP8266)
|
||||||
Dir configDirectory = fs->openDir(FS_CONFIG_DIRECTORY);
|
Dir configDirectory = fs->openDir(FS_CONFIG_DIRECTORY);
|
||||||
while (configDirectory.next()) {
|
while (configDirectory.next()) {
|
||||||
fs->remove(configDirectory.fileName());
|
fs->remove(configDirectory.fileName());
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
RestartService::restartNow();
|
RestartService::restartNow();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user