File indexing completed on 2024-05-12 17:00:15

0001 /*
0002  * SPDX-FileCopyrightText: 2021 David Redondo <kde@david-redondo.de>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "lmsensors.h"
0008 
0009 #include <systemstats/SensorContainer.h>
0010 #include <systemstats/SensorObject.h>
0011 #include <systemstats/SensorsFeatureSensor.h>
0012 
0013 #include <KLocalizedString>
0014 #include <KPluginFactory>
0015 #include <array>
0016 
0017 #include <sensors/sensors.h>
0018 
0019 LmSensorsPlugin::LmSensorsPlugin(QObject *parent, const QVariantList &args)
0020     : KSysGuard::SensorPlugin(parent, args)
0021 {
0022     auto container = new KSysGuard::SensorContainer(QStringLiteral("lmsensors"), i18n( "Hardware Sensors" ), this);
0023 
0024     const std::array<QByteArray, 3> blacklist{"coretemp","k10temp","amdgpu"}; //already handled by other plugins
0025     int chipNumber = 0;
0026     while (const sensors_chip_name * const chipName = sensors_get_detected_chips(nullptr, &chipNumber)) {
0027         if (std::find(blacklist.cbegin(), blacklist.cend(), chipName->prefix) != blacklist.cend()) {
0028             continue;
0029         }
0030         int requiredBytes  = sensors_snprintf_chip_name(nullptr, 0, chipName) + 1;
0031         QByteArray name;
0032         name.resize(requiredBytes);
0033         sensors_snprintf_chip_name(name.data(), name.size(), chipName);
0034         const QString nameString = QString::fromUtf8(name);
0035         KSysGuard::SensorObject *sensorObject = container->object(nameString);
0036         if (!sensorObject) {
0037             sensorObject = new KSysGuard::SensorObject(nameString, nameString, container);
0038         }
0039         int featureNumber = 0;
0040         while (const sensors_feature * const feature = sensors_get_features(chipName, &featureNumber)) {
0041             const QString id = QString::fromUtf8(feature->name);
0042             if (sensorObject->sensor(id)) {
0043                 continue;
0044             }
0045             if (auto sensor = KSysGuard::makeSensorsFeatureSensor(id, chipName, feature, sensorObject)) {
0046                 m_sensors.push_back(sensor);
0047             }
0048         }
0049     }
0050 }
0051 
0052 LmSensorsPlugin::~LmSensorsPlugin()
0053 {
0054 }
0055 
0056 
0057 QString LmSensorsPlugin::providerName() const
0058 {
0059     return QStringLiteral("lmsensors");
0060 }
0061 
0062 void LmSensorsPlugin::update()
0063 {
0064     for (auto sensor : qAsConst(m_sensors)) {
0065         sensor->update();
0066     }
0067 }
0068 
0069 K_PLUGIN_CLASS_WITH_JSON(LmSensorsPlugin, "metadata.json")
0070 #include "lmsensors.moc"