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