2021-03-12 22:21:45 +00:00
|
|
|
#include "configuration.h"
|
2021-01-01 22:23:27 +00:00
|
|
|
#include <SPIFFS.h>
|
|
|
|
#include <logger.h>
|
|
|
|
|
2022-03-19 23:37:29 +00:00
|
|
|
#define MODULE_NAME "ConfigurationManagement"
|
|
|
|
|
|
|
|
ConfigurationManagement::ConfigurationManagement(logging::Logger &logger, String FilePath) : mFilePath(FilePath) {
|
2021-03-12 22:21:45 +00:00
|
|
|
if (!SPIFFS.begin(true)) {
|
2022-03-19 23:37:29 +00:00
|
|
|
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, MODULE_NAME, "Mounting SPIFFS was not possible. Trying to format SPIFFS...");
|
2021-03-12 22:21:45 +00:00
|
|
|
SPIFFS.format();
|
|
|
|
if (!SPIFFS.begin()) {
|
2022-11-16 22:10:06 +00:00
|
|
|
logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, MODULE_NAME, "Formatting SPIFFS was not okay!");
|
2021-03-12 22:21:45 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-01 22:23:27 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 22:21:45 +00:00
|
|
|
ConfigurationManagement::~ConfigurationManagement() {
|
2021-01-01 22:23:27 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 23:37:29 +00:00
|
|
|
void ConfigurationManagement::readConfiguration(logging::Logger &logger, Configuration &conf) {
|
2021-03-12 22:21:45 +00:00
|
|
|
File file = SPIFFS.open(mFilePath);
|
|
|
|
if (!file) {
|
2022-03-19 23:37:29 +00:00
|
|
|
logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, MODULE_NAME, "Failed to open file for reading, using default configuration.");
|
2021-05-18 22:44:37 +00:00
|
|
|
return;
|
2021-03-12 22:21:45 +00:00
|
|
|
}
|
|
|
|
DynamicJsonDocument data(2048);
|
|
|
|
DeserializationError error = deserializeJson(data, file);
|
|
|
|
if (error) {
|
2022-03-19 23:37:29 +00:00
|
|
|
logger.log(logging::LoggerLevel::LOGGER_LEVEL_WARN, MODULE_NAME, "Failed to read file, using default configuration.");
|
2021-03-12 22:21:45 +00:00
|
|
|
}
|
|
|
|
// serializeJson(data, Serial);
|
|
|
|
// Serial.println();
|
|
|
|
file.close();
|
|
|
|
|
2021-05-18 22:44:37 +00:00
|
|
|
readProjectConfiguration(data, conf);
|
2021-03-12 22:21:45 +00:00
|
|
|
|
|
|
|
// update config in memory to get the new fields:
|
2022-05-20 21:09:26 +00:00
|
|
|
// writeConfiguration(logger, conf);
|
2021-01-01 22:23:27 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 23:37:29 +00:00
|
|
|
void ConfigurationManagement::writeConfiguration(logging::Logger &logger, Configuration &conf) {
|
2021-03-12 22:21:45 +00:00
|
|
|
File file = SPIFFS.open(mFilePath, "w");
|
|
|
|
if (!file) {
|
2022-03-19 23:37:29 +00:00
|
|
|
logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, MODULE_NAME, "Failed to open file for writing...");
|
2021-03-12 22:21:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
DynamicJsonDocument data(2048);
|
2021-01-01 22:23:27 +00:00
|
|
|
|
2021-03-12 22:21:45 +00:00
|
|
|
writeProjectConfiguration(conf, data);
|
2021-01-01 22:23:27 +00:00
|
|
|
|
2021-03-12 22:21:45 +00:00
|
|
|
serializeJson(data, file);
|
|
|
|
// serializeJson(data, Serial);
|
|
|
|
// Serial.println();
|
|
|
|
file.close();
|
2021-01-01 22:23:27 +00:00
|
|
|
}
|