Commit 6be3b8de authored by Michael Ott's avatar Michael Ott
Browse files

Add SensorCache class to store latest sample of a sensor reading

parent 1978fdb2
...@@ -2,6 +2,7 @@ include ../config.mk ...@@ -2,6 +2,7 @@ include ../config.mk
CXXFLAGS = -O2 -g --std=c++11 -Wall -Wno-unused-local-typedefs -Wno-deprecated-declarations -Wno-unknown-warning-option -fmessage-length=0 -I$(DCDBDEPLOYPATH)/include/ -I$(DCDBBASEPATH)/include/ -DBOOST_DATE_TIME_POSIX_TIME_STD_CONFIG CXXFLAGS = -O2 -g --std=c++11 -Wall -Wno-unused-local-typedefs -Wno-deprecated-declarations -Wno-unknown-warning-option -fmessage-length=0 -I$(DCDBDEPLOYPATH)/include/ -I$(DCDBBASEPATH)/include/ -DBOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
OBJS = collectagent.o \ OBJS = collectagent.o \
sensorcache.o \
simplemqttserver.o \ simplemqttserver.o \
simplemqttserverthread.o \ simplemqttserverthread.o \
simplemqttservermessage.o simplemqttservermessage.o
......
/*
* SensorCache.cpp
*
* Created on: 3 Aug 2016
* Author: ottmi
*/
#include "sensorcache.h"
#include <exception>
#include <iostream>
namespace DCDB {
SensorCache::SensorCache() {
// TODO Auto-generated constructor stub
}
SensorCache::~SensorCache() {
// TODO Auto-generated destructor stub
}
void SensorCache::storeSensor(SensorId sid, uint64_t ts, uint64_t val) {
cacheEntry_t e(val, ts);
/* Remove the reserved bytes to leverage the standard find function */
sid.setRsvd(0);
sensorCache[sid] = e;
}
uint64_t SensorCache::getSensor(SensorId sid) {
/* Remove the reserved bytes to leverage the standard find function */
sid.setRsvd(0);
sensorCache_t::iterator it = sensorCache.find(sid);
if (it == sensorCache.end()) {
throw std::out_of_range("Sid not found");
}
return it->second.first;
}
void SensorCache::dump() {
sensorCache_t::iterator it;
std::cout << "SensorCache Dump:" << std::endl;
for (it = sensorCache.begin(); it != sensorCache.end(); it++) {
std::cout << " id=" << it->first.toString() << " val=" << it->second.first << " ts=" << it->second.second << std::endl;
}
}
} /* namespace DCDB */
/*
* SensorCache.h
*
* Created on: 3 Aug 2016
* Author: ottmi
*/
#ifndef COLLECTAGENT_SENSORCACHE_H_
#define COLLECTAGENT_SENSORCACHE_H_
#include <map>
#include <utility>
#include <dcdb/sensorid.h>
namespace DCDB {
typedef std::pair<uint64_t, uint64_t> cacheEntry_t;
typedef std::map<SensorId, cacheEntry_t> sensorCache_t;
class SensorCache {
public:
SensorCache();
virtual ~SensorCache();
void storeSensor(SensorId sid, uint64_t ts, uint64_t val);
uint64_t getSensor(SensorId sid);
void dump();
private:
sensorCache_t sensorCache;
};
} /* namespace DCDB */
#endif /* COLLECTAGENT_SENSORCACHE_H_ */
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