Commit 9965c8a0 authored by Micha Müller's avatar Micha Müller
Browse files

Pusher WIP: Adapter all SensorGroups to refactored template

parent 5da840d6
...@@ -29,63 +29,28 @@ ...@@ -29,63 +29,28 @@
#include <functional> #include <functional>
BACnetSensorGroup::BACnetSensorGroup(const std::string& name) : SensorGroupTemplate(name), _deviceInstance(0) { BACnetSensorGroup::BACnetSensorGroup(const std::string& name) : SensorGroupTemplate(name), _deviceInstance(0) {
_bacClient = nullptr;
} }
BACnetSensorGroup::BACnetSensorGroup(const BACnetSensorGroup& other) : SensorGroupTemplate(other), BACnetSensorGroup::BACnetSensorGroup(const BACnetSensorGroup& other) : SensorGroupTemplate(other),
_bacClient(other._bacClient),
_deviceInstance(other._deviceInstance) {} _deviceInstance(other._deviceInstance) {}
BACnetSensorGroup::~BACnetSensorGroup() {} BACnetSensorGroup::~BACnetSensorGroup() {}
BACnetSensorGroup& BACnetSensorGroup::operator=(const BACnetSensorGroup& other) { BACnetSensorGroup& BACnetSensorGroup::operator=(const BACnetSensorGroup& other) {
SensorGroupTemplate::operator =(other); SensorGroupTemplate::operator =(other);
_bacClient = other._bacClient;
_deviceInstance = other._deviceInstance; _deviceInstance = other._deviceInstance;
return *this; return *this;
} }
void BACnetSensorGroup::init(boost::asio::io_service& io) {
SensorGroupTemplate::init(io);
if(_bacClient) {
_bacClient->initEntity(io);
} else {
LOG(error) << "No BACnetClient set for sensor " << _groupName << "! Cannot initialize sensor.";
}
}
void BACnetSensorGroup::start() {
if (_keepRunning) {
//we have been started already
LOG(info) << "Sensorgroup " << _groupName << " already running.";
return;
}
if (_bacClient) {
_keepRunning = 1;
_pendingTasks++;
_timer->async_wait(_bacClient->getStrand()->wrap(std::bind(&BACnetSensorGroup::readAsync, this)));
LOG(info) << "Sensorgroup " << _groupName << " started.";
} else {
LOG(error) << "No BACnetClient set for sensorgroup " << _groupName << "! Cannot start polling.";
}
}
void BACnetSensorGroup::stop() {
_keepRunning = 0;
//cancel any outstanding readAsync()
_timer->cancel();
LOG(info) << "Sensorgroup " << _groupName << " stopped.";
}
void BACnetSensorGroup::read() { void BACnetSensorGroup::read() {
reading_t reading; reading_t reading;
reading.timestamp = getTimestamp(); reading.timestamp = getTimestamp();
for(const auto& s : _sensors) { for(const auto& s : _sensors) {
try { try {
reading.value = _bacClient->readProperty(getDeviceInstance(), s->getObjectInstance(), s->getObjectType(), s->getPropertyId()); reading.value = _entity->readProperty(getDeviceInstance(), s->getObjectInstance(), s->getObjectType(), s->getPropertyId());
#ifdef DEBUG #ifdef DEBUG
LOG(debug) << _groupName << "::" << s->getName() << " raw reading: \"" << reading.value << "\""; LOG(debug) << _groupName << "::" << s->getName() << " raw reading: \"" << reading.value << "\"";
#endif #endif
...@@ -98,21 +63,6 @@ void BACnetSensorGroup::read() { ...@@ -98,21 +63,6 @@ void BACnetSensorGroup::read() {
} }
void BACnetSensorGroup::readAsync() {
read();
if (_timer && _keepRunning) {
_timer->expires_at(timestamp2ptime(nextReadingTime()));
_pendingTasks++;
_timer->async_wait(_bacClient->getStrand()->wrap(std::bind(&BACnetSensorGroup::readAsync, this)));
}
_pendingTasks--;
}
void BACnetSensorGroup::printConfig(LOG_LEVEL ll) { void BACnetSensorGroup::printConfig(LOG_LEVEL ll) {
LOG_VAR(ll) << " deviceInstance: " << _deviceInstance; LOG_VAR(ll) << " deviceInstance: " << _deviceInstance;
if (_bacClient) {
LOG_VAR(ll) << " BACClient set";
} else {
LOG_VAR(ll) << " No BACClient set!";
}
} }
...@@ -30,14 +30,12 @@ ...@@ -30,14 +30,12 @@
#include "BACnetSensorBase.h" #include "BACnetSensorBase.h"
#include "../../includes/SensorGroupTemplate.h" #include "../../includes/SensorGroupTemplate.h"
using BACnetClientPtr = std::shared_ptr<BACnetClient>;
/** /**
* @brief SensorGroupTemplate specialization for this plugin. * @brief SensorGroupTemplate specialization for this plugin.
* *
* @ingroup bacnet * @ingroup bacnet
*/ */
class BACnetSensorGroup: public SensorGroupTemplate<BACnetSensorBase> { class BACnetSensorGroup: public SensorGroupTemplate<BACnetSensorBase, BACnetClient> {
public: public:
BACnetSensorGroup(const std::string& name); BACnetSensorGroup(const std::string& name);
...@@ -45,22 +43,14 @@ public: ...@@ -45,22 +43,14 @@ public:
virtual ~BACnetSensorGroup(); virtual ~BACnetSensorGroup();
BACnetSensorGroup& operator=(const BACnetSensorGroup& other); BACnetSensorGroup& operator=(const BACnetSensorGroup& other);
void init(boost::asio::io_service& io) override;
void start() override;
void stop() override;
void setBACnetClient(BACnetClientPtr bacClient) { _bacClient = bacClient; }
BACnetClientPtr const getBACnetClient() const { return _bacClient; }
void setDeviceInstance(const std::string& deviceInstance) { _deviceInstance = stoul(deviceInstance); } void setDeviceInstance(const std::string& deviceInstance) { _deviceInstance = stoul(deviceInstance); }
uint32_t getDeviceInstance() const { return _deviceInstance; } uint32_t getDeviceInstance() const { return _deviceInstance; }
void printConfig(LOG_LEVEL ll) override; void printConfig(LOG_LEVEL ll) final override;
private: private:
void read() override; void read() final override;
void readAsync() override;
BACnetClientPtr _bacClient;
uint32_t _deviceInstance; uint32_t _deviceInstance;
}; };
......
...@@ -45,11 +45,7 @@ CaliperSensorGroup::CaliperSensorGroup(const CaliperSensorGroup& other) : ...@@ -45,11 +45,7 @@ CaliperSensorGroup::CaliperSensorGroup(const CaliperSensorGroup& other) :
_connection(-1) { _connection(-1) {
} }
CaliperSensorGroup::~CaliperSensorGroup() { CaliperSensorGroup::~CaliperSensorGroup() {}
if(_keepRunning) {
stop();
}
}
CaliperSensorGroup& CaliperSensorGroup::operator=(const CaliperSensorGroup& other) { CaliperSensorGroup& CaliperSensorGroup::operator=(const CaliperSensorGroup& other) {
SensorGroupTemplate::operator=(other); SensorGroupTemplate::operator=(other);
...@@ -59,18 +55,12 @@ CaliperSensorGroup& CaliperSensorGroup::operator=(const CaliperSensorGroup& othe ...@@ -59,18 +55,12 @@ CaliperSensorGroup& CaliperSensorGroup::operator=(const CaliperSensorGroup& othe
return *this; return *this;
} }
void CaliperSensorGroup::start() { bool CaliperSensorGroup::execOnStart() {
if (_keepRunning) {
//we have been started already
LOG(info) << "Sensorgroup " << _groupName << " already running.";
return;
}
_socket = socket(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK, 0); _socket = socket(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK, 0);
if(_socket == -1) { if(_socket == -1) {
LOG(error) << _groupName << ": Failed to open socket: " << strerror(errno); LOG(error) << _groupName << ": Failed to open socket: " << strerror(errno);
return; return false;
} }
sockaddr_un addr; sockaddr_un addr;
...@@ -83,28 +73,20 @@ void CaliperSensorGroup::start() { ...@@ -83,28 +73,20 @@ void CaliperSensorGroup::start() {
LOG(error) << _groupName << ": Failed to bind socket: " << strerror(errno); LOG(error) << _groupName << ": Failed to bind socket: " << strerror(errno);
close(_socket); close(_socket);
_socket = -1; _socket = -1;
return; return false;
} }
if(listen(_socket, 1)) { if(listen(_socket, 1)) {
LOG(error) << _groupName << ": Can not listen on socket: " << strerror(errno); LOG(error) << _groupName << ": Can not listen on socket: " << strerror(errno);
close(_socket); close(_socket);
_socket = -1; _socket = -1;
return; return false;
} }
_keepRunning = 1; return true;
_pendingTasks++;
_timer->async_wait(std::bind(&CaliperSensorGroup::readAsync, this));
LOG(info) << "Sensorgroup " << _groupName << " started.";
} }
void CaliperSensorGroup::stop() { void CaliperSensorGroup::execOnStop() {
_keepRunning = 0;
//wait before closing _file
wait();
if(_connection != -1) { if(_connection != -1) {
close(_connection); close(_connection);
_connection = -1; _connection = -1;
...@@ -113,8 +95,6 @@ void CaliperSensorGroup::stop() { ...@@ -113,8 +95,6 @@ void CaliperSensorGroup::stop() {
close(_socket); close(_socket);
_socket = -1; _socket = -1;
} }
LOG(info) << "Sensorgroup " << _groupName << " stopped.";
} }
void CaliperSensorGroup::read() { void CaliperSensorGroup::read() {
...@@ -170,17 +150,6 @@ void CaliperSensorGroup::read() { ...@@ -170,17 +150,6 @@ void CaliperSensorGroup::read() {
}*/ }*/
} }
void CaliperSensorGroup::readAsync() {
uint64_t now = getTimestamp();
read();
if (_timer && _keepRunning) {
_timer->expires_at(timestamp2ptime(nextReadingTime()));
_pendingTasks++;
_timer->async_wait(std::bind(&CaliperSensorGroup::readAsync, this));
}
_pendingTasks--;
}
void CaliperSensorGroup::printConfig(LOG_LEVEL ll) { void CaliperSensorGroup::printConfig(LOG_LEVEL ll) {
} }
...@@ -43,14 +43,13 @@ public: ...@@ -43,14 +43,13 @@ public:
virtual ~CaliperSensorGroup(); virtual ~CaliperSensorGroup();
CaliperSensorGroup& operator=(const CaliperSensorGroup& other); CaliperSensorGroup& operator=(const CaliperSensorGroup& other);
void start() override; bool execOnStart() final override;
void stop() override; void execOnStop() final override;
void printConfig(LOG_LEVEL ll) override; void printConfig(LOG_LEVEL ll) final override;
private: private:
void read() override; void read() override;
void readAsync() override;
int _socket; int _socket;
int _connection; int _connection;
......
...@@ -38,13 +38,13 @@ GpfsmonSensorGroup::GpfsmonSensorGroup(const std::string& name) : ...@@ -38,13 +38,13 @@ GpfsmonSensorGroup::GpfsmonSensorGroup(const std::string& name) :
} }
GpfsmonSensorGroup& GpfsmonSensorGroup::operator=(const GpfsmonSensorGroup& other){ GpfsmonSensorGroup& GpfsmonSensorGroup::operator=(const GpfsmonSensorGroup& other){
SensorGroupTemplate<GpfsmonSensorBase>::operator=(other); SensorGroupTemplate::operator=(other);
//no need to copy _data //no need to copy _data
return *this; return *this;
} }
GpfsmonSensorGroup::GpfsmonSensorGroup(const GpfsmonSensorGroup& other): GpfsmonSensorGroup::GpfsmonSensorGroup(const GpfsmonSensorGroup& other):
SensorGroupTemplate<GpfsmonSensorBase>(other){ SensorGroupTemplate(other){
_data.resize(GPFS_METRIC::SIZE); _data.resize(GPFS_METRIC::SIZE);
if(!fileExists(TMP_GPFSMON)) createTempFile(); if(!fileExists(TMP_GPFSMON)) createTempFile();
} }
...@@ -53,24 +53,6 @@ GpfsmonSensorGroup::~GpfsmonSensorGroup() { ...@@ -53,24 +53,6 @@ GpfsmonSensorGroup::~GpfsmonSensorGroup() {
_data.clear(); _data.clear();
} }
void GpfsmonSensorGroup::start() {
if (_keepRunning) {
//we have been started already
LOG(info) << "Sensorgroup " << _groupName << " already running.";
return;
}
_keepRunning = 1;
_pendingTasks++;
_timer->async_wait(std::bind(&GpfsmonSensorGroup::readAsync, this));
LOG(info) << "Sensorgroup " << _groupName << " started.";
}
void GpfsmonSensorGroup::stop() {
_keepRunning = 0;
LOG(info) << "Sensorgroup " << _groupName << " stopped.";
}
void GpfsmonSensorGroup::read() { void GpfsmonSensorGroup::read() {
ureading_t reading; ureading_t reading;
reading.timestamp = getTimestamp(); reading.timestamp = getTimestamp();
...@@ -102,17 +84,6 @@ void GpfsmonSensorGroup::read() { ...@@ -102,17 +84,6 @@ void GpfsmonSensorGroup::read() {
} }
} }
void GpfsmonSensorGroup::readAsync() {
uint64_t now = getTimestamp();
read();
if (_timer && _keepRunning) {
_timer->expires_at(timestamp2ptime(nextReadingTime()));
_pendingTasks++;
_timer->async_wait(std::bind(&GpfsmonSensorGroup::readAsync, this));
}
_pendingTasks--;
}
void GpfsmonSensorGroup::printConfig(LOG_LEVEL ll) { void GpfsmonSensorGroup::printConfig(LOG_LEVEL ll) {
LOG_VAR(ll) << " No other specific attributes"; LOG_VAR(ll) << " No other specific attributes";
} }
...@@ -128,8 +99,7 @@ void GpfsmonSensorGroup::createTempFile(){ ...@@ -128,8 +99,7 @@ void GpfsmonSensorGroup::createTempFile(){
} }
} }
void GpfsmonSensorGroup::init(boost::asio::io_service& io) { void GpfsmonSensorGroup::execOnInit() {
SensorGroupTemplate::init(io);
if(!fileExists(TMP_GPFSMON)) createTempFile(); if(!fileExists(TMP_GPFSMON)) createTempFile();
_data.resize(GPFS_METRIC::SIZE); _data.resize(GPFS_METRIC::SIZE);
} }
......
...@@ -43,18 +43,15 @@ public: ...@@ -43,18 +43,15 @@ public:
GpfsmonSensorGroup(const std::string& name); GpfsmonSensorGroup(const std::string& name);
virtual ~GpfsmonSensorGroup(); virtual ~GpfsmonSensorGroup();
void init(boost::asio::io_service& io) override; void execOnInit() final override;
void start() override;
void stop() override;
GpfsmonSensorGroup& operator=(const GpfsmonSensorGroup& other); GpfsmonSensorGroup& operator=(const GpfsmonSensorGroup& other);
GpfsmonSensorGroup(const GpfsmonSensorGroup& other); GpfsmonSensorGroup(const GpfsmonSensorGroup& other);
void printConfig(LOG_LEVEL ll) override; void printConfig(LOG_LEVEL ll) final override;
private: private:
void read() override; void read() final override;
void readAsync() override;
bool fileExists(const char* filename); bool fileExists(const char* filename);
void createTempFile(); void createTempFile();
......
...@@ -299,6 +299,7 @@ double IPMIHost::readSensorRecord(std::vector<uint8_t>& record) { ...@@ -299,6 +299,7 @@ double IPMIHost::readSensorRecord(std::vector<uint8_t>& record) {
double ret = .0; double ret = .0;
if (success && reading) { if (success && reading) {
//TODO should _delayNextReadUntil be reset here?
_errorCount = 0; _errorCount = 0;
ret = *reading; ret = *reading;
free(reading); free(reading);
......
...@@ -39,71 +39,44 @@ ...@@ -39,71 +39,44 @@
IPMISensorGroup::IPMISensorGroup(const std::string& name) : IPMISensorGroup::IPMISensorGroup(const std::string& name) :
SensorGroupTemplate(name) { SensorGroupTemplate(name) {
_host = nullptr;
} }
IPMISensorGroup::IPMISensorGroup(const IPMISensorGroup& other) : IPMISensorGroup::IPMISensorGroup(const IPMISensorGroup& other) :
SensorGroupTemplate(other), SensorGroupTemplate(other) {
_host(other._host) {}
}
IPMISensorGroup::~IPMISensorGroup() {} IPMISensorGroup::~IPMISensorGroup() {}
IPMISensorGroup& IPMISensorGroup::operator=(const IPMISensorGroup& other) { IPMISensorGroup& IPMISensorGroup::operator=(const IPMISensorGroup& other) {
SensorGroupTemplate::operator=(other); SensorGroupTemplate::operator=(other);
_host = other._host;
return *this; return *this;
} }
void IPMISensorGroup::init(boost::asio::io_service& io) {
SensorGroupTemplate::init(io);
if (_host) {
_host->initEntity(io);
} else {
LOG(error)<< "No host set for sensorgroup " << _groupName << "! Cannot initialize sensor.";
}
}
void IPMISensorGroup::start() {
if (_keepRunning) {
//we have been started already
LOG(info)<< "Sensorgroup " << _groupName << " already running.";
return;
}
if (_host) {
_keepRunning = 1;
_pendingTasks++;
_timer->async_wait(_host->getStrand()->wrap(boost::bind(&IPMISensorGroup::readAsync, this)));
LOG(info) << "Sensorgroup " << _groupName << " started.";
} else {
LOG(error) << "No host set for sensorgroup " << _groupName << "! Cannot start polling.";
return;
}
}
void IPMISensorGroup::stop() {
_keepRunning = 0;
//cancel any outstanding readAsync()
_timer->cancel();
LOG(info)<< "Sensorgroup " << _groupName << " stopped.";
}
void IPMISensorGroup::read() { void IPMISensorGroup::read() {
reading_t reading; reading_t reading;
reading.timestamp = getTimestamp(); reading.timestamp = getTimestamp();
//TODO with SensorGroup refactor we lost the ability to sleep until
// delayNextRead. Instead we are now checking every time if we can read
// again. Overhead acceptable? General delayNextReadUntil implementation in
// SensorGroupTemplate required?
if (reading.timestamp < _entity->getDelayNextReadUntil()) {
return;
}
for(const auto& s : _sensors) { for(const auto& s : _sensors) {
try { try {
if (s->getRecordId() != 0) { /* recordId was set */ if (s->getRecordId() != 0) { /* recordId was set */
std::vector<uint8_t> sdrRecord = s->getSdrRecord(); std::vector<uint8_t> sdrRecord = s->getSdrRecord();
if (sdrRecord.size() == 0) { if (sdrRecord.size() == 0) {
_host->getSdrRecord(s->getRecordId(), sdrRecord); _entity->getSdrRecord(s->getRecordId(), sdrRecord);
s->setSdrRecord(sdrRecord); s->setSdrRecord(sdrRecord);
} }
reading.value = _host->readSensorRecord(sdrRecord); reading.value = _entity->readSensorRecord(sdrRecord);
} else { /* use raw command */ } else { /* use raw command */
reading.value = _host->sendRawCmd(s->getRawCmd(), s->getLsb(), s->getMsb()); reading.value = _entity->sendRawCmd(s->getRawCmd(), s->getLsb(), s->getMsb());
} }
#ifdef DEBUG #ifdef DEBUG
LOG(debug) << _groupName << "::" << s->getName() << " raw reading: \"" << reading.value << "\""; LOG(debug) << _groupName << "::" << s->getName() << " raw reading: \"" << reading.value << "\"";
...@@ -116,27 +89,6 @@ void IPMISensorGroup::read() { ...@@ -116,27 +89,6 @@ void IPMISensorGroup::read() {
} }
} }
void IPMISensorGroup::readAsync() {
uint64_t now = getTimestamp();
if (now >= _host->getDelayNextReadUntil()) {
read();
}
if (_timer && _keepRunning) {
uint64_t next = nextReadingTime();
while (next < _host->getDelayNextReadUntil()) {
next += MS_TO_NS(_interval);
}
_timer->expires_at(timestamp2ptime(next));
_pendingTasks++;
_timer->async_wait(_host->getStrand()->wrap(boost::bind(&IPMISensorGroup::readAsync, this)));
}
_pendingTasks--;
}
void IPMISensorGroup::printConfig(LOG_LEVEL ll) { void IPMISensorGroup::printConfig(LOG_LEVEL ll) {
if (_host) {