2018-12-10 22:35:12 +00:00
|
|
|
#ifndef SettingsPersistence_h
|
|
|
|
#define SettingsPersistence_h
|
|
|
|
|
|
|
|
#include <ArduinoJson.h>
|
2019-11-30 09:58:28 +00:00
|
|
|
#include <AsyncJson.h>
|
2019-12-03 23:16:06 +00:00
|
|
|
#include <AsyncJsonWebHandler.h>
|
|
|
|
#include <ESPAsyncWebServer.h>
|
|
|
|
#include <FS.h>
|
2018-12-10 22:35:12 +00:00
|
|
|
|
|
|
|
/**
|
2019-12-03 23:16:06 +00:00
|
|
|
* At the moment, not expecting settings service to have to deal with large JSON
|
|
|
|
* files this could be made configurable fairly simply, it's exposed on
|
|
|
|
* AsyncJsonWebHandler with a setter.
|
|
|
|
*/
|
2018-12-10 22:35:12 +00:00
|
|
|
#define MAX_SETTINGS_SIZE 1024
|
|
|
|
|
|
|
|
/*
|
2019-12-03 23:16:06 +00:00
|
|
|
* Mixin for classes which need to save settings to/from a file on the the file system as JSON.
|
|
|
|
*/
|
2018-12-10 22:35:12 +00:00
|
|
|
class SettingsPersistence {
|
2019-12-03 23:16:06 +00:00
|
|
|
protected:
|
2018-12-10 22:35:12 +00:00
|
|
|
// will store and retrieve config from the file system
|
|
|
|
FS* _fs;
|
|
|
|
|
|
|
|
// the file path our settings will be saved to
|
|
|
|
char const* _filePath;
|
|
|
|
|
|
|
|
bool writeToFS() {
|
|
|
|
// create and populate a new json object
|
2019-04-14 07:52:40 +00:00
|
|
|
DynamicJsonDocument jsonDocument = DynamicJsonDocument(MAX_SETTINGS_SIZE);
|
|
|
|
JsonObject root = jsonDocument.to<JsonObject>();
|
2018-12-10 22:35:12 +00:00
|
|
|
writeToJsonObject(root);
|
|
|
|
|
|
|
|
// serialize it to filesystem
|
|
|
|
File configFile = _fs->open(_filePath, "w");
|
|
|
|
|
|
|
|
// failed to open file, return false
|
|
|
|
if (!configFile) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-14 07:52:40 +00:00
|
|
|
serializeJson(jsonDocument, configFile);
|
2018-12-10 22:35:12 +00:00
|
|
|
configFile.close();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-14 21:13:26 +00:00
|
|
|
void readFromFS() {
|
2018-12-10 22:35:12 +00:00
|
|
|
File configFile = _fs->open(_filePath, "r");
|
|
|
|
|
|
|
|
// use defaults if no config found
|
|
|
|
if (configFile) {
|
|
|
|
// Protect against bad data uploaded to file system
|
|
|
|
// We never expect the config file to get very large, so cap it.
|
|
|
|
size_t size = configFile.size();
|
|
|
|
if (size <= MAX_SETTINGS_SIZE) {
|
2019-04-14 07:52:40 +00:00
|
|
|
DynamicJsonDocument jsonDocument = DynamicJsonDocument(MAX_SETTINGS_SIZE);
|
|
|
|
DeserializationError error = deserializeJson(jsonDocument, configFile);
|
2019-12-03 23:16:06 +00:00
|
|
|
if (error == DeserializationError::Ok && jsonDocument.is<JsonObject>()) {
|
2019-04-14 07:52:40 +00:00
|
|
|
JsonObject root = jsonDocument.as<JsonObject>();
|
2018-12-10 22:35:12 +00:00
|
|
|
readFromJsonObject(root);
|
|
|
|
configFile.close();
|
2019-12-03 23:16:06 +00:00
|
|
|
return;
|
2018-12-10 22:35:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
configFile.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we reach here we have not been successful in loading the config,
|
|
|
|
// hard-coded emergency defaults are now applied.
|
|
|
|
applyDefaultConfig();
|
|
|
|
}
|
|
|
|
|
2019-12-03 23:16:06 +00:00
|
|
|
// serialization routene, from local config to JsonObject
|
|
|
|
virtual void readFromJsonObject(JsonObject& root) {
|
|
|
|
}
|
|
|
|
virtual void writeToJsonObject(JsonObject& root) {
|
|
|
|
}
|
2018-12-10 22:35:12 +00:00
|
|
|
|
2019-12-03 23:16:06 +00:00
|
|
|
// We assume the readFromJsonObject supplies sensible defaults if an empty object
|
|
|
|
// is supplied, this virtual function allows that to be changed.
|
|
|
|
virtual void applyDefaultConfig() {
|
|
|
|
DynamicJsonDocument jsonDocument = DynamicJsonDocument(MAX_SETTINGS_SIZE);
|
|
|
|
JsonObject root = jsonDocument.to<JsonObject>();
|
|
|
|
readFromJsonObject(root);
|
|
|
|
}
|
2018-12-10 22:35:12 +00:00
|
|
|
|
2019-12-03 23:16:06 +00:00
|
|
|
public:
|
|
|
|
SettingsPersistence(FS* fs, char const* filePath) : _fs(fs), _filePath(filePath) {
|
|
|
|
}
|
2018-12-10 22:35:12 +00:00
|
|
|
|
2019-12-03 23:16:06 +00:00
|
|
|
virtual ~SettingsPersistence() {
|
|
|
|
}
|
2018-12-10 22:35:12 +00:00
|
|
|
};
|
|
|
|
|
2019-12-03 23:16:06 +00:00
|
|
|
#endif // end SettingsPersistence
|