Warning, file /plasma/libksysguard/systemstats/SysFsSensor.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "SysFsSensor.h"
0008 
0009 #include <QFile>
0010 
0011 using namespace KSysGuard;
0012 
0013 class Q_DECL_HIDDEN SysFsSensor::Private
0014 {
0015 public:
0016     QString path;
0017     std::function<QVariant(const QByteArray &)> convertFunction;
0018 };
0019 
0020 SysFsSensor::SysFsSensor(const QString &id, const QString &path, SensorObject *parent)
0021     : SysFsSensor(id, path, QVariant{}, parent)
0022 {
0023 }
0024 
0025 SysFsSensor::SysFsSensor(const QString &id, const QString &path, const QVariant &initialValue, SensorObject *parent)
0026     : SensorProperty(id, id, initialValue, parent)
0027     , d(std::make_unique<Private>())
0028 {
0029     d->path = path;
0030 
0031     d->convertFunction = [](const QByteArray &input) {
0032         return std::atoll(input);
0033     };
0034 }
0035 
0036 SysFsSensor::~SysFsSensor() = default;
0037 
0038 void SysFsSensor::setConvertFunction(const std::function<QVariant(const QByteArray &)> &function)
0039 {
0040     d->convertFunction = function;
0041 }
0042 
0043 void SysFsSensor::update()
0044 {
0045     if (!isSubscribed()) {
0046         return;
0047     }
0048 
0049     QFile file(d->path);
0050     if (!file.exists()) {
0051         return;
0052     }
0053 
0054     if (!file.open(QIODevice::ReadOnly)) {
0055         return;
0056     }
0057 
0058     auto value = file.readAll();
0059     setValue(d->convertFunction(value));
0060 }