Commit 5d43fec1 authored by Micha Mueller's avatar Micha Mueller
Browse files

Adapt Sysfs plugin to new class structure

parent d094ba1a
......@@ -72,7 +72,7 @@ src/SensorGroup.o: CXXFLAGS+= $(PLUGINFLAGS)
src/sensors/%.o: CXXFLAGS+= $(PLUGINFLAGS) -I$(DCDBDEPSPATH)/bacnet-stack-$(BACNET-STACK_VERSION)/include -I$(DCDBDEPSPATH)/bacnet-stack-$(BACNET-STACK_VERSION)/ports/$(BACNET_PORT)
#src/sensors/*/%.o: %.cpp
libdcdbplugin_sysfs.$(LIBEXT): src/Sensor.o src/sensors/sysfs/SysfsSensor.o src/sensors/sysfs/SysfsConfigurator.o
libdcdbplugin_sysfs.$(LIBEXT): src/sensors/sysfs/SysfsSingleSensor.o src/sensors/sysfs/SysfsConfigurator.o
$(CXX) $(LIBFLAGS)$@ -o $@ $^ -L$(DCDBDEPLOYPATH)/lib/ -lboost_log -lboost_system
libdcdbplugin_perfevent.$(LIBEXT): src/sensors/perfevent/PerfSingleSensor.o src/sensors/perfevent/PerfeventConfigurator.o src/sensors/perfevent/PerfSensorGroup.o
......
......@@ -18,7 +18,8 @@
class SNMPSensorBase : virtual public SensorBase {
public:
SNMPSensorBase(const std::string& name) :
SensorBase(name), _oidLen(0) {
SensorBase(name),
_oidLen(0) {
memset(_oid, 0x0, sizeof(oid) * MAX_OID_LEN);
_connection = NULL;
}
......
......@@ -21,7 +21,7 @@ bool SysfsConfigurator::derivedReadConfig(boost::property_tree::iptree& cfg) {
if (STRCMP(sensor, "sensor")) {
LOG(debug) << "Sensor \"" << sensor.second.data() << "\"";
if (!sensor.second.empty()) {
SysfsSensor* sysfsSensor = new SysfsSensor(sensor.second.data());
SysfsSingleSensor* sysfsSensor = new SysfsSingleSensor(sensor.second.data());
//first check if default sensor is given
boost::optional<boost::property_tree::iptree&> defaultS = sensor.second.get_child_optional("default");
......@@ -36,7 +36,7 @@ bool SysfsConfigurator::derivedReadConfig(boost::property_tree::iptree& cfg) {
}
}
//read remaining values
if(readSensor(*sysfsSensor, sensor.second)) {
if(readSensorBase(*sysfsSensor, sensor.second)) {
_sensors.push_back(sysfsSensor);
} else {
LOG(warning) << " Sensor \"" << sensor.second.data() << "\" has bad values! Ignoring...";
......@@ -47,7 +47,7 @@ bool SysfsConfigurator::derivedReadConfig(boost::property_tree::iptree& cfg) {
return true;
}
bool SysfsConfigurator::derivedReadSensor(SysfsSensor& sensor, boost::property_tree::iptree& config) {
bool SysfsConfigurator::derivedReadSensorBase(SysfsSensorBase& 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());
......
......@@ -8,11 +8,11 @@
#ifndef SYSFSCONFIGURATOR_H_
#define SYSFSCONFIGURATOR_H_
#include "SysfsSensor.h"
#include "../../ConfiguratorTemplate.h"
class SysfsConfigurator : public ConfiguratorTemplate<SysfsSensor> {
#include "SysfsSingleSensor.h"
class SysfsConfigurator : public ConfiguratorTemplate<SysfsSensorBase, SysfsSingleSensor> {
public:
SysfsConfigurator();
......@@ -21,9 +21,9 @@ public:
protected:
/* Overwritten from ConfiguratorTemplate */
bool derivedReadConfig(boost::property_tree::iptree& cfg) override;
void derivedReReadConfig() override {/* nothing to overwrite */}
void derivedSetGlobalSettings(const pluginSettings_t& pluginSettings) override {/* nothing to overwrite */}
bool derivedReadSensor(SysfsSensor& sensor, boost::property_tree::iptree& config) override;
void derivedReReadConfig() override { /* nothing to overwrite */ }
void derivedSetGlobalSettings(const pluginSettings_t& pluginSettings) override { /* nothing to overwrite */ }
bool derivedReadSensorBase(SysfsSensorBase& sensor, boost::property_tree::iptree& config) override;
};
extern "C" ConfiguratorInterface* create() {
......
/*
* SYSFSSensor.h
*
* Created on: 18.11.2017
* Author: Michael Ott (original), Micha Mueller
*/
#ifndef SYSFSSENSOR_H_
#define SYSFSSENSOR_H_
#include "../../Sensor.h"
#include <regex>
class SysfsSensor : public Sensor {
public:
SysfsSensor(const std::string& name);
virtual ~SysfsSensor();
const std::string& getPath() const {
return _path;
}
void setPath(const std::string& path) {
_path = path;
}
FILE* getFile() const {
return _file;
}
void setFile(FILE* file) {
_file = file;
}
bool hasFilter() const {
return _filter;
}
void setFilter(bool filter) {
_filter = filter;
}
std::regex getRegex() const {
return _regx;
}
void setRegex(std::regex regx) {
_regx = regx;
}
const std::string& getSubstitution() const {
return _substitution;
}
void setSubstitution(const std::string& substitution) {
_substitution = substitution;
}
void read();
void readAsync();
void startPolling();
void stopPolling();
private:
std::string _path;
FILE* _file;
bool _filter;
std::regex _regx;
std::string _substitution;
};
#endif /* SYSFSSENSOR_H_ */
/*
* SysfsSensorBase.h
*
* Created on: 13.08.2018
* Author: Micha Mueller
*/
#ifndef SYSFS_SYSFSSENSORBASE_H_
#define SYSFS_SYSFSSENSORBASE_H_
#include "../../headers/SensorBase.h"
#include <regex>
class SysfsSensorBase : virtual public SensorBase {
public:
SysfsSensorBase(const std::string& name) :
SensorBase(name),
_path(""),
_filter(false),
_substitution("") {
_file = NULL;
//_regx = "";
}
virtual ~SysfsSensorBase() {}
const std::string& getPath() const { return _path; }
FILE* getFile() const { return _file; }
bool hasFilter() const { return _filter; }
std::regex getRegex() const { return _regx; }
const std::string& getSubstitution() const { return _substitution; }
void setPath(const std::string& path) { _path = path; }
void setFile(FILE* file) { _file = file; }
void setFilter(bool filter) { _filter = filter; }
void setRegex(std::regex regx) { _regx = regx; }
void setSubstitution(const std::string& substitution) { _substitution = substitution; }
protected:
std::string _path;
FILE* _file;
bool _filter;
std::regex _regx;
std::string _substitution;
};
#endif /* SYSFS_SYSFSSENSORBASE_H_ */
/*
* SYSFSSensor.cpp
* SysfsSingleSensor.cpp
*
* Created on: 18.11.2017
* Author: Michael Ott (original), Micha Mueller
*/
#include "SysfsSensor.h"
#include "SysfsSingleSensor.h"
#include "timestamp.h"
#include <functional>
using namespace std;
SysfsSensor::SysfsSensor(const std::string& name) :
Sensor(name) {
_path = "";
_file = NULL;
_filter = false;
//_regx = "";
_substitution = "";
SysfsSingleSensor::SysfsSingleSensor(const std::string& name) :
SensorBase(name), SysfsSensorBase(name), SingleSensor(name) {}
SysfsSingleSensor::~SysfsSingleSensor() {}
void SysfsSingleSensor::start() {
if (_keepRunning) {
//we have been started already
LOG(info) << "Sensor " << _name << " already running.";
return;
}
if(_file == NULL) {
_file = fopen(_path.c_str(), "r");
if (_file == NULL) {
LOG(error) << "Error starting sensor \"" << _name << "\": " << strerror(errno);
return;
}
}
_keepRunning = 1;
_pendingTasks++;
_timer->async_wait(std::bind(&SysfsSingleSensor::readAsync, this));
LOG(info) << "Sensor " << _name << " started.";
}
SysfsSensor::~SysfsSensor() {
// TODO Auto-generated destructor stub
void SysfsSingleSensor::stop() {
_keepRunning = 0;
//wait before closing _file
wait();
if (_file != NULL) {
fclose(_file);
_file = NULL;
}
LOG(info) << "Sensor " << _name << " stopped.";
}
void SysfsSensor::read() {
void SysfsSingleSensor::read() {
reading_t reading;
char buf[1024];
......@@ -56,49 +81,18 @@ void SysfsSensor::read() {
LOG(debug) << _name << ": \"" << reading.value << "\"";
#endif
}
storeReading(reading);
storeReading(reading, _cacheIndex);
_cacheIndex = (_cacheIndex + 1) % _cacheSize;
}
void SysfsSensor::readAsync() {
void SysfsSingleSensor::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(std::bind(&SysfsSensor::readAsync, this));
_timer->async_wait(std::bind(&SysfsSingleSensor::readAsync, this));
}
_pendingTasks--;
}
void SysfsSensor::startPolling() {
if (_keepRunning) {
//we have been started already
LOG(info) << "Sensor " << _name << " already running.";
return;
}
if(_file == NULL) {
_file = fopen(_path.c_str(), "r");
if (_file == NULL) {
LOG(error) << "Error starting sensor \"" << _name << "\": " << strerror(errno);
return;
}
}
_keepRunning = 1;
_pendingTasks++;
_timer->async_wait(std::bind(&SysfsSensor::readAsync, this));
LOG(info) << "Sensor " << _name << " started.";
}
void SysfsSensor::stopPolling() {
_keepRunning = 0;
//wait before closing _file
wait();
if (_file != NULL) {
fclose(_file);
_file = NULL;
}
LOG(info) << "Sensor " << _name << " stopped.";
}
/*
* SysfsSingleSensor.h
*
* Created on: 18.11.2017
* Author: Michael Ott (original), Micha Mueller
*/
#ifndef SYSFSSINGLESENSOR_H_
#define SYSFSSINGLESENSOR_H_
#include "SysfsSensorBase.h"
#include "../../headers/SingleSensor.h"
class SysfsSingleSensor : public SysfsSensorBase, public SingleSensor {
public:
SysfsSingleSensor(const std::string& name);
virtual ~SysfsSingleSensor();
void start() override;
void stop() override;
private:
void read() override;
void readAsync() override;
};
#endif /* SYSFSSINGLESENSOR_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