Browse Source

Refactor settings getters and setters

fastled
Xose Pérez 7 years ago
parent
commit
1425a8dded
10 changed files with 35 additions and 35 deletions
  1. +4
    -4
      code/src/emon.ino
  2. +1
    -1
      code/src/fauxmo.ino
  3. +2
    -2
      code/src/main.ino
  4. +1
    -1
      code/src/mqtt.ino
  5. +1
    -1
      code/src/nofuss.ino
  6. +3
    -3
      code/src/pow.ino
  7. +8
    -4
      code/src/relay.ino
  8. +2
    -2
      code/src/rf.ino
  9. +5
    -9
      code/src/settings.ino
  10. +8
    -8
      code/src/web.ino

+ 4
- 4
code/src/emon.ino View File

@ -39,10 +39,10 @@ void powerMonitorSetup() {
// backwards compatibility // backwards compatibility
String tmp; String tmp;
tmp = getSetting("pwMainsVoltage", String() + EMON_MAINS_VOLTAGE);
tmp = getSetting("pwMainsVoltage", EMON_MAINS_VOLTAGE);
setSetting("emonMains", tmp); setSetting("emonMains", tmp);
delSetting("pwMainsVoltage"); delSetting("pwMainsVoltage");
tmp = getSetting("pwCurrentRatio", String() + EMON_CURRENT_RATIO);
tmp = getSetting("pwCurrentRatio", EMON_CURRENT_RATIO);
setSetting("emonRatio", tmp); setSetting("emonRatio", tmp);
delSetting("pwCurrentRatio"); delSetting("pwCurrentRatio");
@ -50,7 +50,7 @@ void powerMonitorSetup() {
currentCallback, currentCallback,
EMON_ADC_BITS, EMON_ADC_BITS,
EMON_REFERENCE_VOLTAGE, EMON_REFERENCE_VOLTAGE,
getSetting("emonRatio", String(EMON_CURRENT_RATIO)).toFloat()
getSetting("emonRatio", EMON_CURRENT_RATIO).toFloat()
); );
emon.setPrecision(EMON_CURRENT_PRECISION); emon.setPrecision(EMON_CURRENT_PRECISION);
} }
@ -91,7 +91,7 @@ void powerMonitorLoop() {
sum += current; sum += current;
++measurements; ++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)); //DEBUG_MSG("[ENERGY] Power now: %dW\n", int(current * mainsVoltage));


+ 1
- 1
code/src/fauxmo.ino View File

@ -18,7 +18,7 @@ fauxmoESP fauxmo;
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void fauxmoConfigure() { void fauxmoConfigure() {
fauxmo.enable(getSetting("fauxmoEnabled", String(FAUXMO_ENABLED)).toInt() == 1);
fauxmo.enable(getSetting("fauxmoEnabled", FAUXMO_ENABLED).toInt() == 1);
} }
void fauxmoSetup() { void fauxmoSetup() {


+ 2
- 2
code/src/main.ino View File

@ -29,8 +29,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>
#include <AsyncMqttClient.h> #include <AsyncMqttClient.h>
#include "FS.h" #include "FS.h"
String getSetting(const String& key, String defaultValue = "");
bool relayStatus(unsigned char id, bool status, bool report = true);
template<typename T> bool setSetting(const String& key, T value);
template<typename T> String getSetting(const String& key, T defaultValue);
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// METHODS // METHODS


+ 1
- 1
code/src/mqtt.ino View File

@ -101,7 +101,7 @@ void mqttConnect() {
if (!mqtt.connected()) { if (!mqtt.connected()) {
String host = getSetting("mqttServer", MQTT_SERVER); String host = getSetting("mqttServer", MQTT_SERVER);
String port = getSetting("mqttPort", String(MQTT_PORT));
String port = getSetting("mqttPort", MQTT_PORT);
String user = getSetting("mqttUser"); String user = getSetting("mqttUser");
String pass = getSetting("mqttPassword"); String pass = getSetting("mqttPassword");


+ 1
- 1
code/src/nofuss.ino View File

@ -74,7 +74,7 @@ void nofussLoop() {
static unsigned long last_check = 0; static unsigned long last_check = 0;
if (!wifiConnected()) return; 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; if ((last_check > 0) && ((millis() - last_check) < interval)) return;
last_check = millis(); last_check = millis();
NoFUSSClient.handle(); NoFUSSClient.handle();


+ 3
- 3
code/src/pow.ino View File

@ -48,11 +48,11 @@ void powSaveCalibration() {
void powRetrieveCalibration() { void powRetrieveCalibration() {
double value; double value;
value = getSetting("powPowerMult", "0").toFloat();
value = getSetting("powPowerMult", 0).toFloat();
if (value > 0) hlw8012.setPowerMultiplier((int) value); if (value > 0) hlw8012.setPowerMultiplier((int) value);
value = getSetting("powCurrentMult", "0").toFloat();
value = getSetting("powCurrentMult", 0).toFloat();
if (value > 0) hlw8012.setCurrentMultiplier((int) value); if (value > 0) hlw8012.setCurrentMultiplier((int) value);
value = getSetting("powVoltageMult", "0").toFloat();
value = getSetting("powVoltageMult", 0).toFloat();
if (value > 0) hlw8012.setVoltageMultiplier((int) value); if (value > 0) hlw8012.setVoltageMultiplier((int) value);
} }


+ 8
- 4
code/src/relay.ino View File

@ -91,7 +91,11 @@ bool relayStatus(unsigned char id, bool status, bool report) {
if (report) relayMQTT(id); if (report) relayMQTT(id);
if (!recursive) relayWS(); if (!recursive) relayWS();
return changed; return changed;
}
bool relayStatus(unsigned char id, bool status) {
return relayStatus(id, status, true);
} }
void relaySync(unsigned char id) { void relaySync(unsigned char id) {
@ -100,7 +104,7 @@ void relaySync(unsigned char id) {
recursive = true; recursive = true;
byte relaySync = getSetting("relaySync", String(RELAY_SYNC)).toInt();
byte relaySync = getSetting("relaySync", RELAY_SYNC).toInt();
bool status = relayStatus(id); bool status = relayStatus(id);
// If RELAY_SYNC_SAME all relays should have the same state // 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 relayMode is not SAME avoid responding to a retained message
if (isFirstMessage) { if (isFirstMessage) {
isFirstMessage = false; isFirstMessage = false;
byte relayMode = getSetting("relayMode", String(RELAY_MODE)).toInt();
byte relayMode = getSetting("relayMode", RELAY_MODE).toInt();
if (relayMode != RELAY_MODE_SAME) return; if (relayMode != RELAY_MODE_SAME) return;
} }
@ -227,7 +231,7 @@ void relaySetup() {
#endif #endif
EEPROM.begin(4096); 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++) { for (unsigned int i=0; i < _relays.size(); i++) {
pinMode(_relays[i], OUTPUT); pinMode(_relays[i], OUTPUT);


+ 2
- 2
code/src/rf.ino View File

@ -33,7 +33,7 @@ void rfBuildCodes() {
unsigned long code = 0; unsigned long code = 0;
// channel // 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++) { for (byte i = 0; i < 5; i++) {
code *= 3; code *= 3;
if (channel & 1) code += 1; if (channel & 1) code += 1;
@ -41,7 +41,7 @@ void rfBuildCodes() {
} }
// device // 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++) { for (byte i = 0; i < 5; i++) {
code *= 3; code *= 3;
if (device != i) code += 2; if (device != i) code += 2;


+ 5
- 9
code/src/settings.ino View File

@ -97,24 +97,20 @@ void settingsLoop() {
embedis.process(); embedis.process();
} }
String getSetting(const String& key, String defaultValue) {
template<typename T> String getSetting(const String& key, T defaultValue) {
String value; String value;
if (!Embedis::get(key, value)) value = defaultValue;
if (!Embedis::get(key, value)) value = String(defaultValue);
return value; 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<typename T> bool setSetting(const String& key, T value) {
return Embedis::set(key, String(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) { bool delSetting(const String& key) {
return Embedis::del(key); return Embedis::del(key);
} }


+ 8
- 8
code/src/web.ino View File

@ -224,7 +224,7 @@ void _wsStart(uint32_t client_id) {
root["mqttStatus"] = mqttConnected(); root["mqttStatus"] = mqttConnected();
root["mqttServer"] = getSetting("mqttServer", MQTT_SERVER); root["mqttServer"] = getSetting("mqttServer", MQTT_SERVER);
root["mqttPort"] = getSetting("mqttPort", String(MQTT_PORT));
root["mqttPort"] = getSetting("mqttPort", MQTT_PORT);
root["mqttUser"] = getSetting("mqttUser"); root["mqttUser"] = getSetting("mqttUser");
root["mqttPassword"] = getSetting("mqttPassword"); root["mqttPassword"] = getSetting("mqttPassword");
root["mqttTopic"] = getSetting("mqttTopic", MQTT_TOPIC); root["mqttTopic"] = getSetting("mqttTopic", MQTT_TOPIC);
@ -233,10 +233,10 @@ void _wsStart(uint32_t client_id) {
for (unsigned char relayID=0; relayID<relayCount(); relayID++) { for (unsigned char relayID=0; relayID<relayCount(); relayID++) {
relay.add(relayStatus(relayID)); relay.add(relayStatus(relayID));
} }
root["relayMode"] = getSetting("relayMode", String(RELAY_MODE));
root["relayMode"] = getSetting("relayMode", RELAY_MODE);
if (relayCount() > 1) { if (relayCount() > 1) {
root["multirelayVisible"] = 1; root["multirelayVisible"] = 1;
root["relaySync"] = getSetting("relaySync", String(RELAY_SYNC));
root["relaySync"] = getSetting("relaySync", RELAY_SYNC);
} }
root["apiEnabled"] = getSetting("apiEnabled").toInt() == 1; root["apiEnabled"] = getSetting("apiEnabled").toInt() == 1;
@ -244,7 +244,7 @@ void _wsStart(uint32_t client_id) {
#if ENABLE_FAUXMO #if ENABLE_FAUXMO
root["fauxmoVisible"] = 1; root["fauxmoVisible"] = 1;
root["fauxmoEnabled"] = getSetting("fauxmoEnabled", String(FAUXMO_ENABLED)).toInt() == 1;
root["fauxmoEnabled"] = getSetting("fauxmoEnabled", FAUXMO_ENABLED).toInt() == 1;
#endif #endif
#if ENABLE_DS18B20 #if ENABLE_DS18B20
@ -260,15 +260,15 @@ void _wsStart(uint32_t client_id) {
#if ENABLE_RF #if ENABLE_RF
root["rfVisible"] = 1; 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 #endif
#if ENABLE_EMON #if ENABLE_EMON
root["emonVisible"] = 1; root["emonVisible"] = 1;
root["emonPower"] = getPower(); 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 #endif
#if ENABLE_POW #if ENABLE_POW


Loading…
Cancel
Save