Sensor3/lib/patrix/config.cpp

82 lines
1.6 KiB
C++

#include "config.h"
#include "log.h"
#ifdef ESP32
#include <nvs_flash.h>
#include <Preferences.h>
Preferences config;
void configSetup() {
config.begin("config", false);
const String &stored = config.getString("HOSTNAME", "");
if (!stored.equals(HOSTNAME)) {
error(R"([WARNING] Stored config hostname mismatch: "%s" != "%s" [WARNING])", stored.c_str(), HOSTNAME);
}
}
void configReset() {
info("[CONFIG RESET]");
config.end();
nvs_flash_erase();
nvs_flash_init();
config.begin("config", false);
config.putString("HOSTNAME", HOSTNAME);
configLoad();
}
String configGetString(const char *name, const char *fallback, bool allowEmpty) {
if (!config.isKey(name)) {
return fallback;
}
String value = config.getString(name);
if (!allowEmpty && value.isEmpty()) {
return fallback;
}
return value;
}
bool configPutString(const char *name, const char *value) {
return config.putString(name, value) == strlen(value);
}
double configGetDouble(const char *name, double fallback) {
if (!config.isKey(name)) {
return fallback;
}
return config.getDouble(name);
}
bool configPutDouble(const char *name, double fallback) {
return config.value(name, fallback) == 0;
}
#else
void configSetup() {
// ESP8266 FAKE
}
void configReset() {
// ESP8266 FAKE
}
String configGetString(const char *name, const char *fallback, bool allowEmpty) {
return fallback;
}
bool configPutString(const char *name, const char *value) {
return false;
}
double configGetDouble(const char *name, double fallback) {
return fallback;
}
bool configPutDouble(const char *name, double fallback) {
return false;
}
#endif