Commit 7b831120 authored by Micha Mueller's avatar Micha Mueller
Browse files

Adapt remaining plugins to new class structure

parent c3877c1c
......@@ -78,13 +78,13 @@ libdcdbplugin_sysfs.$(LIBEXT): src/sensors/sysfs/SysfsSingleSensor.o src/sensors
libdcdbplugin_perfevent.$(LIBEXT): src/sensors/perfevent/PerfSingleSensor.o src/sensors/perfevent/PerfeventConfigurator.o src/sensors/perfevent/PerfSensorGroup.o
$(CXX) $(LIBFLAGS)$@ -o $@ $^ -L$(DCDBDEPLOYPATH)/lib/ -lboost_log -lboost_system
libdcdbplugin_ipmi.$(LIBEXT): src/Sensor.o src/sensors/ipmi/IPMISensor.o src/sensors/ipmi/IPMIHost.o src/sensors/ipmi/IPMIConfigurator.o
libdcdbplugin_ipmi.$(LIBEXT): src/sensors/ipmi/IPMISingleSensor.o src/sensors/ipmi/IPMIHost.o src/sensors/ipmi/IPMIConfigurator.o
$(CXX) $(LIBFLAGS)$@ -o $@ $^ -L$(DCDBDEPLOYPATH)/lib/ -lboost_log -lboost_system -lfreeipmi -lboost_regex
libdcdbplugin_pdu.$(LIBEXT): src/sensors/pdu/PDUSingleSensor.o src/sensors/pdu/PDUUnit.o src/sensors/pdu/PDUConfigurator.o
$(CXX) $(LIBFLAGS)$@ -o $@ $^ -L$(DCDBDEPLOYPATH)/lib/ -lcrypto -lssl -lboost_log -lboost_system
libdcdbplugin_bacnet.$(LIBEXT): src/Sensor.o src/sensors/bacnet/BACnetSensor.o src/sensors/bacnet/BACnetClient.o src/sensors/bacnet/BACnetConfigurator.o
libdcdbplugin_bacnet.$(LIBEXT): src/sensors/bacnet/BACnetSingleSensor.o src/sensors/bacnet/BACnetClient.o src/sensors/bacnet/BACnetConfigurator.o
$(CXX) $(LIBFLAGS)$@ -o $@ $^ -L$(DCDBDEPLOYPATH)/lib/ -lboost_log -lboost_system -lbacnet
libdcdbplugin_snmp.$(LIBEXT): src/sensors/snmp/SNMPSingleSensor.o src/sensors/snmp/SNMPConnection.o src/sensors/snmp/SNMPConfigurator.o
......
......@@ -86,7 +86,7 @@ bool BACnetConfigurator::derivedReadConfig(boost::property_tree::iptree& cfg) {
LOG(debug) << "Property \"" << object.second.data() << "\"";
if (!object.second.empty()) {
std::string name = device.second.data() + "_" + d.second.data() + "_" + object.second.data();
BACnetSensor * bacSensor = new BACnetSensor(name);
BACnetSingleSensor * bacSensor = new BACnetSingleSensor(name);
//first check if default sensor is given
boost::optional<boost::property_tree::iptree&> defaultC = object.second.get_child_optional("default");
......@@ -109,7 +109,7 @@ bool BACnetConfigurator::derivedReadConfig(boost::property_tree::iptree& cfg) {
bacSensor->setBACnetClient(_bacClient);
bacSensor->setMqtt(_mqttPrefix + mqttPartDevice + mqttPartObject);
//read remaining values
if(readSensor(*bacSensor, object.second)) {
if(readSensorBase(*bacSensor, object.second)) {
_sensors.push_back(bacSensor);
} else {
LOG(warning) << " Sensor \"" << object.second.data() << "\" has bad values! Ignoring..." << std::endl;
......@@ -131,7 +131,7 @@ void BACnetConfigurator::derivedReReadConfig() {
_bacClient = NULL;
}
bool BACnetConfigurator::derivedReadSensor(BACnetSensor& sensor, boost::property_tree::iptree& config) {
bool BACnetConfigurator::derivedReadSensorBase(BACnetSensorBase& sensor, boost::property_tree::iptree& config) {
BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config) {
if (STRCMP(val, "mqttsuffix")) {
sensor.setMqtt(sensor.getMqtt() + val.second.data());
......
......@@ -8,15 +8,14 @@
#ifndef BACNETCONFIGURATOR_H_
#define BACNETCONFIGURATOR_H_
#include "BACnetSensor.h"
#include "BACnetClient.h"
#include "../../ConfiguratorTemplate.h"
#include "BACnetSingleSensor.h"
class BACnetConfigurator: public ConfiguratorTemplate<BACnetSensor> {
class BACnetConfigurator: public ConfiguratorTemplate<BACnetSensorBase, BACnetSingleSensor> {
public:
BACnetConfigurator();
virtual ~BACnetConfigurator();
......@@ -24,7 +23,7 @@ protected:
bool derivedReadConfig(boost::property_tree::iptree& cfg) override;
void derivedReReadConfig() override;
void derivedSetGlobalSettings(const pluginSettings_t& pluginSettings) override {/*nothing to overwrite*/}
bool derivedReadSensor(BACnetSensor& sensor, boost::property_tree::iptree& config) override;
bool derivedReadSensorBase(BACnetSensorBase& sensor, boost::property_tree::iptree& config) override;
private:
BACnetClient* _bacClient;
......
/*
* BACnetSensor.h
*
* Created on: 14.04.2018
* Author: Micha Mueller
*/
#ifndef BACNETSENSOR_H_
#define BACNETSENSOR_H_
#include "../../Sensor.h"
#include "BACnetClient.h"
#include "bacenum.h"
class BACnetSensor: public Sensor {
public:
BACnetSensor(const std::string& name);
virtual ~BACnetSensor();
/**** GETTERS + SETTERS ****/
void setFactor(double factor) {
_factor = factor;
}
double getFactor() const {
return _factor;
}
uint32_t getDeviceInstance() const {
return _deviceInstance;
}
void setDeviceInstance(uint32_t deviceInstance) {
_deviceInstance = deviceInstance;
}
int32_t getObjectIndex() const {
return _objectIndex;
}
void setObjectIndex(int32_t objectIndex) {
_objectIndex = objectIndex;
}
uint32_t getObjectInstance() const {
return _objectInstance;
}
void setObjectInstance(uint32_t objectInstance) {
_objectInstance = objectInstance;
}
BACNET_OBJECT_TYPE getObjectType() const {
return _objectType;
}
void setObjectType(BACNET_OBJECT_TYPE objectType) {
_objectType = objectType;
}
BACNET_PROPERTY_ID getPropertyId() const {
return _propertyId;
}
void setPropertyId(BACNET_PROPERTY_ID property) {
_propertyId = property;
}
void setBACnetClient(BACnetClient* bacClient) {
_bacClient = bacClient;
}
BACnetClient* getBACnetClient() const {
return _bacClient;
}
/**** END GETTERS/SETTER ****/
void init(boost::asio::io_service& io);
void read();
void readAsync();
void startPolling();
void stopPolling();
private:
double _factor;
uint32_t _deviceInstance;
uint32_t _objectInstance;
BACNET_OBJECT_TYPE _objectType;
BACNET_PROPERTY_ID _propertyId;
int32_t _objectIndex;
BACnetClient* _bacClient;
};
#endif /* BACNETSENSOR_H_ */
/*
* BACnetSensorBase.h
*
* Created on: 13.08.2018
* Author: Micha Mueller
*/
#ifndef SRC_SENSORS_BACNET_BACNETSENSORBASE_H_
#define SRC_SENSORS_BACNET_BACNETSENSORBASE_H_
#include "../../headers/SensorBase.h"
#include "BACnetClient.h"
#include "bacenum.h"
class BACnetSensorBase : virtual public SensorBase {
public:
BACnetSensorBase(const std::string& name) :
SensorBase(name),
_factor(1),
_deviceInstance(0),
_objectInstance(0) {
_objectType = OBJECT_DEVICE;
_propertyId = PROP_PRESENT_VALUE;
_objectIndex = BACNET_ARRAY_ALL;
_bacClient = NULL;
}
virtual ~BACnetSensorBase() {}
double getFactor() const { return _factor; }
uint32_t getDeviceInstance() const { return _deviceInstance; }
uint32_t getObjectInstance() const { return _objectInstance; }
BACNET_OBJECT_TYPE getObjectType() const { return _objectType; }
BACNET_PROPERTY_ID getPropertyId() const { return _propertyId; }
int32_t getObjectIndex() const { return _objectIndex; }
BACnetClient* getBACnetClient() const { return _bacClient; }
void setFactor(double factor) { _factor = factor; }
void setDeviceInstance(uint32_t deviceInstance) { _deviceInstance = deviceInstance; }
void setObjectInstance(uint32_t objectInstance) { _objectInstance = objectInstance; }
void setObjectType(BACNET_OBJECT_TYPE objectType) { _objectType = objectType; }
void setPropertyId(BACNET_PROPERTY_ID property) { _propertyId = property; }
void setObjectIndex(int32_t objectIndex) { _objectIndex = objectIndex; }
void setBACnetClient(BACnetClient* bacClient) { _bacClient = bacClient; }
protected:
double _factor;
uint32_t _deviceInstance;
uint32_t _objectInstance;
BACNET_OBJECT_TYPE _objectType;
BACNET_PROPERTY_ID _propertyId;
int32_t _objectIndex;
BACnetClient* _bacClient;
};
#endif /* SRC_SENSORS_BACNET_BACNETSENSORBASE_H_ */
/*
* BACnetSensor.cpp
* BACnetSingleSensor.cpp
*
* Created on: 14.04.2018
* Author: Micha Mueller
*/
#include "BACnetSensor.h"
#include "BACnetSingleSensor.h"
#include "timestamp.h"
#include <functional>
BACnetSensor::BACnetSensor(const std::string& name) :
Sensor(name) {
_factor = 1;
_deviceInstance = 0;
_objectInstance = 0;
_objectType = OBJECT_DEVICE;
_propertyId = PROP_PRESENT_VALUE;
_objectIndex = BACNET_ARRAY_ALL;
_bacClient = NULL;
}
BACnetSingleSensor::BACnetSingleSensor(const std::string& name) :
SensorBase(name), BACnetSensorBase(name), SingleSensor(name) {}
BACnetSensor::~BACnetSensor() {
// TODO Auto-generated destructor stub
}
BACnetSingleSensor::~BACnetSingleSensor() {}
void BACnetSensor::read() {
reading_t reading;
reading.timestamp = getTimestamp();
try {
reading.value = _bacClient->readProperty(_deviceInstance, _objectInstance, _objectType, _propertyId) * _factor;
storeReading(reading);
#ifdef DEBUG
LOG(debug) << _name << ": \"" << reading.value << "\"";
#endif
} catch (const std::exception& e) {
LOG(error) << _name << " could not read value: " << e.what();
}
}
void BACnetSensor::readAsync() {
uint64_t now = getTimestamp();
read();
if (_timer != NULL && _keepRunning) {
uint64_t next = now + MS_TO_NS(_interval);
_timer->expires_at(timestamp2ptime(next));
_pendingTasks++;
_timer->async_wait(_bacClient->getStrand()->wrap(std::bind(&BACnetSensor::readAsync, this)));
}
_pendingTasks--;
}
void BACnetSensor::init(boost::asio::io_service& io) {
void BACnetSingleSensor::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);
SingleSensor::init(io);
}
void BACnetSensor::startPolling() {
void BACnetSingleSensor::start() {
if (_keepRunning) {
//we have been started already
LOG(info) << "Sensor " << _name << " already running.";
......@@ -70,16 +34,44 @@ void BACnetSensor::startPolling() {
if (_bacClient) {
_keepRunning = 1;
_pendingTasks++;
_timer->async_wait(_bacClient->getStrand()->wrap(std::bind(&BACnetSensor::readAsync, this)));
_timer->async_wait(_bacClient->getStrand()->wrap(std::bind(&BACnetSingleSensor::readAsync, this)));
LOG(info) << "Sensor " << _name << " started.";
} else {
LOG(error) << "No BACnetClient set for sensor " << _name << "! Cannot start polling.";
}
}
void BACnetSensor::stopPolling() {
void BACnetSingleSensor::stop() {
_keepRunning = 0;
//cancel any outstanding readAsync()
_timer->cancel();
LOG(info) << "Sensor " << _name << " stopped.";
}
void BACnetSingleSensor::read() {
reading_t reading;
reading.timestamp = getTimestamp();
try {
reading.value = _bacClient->readProperty(_deviceInstance, _objectInstance, _objectType, _propertyId) * _factor;
storeReading(reading, _cacheIndex);
_cacheIndex = (_cacheIndex + 1) % _cacheSize;
#ifdef DEBUG
LOG(debug) << _name << ": \"" << reading.value << "\"";
#endif
} catch (const std::exception& e) {
LOG(error) << _name << " could not read value: " << e.what();
}
}
void BACnetSingleSensor::readAsync() {
uint64_t now = getTimestamp();
read();
if (_timer != NULL && _keepRunning) {
uint64_t next = now + MS_TO_NS(_interval);
_timer->expires_at(timestamp2ptime(next));
_pendingTasks++;
_timer->async_wait(_bacClient->getStrand()->wrap(std::bind(&BACnetSingleSensor::readAsync, this)));
}
_pendingTasks--;
}
/*
* BACnetSingleSensor.h
*
* Created on: 14.04.2018
* Author: Micha Mueller
*/
#ifndef BACNETSINGLESENSOR_H_
#define BACNETSINGLESENSOR_H_
#include "BACnetSensorBase.h"
#include "../../headers/SingleSensor.h"
class BACnetSingleSensor: public BACnetSensorBase, public SingleSensor {
public:
BACnetSingleSensor(const std::string& name);
virtual ~BACnetSingleSensor();
void init(boost::asio::io_service& io) override;
void start() override;
void stop() override;
private:
void read() override;
void readAsync() override;
};
#endif /* BACNETSINGLESENSOR_H_ */
......@@ -55,7 +55,7 @@ bool IPMIConfigurator::derivedReadConfig(boost::property_tree::iptree& cfg) {
LOG(debug) << "Sensor \"" << sensor.second.data() << "\"";
if (!sensor.second.empty()) {
std::string name = host.second.data() + "_" + sensor.second.data();
DCDB::IPMISensor* ipmiSensor = new DCDB::IPMISensor(name);
DCDB::IPMISingleSensor* ipmiSensor = new DCDB::IPMISingleSensor(name);
//first check if default sensor is given
boost::optional<boost::property_tree::iptree&> defaultC = sensor.second.get_child_optional("default");
......@@ -73,7 +73,7 @@ bool IPMIConfigurator::derivedReadConfig(boost::property_tree::iptree& cfg) {
//set pointer to corresponding host
ipmiSensor->setHost(&ipmiHost);
//read remaining values
if(readSensor(*ipmiSensor, sensor.second)) {
if(readSensorBase(*ipmiSensor, sensor.second)) {
_sensors.push_back(ipmiSensor);
} else {
LOG(warning) << " Sensor \"" << sensor.second.data() << "\" has bad values! Ignoring..." << std::endl;
......@@ -88,7 +88,7 @@ bool IPMIConfigurator::derivedReadConfig(boost::property_tree::iptree& cfg) {
return true;
}
bool IPMIConfigurator::derivedReadSensor(DCDB::IPMISensor& sensor, boost::property_tree::iptree& config) {
bool IPMIConfigurator::derivedReadSensorBase(IPMISensorBase& sensor, boost::property_tree::iptree& config) {
BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config) {
if (STRCMP(val, "mqttsuffix")) {
if (sensor.getHost() != NULL) {
......
......@@ -8,16 +8,16 @@
#ifndef IPMICONFIGURATOR_H_
#define IPMICONFIGURATOR_H_
#include "IPMISensor.h"
#include "IPMIHost.h"
#include <list>
#include "../../ConfiguratorTemplate.h"
#include "IPMISingleSensor.h"
namespace DCDB {
class IPMIConfigurator: public ConfiguratorTemplate<DCDB::IPMISensor> {
class IPMIConfigurator: public ConfiguratorTemplate<IPMISensorBase, DCDB::IPMISingleSensor> {
typedef std::list<DCDB::IPMIHost> hostList_t;
typedef struct {
......@@ -33,7 +33,7 @@ protected:
bool derivedReadConfig(boost::property_tree::iptree& cfg) override;
void derivedReReadConfig() override { _hosts.clear(); }
void derivedSetGlobalSettings(const pluginSettings_t& pluginSettings) override { _tempdir = pluginSettings.tempdir; }
bool derivedReadSensor(DCDB::IPMISensor& sensor, boost::property_tree::iptree& config) override;
bool derivedReadSensorBase(IPMISensorBase& sensor, boost::property_tree::iptree& config) override;
private:
std::string _tempdir;
......
/*
* IPMISensor.cpp
*
* Created on: 18 Jan 2017
* Author: ottmi
*/
#include "IPMISensor.h"
#include "IPMIHost.h"
#include "timestamp.h"
#include <boost/tokenizer.hpp>
#include <boost/foreach.hpp>
#include <boost/regex.hpp>
#include <boost/bind.hpp>
#include <boost/lockfree/spsc_queue.hpp>
#include <iostream>
#include <exception>
#include <chrono>
namespace DCDB {
IPMISensor::IPMISensor(const std::string& name) :
Sensor(name) {
_recordId = 0;
_factor = 1;
_start = 0;
_stop = 0;
_host = NULL;
}
IPMISensor::~IPMISensor() {
// TODO Auto-generated destructor stub
}
void IPMISensor::read() {
reading_t reading;
reading.timestamp = getTimestamp();
try {
if (_recordId != 0) { /* recordId was set */
reading.value = _host->readSensor_recordId(_recordId) * _factor;
} else { /* use raw command */
reading.value = _host->sendRawCmd(_rawCmd, _start, _stop) * _factor;
}
}
catch (const std::exception& e) {
LOG(error) << _host->getHostName() << "::" << _name << " could not read value: " << e.what();
return;
}
#ifdef DEBUG
LOG(debug) << _host->getHostName() << "::" << _name << ": \"" << reading.value << "\"";
#endif
storeReading(reading);
}
void IPMISensor::readAsync() {
uint64_t now = getTimestamp();
if (now >= _host->getDelayNextReadUntil()) {
read();
}
if (_timer != NULL && _keepRunning) {
uint64_t next = now + MS_TO_NS(_interval);
while (next < _host->getDelayNextReadUntil()) {
next+= MS_TO_NS(_interval);
}
_timer->expires_at(timestamp2ptime(next));
_pendingTasks++;
_timer->async_wait(_host->getStrand()->wrap(boost::bind(&IPMISensor::readAsync, this)));
}
_pendingTasks--;
}
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 (_keepRunning) {
//we have been started already
LOG(info) << "Sensor " << _name << " already running.";
return;
}
if (_host) {
_keepRunning = 1;
_pendingTasks++;
_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;
}
}
void IPMISensor::stopPolling() {
_keepRunning = 0;
//cancel any outstanding readAsync()
_timer->cancel();
LOG(info) << "Sensor " << _name << " stopped.";
}
void IPMISensor::setRawCmd(std::string& rawCmd) {
boost::regex expr("(?:0x)?([0-9a-fA-F]+)");
boost::regex_token_iterator<std::string::iterator> it{rawCmd.begin(), rawCmd.end(), expr, 1};
boost::regex_token_iterator<std::string::iterator> end;
while (it != end) {
_rawCmd.push_back(stoi(*it++, NULL, 16));
}
}
} /* namespace DCDB */
/*
* IPMISensor.h
*
* Created on: 18 Jan 2017
* Author: ottmi
*/
#ifndef IPMISENSOR_H_
#define IPMISENSOR_H_
#include "../../Sensor.h"
#include <vector>
namespace DCDB {
class IPMIHost;
class IPMISensor : public Sensor {
public:
IPMISensor(const std::string& name);
virtual ~IPMISensor();
void setRawCmd(std::string& rawCmd);
void setRecordId(uint16_t recordId) {
_recordId = recordId;
}
uint16_t getRecordId() const {
return _recordId;
}
void setFactor(double factor) {
_factor = factor;
}
double getFactor() const {
return _factor;
}
uint8_t getStart() const {
return _start;
}
void setStart(uint8_t start) {