Commit ff30abeb authored by Micha Mueller's avatar Micha Mueller
Browse files

Add option to synchronize reading of sensors (enabled by default)

parent 60876662
......@@ -381,6 +381,12 @@ protected:
sGroup.setMinValues(stoull(val.second.data()));
} else if (boost::iequals(val.first, "mqttPart")) {
sGroup.setMqttPart(val.second.data());
} else if (boost::iequals(val.first, "sync")) {
if (val.second.data() == "off") {
sGroup.setSync(false);
} else {
sGroup.setSync(true);
}
} else if (boost::iequals(val.first, _baseName)) {
LOG(debug) << " " << _baseName << " " << val.second.data();
SBase* sensor = new SBase(val.second.data());
......
......@@ -20,6 +20,7 @@ public:
SensorGroupInterface(const std::string& groupName) :
_groupName(groupName),
_mqttPart(""),
_sync(true),
_keepRunning(0),
_minValues(1),
_interval(1000),
......@@ -32,6 +33,7 @@ public:
SensorGroupInterface(const SensorGroupInterface& other) :
_groupName(other._groupName),
_mqttPart(other._mqttPart),
_sync(other._sync),
_keepRunning(other._keepRunning),
_minValues(other._minValues),
_interval(other._interval),
......@@ -47,6 +49,7 @@ public:
SensorGroupInterface& operator=(const SensorGroupInterface& other) {
_groupName = other._groupName;
_mqttPart = other._mqttPart;
_sync = other._sync;
_keepRunning = other._keepRunning;
_minValues = other._minValues;
_interval = other._interval;
......@@ -61,12 +64,14 @@ public:
const std::string& getGroupName() const { return _groupName; }
const std::string& getMqttPart() const { return _mqttPart; }
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; }
......@@ -99,6 +104,7 @@ protected:
std::string _groupName;
std::string _mqttPart;
bool _sync;
int _keepRunning;
unsigned int _minValues;
unsigned int _interval;
......
......@@ -8,9 +8,12 @@
#ifndef SENSORGROUPTEMPLATE_H_
#define SENSORGROUPTEMPLATE_H_
#include <vector>
#include "SensorGroupInterface.h"
#include "timestamp.h"
#include <vector>
template <typename S>
class SensorGroupTemplate : public SensorGroupInterface {
//the template shall only be instantiated for classes which derive from SensorBase
......@@ -72,6 +75,25 @@ public:
}
protected:
/** Calculate timestamp for the next reading
* @return Timestamp in the future to wait for
*/
uint64_t nextReadingTime() {
uint64_t now = getTimestamp();
uint64_t next;
if (_sync) {
uint64_t interval64 = static_cast<uint64_t>(_interval);
uint64_t now_ms = now / 1000 / 1000;
uint64_t waitToStart = interval64 - (now_ms%interval64); //synchronize all measurements with other sensors
if(!waitToStart ){ // less than 1 ms seconds is too small, so we wait the entire interval for the next measurement
return (now_ms + interval64)*1000*1000;
}
return (now_ms + waitToStart)*1000*1000;
} else {
return now + MS_TO_NS(_interval);
}
}
std::vector<S*> _sensors;
std::vector<SensorBase*> _baseSensors;
};
......
......@@ -5,7 +5,6 @@
* Author: Micha Mueller
*/
#include "timestamp.h"
#include "BACnetSensorGroup.h"
#include <functional>
......@@ -82,11 +81,9 @@ void BACnetSensorGroup::read() {
}
void BACnetSensorGroup::readAsync() {
uint64_t now = getTimestamp();
read();
if (_timer && _keepRunning) {
uint64_t next = now + MS_TO_NS(_interval);
_timer->expires_at(timestamp2ptime(next));
_timer->expires_at(timestamp2ptime(nextReadingTime()));
_pendingTasks++;
_timer->async_wait(_bacClient->getStrand()->wrap(std::bind(&BACnetSensorGroup::readAsync, this)));
}
......
......@@ -5,7 +5,6 @@
* Author: Michael Ott (original), Micha Müller
*/
#include "timestamp.h"
#include "IPMISensorGroup.h"
#include "IPMIHost.h"
......@@ -95,7 +94,7 @@ void IPMISensorGroup::readAsync() {
read();
}
if (_timer && _keepRunning) {
uint64_t next = now + MS_TO_NS(_interval);
uint64_t next = nextReadingTime();
while (next < _host->getDelayNextReadUntil()) {
next += MS_TO_NS(_interval);
}
......
......@@ -7,8 +7,6 @@
#include "OpaSensorGroup.h"
#include "timestamp.h"
OpaSensorGroup::OpaSensorGroup(const std::string name) :
SensorGroupTemplate(name),
_hfiNum(0),
......@@ -173,11 +171,9 @@ void OpaSensorGroup::read() {
}
void OpaSensorGroup::readAsync() {
uint64_t now = getTimestamp();
read();
if (_timer && _keepRunning) {
uint64_t next = now + MS_TO_NS(_interval);
_timer->expires_at(timestamp2ptime(next));
_timer->expires_at(timestamp2ptime(nextReadingTime()));
_pendingTasks++;
_timer->async_wait(std::bind(&OpaSensorGroup::readAsync, this));
}
......
......@@ -6,7 +6,6 @@
*/
#include "PDUSensorGroup.h"
#include "timestamp.h"
#include <sstream>
......@@ -131,11 +130,9 @@ void PDUSensorGroup::read() {
}
void PDUSensorGroup::readAsync() {
uint64_t now = getTimestamp();
read();
if (_timer && _keepRunning) {
uint64_t next = now + MS_TO_NS(_interval);
_timer->expires_at(timestamp2ptime(next));
_timer->expires_at(timestamp2ptime(nextReadingTime()));
_pendingTasks++;
_timer->async_wait(_pdu->getStrand()->wrap(std::bind(&PDUSensorGroup::readAsync, this)));
}
......
......@@ -7,7 +7,6 @@
#include "PerfSensorGroup.h"
#include "timestamp.h"
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/perf_event.h>
......@@ -164,11 +163,9 @@ void PerfSensorGroup::read() {
}
void PerfSensorGroup::readAsync() {
uint64_t now = getTimestamp();
read();
if (_timer && _keepRunning) {
uint64_t next = now + MS_TO_NS(_interval);
_timer->expires_at(timestamp2ptime(next));
_timer->expires_at(timestamp2ptime(nextReadingTime()));
_pendingTasks++;
_timer->async_wait(std::bind(&PerfSensorGroup::readAsync, this));
}
......
......@@ -94,11 +94,9 @@ void ProcfsSensorGroup::read() {
*
*/
void ProcfsSensorGroup::readAsync() {
uint64_t now = getTimestamp();
read();
if (_timer && _keepRunning) {
uint64_t next = now + MS_TO_NS(_interval);
_timer->expires_at(timestamp2ptime(next));
_timer->expires_at(timestamp2ptime(nextReadingTime()));
_pendingTasks++;
_timer->async_wait(std::bind(&ProcfsSensorGroup::readAsync, this));
}
......
......@@ -5,7 +5,6 @@
* Author: Axel Auweter (original), Micha Mueller
*/
#include "timestamp.h"
#include "SNMPSensorGroup.h"
SNMPSensorGroup::SNMPSensorGroup(const std::string& name) :
......@@ -69,11 +68,9 @@ void SNMPSensorGroup::read() {
}
void SNMPSensorGroup::readAsync() {
uint64_t now = getTimestamp();
read();
if (_timer && _keepRunning) {
uint64_t next = now + MS_TO_NS(_interval);
_timer->expires_at(timestamp2ptime(next));
_timer->expires_at(timestamp2ptime(nextReadingTime()));
_pendingTasks++;
_timer->async_wait(_connection->getStrand()->wrap(std::bind(&SNMPSensorGroup::readAsync, this)));
}
......
......@@ -5,7 +5,6 @@
* Author: Michael Ott (original), Micha Mueller
*/
#include "timestamp.h"
#include <functional>
#include "SysfsSensorGroup.h"
......@@ -91,11 +90,9 @@ void SysfsSensorGroup::read() {
}
void SysfsSensorGroup::readAsync() {
uint64_t now = getTimestamp();
read();
if (_timer && _keepRunning) {
uint64_t next = now + MS_TO_NS(_interval);
_timer->expires_at(timestamp2ptime(next));
_timer->expires_at(timestamp2ptime(nextReadingTime()));
_pendingTasks++;
_timer->async_wait(std::bind(&SysfsSensorGroup::readAsync, this));
}
......
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