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

Adapt PDU plugin to new class structure

parent 5d43fec1
......@@ -81,7 +81,7 @@ libdcdbplugin_perfevent.$(LIBEXT): src/sensors/perfevent/PerfSingleSensor.o src/
libdcdbplugin_ipmi.$(LIBEXT): src/Sensor.o src/sensors/ipmi/IPMISensor.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/Sensor.o src/sensors/pdu/PDUSensor.o src/sensors/pdu/PDUUnit.o src/sensors/pdu/PDUConfigurator.o
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
......
......@@ -48,7 +48,7 @@ bool PDUConfigurator::derivedReadConfig(boost::property_tree::iptree& cfg) {
LOG(debug) << "Sensor \"" << sensor.second.data() << "\"";
if (!sensor.second.empty()) {
std::string name = pdu.second.data() + "_" + sensor.second.data();
PDUSensor* pduSensor = new PDUSensor(name);
PDUSingleSensor* pduSensor = new PDUSingleSensor(name);
//first check if default sensor is given
boost::optional<boost::property_tree::iptree&> defaultC = sensor.second.get_child_optional("default");
......@@ -64,7 +64,7 @@ bool PDUConfigurator::derivedReadConfig(boost::property_tree::iptree& cfg) {
}
//read remaining values
if(readSensor(*pduSensor, sensor.second)) {
if(readSensorBase(*pduSensor, sensor.second)) {
//set pointer to corresponding pdu
pduSensor->setPdu(&pduUnit);
_sensors.push_back(pduSensor);
......@@ -81,7 +81,7 @@ bool PDUConfigurator::derivedReadConfig(boost::property_tree::iptree& cfg) {
return true;
}
bool PDUConfigurator::derivedReadSensor(PDUSensor& sensor, boost::property_tree::iptree& config) {
bool PDUConfigurator::derivedReadSensorBase(PDUSensorBase& sensor, boost::property_tree::iptree& config) {
BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config) {
if (STRCMP(val, "mqttsuffix")) {
sensor.setMqtt(_mqttPrefix + val.second.data());
......@@ -94,7 +94,7 @@ bool PDUConfigurator::derivedReadSensor(PDUSensor& sensor, boost::property_tree:
return true;
}
void PDUConfigurator::parsePathString(PDUSensor& sensor, const std::string& pathString) {
void PDUConfigurator::parsePathString(PDUSensorBase& sensor, const std::string& pathString) {
LOG(debug) << " Using " << pathString << " as XML search path";
std::vector<std::string> subStrings;
......@@ -148,17 +148,17 @@ void PDUConfigurator::parsePathString(PDUSensor& sensor, const std::string& path
if (index != std::string::npos) {
std::string subPathChild(subPath.substr(++index));
subPath.erase(--index);
sensor._xmlPath.push_back(std::make_tuple(subPath, subPathChild, attrs));
sensor.pushBackXMLPathPart(std::make_tuple(subPath, subPathChild, attrs));
LOG(debug) << " Subpath: " << subPath << " ; Child: " << subPathChild;
} else {//the path contained only one node
sensor._xmlPath.push_back(std::make_tuple("", subPath, attrs));
sensor.pushBackXMLPathPart(std::make_tuple("", subPath, attrs));
LOG(debug) << " Child: " << subPath;
}
} else { //no attributes specified. Last (sub)path
if (subStr.front() == '.') {
subStr.erase(0, 1);
}
sensor._xmlPath.push_back(std::make_tuple(subStr, "", attributesVector_t()));
sensor.pushBackXMLPathPart(std::make_tuple(subStr, "", attributesVector_t()));
LOG(debug) << " (Sub)path: " << subStr;
break;
}
......
......@@ -8,14 +8,13 @@
#ifndef SRC_SENSORS_PDU_PDUCONFIGURATOR_H_
#define SRC_SENSORS_PDU_PDUCONFIGURATOR_H_
#include "PDUSensor.h"
#include "PDUUnit.h"
#include <list>
#include "../../ConfiguratorTemplate.h"
#include "PDUSingleSensor.h"
class PDUConfigurator: public ConfiguratorTemplate<PDUSensor> {
class PDUConfigurator: public ConfiguratorTemplate<PDUSensorBase, PDUSingleSensor> {
typedef std::list<PDUUnit> pduList_t;
......@@ -27,8 +26,8 @@ public:
protected:
bool derivedReadConfig(boost::property_tree::iptree& cfg) override;
void derivedReReadConfig() override { _pdus.clear(); }
void derivedSetGlobalSettings(const pluginSettings_t& pluginSettings) override {/*nothing to overwrite*/}
bool derivedReadSensor(PDUSensor& sensor, boost::property_tree::iptree& config) override;
void derivedSetGlobalSettings(const pluginSettings_t& pluginSettings) override { /*nothing to overwrite*/ }
bool derivedReadSensorBase(PDUSensorBase& sensor, boost::property_tree::iptree& config) override;
private:
/**
......@@ -37,7 +36,7 @@ private:
* @param sensor Sensor where the path belongs to
* @param pathString Complete unrefined string as read from the config file
*/
void parsePathString(PDUSensor& sensor, const std::string& pathString);
void parsePathString(PDUSensorBase& sensor, const std::string& pathString);
pduList_t _pdus;
};
......
/*
* PDUSensor.h
*
* Created on: 24.02.2018
* Author: Micha Mueller
*/
#ifndef SRC_SENSORS_PDU_PDUSENSOR_H_
#define SRC_SENSORS_PDU_PDUSENSOR_H_
#include "../../Sensor.h"
#include "PDUUnit.h"
class PDUSensor: public Sensor {
public:
PDUSensor(const std::string& name);
virtual ~PDUSensor();
void setPdu(PDUUnit* pdu) {
_pdu = pdu;
}
PDUUnit* getPdu() const {
return _pdu;
}
void init(boost::asio::io_service& io);
void read();
void readAsync();
void startPolling();
void stopPolling();
xmlPathVector_t _xmlPath;
private:
PDUUnit* _pdu;
};
#endif /* SRC_SENSORS_PDU_PDUSENSOR_H_ */
/*
* PDUSensorBase.h
*
* Created on: 13.08.2018
* Author: Micha Mueller
*/
#ifndef PDU_PDUSENSORBASE_H_
#define PDU_PDUSENSORBASE_H_
#include "../../headers/SensorBase.h"
#include "PDUUnit.h"
class PDUSensorBase : virtual public SensorBase {
public:
PDUSensorBase(const std::string& name) :
SensorBase(name) {
_pdu = NULL;
}
virtual ~PDUSensorBase() {}
void setPdu(PDUUnit* pdu) { _pdu = pdu; }
PDUUnit* getPdu() const { return _pdu; }
void pushBackXMLPathPart(std::tuple<std::string, std::string, attributesVector_t> pathPart) {
_xmlPath.push_back(pathPart);
}
protected:
xmlPathVector_t _xmlPath;
PDUUnit* _pdu;
};
#endif /* PDU_PDUSENSORBASE_H_ */
/*
* PDUSensor.cpp
* PDUSingleSensor.cpp
*
* Created on: 24.02.2018
* Author: Micha Mueller
*/
#include "PDUSensor.h"
#include "PDUSingleSensor.h"
#include "timestamp.h"
#include <string>
PDUSensor::PDUSensor(const std::string& name) :
Sensor(name) {
_pdu = NULL;
}
PDUSingleSensor::PDUSingleSensor(const std::string& name) :
SensorBase(name), PDUSensorBase(name), SingleSensor(name) {}
PDUSensor::~PDUSensor() {
// TODO Auto-generated destructor stub
}
PDUSingleSensor::~PDUSingleSensor() {}
void PDUSensor::read() {
reading_t reading;
reading.timestamp = getTimestamp();
try {
reading.value = _pdu->readValue(_xmlPath);
storeReading(reading);
#ifdef DEBUG
LOG(debug) << _pdu->getHost() << "::" << _name << ": \"" << reading.value << "\"";
#endif
} catch (const std::exception& e) {
LOG(error) << _pdu->getHost() << "::" << _name << " could not read value: " << e.what();
}
}
void PDUSensor::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(_pdu->getStrand()->wrap(std::bind(&PDUSensor::readAsync, this)));
}
_pendingTasks--;
}
void PDUSensor::init(boost::asio::io_service& io) {
void PDUSingleSensor::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);
SingleSensor::init(io);
}
void PDUSensor::startPolling() {
void PDUSingleSensor::start() {
if (_keepRunning) {
//we have been started already
LOG(info) << "Sensor " << _name << " already running.";
......@@ -64,16 +34,44 @@ void PDUSensor::startPolling() {
if (_pdu) {
_keepRunning = 1;
_pendingTasks++;
_timer->async_wait(_pdu->getStrand()->wrap(std::bind(&PDUSensor::readAsync, this)));
_timer->async_wait(_pdu->getStrand()->wrap(std::bind(&PDUSingleSensor::readAsync, this)));
LOG(info) << "Sensor " << _name << " started.";
} else {
LOG(error) << "No PDUUnit set for sensor " << _name << "! Cannot start polling.";
}
}
void PDUSensor::stopPolling() {
void PDUSingleSensor::stop() {
_keepRunning = 0;
//cancel any outstanding readAsync()
_timer->cancel();
LOG(info) << "Sensor " << _name << " stopped.";
}
void PDUSingleSensor::read() {
reading_t reading;
reading.timestamp = getTimestamp();
try {
reading.value = _pdu->readValue(_xmlPath);
storeReading(reading, _cacheIndex);
_cacheIndex = (_cacheIndex + 1) % _cacheSize;
#ifdef DEBUG
LOG(debug) << _pdu->getHost() << "::" << _name << ": \"" << reading.value << "\"";
#endif
} catch (const std::exception& e) {
LOG(error) << _pdu->getHost() << "::" << _name << " could not read value: " << e.what();
}
}
void PDUSingleSensor::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(_pdu->getStrand()->wrap(std::bind(&PDUSingleSensor::readAsync, this)));
}
_pendingTasks--;
}
/*
* PDUSingleSensor.h
*
* Created on: 24.02.2018
* Author: Micha Mueller
*/
#ifndef SRC_SENSORS_PDU_PDUSINGLESENSOR_H_
#define SRC_SENSORS_PDU_PDUSINGLESENSOR_H_
#include "PDUSensorBase.h"
#include "../../headers/SingleSensor.h"
class PDUSingleSensor: public PDUSensorBase, public SingleSensor {
public:
PDUSingleSensor(const std::string& name);
virtual ~PDUSingleSensor();
void init(boost::asio::io_service& io) override;
void start() override;
void stop() override;
private:
void read() override;
void readAsync() override;
};
#endif /* SRC_SENSORS_PDU_PDUSINGLESENSOR_H_ */
......@@ -75,10 +75,7 @@ uint64_t PDUUnit::readValue(const xmlPathVector_t& xmlPath) {
}
if (reading == "") {
#ifdef DEBUG
LOG(debug) << "PDU: Value not found!";
#endif
return 0;
throw std::runtime_error("Value not found!");
}
#ifdef DEBUG
LOG(debug) << "Read: " << reading;
......
......@@ -9,11 +9,11 @@
#define SNMPCONFIGURATOR_H_
#include "SNMPConnection.h"
#include "SNMPSingleSensor.h"
#include <list>
#include "../../ConfiguratorTemplate.h"
#include "SNMPSingleSensor.h"
class SNMPConfigurator : public ConfiguratorTemplate<SNMPSensorBase, SNMPSingleSensor> {
......
......@@ -25,8 +25,6 @@ private:
void read() override;
void readAsync() override;
private:
};
#endif /* SNMPSINGLESENSOR_H_ */
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