Commit 15edf163 authored by Micha Mueller's avatar Micha Mueller
Browse files

Reduce code duplication in sensorbase::storeReading methods

parent 6baa5ecd
......@@ -164,31 +164,7 @@ public:
else
reading.value = rawReading.value * factor;
if( _delta )
// If in delta mode, _accumulator acts as a buffer, summing all deltas for the subsampling period
_accumulator.value += reading.value;
else
_accumulator.value = reading.value;
if (_subsamplingIndex++ % _subsamplingFactor == 0) {
_accumulator.timestamp = reading.timestamp;
//TODO: if sensor starts with values of 0, these won't be pushed. This should be fixed
if( !(_skipConstVal && (_accumulator.value == _lastSentValue.value)) ) {
_readingQueue->push(_accumulator);
_lastSentValue = _accumulator;
}
// We reset the accumulator's value for the correct accumulation of deltas
_accumulator.value = 0;
}
if (_sinkFile) {
try {
_sinkFile->seekp(0, std::ios::beg);
*_sinkFile << reading.value << std::endl;
} catch(const std::exception &e) { _sinkFile->close(); _sinkFile.reset(nullptr); }
}
_cache->store(reading);
_latestValue = reading;
commonStoreReading(reading);
}
/**
......@@ -202,7 +178,6 @@ public:
* in the database, therefore all such sensors should enable storage of deltas!
*
* This variant only adapts the delta computation for ureading_t actually.
* FIXME: Avoid code duplication
*
* @param rawReading Reading struct with (usigned) value and timestamp to be stored.
* @param factor Scaling factor, which is applied to the reading value (optional)
......@@ -228,6 +203,28 @@ public:
else
reading.value = rawReading.value * factor;
commonStoreReading(reading);
}
static std::string formatName(const std::string& name, int cpuID=-1) {return cpuID<0 ? name : "cpu" + std::to_string(cpuID) + "." + name;}
virtual void printConfig(LOG_LEVEL ll, LOGGER& lg, unsigned leadingSpaces=16) {
std::string leading(leadingSpaces, ' ');
LOG_VAR(ll) << leading << "Sensor: " << _name;
LOG_VAR(ll) << leading << " MQTT Topic: " << _mqtt;
LOG_VAR(ll) << leading << " Sink: " << (getSinkPath() != "" ? getSinkPath() : "none");
LOG_VAR(ll) << leading << " SubSampling: " << getSubsampling();
LOG_VAR(ll) << leading << (_skipConstVal ? " Skipping constant values" : " No skipping of constant values");
LOG_VAR(ll) << leading << (_delta ? " Storing delta readings" : " Storing absolute readings");
}
protected:
/**
* Internal method for all shared logic of signed/unsigned storeReading().
*/
inline
void commonStoreReading(reading_t reading) {
if( _delta )
// If in delta mode, _accumulator acts as a buffer, summing all deltas for the subsampling period
_accumulator.value += reading.value;
......@@ -253,22 +250,8 @@ public:
_cache->store(reading);
_latestValue = reading;
}
static std::string formatName(const std::string& name, int cpuID=-1) {return cpuID<0 ? name : "cpu" + std::to_string(cpuID) + "." + name;}
virtual void printConfig(LOG_LEVEL ll, LOGGER& lg, unsigned leadingSpaces=16) {
std::string leading(leadingSpaces, ' ');
LOG_VAR(ll) << leading << "Sensor: " << _name;
LOG_VAR(ll) << leading << " MQTT Topic: " << _mqtt;
LOG_VAR(ll) << leading << " Sink: " << (getSinkPath() != "" ? getSinkPath() : "none");
LOG_VAR(ll) << leading << " SubSampling: " << getSubsampling();
LOG_VAR(ll) << leading << (_skipConstVal ? " Skipping constant values" : " No skipping of constant values");
LOG_VAR(ll) << leading << (_delta ? " Storing delta readings" : " Storing absolute readings");
}
protected:
std::string _name;
std::string _mqtt;
std::string _sinkPath;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment