Created
February 19, 2018 17:45
-
-
Save s-bauer/894bd4f8ab78039604d90a6dbbf86e8c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ConfigManager.h" | |
const char Settings::current_version[4] = "0.2"; | |
ConfigManager::ConfigManager() { | |
SPIFFS.begin(); | |
} | |
void ConfigManager::open_file(const char *permission) { | |
Log.notice(R"([ConfigManager] Trying to open file "config.json" with permission "%s")", permission); | |
configFile = SPIFFS.open("/config.json", permission); | |
if (!configFile) | |
Log.notice("[ConfigManager] Unable to open file \"config.json\""); | |
else | |
Log.notice("[ConfigManager] Opened file \"config.json\""); | |
} | |
void ConfigManager::recreate_file() { | |
Log.notice("[ConfigManager] Overwriting file \"config.json\""); | |
configFile.close(); | |
open_file("w"); | |
} | |
void ConfigManager::read_json() { | |
size_t size = configFile.size(); | |
Log.verbose("[ConfigManager] Config file size: %d", size); | |
std::unique_ptr<char[]> buf(new char[size]); | |
configFile.readBytes(buf.get(), size); | |
Log.verbose("[ConfigManager] File content: %s", buf.get()); | |
Log.verbose("[ConfigManager] Parsing json"); | |
json = &jsonBuffer.parseObject(buf.get()); | |
Log.notice("[ConfigManager] Json is:"); | |
json->printTo(Serial); | |
Serial.println(); | |
auto version = json->get<const char*>("version"); | |
Log.notice("[ConfigManager] Version is: %s", version); | |
} | |
void ConfigManager::check_success() { | |
Log.notice("[ConfigManager] Json is:"); | |
Serial.println(); | |
bool should_recreate = true; | |
if (json->success()) { | |
Log.notice("[ConfigManager] Parsed json successfully"); | |
auto version = json->get<const char*>("version"); | |
if (version) { | |
if (strcmp(version, Settings::current_version) == 0) { | |
Log.notice("[ConfigManager] Config version is up2date"); | |
should_recreate = false; | |
} else { | |
Log.warning("[ConfigManager] Config version outdated"); | |
} | |
} else { | |
Log.warning("[ConfigManager] Invalid config file"); | |
} | |
} else { | |
Log.warning("[ConfigManager] Config file is not valid json"); | |
} | |
if (should_recreate) { | |
Log.notice("[ConfigManager] Recreating config file"); | |
recreate_file(); | |
create_json(); | |
} | |
Log.notice("[ConfigManager] Json is:"); | |
json->printTo(Serial); | |
Serial.println(); | |
Log.notice("[ConfigManager] Sucessfully read json"); | |
} | |
void ConfigManager::create_json() { | |
Log.notice("[ConfigManager] Creating new config"); | |
Log.notice("[ConfigManager] Creating json object"); | |
json = &jsonBuffer.createObject(); | |
(*json)["version"] = "0.2"; | |
(*json)["led_count"] = "64"; | |
Log.notice("[ConfigManager] Writing to file"); | |
json->printTo(configFile); | |
Log.notice("[ConfigManager] Flushing file"); | |
configFile.flush(); | |
} | |
void ConfigManager::read_from_eeprom() { | |
open_file("r"); | |
read_json(); | |
check_success(); | |
populate_settings(); | |
clean_up(); | |
} | |
void ConfigManager::populate_settings() { | |
Log.notice("[ConfigManager] Creating settings"); | |
settings = new Settings(); | |
Log.notice("[ConfigManager] Populating settings"); | |
Log.notice(R"([ConfigManager] Populating "version"="%s")", json->get<const char *>("version")); | |
strcpy(settings->version, json->get<const char *>("version")); | |
Log.notice(R"([ConfigManager] Populating "led_count"="%d")", json->get<int>("led_count")); | |
settings->led_count = json->get<int>("led_count"); | |
Log.notice("[ConfigManager] Sucessfully parsed settings file"); | |
} | |
void ConfigManager::clean_up() { configFile.close(); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment