replaced config-overloads by explicitly named functions + password & log cleanup
This commit is contained in:
parent
46fcc4b872
commit
1336d3103f
44
src/Relay.h
44
src/Relay.h
@ -38,20 +38,20 @@ public:
|
|||||||
void setup() override {
|
void setup() override {
|
||||||
Output::setup();
|
Output::setup();
|
||||||
|
|
||||||
Output::setName(configRead(path("name"), nameFallback));
|
Output::setName(loadString(path("name"), nameFallback));
|
||||||
Output::setInitial(configRead(path("initial"), INITIAL_OFF));
|
Output::setInitial(loadInitial(path("initial"), INITIAL_OFF));
|
||||||
Output::setOnMillis(configRead(path("onMillis"), 0L));
|
Output::setOnMillis(loadLong(path("onMillis"), 0L));
|
||||||
Output::setOffMillis(configRead(path("offMillis"), 0L));
|
Output::setOffMillis(loadLong(path("offMillis"), 0L));
|
||||||
|
|
||||||
topic = configRead(path("topic"), topicFallback);
|
topic = loadString(path("topic"), topicFallback);
|
||||||
|
|
||||||
gridPowerDeltaOnEnabled = configRead(path("gridPowerDeltaOnEnabled"), false);
|
gridPowerDeltaOnEnabled = loadBool(path("gridPowerDeltaOnEnabled"), false);
|
||||||
gridPowerDeltaOnThreshold = configRead(path("gridPowerDeltaOnThreshold"), 0L);
|
gridPowerDeltaOnThreshold = loadLong(path("gridPowerDeltaOnThreshold"), 0L);
|
||||||
gridPowerDeltaOnDelay = configRead(path("gridPowerDeltaOnDelay"), 0L);
|
gridPowerDeltaOnDelay = loadLong(path("gridPowerDeltaOnDelay"), 0L);
|
||||||
|
|
||||||
gridPowerDeltaOffEnabled = configRead(path("gridPowerDeltaOffEnabled"), false);
|
gridPowerDeltaOffEnabled = loadBool(path("gridPowerDeltaOffEnabled"), false);
|
||||||
gridPowerDeltaOffThreshold = configRead(path("gridPowerDeltaOffThreshold"), 0L);
|
gridPowerDeltaOffThreshold = loadLong(path("gridPowerDeltaOffThreshold"), 0L);
|
||||||
gridPowerDeltaOffDelay = configRead(path("gridPowerDeltaOffDelay"), 0L);
|
gridPowerDeltaOffDelay = loadLong(path("gridPowerDeltaOffDelay"), 0L);
|
||||||
|
|
||||||
_applyInitial();
|
_applyInitial();
|
||||||
}
|
}
|
||||||
@ -63,57 +63,57 @@ public:
|
|||||||
|
|
||||||
void setName(const String &value) override {
|
void setName(const String &value) override {
|
||||||
Output::setName(value);
|
Output::setName(value);
|
||||||
configWrite(path("name"), nameFallback, value);
|
storeString(path("name"), nameFallback, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setInitial(const Initial value) override {
|
void setInitial(const Initial value) override {
|
||||||
Output::setInitial(value);
|
Output::setInitial(value);
|
||||||
configWrite(path("initial"), INITIAL_OFF, value);
|
storeInitial(path("initial"), INITIAL_OFF, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setOnMillis(const unsigned long value) override {
|
void setOnMillis(const unsigned long value) override {
|
||||||
Output::setOnMillis(value);
|
Output::setOnMillis(value);
|
||||||
configWrite(path("onMillis"), 0L, value);
|
storeLong(path("onMillis"), 0L, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setOffMillis(const unsigned long value) override {
|
void setOffMillis(const unsigned long value) override {
|
||||||
Output::setOffMillis(value);
|
Output::setOffMillis(value);
|
||||||
configWrite(path("offMillis"), 0L, value);
|
storeLong(path("offMillis"), 0L, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setTopic(const String &value) {
|
void setTopic(const String &value) {
|
||||||
topic = value;
|
topic = value;
|
||||||
configWrite(path("topic"), topicFallback, value);
|
storeString(path("topic"), topicFallback, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setGridPowerDeltaOnEnabled(const bool value) {
|
void setGridPowerDeltaOnEnabled(const bool value) {
|
||||||
gridPowerDeltaOnEnabled = value;
|
gridPowerDeltaOnEnabled = value;
|
||||||
configWrite(path("gridPowerDeltaOnEnabled"), false, value);
|
storeBool(path("gridPowerDeltaOnEnabled"), false, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setGridPowerDeltaOnThreshold(const long value) {
|
void setGridPowerDeltaOnThreshold(const long value) {
|
||||||
gridPowerDeltaOnThreshold = value;
|
gridPowerDeltaOnThreshold = value;
|
||||||
configWrite(path("gridPowerDeltaOnThreshold"), 0L, value);
|
storeLong(path("gridPowerDeltaOnThreshold"), 0L, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setGridPowerDeltaOnDelay(const long value) {
|
void setGridPowerDeltaOnDelay(const long value) {
|
||||||
gridPowerDeltaOnDelay = value;
|
gridPowerDeltaOnDelay = value;
|
||||||
configWrite(path("gridPowerDeltaOnDelay"), 0L, value);
|
storeLong(path("gridPowerDeltaOnDelay"), 0L, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setGridPowerDeltaOffEnabled(const bool value) {
|
void setGridPowerDeltaOffEnabled(const bool value) {
|
||||||
gridPowerDeltaOffEnabled = value;
|
gridPowerDeltaOffEnabled = value;
|
||||||
configWrite(path("gridPowerDeltaOffEnabled"), false, value);
|
storeBool(path("gridPowerDeltaOffEnabled"), false, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setGridPowerDeltaOffThreshold(const long value) {
|
void setGridPowerDeltaOffThreshold(const long value) {
|
||||||
gridPowerDeltaOffThreshold = value;
|
gridPowerDeltaOffThreshold = value;
|
||||||
configWrite(path("gridPowerDeltaOffThreshold"), 0L, value);
|
storeLong(path("gridPowerDeltaOffThreshold"), 0L, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setGridPowerDeltaOffDelay(const long value) {
|
void setGridPowerDeltaOffDelay(const long value) {
|
||||||
gridPowerDeltaOffDelay = value;
|
gridPowerDeltaOffDelay = value;
|
||||||
configWrite(path("gridPowerDeltaOffDelay"), 0L, value);
|
storeLong(path("gridPowerDeltaOffDelay"), 0L, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void json(const JsonObject json) const {
|
void json(const JsonObject json) const {
|
||||||
|
|||||||
@ -82,7 +82,7 @@ void doLog(const String &path, const String &value, const bool isPassword, const
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
long configRead(const String &path, const long fallback, const bool log) {
|
long loadLong(const String &path, const long fallback, const bool log) {
|
||||||
if (auto file = configOpen(path, false)) {
|
if (auto file = configOpen(path, false)) {
|
||||||
const auto content = file.readString();
|
const auto content = file.readString();
|
||||||
file.close();
|
file.close();
|
||||||
@ -94,8 +94,8 @@ long configRead(const String &path, const long fallback, const bool log) {
|
|||||||
return fallback;
|
return fallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool configWrite(const String &path, const long fallback, const long value) {
|
bool storeLong(const String &path, const long fallback, const long value) {
|
||||||
if (configRead(path, fallback, false) == value) {
|
if (loadLong(path, fallback, false) == value) {
|
||||||
doLog(path, String(value), false, CONFIG_LOG_UNCHANGED, true);
|
doLog(path, String(value), false, CONFIG_LOG_UNCHANGED, true);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -109,47 +109,67 @@ bool configWrite(const String &path, const long fallback, const long value) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool configRead(const String &path, const bool fallback) {
|
bool loadBool(const String &path, const bool fallback, const bool log) {
|
||||||
return configRead(path, fallback ? 1L : 0L) > 0;
|
const auto value = loadString(path, fallback ? "true" : "false", log);
|
||||||
}
|
if (value == "true") {
|
||||||
|
return true;
|
||||||
bool configWrite(const String &path, const bool fallback, const bool value) {
|
|
||||||
return configWrite(path, fallback ? 1L : 0L, value ? 1L : 0L);
|
|
||||||
}
|
|
||||||
|
|
||||||
String configRead(const String &path, const char *fallback) {
|
|
||||||
return configRead(path, String(fallback));
|
|
||||||
}
|
|
||||||
|
|
||||||
String configRead(const String &path, const String &fallback, const bool log, const bool isPassword) {
|
|
||||||
if (auto file = configOpen(path, false)) {
|
|
||||||
const auto value = file.readString();
|
|
||||||
file.close();
|
|
||||||
doLog(path, value.c_str(), isPassword, CONFIG_LOG_READ, log);
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
doLog(path, fallback.c_str(), isPassword, CONFIG_LOG_FALLBACK, log);
|
if (value == "false") {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Serial.printf("[CONFIG] Not a boolean: path=%s, value=%s\n", path.c_str(), value.c_str());
|
||||||
return fallback;
|
return fallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool configWrite(const String &path, const String &fallback, const String &value, const bool isPassword) {
|
bool storeBool(const String &path, const bool fallback, const bool value) {
|
||||||
if (configRead(path, fallback, false) == value) {
|
return storeString(path, fallback ? "true" : "false", value ? "true" : "false");
|
||||||
doLog(path, value.c_str(), isPassword, CONFIG_LOG_UNCHANGED, true);
|
}
|
||||||
|
|
||||||
|
String _loadString(const String &path, const String &fallback, const bool password, const bool log) {
|
||||||
|
if (auto file = configOpen(path, false)) {
|
||||||
|
const auto value = file.readString();
|
||||||
|
file.close();
|
||||||
|
doLog(path, value.c_str(), password, CONFIG_LOG_READ, log);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
doLog(path, fallback.c_str(), password, CONFIG_LOG_FALLBACK, log);
|
||||||
|
return fallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool _storeString(const String &path, const String &fallback, const bool password, const String &value) {
|
||||||
|
if (_loadString(path, fallback, password, false) == value) {
|
||||||
|
doLog(path, value.c_str(), password, CONFIG_LOG_UNCHANGED, true);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (auto file = configOpen(path, true)) {
|
if (auto file = configOpen(path, true)) {
|
||||||
file.write(reinterpret_cast<const uint8_t *>(value.c_str()), value.length());
|
file.write(reinterpret_cast<const uint8_t *>(value.c_str()), value.length());
|
||||||
file.close();
|
file.close();
|
||||||
doLog(path, value.c_str(), isPassword, CONFIG_LOG_WRITE, true);
|
doLog(path, value.c_str(), password, CONFIG_LOG_WRITE, true);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Initial configRead(const String &path, const Initial &fallback) {
|
String loadString(const String &path, const String &fallback, const bool log) {
|
||||||
return stringToInitial(configRead(path, initialToString(fallback)));
|
return _loadString(path, fallback, false, log);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool configWrite(const String &path, const Initial &fallback, const Initial &value) {
|
bool storeString(const String &path, const String &fallback, const String &value) {
|
||||||
return configWrite(path, initialToString(fallback), initialToString(value));
|
return _storeString(path, fallback, false, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
String loadPassword(const String &path, const String &fallback, const bool log) {
|
||||||
|
return _loadString(path, fallback, true, log);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool storePassword(const String &path, const String &fallback, const String &value) {
|
||||||
|
return _storeString(path, fallback, true, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
Initial loadInitial(const String &path, const Initial &fallback, const bool log) {
|
||||||
|
return stringToInitial(loadString(path, initialToString(fallback), log));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool storeInitial(const String &path, const Initial &fallback, const Initial &value) {
|
||||||
|
return storeString(path, initialToString(fallback), initialToString(value));
|
||||||
}
|
}
|
||||||
|
|||||||
20
src/config.h
20
src/config.h
@ -7,22 +7,24 @@
|
|||||||
|
|
||||||
void configSetup();
|
void configSetup();
|
||||||
|
|
||||||
long configRead(const String &path, long fallback, bool log = true);
|
long loadLong(const String &path, long fallback, bool log = true);
|
||||||
|
|
||||||
bool configWrite(const String &path, long fallback, long value);
|
bool storeLong(const String &path, long fallback, long value);
|
||||||
|
|
||||||
bool configRead(const String &path, bool fallback);
|
bool loadBool(const String &path, bool fallback, bool log = true);
|
||||||
|
|
||||||
bool configWrite(const String &path, bool fallback, bool value);
|
bool storeBool(const String &path, bool fallback, bool value);
|
||||||
|
|
||||||
String configRead(const String &path, const char *fallback);
|
String loadString(const String &path, const String &fallback, bool log = true);
|
||||||
|
|
||||||
String configRead(const String &path, const String &fallback, bool log = true, bool isPassword = false);
|
bool storeString(const String &path, const String &fallback, const String &value);
|
||||||
|
|
||||||
bool configWrite(const String &path, const String &fallback, const String &value, bool isPassword = false);
|
String loadPassword(const String &path, const String &fallback, bool log = true);
|
||||||
|
|
||||||
Initial configRead(const String &path, const Initial &fallback);
|
bool storePassword(const String &path, const String &fallback, const String &value);
|
||||||
|
|
||||||
bool configWrite(const String &path, const Initial &fallback, const Initial &value);
|
Initial loadInitial(const String &path, const Initial &fallback, bool log = true);
|
||||||
|
|
||||||
|
bool storeInitial(const String &path, const Initial &fallback, const Initial &value);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
16
src/mqtt.cpp
16
src/mqtt.cpp
@ -75,10 +75,10 @@ void mqttLoop() {
|
|||||||
Serial.println("[MQTT] Stopped.");
|
Serial.println("[MQTT] Stopped.");
|
||||||
}
|
}
|
||||||
} else if (mqttShouldConnect) {
|
} else if (mqttShouldConnect) {
|
||||||
mqttHost = configRead(MQTT_HOST_KEY, MQTT_HOST_FALLBACK, false);
|
mqttHost = loadString(MQTT_HOST_KEY, MQTT_HOST_FALLBACK);
|
||||||
mqttPort = configRead(MQTT_PORT_KEY, MQTT_PORT_FALLBACK, true);
|
mqttPort = loadLong(MQTT_PORT_KEY, MQTT_PORT_FALLBACK);
|
||||||
mqttUser = configRead(MQTT_USER_KEY, MQTT_USER_FALLBACK);
|
mqttUser = loadString(MQTT_USER_KEY, MQTT_USER_FALLBACK);
|
||||||
const auto mqttPass = configRead(MQTT_PASSWORD_KEY, MQTT_PASSWORD_FALLBACK, true, true);
|
const auto mqttPass = loadPassword(MQTT_PASSWORD_KEY, MQTT_PASSWORD_FALLBACK);
|
||||||
if (mqttHost == "") {
|
if (mqttHost == "") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -125,19 +125,19 @@ long getMqttPort() {
|
|||||||
|
|
||||||
void mqttSetHost(const String &value) {
|
void mqttSetHost(const String &value) {
|
||||||
mqttHost = value;
|
mqttHost = value;
|
||||||
configWrite(MQTT_HOST_KEY, String(MQTT_HOST_FALLBACK), value);
|
storeString(MQTT_HOST_KEY, String(MQTT_HOST_FALLBACK), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mqttSetPort(const long &value) {
|
void mqttSetPort(const long &value) {
|
||||||
mqttPort = value;
|
mqttPort = value;
|
||||||
configWrite(MQTT_PORT_KEY,MQTT_HOST_FALLBACK, value);
|
storeLong(MQTT_PORT_KEY,MQTT_PORT_FALLBACK, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mqttSetUser(const String &value) {
|
void mqttSetUser(const String &value) {
|
||||||
mqttUser = value;
|
mqttUser = value;
|
||||||
configWrite(MQTT_USER_KEY, String(MQTT_HOST_FALLBACK), value);
|
storeString(MQTT_USER_KEY, String(MQTT_USER_FALLBACK), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mqttSetPassword(const String &value) {
|
void mqttSetPassword(const String &value) {
|
||||||
configWrite(MQTT_PASSWORD_KEY,MQTT_HOST_FALLBACK, value, true);
|
storePassword(MQTT_PASSWORD_KEY,MQTT_PASSWORD_FALLBACK, value);
|
||||||
}
|
}
|
||||||
|
|||||||
12
src/wifi.cpp
12
src/wifi.cpp
@ -26,9 +26,9 @@ void wifiConnect() {
|
|||||||
|
|
||||||
status.cycle(500, 500);
|
status.cycle(500, 500);
|
||||||
|
|
||||||
const auto hostname = configRead(WIFI_HOSTNAME_KEY, WIFI_HOSTNAME_FALLBACK);
|
const auto hostname = loadString(WIFI_HOSTNAME_KEY, WIFI_HOSTNAME_FALLBACK);
|
||||||
const auto wifiSSID = configRead(WIFI_SSID_KEY, WIFI_SSID_FALLBACK);
|
const auto wifiSSID = loadString(WIFI_SSID_KEY, WIFI_SSID_FALLBACK);
|
||||||
const auto wifiPass = configRead(WIFI_PASSWORD_KEY, WIFI_PASSWORD_FALLBACK, true, true);
|
const auto wifiPass = loadPassword(WIFI_PASSWORD_KEY, WIFI_PASSWORD_FALLBACK);
|
||||||
|
|
||||||
Serial.printf("[WiFi] Connecting: \"%s\"\n", wifiSSID.c_str());
|
Serial.printf("[WiFi] Connecting: \"%s\"\n", wifiSSID.c_str());
|
||||||
WiFi.hostname(hostname);
|
WiFi.hostname(hostname);
|
||||||
@ -90,15 +90,15 @@ void wifiSetup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void wifiSetHostname(const String &hostname) {
|
void wifiSetHostname(const String &hostname) {
|
||||||
if (configWrite(WIFI_HOSTNAME_KEY, WIFI_HOSTNAME_FALLBACK, hostname.c_str())) {
|
if (storeString(WIFI_HOSTNAME_KEY, WIFI_HOSTNAME_FALLBACK, hostname)) {
|
||||||
WiFi.setHostname(hostname.c_str());
|
WiFi.setHostname(hostname.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void wifiSetSSID(const String &ssid) {
|
void wifiSetSSID(const String &ssid) {
|
||||||
configWrite(WIFI_SSID_KEY, WIFI_SSID_FALLBACK, ssid.c_str());
|
storeString(WIFI_SSID_KEY, WIFI_SSID_FALLBACK, ssid);
|
||||||
}
|
}
|
||||||
|
|
||||||
void wifiSetPassword(const String &password) {
|
void wifiSetPassword(const String &password) {
|
||||||
configWrite(WIFI_PASSWORD_KEY, WIFI_PASSWORD_FALLBACK, password, true);
|
storePassword(WIFI_PASSWORD_KEY, WIFI_PASSWORD_FALLBACK, password);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user