Skip to content

Commit

Permalink
sns: more annoying dummy sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
mcspr committed Apr 10, 2024
1 parent 0d84c76 commit 56a772e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
2 changes: 1 addition & 1 deletion code/espurna/sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2218,7 +2218,7 @@ void load() {

#if DUMMY_SENSOR_SUPPORT
{
add(new DummySensor());
add(new driver::dummy::Sensor());
}
#endif

Expand Down
62 changes: 47 additions & 15 deletions code/espurna/sensors/DummySensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,31 @@ Copyright (C) 2020 by Maxim Prokhorov <prokhorov dot max at outlook dot com>

// In sensor.cpp:
// - #include "sensors/DummySensor.h"
// - add `_sensors.push_back(new DummySensor());` at the end of _sensorLoad();
// - wrap w/ DUMMY_SENSOR_SUPPORT
// - add `_sensors.push_back(new driver::dummy::Sensor());` at the end of _sensorLoad();

#if SENSOR_SUPPORT && DUMMY_SENSOR_SUPPORT

#include "BaseEmonSensor.h"

struct DummySensor : public BaseEmonSensor {
namespace espurna {
namespace sensor {
namespace driver {
namespace dummy {
namespace {

struct Sensor : public BaseEmonSensor {

static constexpr Magnitude Magnitudes[] {
MAGNITUDE_TEMPERATURE,
MAGNITUDE_HUMIDITY,
MAGNITUDE_PRESSURE,
MAGNITUDE_LUX,
MAGNITUDE_ENERGY_DELTA,
MAGNITUDE_ENERGY,
{MAGNITUDE_TEMPERATURE},
{MAGNITUDE_HUMIDITY},
{MAGNITUDE_PRESSURE},
{MAGNITUDE_LUX},
{MAGNITUDE_ENERGY_DELTA},
{MAGNITUDE_ENERGY},
};

DummySensor() :
Sensor() :
BaseEmonSensor(Magnitudes)
{}

Expand All @@ -36,16 +45,22 @@ struct DummySensor : public BaseEmonSensor {
}

void begin() override {
_ready = true;
if (_fail_begin) {
_fail_begin = false;
_error = SENSOR_ERROR_NOT_READY;
return;
}

_error = SENSOR_ERROR_OK;
_ready = true;
}

String description() const override {
return F("DummySensor");
return STRING_VIEW("DummySensor").toString();
}

String address(unsigned char) const override {
return F("/dev/null");
return STRING_VIEW("/dev/null").toString();
}

unsigned char type(unsigned char index) const override {
Expand Down Expand Up @@ -78,6 +93,13 @@ struct DummySensor : public BaseEmonSensor {
}

void pre() override {
_error = SENSOR_ERROR_OK;
if (_fail_pre) {
_fail_pre = false;
_error = SENSOR_ERROR_TIMEOUT;
return;
}

++_temperature;
++_humidity;
++_pressure;
Expand All @@ -104,11 +126,13 @@ struct DummySensor : public BaseEmonSensor {
_delta = 0.0;
}

_energy[0] += espurna::sensor::Energy(
espurna::sensor::WattSeconds(_delta));
_energy[0] += Energy(WattSeconds(_delta));
}

private:
bool _fail_begin { true };
bool _fail_pre { true };

double _temperature { 25.0 };
double _humidity { 50.0 };
double _pressure { 1000.0 };
Expand All @@ -117,5 +141,13 @@ struct DummySensor : public BaseEmonSensor {
};

#if __cplusplus < 201703L
constexpr BaseSensor::Magnitude DummySensor::Magnitudes[];
constexpr BaseSensor::Magnitude Sensor::Magnitudes[];
#endif

} // namespace
} // namespace dummy
} // namespace driver
} // namespace sensor
} // namespace espurna

#endif

0 comments on commit 56a772e

Please sign in to comment.