Commit 08e9967c authored by David Redondo's avatar David Redondo 🏎

Move SysctlSensor to libkstats

It can be useful when a sensor property can be obtained by directly reading from
sysctl on FreeBSD. Add a conversion function to it. Also add a simple base struct
so it is possible to store SysctlSensors of different types in a container.
parent 6b67279d
......@@ -48,7 +48,7 @@ public:
* 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();
void update() override;
private:
QString m_path;
......
......@@ -22,13 +22,37 @@
#include <SensorProperty.h>
/**
* Convenience subclass of SensorProperty that reads a value of type T from FreeBSD's sysctl interface
*/
template <typename T>
class SysctlSensor : public SensorProperty {
public:
/**
* Contstrucor.
* @param sysctlName The name of the sysctl entry
*/
SysctlSensor(const QString &id, const QByteArray &sysctlName, SensorObject *parent);
void update();
SysctlSensor(const QString &id, const QString &name, const QByteArray &sysctlName, SensorObject *parent);
/**
* Update this sensor.
*
* This will cause the sensor to call syscctl and update the value from that.
* It should be called periodically so values are updated properly.
*/
void update() override;
/**
* Set the function used to convert the data from sysctl to the value of this sensor.
*
* This accepts a function that takes a reference to an object of type T and converts that to a
* QVariant. Use this if you need to convert the value into another unit or do a calculation to
* arrive at the sensor value. By default the value is stored as is in a QVariant.
*/
void setConversionFunction(const std::function<QVariant(const T&)> &function) {m_conversionFunction = function;}
private:
const QByteArray m_sysctlName;
std::function<QVariant(const T&)> m_conversionFunction = [](const T& t){return QVariant::fromValue(t);};
};
#ifdef Q_OS_FREEBSD
......@@ -42,16 +66,23 @@ SysctlSensor<T>::SysctlSensor(const QString& id, const QByteArray &sysctlName, S
{
}
template<typename T>
SysctlSensor<T>::SysctlSensor(const QString& id, const QString& name, const QByteArray& sysctlName, SensorObject* parent)
: SensorProperty(id, name, parent)
, m_sysctlName(sysctlName)
{
}
template <typename T>
void SysctlSensor<T>::update()
{
if (!isSubscribed()) {
return;
}
T value;
T value{};
size_t size = sizeof(T);
if (sysctlbyname(m_sysctlName.constData(), &value, &size, nullptr, 0) != -1) {
setValue(value);
setValue(m_conversionFunction(value));
}
}
#endif
......
......@@ -19,8 +19,6 @@
#include "freebsdcpuplugin.h"
#include "sysctlsensor.h"
#include <algorithm>
#include <vector>
......@@ -31,6 +29,7 @@
#include <KLocalizedString>
#include <SensorContainer.h>
#include <SysctlSensor.h>
template <typename T>
bool readSysctl(const char *name, T *buffer, size_t size = sizeof(T)) {
......
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