From 1425a8dded13e23a16ea97cd38bbbb6ce967171d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Fri, 23 Dec 2016 13:01:41 +0100 Subject: [PATCH] Refactor settings getters and setters --- code/src/emon.ino | 8 ++++---- code/src/fauxmo.ino | 2 +- code/src/main.ino | 4 ++-- code/src/mqtt.ino | 2 +- code/src/nofuss.ino | 2 +- code/src/pow.ino | 6 +++--- code/src/relay.ino | 12 ++++++++---- code/src/rf.ino | 4 ++-- code/src/settings.ino | 14 +++++--------- code/src/web.ino | 16 ++++++++-------- 10 files changed, 35 insertions(+), 35 deletions(-) diff --git a/code/src/emon.ino b/code/src/emon.ino index c01ab157..7a85ba0a 100644 --- a/code/src/emon.ino +++ b/code/src/emon.ino @@ -39,10 +39,10 @@ void powerMonitorSetup() { // backwards compatibility String tmp; - tmp = getSetting("pwMainsVoltage", String() + EMON_MAINS_VOLTAGE); + tmp = getSetting("pwMainsVoltage", EMON_MAINS_VOLTAGE); setSetting("emonMains", tmp); delSetting("pwMainsVoltage"); - tmp = getSetting("pwCurrentRatio", String() + EMON_CURRENT_RATIO); + tmp = getSetting("pwCurrentRatio", EMON_CURRENT_RATIO); setSetting("emonRatio", tmp); delSetting("pwCurrentRatio"); @@ -50,7 +50,7 @@ void powerMonitorSetup() { currentCallback, EMON_ADC_BITS, EMON_REFERENCE_VOLTAGE, - getSetting("emonRatio", String(EMON_CURRENT_RATIO)).toFloat() + getSetting("emonRatio", EMON_CURRENT_RATIO).toFloat() ); emon.setPrecision(EMON_CURRENT_PRECISION); } @@ -91,7 +91,7 @@ void powerMonitorLoop() { sum += current; ++measurements; - float mainsVoltage = getSetting("emonMains", String(EMON_MAINS_VOLTAGE)).toFloat(); + float mainsVoltage = getSetting("emonMains", EMON_MAINS_VOLTAGE).toFloat(); //DEBUG_MSG("[ENERGY] Power now: %dW\n", int(current * mainsVoltage)); diff --git a/code/src/fauxmo.ino b/code/src/fauxmo.ino index 3d89669a..1d131892 100644 --- a/code/src/fauxmo.ino +++ b/code/src/fauxmo.ino @@ -18,7 +18,7 @@ fauxmoESP fauxmo; // ----------------------------------------------------------------------------- void fauxmoConfigure() { - fauxmo.enable(getSetting("fauxmoEnabled", String(FAUXMO_ENABLED)).toInt() == 1); + fauxmo.enable(getSetting("fauxmoEnabled", FAUXMO_ENABLED).toInt() == 1); } void fauxmoSetup() { diff --git a/code/src/main.ino b/code/src/main.ino index 695f1e01..c376f2bb 100644 --- a/code/src/main.ino +++ b/code/src/main.ino @@ -29,8 +29,8 @@ along with this program. If not, see . #include #include #include "FS.h" -String getSetting(const String& key, String defaultValue = ""); -bool relayStatus(unsigned char id, bool status, bool report = true); +template bool setSetting(const String& key, T value); +template String getSetting(const String& key, T defaultValue); // ----------------------------------------------------------------------------- // METHODS diff --git a/code/src/mqtt.ino b/code/src/mqtt.ino index 3e41e48d..d69b5d82 100644 --- a/code/src/mqtt.ino +++ b/code/src/mqtt.ino @@ -101,7 +101,7 @@ void mqttConnect() { if (!mqtt.connected()) { String host = getSetting("mqttServer", MQTT_SERVER); - String port = getSetting("mqttPort", String(MQTT_PORT)); + String port = getSetting("mqttPort", MQTT_PORT); String user = getSetting("mqttUser"); String pass = getSetting("mqttPassword"); diff --git a/code/src/nofuss.ino b/code/src/nofuss.ino index bcb94415..35b1e20e 100644 --- a/code/src/nofuss.ino +++ b/code/src/nofuss.ino @@ -74,7 +74,7 @@ void nofussLoop() { static unsigned long last_check = 0; if (!wifiConnected()) return; -unsigned long interval = getSetting("nofussInterval", String(NOFUSS_INTERVAL)).toInt(); +unsigned long interval = getSetting("nofussInterval", NOFUSS_INTERVAL).toInt(); if ((last_check > 0) && ((millis() - last_check) < interval)) return; last_check = millis(); NoFUSSClient.handle(); diff --git a/code/src/pow.ino b/code/src/pow.ino index 431e3e3d..428838e5 100644 --- a/code/src/pow.ino +++ b/code/src/pow.ino @@ -48,11 +48,11 @@ void powSaveCalibration() { void powRetrieveCalibration() { double value; - value = getSetting("powPowerMult", "0").toFloat(); + value = getSetting("powPowerMult", 0).toFloat(); if (value > 0) hlw8012.setPowerMultiplier((int) value); - value = getSetting("powCurrentMult", "0").toFloat(); + value = getSetting("powCurrentMult", 0).toFloat(); if (value > 0) hlw8012.setCurrentMultiplier((int) value); - value = getSetting("powVoltageMult", "0").toFloat(); + value = getSetting("powVoltageMult", 0).toFloat(); if (value > 0) hlw8012.setVoltageMultiplier((int) value); } diff --git a/code/src/relay.ino b/code/src/relay.ino index 77b7cb92..a61ca50b 100644 --- a/code/src/relay.ino +++ b/code/src/relay.ino @@ -91,7 +91,11 @@ bool relayStatus(unsigned char id, bool status, bool report) { if (report) relayMQTT(id); if (!recursive) relayWS(); return changed; - + +} + +bool relayStatus(unsigned char id, bool status) { + return relayStatus(id, status, true); } void relaySync(unsigned char id) { @@ -100,7 +104,7 @@ void relaySync(unsigned char id) { recursive = true; - byte relaySync = getSetting("relaySync", String(RELAY_SYNC)).toInt(); + byte relaySync = getSetting("relaySync", RELAY_SYNC).toInt(); bool status = relayStatus(id); // If RELAY_SYNC_SAME all relays should have the same state @@ -178,7 +182,7 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo // If relayMode is not SAME avoid responding to a retained message if (isFirstMessage) { isFirstMessage = false; - byte relayMode = getSetting("relayMode", String(RELAY_MODE)).toInt(); + byte relayMode = getSetting("relayMode", RELAY_MODE).toInt(); if (relayMode != RELAY_MODE_SAME) return; } @@ -227,7 +231,7 @@ void relaySetup() { #endif EEPROM.begin(4096); - byte relayMode = getSetting("relayMode", String(RELAY_MODE)).toInt(); + byte relayMode = getSetting("relayMode", RELAY_MODE).toInt(); for (unsigned int i=0; i < _relays.size(); i++) { pinMode(_relays[i], OUTPUT); diff --git a/code/src/rf.ino b/code/src/rf.ino index 8a07f9d7..817c0a8e 100644 --- a/code/src/rf.ino +++ b/code/src/rf.ino @@ -33,7 +33,7 @@ void rfBuildCodes() { unsigned long code = 0; // channel - unsigned int channel = getSetting("rfChannel", String(RF_CHANNEL)).toInt(); + unsigned int channel = getSetting("rfChannel", RF_CHANNEL).toInt(); for (byte i = 0; i < 5; i++) { code *= 3; if (channel & 1) code += 1; @@ -41,7 +41,7 @@ void rfBuildCodes() { } // device - unsigned int device = getSetting("rfDevice", String(RF_DEVICE)).toInt(); + unsigned int device = getSetting("rfDevice", RF_DEVICE).toInt(); for (byte i = 0; i < 5; i++) { code *= 3; if (device != i) code += 2; diff --git a/code/src/settings.ino b/code/src/settings.ino index 6d3dc28e..b5093d72 100644 --- a/code/src/settings.ino +++ b/code/src/settings.ino @@ -97,24 +97,20 @@ void settingsLoop() { embedis.process(); } -String getSetting(const String& key, String defaultValue) { +template String getSetting(const String& key, T defaultValue) { String value; - if (!Embedis::get(key, value)) value = defaultValue; + if (!Embedis::get(key, value)) value = String(defaultValue); return value; } -bool setSetting(const String& key, String& value) { - return Embedis::set(key, value); +String getSetting(const String& key) { + return getSetting(key, ""); } -bool setSetting(const String& key, char * value) { +template bool setSetting(const String& key, T value) { return Embedis::set(key, String(value)); } -bool setSetting(const String& key, bool value) { - return Embedis::set(key, value ? String("0") : String("1")); -} - bool delSetting(const String& key) { return Embedis::del(key); } diff --git a/code/src/web.ino b/code/src/web.ino index 95e69336..6419bf2a 100644 --- a/code/src/web.ino +++ b/code/src/web.ino @@ -224,7 +224,7 @@ void _wsStart(uint32_t client_id) { root["mqttStatus"] = mqttConnected(); root["mqttServer"] = getSetting("mqttServer", MQTT_SERVER); - root["mqttPort"] = getSetting("mqttPort", String(MQTT_PORT)); + root["mqttPort"] = getSetting("mqttPort", MQTT_PORT); root["mqttUser"] = getSetting("mqttUser"); root["mqttPassword"] = getSetting("mqttPassword"); root["mqttTopic"] = getSetting("mqttTopic", MQTT_TOPIC); @@ -233,10 +233,10 @@ void _wsStart(uint32_t client_id) { for (unsigned char relayID=0; relayID 1) { root["multirelayVisible"] = 1; - root["relaySync"] = getSetting("relaySync", String(RELAY_SYNC)); + root["relaySync"] = getSetting("relaySync", RELAY_SYNC); } root["apiEnabled"] = getSetting("apiEnabled").toInt() == 1; @@ -244,7 +244,7 @@ void _wsStart(uint32_t client_id) { #if ENABLE_FAUXMO root["fauxmoVisible"] = 1; - root["fauxmoEnabled"] = getSetting("fauxmoEnabled", String(FAUXMO_ENABLED)).toInt() == 1; + root["fauxmoEnabled"] = getSetting("fauxmoEnabled", FAUXMO_ENABLED).toInt() == 1; #endif #if ENABLE_DS18B20 @@ -260,15 +260,15 @@ void _wsStart(uint32_t client_id) { #if ENABLE_RF root["rfVisible"] = 1; - root["rfChannel"] = getSetting("rfChannel", String(RF_CHANNEL)); - root["rfDevice"] = getSetting("rfDevice", String(RF_DEVICE)); + root["rfChannel"] = getSetting("rfChannel", RF_CHANNEL); + root["rfDevice"] = getSetting("rfDevice", RF_DEVICE); #endif #if ENABLE_EMON root["emonVisible"] = 1; root["emonPower"] = getPower(); - root["emonMains"] = getSetting("emonMains", String(EMON_MAINS_VOLTAGE)); - root["emonRatio"] = getSetting("emonRatio", String(EMON_CURRENT_RATIO)); + root["emonMains"] = getSetting("emonMains", EMON_MAINS_VOLTAGE); + root["emonRatio"] = getSetting("emonRatio", EMON_CURRENT_RATIO); #endif #if ENABLE_POW