2020-05-14 22:23:45 +00:00
|
|
|
#ifndef FSPersistence_h
|
|
|
|
#define FSPersistence_h
|
|
|
|
|
|
|
|
#include <StatefulService.h>
|
|
|
|
#include <FS.h>
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class FSPersistence {
|
|
|
|
public:
|
2020-05-29 19:18:43 +00:00
|
|
|
FSPersistence(JsonStateReader<T> stateReader,
|
|
|
|
JsonStateUpdater<T> stateUpdater,
|
2020-05-14 22:23:45 +00:00
|
|
|
StatefulService<T>* statefulService,
|
|
|
|
FS* fs,
|
2020-05-22 18:26:12 +00:00
|
|
|
char const* filePath,
|
|
|
|
size_t bufferSize = DEFAULT_BUFFER_SIZE) :
|
2020-05-29 19:18:43 +00:00
|
|
|
_stateReader(stateReader),
|
|
|
|
_stateUpdater(stateUpdater),
|
2020-05-14 22:23:45 +00:00
|
|
|
_statefulService(statefulService),
|
|
|
|
_fs(fs),
|
2020-05-21 22:41:29 +00:00
|
|
|
_filePath(filePath),
|
2020-05-22 18:26:12 +00:00
|
|
|
_bufferSize(bufferSize),
|
2020-05-21 22:41:29 +00:00
|
|
|
_updateHandlerId(0) {
|
2020-05-14 22:23:45 +00:00
|
|
|
enableUpdateHandler();
|
|
|
|
}
|
|
|
|
|
|
|
|
void readFromFS() {
|
|
|
|
File settingsFile = _fs->open(_filePath, "r");
|
|
|
|
|
|
|
|
if (settingsFile) {
|
2020-05-22 18:26:12 +00:00
|
|
|
DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize);
|
|
|
|
DeserializationError error = deserializeJson(jsonDocument, settingsFile);
|
|
|
|
if (error == DeserializationError::Ok && jsonDocument.is<JsonObject>()) {
|
|
|
|
JsonObject jsonObject = jsonDocument.as<JsonObject>();
|
2020-05-29 19:18:43 +00:00
|
|
|
_statefulService->updateWithoutPropagation(jsonObject, _stateUpdater);
|
2020-05-22 18:26:12 +00:00
|
|
|
settingsFile.close();
|
|
|
|
return;
|
2020-05-14 22:23:45 +00:00
|
|
|
}
|
|
|
|
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
|
2020-05-22 18:26:12 +00:00
|
|
|
DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize);
|
2020-05-14 22:23:45 +00:00
|
|
|
JsonObject jsonObject = jsonDocument.to<JsonObject>();
|
2020-05-29 19:18:43 +00:00
|
|
|
_statefulService->read(jsonObject, _stateReader);
|
2020-05-14 22:23:45 +00:00
|
|
|
|
|
|
|
// 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) {
|
2020-05-21 07:42:21 +00:00
|
|
|
_updateHandlerId = _statefulService->addUpdateHandler([&](const String& originId) { writeToFS(); });
|
2020-05-14 22:23:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-05-29 19:18:43 +00:00
|
|
|
JsonStateReader<T> _stateReader;
|
|
|
|
JsonStateUpdater<T> _stateUpdater;
|
2020-05-14 22:23:45 +00:00
|
|
|
StatefulService<T>* _statefulService;
|
2020-05-29 19:18:43 +00:00
|
|
|
FS* _fs;
|
2020-05-14 22:23:45 +00:00
|
|
|
char const* _filePath;
|
2020-05-22 18:26:12 +00:00
|
|
|
size_t _bufferSize;
|
2020-05-21 22:41:29 +00:00
|
|
|
update_handler_id_t _updateHandlerId;
|
2020-05-14 22:23:45 +00:00
|
|
|
|
|
|
|
protected:
|
2020-05-29 19:18:43 +00:00
|
|
|
// We assume the updater supplies sensible defaults if an empty object
|
2020-05-14 22:23:45 +00:00
|
|
|
// is supplied, this virtual function allows that to be changed.
|
|
|
|
virtual void applyDefaults() {
|
2020-05-22 18:26:12 +00:00
|
|
|
DynamicJsonDocument jsonDocument = DynamicJsonDocument(_bufferSize);
|
2020-05-21 07:42:21 +00:00
|
|
|
JsonObject jsonObject = jsonDocument.as<JsonObject>();
|
2020-05-29 19:18:43 +00:00
|
|
|
_statefulService->updateWithoutPropagation(jsonObject, _stateUpdater);
|
2020-05-14 22:23:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // end FSPersistence
|