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

sysfs plugin: make path a group attribute

parent f455df24
...@@ -6,9 +6,9 @@ template_group def1 { ...@@ -6,9 +6,9 @@ template_group def1 {
interval 1000 interval 1000
minValues 3 minValues 3
mqttPart 00 mqttPart 00
path /sys/devices/virtual/thermal/thermal_zone2/temp
sensor temp0 { sensor temp0 {
path /sys/devices/virtual/thermal/thermal_zone2/temp
mqttsuffix 00 mqttsuffix 00
; filter "s/Temperature ([[:digit:]]+)\\.([[:digit:]]+)°C/\\1\\200/" ; filter "s/Temperature ([[:digit:]]+)\\.([[:digit:]]+)°C/\\1\\200/"
} }
...@@ -20,9 +20,9 @@ template_group def2 { ...@@ -20,9 +20,9 @@ template_group def2 {
group temp { group temp {
default def1 default def1
path /sys/devices/virtual/thermal/thermal_zone1/temp
sensor temp1 { sensor temp1 {
path /sys/devices/virtual/thermal/thermal_zone1/temp
mqttsuffix 01 mqttsuffix 01
} }
} }
...@@ -30,9 +30,9 @@ group temp { ...@@ -30,9 +30,9 @@ group temp {
group freq { group freq {
interval 1000 interval 1000
minValues 10 minValues 10
path /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
sensor freq0 { sensor freq0 {
path /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
mqttsuffix 0010 mqttsuffix 0010
; filter s/Frequency=([[:digit:]]+)/\\1/ ; filter s/Frequency=([[:digit:]]+)/\\1/
} }
...@@ -40,9 +40,9 @@ group freq { ...@@ -40,9 +40,9 @@ group freq {
group freq { group freq {
default def2 default def2
path /sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq
sensor freq1 { sensor freq1 {
path /sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq
mqttsuffix 0011 mqttsuffix 0011
} }
} }
...@@ -16,13 +16,12 @@ SysfsConfigurator::~SysfsConfigurator() {} ...@@ -16,13 +16,12 @@ SysfsConfigurator::~SysfsConfigurator() {}
void SysfsConfigurator::sensorBase(SysfsSensorBase& s, CFG_VAL config) { void SysfsConfigurator::sensorBase(SysfsSensorBase& s, CFG_VAL config) {
ADD { ADD {
ATTRIBUTE("path", setPath);
ATTRIBUTE("filter", setFilter); ATTRIBUTE("filter", setFilter);
} }
} }
void SysfsConfigurator::sensorGroup(SysfsSensorGroup& s, CFG_VAL config) { void SysfsConfigurator::sensorGroup(SysfsSensorGroup& s, CFG_VAL config) {
ADD { ADD {
//no group attributes currently ATTRIBUTE("path", setPath);
} }
} }
...@@ -16,23 +16,17 @@ class SysfsSensorBase : virtual public SensorBase { ...@@ -16,23 +16,17 @@ class SysfsSensorBase : virtual public SensorBase {
public: public:
SysfsSensorBase(const std::string& name) : SysfsSensorBase(const std::string& name) :
SensorBase(name), SensorBase(name),
_path(""),
_filter(false), _filter(false),
_substitution("") { _substitution("") {
_file = NULL;
//_regx = ""; //_regx = "";
} }
virtual ~SysfsSensorBase() {} virtual ~SysfsSensorBase() {}
const std::string& getPath() const { return _path; }
FILE* getFile() const { return _file; }
bool hasFilter() const { return _filter; } bool hasFilter() const { return _filter; }
std::regex getRegex() const { return _regx; } std::regex getRegex() const { return _regx; }
const std::string& getSubstitution() const { return _substitution; } 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 setFilter(bool filter) { _filter = filter; }
void setFilter(const std::string& filter) { void setFilter(const std::string& filter) {
setFilter(true); setFilter(true);
...@@ -55,12 +49,9 @@ public: ...@@ -55,12 +49,9 @@ public:
void setSubstitution(const std::string& substitution) { _substitution = substitution; } void setSubstitution(const std::string& substitution) { _substitution = substitution; }
protected: protected:
std::string _path;
FILE* _file;
bool _filter; bool _filter;
std::regex _regx; std::regex _regx;
std::string _substitution; std::string _substitution;
}; };
#endif /* SYSFS_SYSFSSENSORBASE_H_ */ #endif /* SYSFS_SYSFSSENSORBASE_H_ */
...@@ -12,7 +12,10 @@ ...@@ -12,7 +12,10 @@
using namespace std; using namespace std;
SysfsSensorGroup::SysfsSensorGroup(const std::string& name) : SysfsSensorGroup::SysfsSensorGroup(const std::string& name) :
SensorGroupTemplate(name) {} SensorGroupTemplate(name),
_path("") {
_file = NULL;
}
SysfsSensorGroup::~SysfsSensorGroup() {} SysfsSensorGroup::~SysfsSensorGroup() {}
...@@ -23,14 +26,11 @@ void SysfsSensorGroup::start() { ...@@ -23,14 +26,11 @@ void SysfsSensorGroup::start() {
return; return;
} }
for(auto s : _sensors) { if(_file == NULL) {
if(s->getFile() == NULL) { _file = fopen(_path.c_str(), "r");
s->setFile(fopen(s->getPath().c_str(), "r")); if (_file == NULL) {
if (s->getFile() == NULL) { LOG(error) << "Error starting group\"" << _groupName << "\": " << strerror(errno);
LOG(error) << "Error starting sensor \"" << s->getName() << "\": " << strerror(errno); return;
LOG(warning) << "Group " << _groupName << " not started due to problematic sensor";
return;
}
} }
} }
...@@ -45,11 +45,9 @@ void SysfsSensorGroup::stop() { ...@@ -45,11 +45,9 @@ void SysfsSensorGroup::stop() {
//wait before closing _file //wait before closing _file
wait(); wait();
for(auto s : _sensors) { if (_file != NULL) {
if (s->getFile() != NULL) { fclose(_file);
fclose(s->getFile()); _file = NULL;
s->setFile(NULL);
}
} }
LOG(info) << "Sensorgroup " << _groupName << " stopped."; LOG(info) << "Sensorgroup " << _groupName << " stopped.";
} }
...@@ -58,15 +56,14 @@ void SysfsSensorGroup::read() { ...@@ -58,15 +56,14 @@ void SysfsSensorGroup::read() {
reading_t reading; reading_t reading;
char buf[1024]; char buf[1024];
fseek(_file, 0, SEEK_SET);
size_t nelem = fread(buf, 1, 1024, _file);
reading.timestamp = getTimestamp(); reading.timestamp = getTimestamp();
for(auto s : _sensors) { if (nelem) {
reading.value = 0; buf[nelem-1] = 0;
fseek(s->getFile(), 0, SEEK_SET); for(auto s : _sensors) {
size_t nelem = fread(buf, 1, 1024, s->getFile()); reading.value = 0;
if (nelem) {
buf[nelem-1] = 0;
try { try {
//filter the payload if necessary //filter the payload if necessary
if(s->hasFilter()) { if(s->hasFilter()) {
...@@ -82,14 +79,16 @@ void SysfsSensorGroup::read() { ...@@ -82,14 +79,16 @@ void SysfsSensorGroup::read() {
//dummy value //dummy value
reading.value = 0; reading.value = 0;
} }
}
#ifdef DEBUG #ifdef DEBUG
LOG(debug) << _groupName << "::" << s->getName() << ": \"" << reading.value << "\""; LOG(debug) << _groupName << "::" << s->getName() << ": \"" << reading.value << "\"";
#endif #endif
//to keep the _cacheIndex uniform for all sensors store value in every case //to keep the _cacheIndex uniform for all sensors store value in every case
s->storeReading(reading, _cacheIndex); s->storeReading(reading, _cacheIndex);
}
_cacheIndex = (_cacheIndex + 1) % _cacheSize;
} else {
LOG(error) << _groupName << " could not read file!";
} }
_cacheIndex = (_cacheIndex + 1) % _cacheSize;
} }
void SysfsSensorGroup::readAsync() { void SysfsSensorGroup::readAsync() {
......
...@@ -20,10 +20,17 @@ public: ...@@ -20,10 +20,17 @@ public:
void start() override; void start() override;
void stop() override; void stop() override;
void setPath(const std::string& path) { _path = path; }
// void setFile(FILE* file) { _file = file; }
// const std::string& getPath() const { return _path; }
// FILE* getFile() const { return _file; }
private: private:
void read() override; void read() override;
void readAsync() override; void readAsync() override;
std::string _path;
FILE* _file;
}; };
#endif /* SYSFSSENSORGROUP_H_ */ #endif /* SYSFSSENSORGROUP_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