Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
dcdb
dcdb
Commits
c317f466
Commit
c317f466
authored
Oct 18, 2018
by
Micha Mueller
Browse files
sysfs plugin: make path a group attribute
parent
f455df24
Changes
5
Hide whitespace changes
Inline
Side-by-side
config/sysfs.conf
View file @
c317f466
...
...
@@ -6,9 +6,9 @@ template_group def1 {
interval
1000
minValues
3
mqttPart
00
path
/
sys
/
devices
/
virtual
/
thermal
/
thermal_zone2
/
temp
sensor
temp0
{
path
/
sys
/
devices
/
virtual
/
thermal
/
thermal_zone2
/
temp
mqttsuffix
00
;
filter
"s/Temperature ([[:digit:]]+)\\.([[:digit:]]+)°C/\\1\\200/"
}
...
...
@@ -20,9 +20,9 @@ template_group def2 {
group
temp
{
default
def1
path
/
sys
/
devices
/
virtual
/
thermal
/
thermal_zone1
/
temp
sensor
temp1
{
path
/
sys
/
devices
/
virtual
/
thermal
/
thermal_zone1
/
temp
sensor
temp1
{
mqttsuffix
01
}
}
...
...
@@ -30,9 +30,9 @@ group temp {
group
freq
{
interval
1000
minValues
10
path
/
sys
/
devices
/
system
/
cpu
/
cpu0
/
cpufreq
/
scaling_cur_freq
sensor
freq0
{
path
/
sys
/
devices
/
system
/
cpu
/
cpu0
/
cpufreq
/
scaling_cur_freq
sensor
freq0
{
mqttsuffix
0010
;
filter
s
/
Frequency
=([[:
digit
:]]+)/\\
1
/
}
...
...
@@ -40,9 +40,9 @@ group freq {
group
freq
{
default
def2
path
/
sys
/
devices
/
system
/
cpu
/
cpu1
/
cpufreq
/
scaling_cur_freq
sensor
freq1
{
path
/
sys
/
devices
/
system
/
cpu
/
cpu1
/
cpufreq
/
scaling_cur_freq
mqttsuffix
0011
}
}
src/sensors/sysfs/SysfsConfigurator.cpp
View file @
c317f466
...
...
@@ -16,13 +16,12 @@ SysfsConfigurator::~SysfsConfigurator() {}
void
SysfsConfigurator
::
sensorBase
(
SysfsSensorBase
&
s
,
CFG_VAL
config
)
{
ADD
{
ATTRIBUTE
(
"path"
,
setPath
);
ATTRIBUTE
(
"filter"
,
setFilter
);
}
}
void
SysfsConfigurator
::
sensorGroup
(
SysfsSensorGroup
&
s
,
CFG_VAL
config
)
{
ADD
{
//no group attributes currently
ATTRIBUTE
(
"path"
,
setPath
);
}
}
src/sensors/sysfs/SysfsSensorBase.h
View file @
c317f466
...
...
@@ -16,23 +16,17 @@ 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
setFilter
(
const
std
::
string
&
filter
)
{
setFilter
(
true
);
...
...
@@ -55,12 +49,9 @@ public:
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_ */
src/sensors/sysfs/SysfsSensorGroup.cpp
View file @
c317f466
...
...
@@ -12,7 +12,10 @@
using
namespace
std
;
SysfsSensorGroup
::
SysfsSensorGroup
(
const
std
::
string
&
name
)
:
SensorGroupTemplate
(
name
)
{}
SensorGroupTemplate
(
name
),
_path
(
""
)
{
_file
=
NULL
;
}
SysfsSensorGroup
::~
SysfsSensorGroup
()
{}
...
...
@@ -23,14 +26,11 @@ void SysfsSensorGroup::start() {
return
;
}
for
(
auto
s
:
_sensors
)
{
if
(
s
->
getFile
()
==
NULL
)
{
s
->
setFile
(
fopen
(
s
->
getPath
().
c_str
(),
"r"
));
if
(
s
->
getFile
()
==
NULL
)
{
LOG
(
error
)
<<
"Error starting sensor
\"
"
<<
s
->
getName
()
<<
"
\"
: "
<<
strerror
(
errno
);
LOG
(
warning
)
<<
"Group "
<<
_groupName
<<
" not started due to problematic sensor"
;
return
;
}
if
(
_file
==
NULL
)
{
_file
=
fopen
(
_path
.
c_str
(),
"r"
);
if
(
_file
==
NULL
)
{
LOG
(
error
)
<<
"Error starting group
\"
"
<<
_groupName
<<
"
\"
: "
<<
strerror
(
errno
);
return
;
}
}
...
...
@@ -45,11 +45,9 @@ void SysfsSensorGroup::stop() {
//wait before closing _file
wait
();
for
(
auto
s
:
_sensors
)
{
if
(
s
->
getFile
()
!=
NULL
)
{
fclose
(
s
->
getFile
());
s
->
setFile
(
NULL
);
}
if
(
_file
!=
NULL
)
{
fclose
(
_file
);
_file
=
NULL
;
}
LOG
(
info
)
<<
"Sensorgroup "
<<
_groupName
<<
" stopped."
;
}
...
...
@@ -58,15 +56,14 @@ void SysfsSensorGroup::read() {
reading_t
reading
;
char
buf
[
1024
];
fseek
(
_file
,
0
,
SEEK_SET
);
size_t
nelem
=
fread
(
buf
,
1
,
1024
,
_file
);
reading
.
timestamp
=
getTimestamp
();
for
(
auto
s
:
_sensors
)
{
reading
.
value
=
0
;
fseek
(
s
->
getFile
(),
0
,
SEEK_SET
);
size_t
nelem
=
fread
(
buf
,
1
,
1024
,
s
->
getFile
());
if
(
nelem
)
{
buf
[
nelem
-
1
]
=
0
;
if
(
nelem
)
{
buf
[
nelem
-
1
]
=
0
;
for
(
auto
s
:
_sensors
)
{
reading
.
value
=
0
;
try
{
//filter the payload if necessary
if
(
s
->
hasFilter
())
{
...
...
@@ -82,14 +79,16 @@ void SysfsSensorGroup::read() {
//dummy value
reading
.
value
=
0
;
}
}
#ifdef DEBUG
LOG
(
debug
)
<<
_groupName
<<
"::"
<<
s
->
getName
()
<<
":
\"
"
<<
reading
.
value
<<
"
\"
"
;
LOG
(
debug
)
<<
_groupName
<<
"::"
<<
s
->
getName
()
<<
":
\"
"
<<
reading
.
value
<<
"
\"
"
;
#endif
//to keep the _cacheIndex uniform for all sensors store value in every case
s
->
storeReading
(
reading
,
_cacheIndex
);
//to keep the _cacheIndex uniform for all sensors store value in every case
s
->
storeReading
(
reading
,
_cacheIndex
);
}
_cacheIndex
=
(
_cacheIndex
+
1
)
%
_cacheSize
;
}
else
{
LOG
(
error
)
<<
_groupName
<<
" could not read file!"
;
}
_cacheIndex
=
(
_cacheIndex
+
1
)
%
_cacheSize
;
}
void
SysfsSensorGroup
::
readAsync
()
{
...
...
src/sensors/sysfs/SysfsSensorGroup.h
View file @
c317f466
...
...
@@ -20,10 +20,17 @@ public:
void
start
()
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:
void
read
()
override
;
void
readAsync
()
override
;
std
::
string
_path
;
FILE
*
_file
;
};
#endif
/* SYSFSSENSORGROUP_H_ */
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment