commit changes to eeprom

master
Kenneth Barbour 2020-06-12 21:35:13 -04:00
parent 492ced86c8
commit 863d5eff78
3 changed files with 20 additions and 2 deletions

View File

@ -6,12 +6,14 @@ cUWeatherConfig Config;
const uint8_t CONFIG_SIG[2] = {0xAB, 0xCD};
const uint8_t CONFIG_VERSION = 1;
const size_t CONFIG_EEPROM_SECTOR_SZ = 256;
uint8_t CONFIG_EEPROM_ADDR = 0;
const uint8_t CONFIG_FLAG_HTTP = 1 << 0;
void cUWeatherConfig::begin()
{
EEPROM.begin(CONFIG_EEPROM_SECTOR_SZ);
if (!load()) {
Serial.println("Unable to find config. Loading defaults");
loadDefaults();
@ -58,8 +60,11 @@ bool cUWeatherConfig::load()
bool cUWeatherConfig::save()
{
Serial.println("Saving config.");
EEPROM.put(CONFIG_EEPROM_ADDR, _data);
return true;
bool result = EEPROM.commit();
Serial.println(result ? "OK" : "ERROR");
return result;
}
size_t cUWeatherConfig::print(Stream& out)

View File

@ -37,7 +37,7 @@ void setup()
uweather_print_version_info(Serial);
// Load configuration
Serial.print( F("Loading config"));
Serial.println( F("Loading config"));
Config.begin();

View File

@ -139,6 +139,18 @@ void handle_config(HttpRequest& req, HttpResponse& res)
return;
}
void handle_config_commit(HttpRequest& req, HttpResponse& res)
{
bool result = Config.save();
res.headers.set("Content-Type", "application/json");
if (!result) {
res.code = 500;
content.println("{error: true, message: \"Unable to commit configuration\"}");
} else {
Config.print(content);
}
}
void handle_config_set(HttpRequest& req, HttpResponse& res)
{
const char* key = req.getUrl()+8;
@ -244,6 +256,7 @@ Route routes[] = {
{ GET, "/", handle_index},
{ GET, "/setup", handle_setup},
{ GET, "/config", handle_config},
{ GET | PUT | POST, "/config/commit", handle_config_commit},
{ GET | PUT | POST | DELETE, "/config/#", handle_config_set},
{ GET, "/wifi/scan", handle_wifi_scan},
{ GET | POST | DELETE, "/wifi", handle_wifi_state},