Skip to content

Commit

Permalink
sns: rename generic pwr keys with a typed prefix
Browse files Browse the repository at this point in the history
Ratio, Mains are specific to a magnitude type.
Make keys use global index, and do a (temporary) WebUI workaround
to use 0 index instead of the global key.
Migrate to the new settings keys.

Local magnitude-specific index is still in play, pending changes in the rest of sensors
(ADE7953, PZEM004T{,V30}, ...) to remove it completely.

Local sensor index could still be useful though, to help out with some settings
in the future as we don't really enforce magnitude type uniqueness within sensors.
  • Loading branch information
mcspr committed Jun 18, 2021
1 parent 23da0b7 commit 1a36efb
Show file tree
Hide file tree
Showing 9 changed files with 3,550 additions and 3,538 deletions.
2 changes: 1 addition & 1 deletion code/espurna/config/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
#endif

#ifndef CFG_VERSION
#define CFG_VERSION 6
#define CFG_VERSION 7
#endif
Binary file modified code/espurna/data/index.all.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.sensor.html.gz
Binary file not shown.
76 changes: 44 additions & 32 deletions code/espurna/sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1044,23 +1044,38 @@ const char * const _magnitudeSettingsPrefix(unsigned char type) {
}
}

template <typename T>
String _magnitudeSettingsKey(unsigned char type, T&& suffix) {
return String(_magnitudeSettingsPrefix(type)) + suffix;
}

template <typename T>
String _magnitudeSettingsKey(sensor_magnitude_t& magnitude, T&& suffix) {
return String(_magnitudeSettingsPrefix(magnitude.type)) + suffix;
return _magnitudeSettingsKey(magnitude.type, std::forward<T>(suffix));
}

bool _sensorMatchKeyPrefix(const char * key) {

if (strncmp(key, "sns", 3) == 0) return true;
if (strncmp(key, "pwr", 3) == 0) return true;

return _magnitudeForEachCountedCheck([key](unsigned char type) {
const char* const prefix { _magnitudeSettingsPrefix(type) };
return (strncmp(prefix, key, strlen(prefix)) == 0);
});
}

SettingsKey _magnitudeSettingsRatioKey(unsigned char type, size_t index) {
return {_magnitudeSettingsKey(type, F("Ratio")), index};
}

SettingsKey _magnitudeSettingsRatioKey(const sensor_magnitude_t& magnitude) {
return _magnitudeSettingsRatioKey(magnitude.type, magnitude.index_global);
}

double _magnitudeSettingsRatio(const sensor_magnitude_t& magnitude, double defaultValue) {
return getSetting(_magnitudeSettingsRatioKey(magnitude), defaultValue);
};

const String _sensorQueryDefault(const String& key) {

auto get_defaults = [](unsigned char type, BaseSensor* ptr) -> String {
Expand All @@ -1083,16 +1098,13 @@ const String _sensorQueryDefault(const String& key) {
auto magnitude_key = [](const sensor_magnitude_t& magnitude) -> SettingsKey {
switch (magnitude.type) {
case MAGNITUDE_CURRENT:
return {"pwrRatioC", magnitude.index_global};
case MAGNITUDE_VOLTAGE:
return {"pwrRatioV", magnitude.index_global};
case MAGNITUDE_POWER_ACTIVE:
return {"pwrRatioP", magnitude.index_global};
case MAGNITUDE_ENERGY:
return {"pwrRatioE", magnitude.index_global};
default:
return "";
return _magnitudeSettingsRatioKey(magnitude);
}

return "";
};

unsigned char type = MAGNITUDE_NONE;
Expand Down Expand Up @@ -1416,11 +1428,9 @@ void _sensorWebSocketOnConnected(JsonObject& root) {
root["pwrVisible"] = 1;
}

#if EMON_ANALOG_SUPPORT
if (sensor->getID() == SENSOR_EMON_ANALOG_ID) {
root["pwrVoltage"] = ((EmonAnalogSensor *) sensor)->getVoltage();
}
#endif
if (_sensorIsAnalogEmon(sensor)) {
root["voltMains0"] = static_cast<BaseAnalogEmonSensor*>(sensor)->getVoltage();
}

#if HLW8012_SUPPORT
if (sensor->getID() == SENSOR_HLW8012_ID) {
Expand Down Expand Up @@ -1448,7 +1458,7 @@ void _sensorWebSocketOnConnected(JsonObject& root) {
#if PULSEMETER_SUPPORT
if (sensor->getID() == SENSOR_PULSEMETER_ID) {
root["pmVisible"] = 1;
root["pwrRatioE"] = ((PulseMeterSensor *) sensor)->getEnergyRatio();
root["eneRatio0"] = ((PulseMeterSensor *) sensor)->getEnergyRatio();
}
#endif

Expand Down Expand Up @@ -2474,19 +2484,19 @@ void _sensorConfigure() {
if ((value = getSetting("pwrExpectedC", 0.0))) {
sensor->expectedCurrent(value);
delSetting("pwrExpectedC");
setSetting("pwrRatioC", sensor->getCurrentRatio());
setSetting(_magnitudeSettingsRatioKey(MAGNITUDE_CURRENT, 0), sensor->getCurrentRatio());
}

if ((value = getSetting("pwrExpectedV", 0.0))) {
delSetting("pwrExpectedV");
sensor->expectedVoltage(value);
setSetting("pwrRatioV", sensor->getVoltageRatio());
setSetting(_magnitudeSettingsRatioKey(MAGNITUDE_VOLTAGE, 0), sensor->getVoltageRatio());
}

if ((value = getSetting("pwrExpectedP", 0.0))) {
delSetting("pwrExpectedP");
sensor->expectedPower(value);
setSetting("pwrRatioP", sensor->getPowerRatio());
setSetting(_magnitudeSettingsRatioKey(MAGNITUDE_POWER_ACTIVE, 0), sensor->getPowerRatio());
}

if (getSetting("pwrResetE", false)) {
Expand All @@ -2499,9 +2509,9 @@ void _sensorConfigure() {

if (getSetting("pwrResetCalibration", false)) {
delSetting("pwrResetCalibration");
delSetting("pwrRatioC");
delSetting("pwrRatioV");
delSetting("pwrRatioP");
delSetting(_magnitudeSettingsRatioKey(MAGNITUDE_CURRENT, 0));
delSetting(_magnitudeSettingsRatioKey(MAGNITUDE_VOLTAGE, 0));
delSetting(_magnitudeSettingsRatioKey(MAGNITUDE_ENERGY, 0));
sensor->resetRatios();
}

Expand All @@ -2511,40 +2521,33 @@ void _sensorConfigure() {

// Update magnitude config, filter sizes and reset energy if needed
{
for (unsigned char index = 0; index < _magnitudes.size(); ++index) {
for (size_t index = 0; index < _magnitudes.size(); ++index) {

auto& magnitude = _magnitudes.at(index);

// process emon-specific settings first. ensure that settings use global index and we access sensor with the local one
if (_sensorIsEmon(magnitude.sensor)) {
// TODO: compatibility proxy, fetch global key before indexed
// TODO: *remove* local index, favour separate sensor instances instead of index magic
auto get_ratio = [](const char* key, unsigned char index, double default_value) -> double {
return getSetting({key, index}, getSetting(key, default_value));
};

auto* sensor = static_cast<BaseEmonSensor*>(magnitude.sensor);

switch (magnitude.type) {
case MAGNITUDE_CURRENT:
sensor->setCurrentRatio(
magnitude.index_local, get_ratio("pwrRatioC", magnitude.index_global, sensor->defaultCurrentRatio())
);
magnitude.index_local, _magnitudeSettingsRatio(magnitude, sensor->defaultCurrentRatio()));
break;
case MAGNITUDE_POWER_ACTIVE:
sensor->setPowerRatio(
magnitude.index_local, get_ratio("pwrRatioP", magnitude.index_global, sensor->defaultPowerRatio())
);
magnitude.index_local, _magnitudeSettingsRatio(magnitude, sensor->defaultPowerRatio()));
break;
case MAGNITUDE_VOLTAGE:
sensor->setVoltageRatio(
magnitude.index_local, get_ratio("pwrRatioV", magnitude.index_global, sensor->defaultVoltageRatio())
);
magnitude.index_local, _magnitudeSettingsRatio(magnitude, sensor->defaultVoltageRatio()));
break;
case MAGNITUDE_ENERGY:
sensor->setEnergyRatio(
magnitude.index_local, get_ratio("pwrRatioE", magnitude.index_global, sensor->defaultEnergyRatio())
);
magnitude.index_local, _magnitudeSettingsRatio(magnitude, sensor->defaultEnergyRatio()));
break;
default:
break;
Expand Down Expand Up @@ -2732,6 +2735,15 @@ void _sensorBackwards(int version) {
delSetting("eneUnits");
delSetting("tmpUnits");
}

// generic pwr settings have magnitude prefixes
if (version < 7) {
moveSetting(F("pwrVoltage"), _magnitudeSettingsKey(MAGNITUDE_VOLTAGE, F("Mains0")));
moveSetting(F("pwrRatioC"), _magnitudeSettingsRatioKey(MAGNITUDE_CURRENT, 0).value());
moveSetting(F("pwrRatioV"), _magnitudeSettingsRatioKey(MAGNITUDE_VOLTAGE, 0).value());
moveSetting(F("pwrRatioP"), _magnitudeSettingsRatioKey(MAGNITUDE_POWER_ACTIVE, 0).value());
moveSetting(F("pwrRatioE"), _magnitudeSettingsRatioKey(MAGNITUDE_ENERGY, 0).value());
}
}

void sensorSetup() {
Expand Down
2 changes: 1 addition & 1 deletion code/espurna/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ void moveSetting(const String& from, const String& to) {
delSetting(from);
}

void moveSetting(const String& from, const String& to, unsigned char index) {
void moveSetting(const String& from, const String& to, size_t index) {
const auto keys = _moveKeys(from, to, index);

auto result = settings::kv_store.get(keys.first.value());
Expand Down
2 changes: 1 addition & 1 deletion code/espurna/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ String settingsQueryDefaults(const String& key);
// --------------------------------------------------------------------------

void moveSetting(const String& from, const String& to);
void moveSetting(const String& from, const String& to, unsigned int index);
void moveSetting(const String& from, const String& to, size_t index);
void moveSettings(const String& from, const String& to);

template <typename T, typename = typename settings::internal::enable_if_not_arduino_string<T>::type>
Expand Down
Loading

0 comments on commit 1a36efb

Please sign in to comment.