Commit 19e3ff9c authored by Micha Mueller's avatar Micha Mueller
Browse files

Add cache of last sensor values

parent 04d588df
......@@ -17,6 +17,11 @@ Sensor::Sensor(const std::string name) :
_latestValue.timestamp = 0;
_latestValue.value = 0;
_cacheIndex = 0;
for(int i = 0; i < CACHE_SIZE; i++) {
_cache[i] = _latestValue;
}
_timer = NULL;
_readingQueue = NULL;
}
......@@ -62,6 +67,10 @@ void Sensor::setInterval(unsigned interval) {
_interval = interval;
}
const reading_t * const Sensor::getCache() const {
return _cache;
}
const std::size_t Sensor::getSizeOfReadingQueue() const {
return _readingQueue->read_available();
}
......@@ -82,3 +91,9 @@ void Sensor::init(boost::asio::io_service& io) {
_timer = new boost::asio::deadline_timer(io, boost::posix_time::seconds(0));
}
}
void Sensor::storeReading(reading_t reading) {
_readingQueue->push(reading);
_cache[_cacheIndex] = reading;
_cacheIndex = (_cacheIndex + 1) % CACHE_SIZE;
}
......@@ -14,6 +14,8 @@
#include "Logging.h"
#define CACHE_SIZE 5
typedef struct {
uint64_t value;
uint64_t timestamp;
......@@ -43,6 +45,8 @@ public:
unsigned getInterval() const;
void setInterval(unsigned interval);
const reading_t * const getCache() const;
const std::size_t getSizeOfReadingQueue() const;
std::size_t popReadingQueue(reading_t *reads, std::size_t max) const;
void pushReadingQueue(reading_t *reads, std::size_t count) const;
......@@ -57,12 +61,16 @@ public:
virtual void stopPolling() = 0;
protected:
void storeReading(reading_t reading);
std::string _name;
std::string _mqtt;
int _keepRunning;
unsigned int _minValues;
unsigned int _interval;
unsigned int _cacheIndex;
reading_t _latestValue;
reading_t _cache[CACHE_SIZE];
boost::asio::deadline_timer* _timer;
boost::lockfree::spsc_queue<reading_t>* _readingQueue;
boost::log::sources::severity_logger<boost::log::trivial::severity_level> lg;
......
......@@ -30,7 +30,7 @@ void BACnetSensor::read() {
try {
reading.value = _bacClient->readProperty(_deviceInstance, _objectInstance, _objectType, _propertyId) * _factor;
_readingQueue->push(reading);
storeReading(reading);
_latestValue.value = reading.value;
_latestValue.timestamp = reading.timestamp;
#ifdef DEBUG
......
......@@ -51,7 +51,7 @@ namespace DCDB {
#ifdef DEBUG
LOG(debug) << _host->getHostName() << "::" << _name << ": \"" << reading.value << "\"";
#endif
_readingQueue->push(reading);
storeReading(reading);
}
void IPMISensor::readAsync() {
......
......@@ -24,7 +24,7 @@ void PDUSensor::read() {
try {
reading.value = _pdu->readValue(_xmlPath);
_readingQueue->push(reading);
storeReading(reading);
_latestValue.value = reading.value;
_latestValue.timestamp = reading.timestamp;
#ifdef DEBUG
......
......@@ -48,7 +48,7 @@ void PerfCounter::read() {
#ifdef DEBUG
LOG(debug) << _name << ": \"" << reading.value << "\"";
#endif
_readingQueue->push(reading);
storeReading(reading);
_latestValue.value = count;
_latestValue.timestamp = reading.timestamp;
}
......
......@@ -56,7 +56,7 @@ void SysfsSensor::read() {
LOG(debug) << _name << ": \"" << reading.value << "\"";
#endif
}
_readingQueue->push(reading);
storeReading(reading);
}
void SysfsSensor::readAsync() {
......
Supports Markdown
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