Commit 40c685ee authored by Micha Mueller's avatar Micha Mueller
Browse files

Refactor SensorBase: split storeReading into local and global store

parent 6f9f4d9d
......@@ -139,7 +139,7 @@ public:
/**
* Store a reading, in order to get it pushed to the data base eventually.
* Also this methods takes care of other optional reading post-processing,
* Also this method takes care of other optional reading post-processing,
* e.g. delta computation, subsampling, caching, scaling, etc.
*
* This is the primary storeReading() and should be used whenever possible.
......@@ -149,30 +149,33 @@ public:
* @param maxValue Maximum possible value of the reading; required for the
* delta computation to detect an overflow.
*/
void storeReading(reading_t rawReading, double factor=1.0, long long maxValue=LLONG_MAX) {
reading_t reading = rawReading;
if( _delta ) {
if (!_firstReading) {
if (rawReading.value < _lastRawValue.value)
reading.value = (rawReading.value + (maxValue - _lastRawValue.value)) * factor;
else
reading.value = (rawReading.value - _lastRawValue.value) * factor;
} else {
_firstReading = false;
_lastRawValue = rawReading;
return;
}
_lastRawValue = rawReading;
}
else
reading.value = rawReading.value * factor;
void storeReading(reading_t rawReading, double factor=1.0, long long maxValue=LLONG_MAX, bool storeGlobal=true) {
reading_t reading = rawReading;
if( _delta ) {
if (!_firstReading) {
if (rawReading.value < _lastRawValue.value)
reading.value = (rawReading.value + (maxValue - _lastRawValue.value)) * factor;
else
reading.value = (rawReading.value - _lastRawValue.value) * factor;
} else {
_firstReading = false;
_lastRawValue = rawReading;
return;
}
_lastRawValue = rawReading;
}
else
reading.value = rawReading.value * factor;
commonStoreReading(reading);
storeReadingLocal(reading);
if (storeGlobal) {
storeReadingGlobal(reading);
}
}
/**
* Store an unsigned reading, in order to get it pushed to the data base eventually.
* Also this methods takes care of other optional reading post-processing,
* Also this method takes care of other optional reading post-processing,
* e.g. delta computation, subsampling, caching, scaling, etc.
*
* This is a variant of the primary storeReading() for monotonically increasing
......@@ -187,26 +190,29 @@ public:
* @param maxValue Maximum possible value of the reading; required for the
* delta computation to detect an overflow.
*/
void storeReading(ureading_t rawReading, double factor=1.0, unsigned long long maxValue=ULLONG_MAX) {
void storeReading(ureading_t rawReading, double factor=1.0, unsigned long long maxValue=ULLONG_MAX, bool storeGlobal=true) {
reading_t reading;
reading.timestamp = rawReading.timestamp;
if( _delta ) {
if (!_firstReading) {
if (rawReading.value < _lastRawUValue.value)
reading.value = (rawReading.value + (maxValue - _lastRawUValue.value)) * factor;
else
reading.value = (rawReading.value - _lastRawUValue.value) * factor;
if (rawReading.value < _lastRawUValue.value)
reading.value = (rawReading.value + (maxValue - _lastRawUValue.value)) * factor;
else
reading.value = (rawReading.value - _lastRawUValue.value) * factor;
} else {
_firstReading = false;
_lastRawUValue = rawReading;
return;
_firstReading = false;
_lastRawUValue = rawReading;
return;
}
_lastRawUValue = rawReading;
}
else
reading.value = rawReading.value * factor;
commonStoreReading(reading);
storeReadingLocal(reading);
if (storeGlobal) {
storeReadingGlobal(reading);
}
}
static std::string formatName(const std::string& name, int cpuID=-1) {return cpuID<0 ? name : "cpu" + std::to_string(cpuID) + "." + name;}
......@@ -224,11 +230,28 @@ public:
protected:
/**
* Internal method for all shared logic of signed/unsigned storeReading().
* Store reading within the sensor, but do not put it in the readingQueue
* so the reading does not get pushed but the caches are still updated.
*/
inline
void commonStoreReading(reading_t reading) {
if( _delta )
void storeReadingLocal(reading_t reading) {
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;
}
/**
* Store the reading in the readingQueue so it can get pushed.
*/
inline
void storeReadingGlobal(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;
else
......@@ -244,15 +267,6 @@ protected:
// 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;
}
std::string _name;
......
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