Commit 91ba181a authored by Arjen Hiemstra's avatar Arjen Hiemstra
Browse files

Move SysFsSensor to libkstats and document it more properly

parent 390c02f6
......@@ -4,6 +4,8 @@ set(ksgrdbackend_LIB_SRCS
SensorContainer.cpp
SensorPlugin.cpp
SensorProperty.cpp
SysFsSensor.cpp
)
add_library(ksgrdbackend ${ksgrdbackend_LIB_SRCS})
......
/*
Copyright (c) 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "SysFsSensor.h"
#include <QFile>
SysFsSensor::SysFsSensor(const QString& id, const QString& path, SensorObject* parent)
: SensorProperty(id, parent)
{
m_path = path;
m_convertFunction = [](const QByteArray &input) {
return std::atoll(input);
};
}
void SysFsSensor::setConvertFunction(const std::function<QVariant (const QByteArray &)>& function)
{
m_convertFunction = function;
}
void SysFsSensor::update()
{
if (!isSubscribed()) {
return;
}
QFile file(m_path);
if (!file.exists()) {
return;
}
if (!file.open(QIODevice::ReadOnly)) {
return;
}
auto value = file.readAll();
setValue(m_convertFunction(value));
}
/*
Copyright (c) 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#pragma once
#include <QObject>
#include "SensorProperty.h"
/**
* Convenience subclass of SensorProperty that reads a sysfs file and uses the result as value.
*/
class Q_DECL_EXPORT SysFsSensor : public SensorProperty
{
Q_OBJECT
public:
SysFsSensor(const QString &id, const QString &path, SensorObject *parent);
/**
* Set the function used to convert the data from sysfs to the value of this sensor.
*
* This accepts a function that takes a QByteArray and converts that to a QVariant.
* By default this is set to `std::atoll` or in other words, any numeric value
* should automatically be converted to a proper QVariant.
*/
void setConvertFunction(const std::function<QVariant(const QByteArray&)> &function);
/**
* Update this sensor.
*
* This will cause the sensor to read sysfs and update the value from that.
* It should be called periodically so values are updated properly.
*/
void update();
private:
QString m_path;
std::function<QVariant(const QByteArray&)> m_convertFunction;
};
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