Commit 6f4c2e4f authored by Micha Mueller's avatar Micha Mueller
Browse files

Add stopPolling() functionality to all sensor classes

parent 27eaa1a5
......@@ -9,8 +9,6 @@
#include "timestamp.h"
#include <functional>
extern volatile int keepRunning;
BACnetSensor::BACnetSensor(const std::string& name) :
Sensor(name) {
_deviceInstance = 0;
......@@ -45,25 +43,35 @@ void BACnetSensor::read() {
void BACnetSensor::readAsync() {
uint64_t now = getTimestamp();
read();
if (_timer != NULL && keepRunning) {
if (_timer != NULL && _keepRunning) {
uint64_t next = now + MS_TO_NS(_interval);
_timer->expires_at(timestamp2ptime(next));
_timer->async_wait(_bacClient->getStrand()->wrap(std::bind(&BACnetSensor::readAsync, this)));
}
}
void BACnetSensor::startPolling(boost::asio::io_service& io) {
if (!_readingQueue) {
_readingQueue = new boost::lockfree::spsc_queue<reading_t>(1024);
}
void BACnetSensor::init(boost::asio::io_service& io) {
if(_bacClient) {
_bacClient->initializeStrand(io);
} else {
LOG(error) << "No BACnetClient set for sensor " << _name << "! Cannot initialize sensor.";
}
Sensor::init(io);
}
void BACnetSensor::startPolling() {
if (_bacClient) {
_bacClient->initializeStrand(io);
_timer = new boost::asio::deadline_timer(io, boost::posix_time::seconds(0));
_keepRunning = 1;
_timer->async_wait(_bacClient->getStrand()->wrap(std::bind(&BACnetSensor::readAsync, this)));
LOG(info) << "Sensor " << _name << " started.";
} else {
LOG(error) << "No BACnetClient set for sensor " << _name << "! Cannot start polling.";
}
}
void BACnetSensor::stopPolling() {
_keepRunning = 0;
//cancel any outstanding readAsync()
_timer->cancel();
LOG(info) << "Sensor " << _name << " stopped.";
}
......@@ -69,11 +69,15 @@ public:
}
/**** END GETTERS/SETTER ****/
void init(boost::asio::io_service& io);
void read();
void readAsync();
void startPolling(boost::asio::io_service& io);
void startPolling();
void stopPolling();
private:
uint32_t _deviceInstance;
......
......@@ -19,8 +19,6 @@
#include <exception>
#include <chrono>
extern volatile int keepRunning;
namespace DCDB {
IPMISensor::IPMISensor(const std::string& name) :
......@@ -33,7 +31,7 @@ namespace DCDB {
}
IPMISensor::~IPMISensor() {
// TODO Auto-generated destructor stub
}
void IPMISensor::read() {
......@@ -61,7 +59,7 @@ namespace DCDB {
if (now >= _host->getDelayNextReadUntil()) {
read();
}
if (_timer != NULL && keepRunning) {
if (_timer != NULL && _keepRunning) {
uint64_t next = now + MS_TO_NS(_interval);
while (next < _host->getDelayNextReadUntil()) {
next+= MS_TO_NS(_interval);
......@@ -71,18 +69,31 @@ namespace DCDB {
}
}
void IPMISensor::startPolling(boost::asio::io_service& io) {
void IPMISensor::init(boost::asio::io_service& io) {
if(_host) {
_host->initializeStrand(io);
} else {
LOG(error) << "No host set for sensor " << _name << "! Cannot initialize sensor.";
}
Sensor::init(io);
}
void IPMISensor::startPolling() {
if (_host) {
_host->initalizeStrand(io);
_keepRunning = 1;
_timer->async_wait(_host->getStrand()->wrap(boost::bind(&IPMISensor::readAsync, this)));
LOG(info) << "Sensor " << _name << " started.";
} else {
LOG(error) << "No host set for sensor " << _name << "! Cannot start polling.";
return;
}
if (!_readingQueue) {
_readingQueue = new boost::lockfree::spsc_queue<reading_t>(1024);
}
_timer = new boost::asio::deadline_timer(io, boost::posix_time::seconds(0));
_timer->async_wait(_host->getStrand()->wrap(boost::bind(&IPMISensor::readAsync, this)));
}
void IPMISensor::stopPolling() {
_keepRunning = 0;
//cancel any outstanding readAsync()
_timer->cancel();
LOG(info) << "Sensor " << _name << " stopped.";
}
void IPMISensor::setRawCmd(std::string& rawCmd) {
......
......@@ -66,11 +66,15 @@ namespace DCDB {
_host = host;
}
void init(boost::asio::io_service& io);
void read();
void readAsync();
void startPolling(boost::asio::io_service& io);
void startPolling();
void stopPolling();
private:
uint16_t _recordId;
......
......@@ -9,8 +9,6 @@
#include "timestamp.h"
#include <string>
extern volatile int keepRunning;
PDUSensor::PDUSensor(const std::string& name) :
Sensor(name) {
_pdu = NULL;
......@@ -40,23 +38,35 @@ void PDUSensor::read() {
void PDUSensor::readAsync() {
uint64_t now = getTimestamp();
read();
if (_timer != NULL && keepRunning) {
if (_timer != NULL && _keepRunning) {
uint64_t next = now + MS_TO_NS(_interval);
_timer->expires_at(timestamp2ptime(next));
_timer->async_wait(_pdu->getStrand()->wrap(std::bind(&PDUSensor::readAsync, this)));
}
}
void PDUSensor::startPolling(boost::asio::io_service& io) {
if(!_readingQueue) {
_readingQueue = new boost::lockfree::spsc_queue<reading_t>(1024);
void PDUSensor::init(boost::asio::io_service& io) {
if(_pdu) {
_pdu->initializeStrand(io);
} else {
LOG(error) << "No PDUUnit set for sensor " << _name << "! Cannot initialize sensor.";
}
Sensor::init(io);
}
void PDUSensor::startPolling() {
if (_pdu) {
_pdu->initializeStrand(io);
_timer = new boost::asio::deadline_timer(io, boost::posix_time::seconds(0));
_keepRunning = 1;
_timer->async_wait(_pdu->getStrand()->wrap(std::bind(&PDUSensor::readAsync, this)));
LOG(info) << "Sensor " << _name << " started.";
} else {
LOG(error) << "No PDUUnit set for sensor " << _name << "! Cannot start polling.";
}
}
void PDUSensor::stopPolling() {
_keepRunning = 0;
//cancel any outstanding readAsync()
_timer->cancel();
LOG(info) << "Sensor " << _name << " stopped.";
}
......@@ -25,9 +25,12 @@ public:
return _pdu;
}
void init(boost::asio::io_service& io);
void read();
void readAsync();
void startPolling(boost::asio::io_service& io);
void startPolling();
void stopPolling();
xmlPathVector_t _xmlPath;
......
......@@ -15,20 +15,16 @@
#include <functional>
#include <limits.h>
extern volatile int keepRunning;
PerfCounter::PerfCounter(const std::string& name) :
Sensor(name) {
_cpuId = 0;
_type = 0;
_config = 0;
_fd = -1;
_fd = -1;
}
PerfCounter::~PerfCounter() {
if(_fd != -1) {
close(_fd);
}
// TODO Auto-generated destructor stub
}
void PerfCounter::read() {
......@@ -39,6 +35,7 @@ void PerfCounter::read() {
if (::read(_fd, &count, sizeof(long long)) < 0) {
LOG(error) << _name << " could not read value";
return;
}
if (count >= _latestValue.value) {
reading.value = count - _latestValue.value;
......@@ -59,18 +56,14 @@ void PerfCounter::read() {
void PerfCounter::readAsync() {
uint64_t now = getTimestamp();
read();
if (_timer != NULL && keepRunning) {
if (_timer != NULL && _keepRunning) {
uint64_t next = now + MS_TO_NS(_interval);
_timer->expires_at(timestamp2ptime(next));
_timer->async_wait(std::bind(&PerfCounter::readAsync, this));
}
}
void PerfCounter::startPolling(boost::asio::io_service& io) {
if (!_readingQueue) {
_readingQueue = new boost::lockfree::spsc_queue<reading_t>(1024);
}
void PerfCounter::startPolling() {
//open perf-counter
struct perf_event_attr pe;
......@@ -93,6 +86,18 @@ void PerfCounter::startPolling(boost::asio::io_service& io) {
ioctl(_fd, PERF_EVENT_IOC_RESET, 0);
ioctl(_fd, PERF_EVENT_IOC_ENABLE, 0);
_timer = new boost::asio::deadline_timer(io, boost::posix_time::seconds(0));
_keepRunning = 1;
_timer->async_wait(std::bind(&PerfCounter::readAsync, this));
LOG(info) << "Sensor " << _name << " started.";
}
void PerfCounter::stopPolling() {
_keepRunning = 0;
//cancel any outstanding readAsync()
_timer->cancel();
if(_fd != -1) {
close(_fd);
_fd = -1;
}
LOG(info) << "Sensor " << _name << " stopped.";
}
......@@ -8,8 +8,6 @@
#ifndef PERFCOUNTER_H_
#define PERFCOUNTER_H_
//#define DEBUG
#include "../../Sensor.h"
class PerfCounter : public Sensor{
......@@ -46,7 +44,9 @@ public:
void readAsync();
void startPolling(boost::asio::io_service& io);
void startPolling();
void stopPolling();
private:
int _cpuId;
......
......@@ -9,8 +9,6 @@
#include "timestamp.h"
#include <functional>
extern volatile int keepRunning;
using namespace std;
SysfsSensor::SysfsSensor(const std::string& name) :
......@@ -23,10 +21,7 @@ SysfsSensor::SysfsSensor(const std::string& name) :
}
SysfsSensor::~SysfsSensor() {
if (_file != NULL) {
fclose(_file);
_file = NULL;
}
// TODO Auto-generated destructor stub
}
void SysfsSensor::read() {
......@@ -67,24 +62,36 @@ void SysfsSensor::read() {
void SysfsSensor::readAsync() {
uint64_t now = getTimestamp();
read();
if (_timer != NULL && keepRunning) {
if (_timer != NULL && _keepRunning) {
uint64_t next = now + MS_TO_NS(_interval);
_timer->expires_at(timestamp2ptime(next));
_timer->async_wait(std::bind(&SysfsSensor::readAsync, this));
}
}
void SysfsSensor::startPolling(boost::asio::io_service& io) {
if(!_readingQueue) {
_readingQueue = new boost::lockfree::spsc_queue<reading_t>(1024);
}
void SysfsSensor::startPolling() {
if(_file == NULL) {
_file = fopen(_path.c_str(), "r");
if (_file == NULL) {
LOG(error) << "Error opening sensor \"" << _name << "\": " << strerror(errno);
LOG(error) << "Error starting sensor \"" << _name << "\": " << strerror(errno);
return;
}
}
_timer = new boost::asio::deadline_timer(io, boost::posix_time::seconds(0));
_keepRunning = 1;
_timer->async_wait(std::bind(&SysfsSensor::readAsync, this));
LOG(info) << "Sensor " << _name << " started.";
}
void SysfsSensor::stopPolling() {
_keepRunning = 0;
//cancel any outstanding readAsync()
_timer->cancel();
//wait until read() finished before closing _file
_timer->wait();
if (_file != NULL) {
fclose(_file);
_file = NULL;
}
LOG(info) << "Sensor " << _name << " stopped.";
}
......@@ -61,7 +61,9 @@ public:
void readAsync();
void startPolling(boost::asio::io_service& io);
void startPolling();
void stopPolling();
private:
std::string _path;
......
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