File indexing completed on 2024-05-12 13:32:06
0001 /* 0002 SPDX-FileCopyrightText: 2019 David Edmundson <davidedmundson@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 #include "client.h" 0008 0009 #include <QDBusConnection> 0010 #include <QDBusMessage> 0011 0012 #include <algorithm> 0013 0014 #include <systemstats/DBusInterface.h> 0015 #include <systemstats/SensorObject.h> 0016 #include <systemstats/SensorPlugin.h> 0017 #include <systemstats/SensorProperty.h> 0018 0019 #include "daemon.h" 0020 0021 Client::Client(Daemon *parent, const QString &serviceName) 0022 : QObject(parent) 0023 , m_serviceName(serviceName) 0024 , m_daemon(parent) 0025 { 0026 connect(m_daemon, &Daemon::sensorRemoved, this, [this](const QString &sensor) { 0027 m_subscribedSensors.remove(sensor); 0028 }); 0029 } 0030 0031 Client::~Client() 0032 { 0033 for (auto sensor : qAsConst(m_subscribedSensors)) { 0034 sensor->unsubscribe(); 0035 } 0036 } 0037 0038 void Client::subscribeSensors(const QStringList &sensorPaths) 0039 { 0040 KSysGuard::SensorDataList entries; 0041 0042 for (const QString &sensorPath : sensorPaths) { 0043 if (auto sensor = m_daemon->findSensor(sensorPath)) { 0044 m_connections.insert(sensor, connect(sensor, &KSysGuard::SensorProperty::valueChanged, this, [this, sensor]() { 0045 const QVariant value = sensor->value(); 0046 if (!value.isValid()) { 0047 return; 0048 } 0049 m_pendingUpdates << KSysGuard::SensorData(sensor->path(), value); 0050 })); 0051 m_connections.insert(sensor, connect(sensor, &KSysGuard::SensorProperty::sensorInfoChanged, this, [this, sensor]() { 0052 m_pendingMetaDataChanges[sensor->path()] = sensor->info(); 0053 })); 0054 0055 sensor->subscribe(); 0056 0057 m_subscribedSensors.insert(sensorPath, sensor); 0058 } 0059 } 0060 } 0061 0062 void Client::unsubscribeSensors(const QStringList &sensorPaths) 0063 { 0064 for (const QString &sensorPath : sensorPaths) { 0065 if (auto sensor = m_subscribedSensors.take(sensorPath)) { 0066 disconnect(m_connections.take(sensor)); 0067 disconnect(m_connections.take(sensor)); 0068 sensor->unsubscribe(); 0069 } 0070 } 0071 } 0072 0073 void Client::sendFrame() 0074 { 0075 sendMetaDataChanged(m_pendingMetaDataChanges); 0076 sendValues(m_pendingUpdates); 0077 m_pendingUpdates.clear(); 0078 m_pendingMetaDataChanges.clear(); 0079 } 0080 0081 void Client::sendValues(const KSysGuard::SensorDataList &entries) 0082 { 0083 if (entries.isEmpty()) { 0084 return; 0085 } 0086 auto msg = QDBusMessage::createTargetedSignal(m_serviceName, 0087 KSysGuard::SystemStats::ObjectPath, 0088 KSysGuard::SystemStats::DBusInterface::staticInterfaceName(), 0089 "newSensorData"); 0090 msg.setArguments({QVariant::fromValue(entries)}); 0091 QDBusConnection::sessionBus().send(msg); 0092 } 0093 0094 void Client::sendMetaDataChanged(const KSysGuard::SensorInfoMap &sensors) 0095 { 0096 if (sensors.isEmpty()) { 0097 return; 0098 } 0099 auto msg = QDBusMessage::createTargetedSignal(m_serviceName, 0100 KSysGuard::SystemStats::ObjectPath, 0101 KSysGuard::SystemStats::DBusInterface::staticInterfaceName(), 0102 "sensorMetaDataChanged"); 0103 msg.setArguments({QVariant::fromValue(sensors)}); 0104 QDBusConnection::sessionBus().send(msg); 0105 }