File indexing completed on 2024-04-21 05:31:41

0001 /*
0002     SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "SensorDaemonInterface_p.h"
0008 
0009 #include <QDBusPendingCallWatcher>
0010 
0011 #include "systemstats/DBusInterface.h"
0012 
0013 using namespace KSysGuard;
0014 
0015 class SensorDaemonInterface::Private
0016 {
0017 public:
0018     std::unique_ptr<SystemStats::DBusInterface> dbusInterface;
0019     std::unique_ptr<QDBusServiceWatcher> serviceWatcher;
0020     QStringList subscribedSensors;
0021 };
0022 
0023 SensorDaemonInterface::SensorDaemonInterface(QObject *parent)
0024     : QObject(parent)
0025     , d(new Private)
0026 {
0027     qDBusRegisterMetaType<SensorData>();
0028     qDBusRegisterMetaType<SensorInfo>();
0029     qDBusRegisterMetaType<SensorDataList>();
0030     qDBusRegisterMetaType<SensorInfoMap>();
0031 
0032     d->serviceWatcher =
0033         std::make_unique<QDBusServiceWatcher>(SystemStats::ServiceName, QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForUnregistration);
0034     connect(d->serviceWatcher.get(), &QDBusServiceWatcher::serviceUnregistered, this, &SensorDaemonInterface::reconnect);
0035     reconnect();
0036 }
0037 
0038 void KSysGuard::SensorDaemonInterface::reconnect()
0039 {
0040     d->dbusInterface = std::make_unique<SystemStats::DBusInterface>();
0041     connect(d->dbusInterface.get(), &SystemStats::DBusInterface::sensorMetaDataChanged, this, &SensorDaemonInterface::onMetaDataChanged);
0042     connect(d->dbusInterface.get(), &SystemStats::DBusInterface::newSensorData, this, &SensorDaemonInterface::onValueChanged);
0043     connect(d->dbusInterface.get(), &SystemStats::DBusInterface::sensorAdded, this, &SensorDaemonInterface::sensorAdded);
0044     connect(d->dbusInterface.get(), &SystemStats::DBusInterface::sensorRemoved, this, &SensorDaemonInterface::sensorRemoved);
0045     subscribe(d->subscribedSensors);
0046 }
0047 
0048 SensorDaemonInterface::~SensorDaemonInterface()
0049 {
0050 }
0051 
0052 void SensorDaemonInterface::requestMetaData(const QString &sensorId)
0053 {
0054     requestMetaData(QStringList{sensorId});
0055 }
0056 
0057 void SensorDaemonInterface::requestMetaData(const QStringList &sensorIds)
0058 {
0059     auto watcher = new QDBusPendingCallWatcher{d->dbusInterface->sensors(sensorIds), this};
0060     connect(watcher, &QDBusPendingCallWatcher::finished, watcher, [this](QDBusPendingCallWatcher *self) {
0061         self->deleteLater();
0062 
0063         const QDBusPendingReply<SensorInfoMap> reply = *self;
0064         if (reply.isError()) {
0065             return;
0066         }
0067 
0068         const auto infos = reply.value();
0069         for (auto itr = infos.begin(); itr != infos.end(); ++itr) {
0070             Q_EMIT metaDataChanged(itr.key(), itr.value());
0071         }
0072     });
0073 }
0074 
0075 void SensorDaemonInterface::requestValue(const QString &sensorId)
0076 {
0077     auto watcher = new QDBusPendingCallWatcher{d->dbusInterface->sensorData({sensorId}), this};
0078     connect(watcher, &QDBusPendingCallWatcher::finished, watcher, [this](QDBusPendingCallWatcher *self) {
0079         self->deleteLater();
0080 
0081         const QDBusPendingReply<SensorDataList> reply = *self;
0082         if (reply.isError()) {
0083             return;
0084         }
0085 
0086         const auto allData = reply.value();
0087         for (auto data : allData) {
0088             Q_EMIT valueChanged(data.sensorProperty, data.payload);
0089         }
0090     });
0091 }
0092 
0093 QDBusPendingCallWatcher *SensorDaemonInterface::allSensors() const
0094 {
0095     return new QDBusPendingCallWatcher{d->dbusInterface->allSensors()};
0096 }
0097 
0098 void SensorDaemonInterface::subscribe(const QString &sensorId)
0099 {
0100     subscribe(QStringList{sensorId});
0101 }
0102 
0103 void KSysGuard::SensorDaemonInterface::subscribe(const QStringList &sensorIds)
0104 {
0105     d->dbusInterface->subscribe(sensorIds);
0106     d->subscribedSensors.append(sensorIds);
0107 }
0108 
0109 void SensorDaemonInterface::unsubscribe(const QString &sensorId)
0110 {
0111     unsubscribe(QStringList{sensorId});
0112 }
0113 
0114 void KSysGuard::SensorDaemonInterface::unsubscribe(const QStringList &sensorIds)
0115 {
0116     d->dbusInterface->unsubscribe(sensorIds);
0117 }
0118 
0119 SensorDaemonInterface *SensorDaemonInterface::instance()
0120 {
0121     static SensorDaemonInterface instance;
0122     return &instance;
0123 }
0124 
0125 void SensorDaemonInterface::onMetaDataChanged(const QHash<QString, SensorInfo> &metaData)
0126 {
0127     for (auto itr = metaData.begin(); itr != metaData.end(); ++itr) {
0128         Q_EMIT metaDataChanged(itr.key(), itr.value());
0129     }
0130 }
0131 
0132 void SensorDaemonInterface::onValueChanged(const SensorDataList &values)
0133 {
0134     for (auto entry : values) {
0135         Q_EMIT valueChanged(entry.sensorProperty, entry.payload);
0136     }
0137 }