Commit 8e57b63d authored by Micha Mueller's avatar Micha Mueller
Browse files

Move all cache related attributes to SensorBase

parent 73fa41e2
......@@ -196,7 +196,7 @@ void HttpsServer::requestHandler::operator()(server::request const &request, ser
for(const auto& g : p.configurator->getSensorGroups()) {
for(const auto& s : g->getSensors()) {
if (s->getName() == sensor) {
response = pathStrs[0] + "::" + sensor + _httpsServer.calcAvg(*s, g->getCacheSize(), time);
response = pathStrs[0] + "::" + sensor + _httpsServer.calcAvg(*s, s->getCacheSize(), time);
connection->set_status(server::connection::ok);
break;
}
......
......@@ -325,6 +325,7 @@ protected:
* @return True on success, false otherwise
*/
bool readSensorBase(SBase& sBase, CFG_VAL config) {
sBase.setCacheInterval(_cacheInterval);
boost::optional<boost::property_tree::iptree&> def = config.get_child_optional("default");
if(def) {
//we copy all values from default (including copy constructing its sensors)
......@@ -362,7 +363,6 @@ protected:
* @return True on success, false otherwise
*/
bool readSensorGroup(SGroup& sGroup, CFG_VAL config) {
sGroup.setCacheInterval(_cacheInterval);
//first check if default group is given
boost::optional<boost::property_tree::iptree&> def = config.get_child_optional("default");
if(def) {
......
......@@ -25,6 +25,8 @@ public:
SensorBase(const std::string& name) :
_name(name),
_mqtt(""),
_cacheInterval(900000),
_cacheSize(1),
_cacheIndex(0),
_cache(nullptr),
_delta(false),
......@@ -39,6 +41,8 @@ public:
SensorBase(const SensorBase& other) :
_name(other._name),
_mqtt(other._mqtt),
_cacheInterval(other._cacheInterval),
_cacheSize(other._cacheSize),
_cacheIndex(0),
_cache(nullptr),
_delta(other._delta),
......@@ -51,6 +55,8 @@ public:
SensorBase& operator=(const SensorBase& other) {
_name = other._name;
_mqtt = other._mqtt;
_cacheInterval = other._cacheInterval;
_cacheSize = other._cacheSize;
_cacheIndex = 0;
_cache.reset(nullptr);
_delta = other._delta;
......@@ -66,21 +72,25 @@ public:
const bool isDelta() const { return _delta;}
const std::string& getName() const { return _name; }
const std::string& getMqtt() const { return _mqtt; }
unsigned getCacheSize() const { return _cacheSize; }
const reading_t * const getCache() const { return _cache.get(); }
const reading_t getLatestValue() const { return _latestValue; } /*TODO return reference*/
void setDelta(const bool delta) { _delta = delta; }
void setName(const std::string& name, int cpuID=-1) { _name = formatName(name, cpuID); }
void setMqtt(const std::string& mqtt) { _mqtt = mqtt; }
void setDelta(const bool delta) { _delta = delta; }
void setName(const std::string& name, int cpuID=-1) { _name = formatName(name, cpuID); }
void setMqtt(const std::string& mqtt) { _mqtt = mqtt; }
void setCacheInterval(unsigned cacheInterval) { _cacheInterval = cacheInterval; }
const std::size_t getSizeOfReadingQueue() const { return _readingQueue->read_available(); }
std::size_t popReadingQueue(reading_t *reads, std::size_t max) const { return _readingQueue->pop(reads, max); }
void pushReadingQueue(reading_t *reads, std::size_t count) const { _readingQueue->push(reads, count); }
void initSensor(unsigned cacheSize) {
void initSensor(unsigned interval) {
_cacheSize = _cacheInterval / interval + 1;
if(!_cache) {
_cache.reset(new reading_t[cacheSize]);
for(unsigned i = 0; i < cacheSize; i++) {
_cache.reset(new reading_t[_cacheSize]);
for(unsigned i = 0; i < _cacheSize; i++) {
_cache[i] = _latestValue; //_latestValue should equal (0,0) at this point
}
}
......@@ -89,7 +99,7 @@ public:
}
}
virtual void storeReading(reading_t reading, unsigned int cacheSize, unsigned long long maxValue=ULLONG_MAX) {
virtual void storeReading(reading_t reading, unsigned long long maxValue=ULLONG_MAX) {
_latestValue.timestamp = reading.timestamp;
if( _delta ) {
if (reading.value < _lastRawValue.value)
......@@ -103,7 +113,7 @@ public:
_readingQueue->push(_latestValue);
_cache[_cacheIndex] = _latestValue;
_cacheIndex = (_cacheIndex + 1) % cacheSize;
_cacheIndex = (_cacheIndex + 1) % _cacheSize;
}
static std::string formatName(const std::string& name, int cpuID=-1) {return cpuID<0 ? name : "cpu" + std::to_string(cpuID) + "." + name;}
......@@ -112,6 +122,8 @@ protected:
std::string _name;
std::string _mqtt;
unsigned int _cacheInterval;
unsigned int _cacheSize;
unsigned int _cacheIndex;
std::unique_ptr<reading_t[]> _cache;
bool _delta;
......
......@@ -25,8 +25,6 @@ public:
_keepRunning(0),
_minValues(1),
_interval(1000),
_cacheInterval(900000),
_cacheSize(1),
_pendingTasks(0),
_timer(nullptr) {}
......@@ -37,8 +35,6 @@ public:
_keepRunning(other._keepRunning),
_minValues(other._minValues),
_interval(other._interval),
_cacheInterval(other._cacheInterval),
_cacheSize(other._cacheSize),
_timer(nullptr) {
_pendingTasks.store(other._pendingTasks.load());
}
......@@ -52,8 +48,6 @@ public:
_keepRunning = other._keepRunning;
_minValues = other._minValues;
_interval = other._interval;
_cacheInterval = other._cacheInterval;
_cacheSize = other._cacheSize;
_pendingTasks.store(other._pendingTasks.load());
_timer = nullptr;
......@@ -65,14 +59,12 @@ public:
bool getSync() const { return _sync; }
unsigned getMinValues() const { return _minValues; }
unsigned getInterval() const { return _interval; }
unsigned getCacheSize() const { return _cacheSize; }
void setGroupName(const std::string& groupName) { _groupName = groupName; }
void setMqttPart(const std::string& mqttPart) { _mqttPart = mqttPart; }
void setSync(bool sync) { _sync = sync; }
void setMinValues(unsigned minValues) { _minValues = minValues; }
void setInterval(unsigned interval) { _interval = interval; }
void setCacheInterval(unsigned cacheInterval) { _cacheInterval = cacheInterval; }
/**
* Does a busy wait until all dispatched handlers are finished (_pendingTasks == 0)
......@@ -85,7 +77,6 @@ public:
//can be overwritten
virtual void init(boost::asio::io_service& io) {
_cacheSize = _cacheInterval / _interval + 1;
_timer.reset(new boost::asio::deadline_timer(io, boost::posix_time::seconds(0)));
}
......@@ -106,8 +97,6 @@ protected:
int _keepRunning;
unsigned int _minValues;
unsigned int _interval;
unsigned int _cacheInterval;
unsigned int _cacheSize;
std::atomic_uint _pendingTasks;
std::unique_ptr<boost::asio::deadline_timer> _timer;
boost::log::sources::severity_logger<boost::log::trivial::severity_level> lg;
......
......@@ -71,7 +71,7 @@ public:
SensorGroupInterface::init(io);
for(auto s : _sensors) {
s->initSensor(_cacheSize);
s->initSensor(_interval);
}
}
......
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