Commit 6d296707 authored by Alessio Netti's avatar Alessio Netti
Browse files

Adding "retain" option in SysFS to allow for opening/closing files at each sampling interval

parent 4defb951
......@@ -564,6 +564,7 @@ Explanation of the values specific for the sysFS plugin:
|:----- |:----------- |
| path | Path to the sysFS file the sensor should read from. This parameter is mandatory.
| filter | One can define an optional filter if the sysFS file consists of more than only the sensor value. Please note the following points for filters: <br> 1. The filter supports substitutions. For substitution sed syntax ("s/.../.../") is used. Therefore extended regular expressions (ERE) are used as regex-syntax. ERE is closest to Basic RE (BRE), which is actually used by sed, but requires less escaping. <br> 2. If a \ ("backslash") is needed in the regex (for escaping), always use \\ ("double backslash") as the regex is read in as string and strings also escape with backslash <br> 3. Whitespaces are actually used as value separators in the config files. If your filter requires whitespaces either use [[:space:]] in the regex or put it in quotation marks ("") <br> 4. To be able to reference parts of the match (for substitution) use groups. Groups are created with parentheses. <br> 5. If using character classes like [[:digit:]] always make sure to use double brackets ("[[" and "]]") or they will not be recognized. <br> See [ERE-syntax](https://www.gnu.org/software/sed/manual/html_node/ERE-syntax.html#ERE-syntax) <br> See [substitution syntax](http://www.boost.org/doc/libs/1_65_1/libs/regex/doc/html/boost_regex/format/sed_format.html)
| retain | If False, the file is closed and re-opened at every sampling interval. Default is True.
## PDU <a name="pdu"></a>
......
......@@ -41,7 +41,10 @@ void SysfsConfigurator::sensorBase(SysfsSensorBase &s, CFG_VAL config) {
}
void SysfsConfigurator::sensorGroup(SysfsSensorGroup &s, CFG_VAL config) {
ADD {
ATTRIBUTE("path", setPath);
BOOST_FOREACH (boost::property_tree::iptree::value_type &val, config) {
if (boost::iequals(val.first, "path"))
s.setPath(val.second.data());
else if (boost::iequals(val.first, "retain"))
s.setRetain(to_bool(val.second.data()));
}
}
......@@ -35,12 +35,14 @@ SysfsSensorGroup::SysfsSensorGroup(const std::string &name)
: SensorGroupTemplate(name),
_path("") {
_file = NULL;
_retain = true;
}
SysfsSensorGroup::SysfsSensorGroup(const SysfsSensorGroup &other)
: SensorGroupTemplate(other),
_path(other._path) {
_file = NULL;
_retain = other._retain;
}
SysfsSensorGroup::~SysfsSensorGroup() {}
......@@ -49,12 +51,13 @@ SysfsSensorGroup &SysfsSensorGroup::operator=(const SysfsSensorGroup &other) {
SensorGroupTemplate::operator=(other);
_path = other._path;
_file = NULL;
_retain = other._retain;
return *this;
}
bool SysfsSensorGroup::execOnStart() {
if (_file == NULL) {
if (_retain && _file == NULL) {
_file = fopen(_path.c_str(), "r");
if (_file == NULL) {
LOG(error) << "Error starting group\"" << _groupName << "\": " << strerror(errno);
......@@ -65,7 +68,7 @@ bool SysfsSensorGroup::execOnStart() {
}
void SysfsSensorGroup::execOnStop() {
if (_file != NULL) {
if (_retain && _file != NULL) {
fclose(_file);
_file = NULL;
}
......@@ -75,10 +78,24 @@ void SysfsSensorGroup::read() {
reading_t reading;
char buf[1024];
// In case the file needs to be opened and closed at every read
if(!_retain && _file == NULL) {
_file = fopen(_path.c_str(), "r");
if (_file == NULL) {
LOG(error) << "Error opening file " << _path << ":" << strerror(errno);
return;
}
}
fseek(_file, 0, SEEK_SET);
size_t nelem = fread(buf, 1, 1024, _file);
reading.timestamp = getTimestamp();
if (!_retain && _file != NULL) {
fclose(_file);
_file = NULL;
}
if (nelem) {
buf[nelem - 1] = 0;
for (const auto &s : _sensors) {
......
......@@ -47,10 +47,13 @@ class SysfsSensorGroup : public SensorGroupTemplate<SysfsSensorBase> {
bool execOnStart() final override;
void execOnStop() final override;
void setRetain(const bool r) { _retain = r; }
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; }
bool getRetain() { return _retain; }
void printGroupConfig(LOG_LEVEL ll, unsigned int leadingSpaces) final override;
......@@ -59,6 +62,7 @@ class SysfsSensorGroup : public SensorGroupTemplate<SysfsSensorBase> {
std::string _path;
FILE * _file;
bool _retain;
};
#endif /* SYSFSSENSORGROUP_H_ */
Markdown is supported
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