Skip to content

Commit

Permalink
sns: always try to load magnitude units from settings
Browse files Browse the repository at this point in the history
also, drop special builds flags in favour of sane defaults
  • Loading branch information
mcspr committed Sep 30, 2020
1 parent b0bb384 commit 1f1d6a9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 90 deletions.
6 changes: 0 additions & 6 deletions code/espurna/config/hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -4655,9 +4655,6 @@
#define HLW8012_POWER_RATIO 3414290
#define HLW8012_INTERRUPT_ON FALLING

#define SENSOR_ENERGY_UNITS ENERGY_KWH
#define SENSOR_POWER_UNITS POWER_WATTS

// -----------------------------------------------------------------------------
// Kogan Smarter Home Plug with Energy Meter (Australia)
// Product code: KASPEMHA
Expand Down Expand Up @@ -4703,9 +4700,6 @@
#define HLW8012_POWER_RATIO 3414290
#define HLW8012_INTERRUPT_ON FALLING

#define SENSOR_ENERGY_UNITS ENERGY_KWH
#define SENSOR_POWER_UNITS POWER_WATTS

// -----------------------------------------------------------------------------
// KINGART_CURTAIN_SWITCH
// -----------------------------------------------------------------------------
Expand Down
12 changes: 0 additions & 12 deletions code/espurna/config/sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,6 @@
#define SENSOR_ADDRESS_TOPIC "address" // Topic to publish sensor addresses


#ifndef SENSOR_TEMPERATURE_UNITS
#define SENSOR_TEMPERATURE_UNITS TMP_CELSIUS // Temperature units (TMP_CELSIUS | TMP_FAHRENHEIT)
#endif

#ifndef SENSOR_ENERGY_UNITS
#define SENSOR_ENERGY_UNITS ENERGY_JOULES // Energy units (ENERGY_JOULES | ENERGY_KWH)
#endif

#ifndef SENSOR_POWER_UNITS
#define SENSOR_POWER_UNITS POWER_WATTS // Power units (POWER_WATTS | POWER_KILOWATTS)
#endif

// -----------------------------------------------------------------------------
// Magnitude offset correction
// -----------------------------------------------------------------------------
Expand Down
14 changes: 0 additions & 14 deletions code/espurna/config/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,20 +275,6 @@
#define UV_INDEX_EXTREME 4 // 11 or more means extreme risk of harm from unprotected sun exposure.
// Take all precautions because unprotected skin and eyes can burn in minutes.

//------------------------------------------------------------------------------
// UNITS
//------------------------------------------------------------------------------

#define POWER_WATTS sensor::Unit::Watt
#define POWER_KILOWATTS sensor::Unit::Kilowatt

#define ENERGY_JOULES sensor::Unit::Joule
#define ENERGY_KWH sensor::Unit::KilowattHour

#define TMP_CELSIUS sensor::Unit::Celcius
#define TMP_FAHRENHEIT sensor::Unit::Farenheit
#define TMP_KELVIN sensor::Unit::Kelvin

//--------------------------------------------------------------------------------
// Sensor ID
// These should remain over time, do not modify them, only add new ones at the end
Expand Down
105 changes: 47 additions & 58 deletions code/espurna/sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -829,40 +829,48 @@ sensor::Unit _magnitudeUnitFilter(const sensor_magnitude_t& magnitude, sensor::U
auto result = magnitude.units;

switch (magnitude.type) {
case MAGNITUDE_TEMPERATURE: {
switch (updated) {
case sensor::Unit::Celcius:
case sensor::Unit::Farenheit:
case sensor::Unit::Kelvin:
result = updated;
break;
default:
break;
}

case MAGNITUDE_TEMPERATURE: {
switch (updated) {
case sensor::Unit::Celcius:
case sensor::Unit::Farenheit:
case sensor::Unit::Kelvin:
result = updated;
break;
}
case MAGNITUDE_POWER_ACTIVE: {
switch (updated) {
case sensor::Unit::Kilowatt:
case sensor::Unit::Watt:
result = updated;
break;
default:
break;
}
default:
break;
}
case MAGNITUDE_ENERGY: {
switch (updated) {
case sensor::Unit::KilowattHour:
case sensor::Unit::Joule:
result = updated;
break;
default:
break;
}
break;
}

case MAGNITUDE_POWER_ACTIVE: {
switch (updated) {
case sensor::Unit::Kilowatt:
case sensor::Unit::Watt:
result = updated;
break;
default:
break;
}
break;
}

case MAGNITUDE_ENERGY: {
switch (updated) {
case sensor::Unit::KilowattHour:
case sensor::Unit::Joule:
result = updated;
break;
default:
break;
}
break;
}

default:
result = updated;
break;

}

return result;
Expand Down Expand Up @@ -2396,12 +2404,6 @@ void _sensorConfigure() {

// Update magnitude config, filter sizes and reset energy if needed
{

// TODO: instead of using global enum, have a local mapping?
const auto tmpUnits = getSetting("tmpUnits", SENSOR_TEMPERATURE_UNITS);
const auto pwrUnits = getSetting("pwrUnits", SENSOR_POWER_UNITS);
const auto eneUnits = getSetting("eneUnits", SENSOR_ENERGY_UNITS);

for (unsigned char index = 0; index < _magnitudes.size(); ++index) {

auto& magnitude = _magnitudes.at(index);
Expand Down Expand Up @@ -2444,29 +2446,16 @@ void _sensorConfigure() {
}
}

// adjust type-specific units (TODO: try to adjust settings to use type prefixes?)
switch (magnitude.type) {
case MAGNITUDE_TEMPERATURE:
magnitude.units = _magnitudeUnitFilter(
magnitude,
getSetting({"tmpUnits", magnitude.index_global}, tmpUnits)
);
break;
case MAGNITUDE_POWER_ACTIVE:
magnitude.units = _magnitudeUnitFilter(
magnitude,
getSetting({"pwrUnits", magnitude.index_global}, pwrUnits)
);
break;
case MAGNITUDE_ENERGY:
magnitude.units = _magnitudeUnitFilter(
magnitude,
getSetting({"eneUnits", magnitude.index_global}, eneUnits)
);
break;
default:
magnitude.units = magnitude.sensor->units(magnitude.slot);
break;
// adjust type-specific units
{
const sensor::Unit default_unit { magnitude.sensor->units(magnitude.slot) };
const settings_key_t key {
String(_magnitudeSettingsPrefix(magnitude.type)) + F("Units") + String(magnitude.index_global, 10) };

magnitude.units = _magnitudeUnitFilter(
magnitude,
getSetting(key, default_unit)
);
}

// some magnitudes allow to be corrected with an offset
Expand Down

0 comments on commit 1f1d6a9

Please sign in to comment.