Browse Source

sns: rename generic pwr keys with a typed prefix

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.
pull/2471/head
Maxim Prokhorov 3 years ago
parent
commit
1a36efb8f2
9 changed files with 3550 additions and 3538 deletions
  1. +1
    -1
      code/espurna/config/version.h
  2. BIN
      code/espurna/data/index.all.html.gz
  3. BIN
      code/espurna/data/index.sensor.html.gz
  4. +44
    -32
      code/espurna/sensor.cpp
  5. +1
    -1
      code/espurna/settings.cpp
  6. +1
    -1
      code/espurna/settings.h
  7. +2063
    -2063
      code/espurna/static/index.all.html.gz.h
  8. +1438
    -1438
      code/espurna/static/index.sensor.html.gz.h
  9. +2
    -2
      code/html/index.html

+ 1
- 1
code/espurna/config/version.h View File

@ -21,5 +21,5 @@
#endif
#ifndef CFG_VERSION
#define CFG_VERSION 6
#define CFG_VERSION 7
#endif

BIN
code/espurna/data/index.all.html.gz View File


BIN
code/espurna/data/index.sensor.html.gz View File


+ 44
- 32
code/espurna/sensor.cpp View File

@ -1044,13 +1044,17 @@ 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;
@ -1058,9 +1062,20 @@ bool _sensorMatchKeyPrefix(const char * key) {
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 {
@ -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;
@ -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) {
@ -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
@ -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)) {
@ -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();
}
@ -2511,7 +2521,7 @@ 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);
@ -2519,32 +2529,25 @@ void _sensorConfigure() {
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;
@ -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() {


+ 1
- 1
code/espurna/settings.cpp View File

@ -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());


+ 1
- 1
code/espurna/settings.h View File

@ -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>


+ 2063
- 2063
code/espurna/static/index.all.html.gz.h
File diff suppressed because it is too large
View File


+ 1438
- 1438
code/espurna/static/index.sensor.html.gz.h
File diff suppressed because it is too large
View File


+ 2
- 2
code/html/index.html View File

@ -1772,7 +1772,7 @@
<div class="pure-g module module-emon">
<label class="pure-u-1 pure-u-lg-1-4">Voltage</label>
<input class="pure-u-1 pure-u-lg-3-4" name="pwrVoltage" type="text" />
<input class="pure-u-1 pure-u-lg-3-4" name="voltMains0" type="text" />
<div class="pure-u-0 pure-u-lg-1-4"></div>
<div class="pure-u-1 pure-u-lg-3-4 hint">Mains voltage in your system (in V).</div>
</div>
@ -1800,7 +1800,7 @@
<div class="pure-g module module-pm">
<label class="pure-u-1 pure-u-lg-1-4">Energy Ratio</label>
<input class="pure-u-1 pure-u-lg-3-4" name="pwrRatioE" type="text" placeholder="0" />
<input class="pure-u-1 pure-u-lg-3-4" name="eneRatio0" type="text" placeholder="0" />
<div class="pure-u-0 pure-u-lg-1-4"></div>
<div class="pure-u-1 pure-u-lg-3-4 hint">Energy ratio in pulses/kWh.</div>
</div>


Loading…
Cancel
Save