diff --git a/sources/awesome-widget/plugin/awdataengineaggregator.cpp b/sources/awesome-widget/plugin/awdataengineaggregator.cpp index 7a0b85d..4e86701 100644 --- a/sources/awesome-widget/plugin/awdataengineaggregator.cpp +++ b/sources/awesome-widget/plugin/awdataengineaggregator.cpp @@ -17,15 +17,17 @@ #include "awdataengineaggregator.h" -#include #include #include +#include + #include "awdebug.h" AWDataEngineAggregator::AWDataEngineAggregator(QObject *_parent) : QObject(_parent) + , m_interface(new KSysGuard::SystemStats::DBusInterface()) { qCDebug(LOG_AW) << __PRETTY_FUNCTION__; @@ -34,12 +36,14 @@ AWDataEngineAggregator::AWDataEngineAggregator(QObject *_parent) qDBusRegisterMetaType(); qDBusRegisterMetaType>(); - m_interface = new KSysGuard::SystemStats::DBusInterface(); - - connect(m_interface, &KSysGuard::SystemStats::DBusInterface::newSensorData, this, &AWDataEngineAggregator::updateData); - connect(m_interface, &KSysGuard::SystemStats::DBusInterface::sensorMetaDataChanged, this, &AWDataEngineAggregator::updateSensors); - connect(m_interface, &KSysGuard::SystemStats::DBusInterface::sensorAdded, this, &AWDataEngineAggregator::sensorAdded); - connect(m_interface, &KSysGuard::SystemStats::DBusInterface::sensorRemoved, this, &AWDataEngineAggregator::sensorRemoved); + connect(m_interface, &KSysGuard::SystemStats::DBusInterface::newSensorData, this, + &AWDataEngineAggregator::updateData); + connect(m_interface, &KSysGuard::SystemStats::DBusInterface::sensorMetaDataChanged, this, + &AWDataEngineAggregator::updateSensors); + connect(m_interface, &KSysGuard::SystemStats::DBusInterface::sensorAdded, this, + &AWDataEngineAggregator::sensorAdded); + connect(m_interface, &KSysGuard::SystemStats::DBusInterface::sensorRemoved, this, + &AWDataEngineAggregator::sensorRemoved); loadSources(); } @@ -54,9 +58,20 @@ AWDataEngineAggregator::~AWDataEngineAggregator() } +void AWDataEngineAggregator::connectSources() +{ + auto keys = m_sensors.keys(); + auto newKeys = QSet(keys.cbegin(), keys.cend()) - m_subscribed; + + m_interface->subscribe(newKeys.values()).waitForFinished(); + m_subscribed.unite(newKeys); +} + + void AWDataEngineAggregator::disconnectSources() { - m_interface->unsubscribe(m_sensors.keys()); + m_interface->unsubscribe(m_subscribed.values()).waitForFinished(); + m_subscribed.clear(); } @@ -73,15 +88,7 @@ void AWDataEngineAggregator::loadSources() auto sensors = response.value(); updateSensors(sensors); -} - - -void AWDataEngineAggregator::reconnectSources(const int interval) -{ - qCDebug(LOG_AW) << "Reconnect all sensors with update interval" << interval; - - disconnectSources(); - m_interface->subscribe(m_sensors.keys()); + connectSources(); } @@ -89,7 +96,10 @@ void AWDataEngineAggregator::dropSource(const QString &_source) { qCDebug(LOG_AW) << "Disconnect sensor" << _source; - m_interface->unsubscribe({_source}); + if (m_subscribed.contains(_source)) { + m_interface->unsubscribe({_source}).waitForFinished(); + m_subscribed.remove(_source); + } } @@ -100,13 +110,17 @@ void AWDataEngineAggregator::sensorAdded(const QString &_sensor) // check if sensor is actually valid auto response = m_interface->sensors({_sensor}); response.waitForFinished(); - auto info = response.value(); - if (info.count() != 1) + + auto info = response.value().value(_sensor); + if (!isValidSensor(info)) return; - qCWarning(LOG_AW) << info.keys(); - - m_interface->subscribe({_sensor}); + m_sensors[_sensor] = info; + dropSource(_sensor); // force reconnect + if (!m_subscribed.contains(_sensor)) { + m_interface->subscribe({_sensor}).waitForFinished(); + m_subscribed.insert(_sensor); + } } @@ -115,9 +129,10 @@ void AWDataEngineAggregator::sensorRemoved(const QString &_sensor) qCDebug(LOG_AW) << "Sensor" << _sensor << "has been removed"; m_sensors.remove(_sensor); - m_interface->unsubscribe({_sensor}); + dropSource(_sensor); } + void AWDataEngineAggregator::updateData(KSysGuard::SensorDataList _data) { emit(dataUpdated(m_sensors, _data)); diff --git a/sources/awesome-widget/plugin/awdataengineaggregator.h b/sources/awesome-widget/plugin/awdataengineaggregator.h index fcdab62..244811a 100644 --- a/sources/awesome-widget/plugin/awdataengineaggregator.h +++ b/sources/awesome-widget/plugin/awdataengineaggregator.h @@ -15,9 +15,7 @@ * along with awesome-widgets. If not, see http://www.gnu.org/licenses/ * ***************************************************************************/ - -#ifndef AWDATAENGINEAGGREGATOR_H -#define AWDATAENGINEAGGREGATOR_H +#pragma once #include @@ -31,7 +29,6 @@ namespace KSysGuard::SystemStats class DBusInterface; } - class AWDataEngineAggregator : public QObject { Q_OBJECT @@ -39,10 +36,10 @@ class AWDataEngineAggregator : public QObject public: explicit AWDataEngineAggregator(QObject *_parent = nullptr); ~AWDataEngineAggregator() override; + void connectSources(); void disconnectSources(); - [[nodiscard]] bool isValidSensor(const KSysGuard::SensorInfo &_sensor); + [[nodiscard]] static bool isValidSensor(const KSysGuard::SensorInfo &_sensor); void loadSources(); - void reconnectSources(const int interval); signals: void dataUpdated(const QHash &_sensors, const KSysGuard::SensorDataList &_data); @@ -58,7 +55,5 @@ public slots: private: KSysGuard::SystemStats::DBusInterface *m_interface = nullptr; QHash m_sensors; + QSet m_subscribed; }; - - -#endif /* AWDATAENGINEAGGREGATOR_H */ diff --git a/sources/awesome-widget/plugin/awdataenginemapper.cpp b/sources/awesome-widget/plugin/awdataenginemapper.cpp index b2226f2..197d9d4 100644 --- a/sources/awesome-widget/plugin/awdataenginemapper.cpp +++ b/sources/awesome-widget/plugin/awdataenginemapper.cpp @@ -347,7 +347,8 @@ QStringList AWDataEngineMapper::registerSource(const QString &_source, const KSy // gb m_map.insert(_source, "swapgb"); m_formatter["swapgb"] = AWKeysAggregator::FormatterType::MemGBFormat; - } else if (_source.startsWith("lmsensors/") || _source.contains(cpuTempRegExp) || _source == "cpu/all/averageTemperature") { + } else if (_source.startsWith("lmsensors/") || _source.contains(cpuTempRegExp) + || _source == "cpu/all/averageTemperature") { // temperature auto index = m_devices["temp"].indexOf(_source); // HACK on DE initialization there are no units key diff --git a/sources/awesome-widget/plugin/awkeyoperations.cpp b/sources/awesome-widget/plugin/awkeyoperations.cpp index d4bb085..01abdf2 100644 --- a/sources/awesome-widget/plugin/awkeyoperations.cpp +++ b/sources/awesome-widget/plugin/awkeyoperations.cpp @@ -314,7 +314,8 @@ void AWKeyOperations::addDevice(const QString &_source) QString device = _source; device.remove("partitions").remove("/filllevel"); addKeyToCache("mount", device); - } else if (_source.startsWith("lmsensors") || _source.contains(cpuTempRegExp) || _source == "cpu/all/averageTemperature") { + } else if (_source.startsWith("lmsensors") || _source.contains(cpuTempRegExp) + || _source == "cpu/all/averageTemperature") { addKeyToCache("temp", _source); } } diff --git a/sources/awesome-widget/plugin/awkeys.cpp b/sources/awesome-widget/plugin/awkeys.cpp index d3bfbfa..05054c8 100644 --- a/sources/awesome-widget/plugin/awkeys.cpp +++ b/sources/awesome-widget/plugin/awkeys.cpp @@ -60,7 +60,8 @@ AWKeys::AWKeys(QObject *_parent) connect(m_timer, &QTimer::timeout, this, &AWKeys::updateTextData); // transfer signal from AWDataAggregator object to QML ui - connect(m_dataAggregator, &AWDataAggregator::toolTipPainted, [this](const QString &_tooltip) { emit(needToolTipToBeUpdated(_tooltip)); }); + connect(m_dataAggregator, &AWDataAggregator::toolTipPainted, + [this](const QString &_tooltip) { emit(needToolTipToBeUpdated(_tooltip)); }); connect(this, &AWKeys::dropSourceFromDataengine, m_dataEngineAggregator, &AWDataEngineAggregator::dropSource); connect(m_dataEngineAggregator, &AWDataEngineAggregator::dataUpdated, this, &AWKeys::dataUpdated); @@ -102,7 +103,6 @@ void AWKeys::initKeys(const QString &_currentPattern, const int _interval, const m_aggregator->initFormatters(); m_keyOperator->setPattern(_currentPattern); m_keyOperator->updateCache(); - m_dataEngineAggregator->reconnectSources(_interval); // timer m_timer->setInterval(_interval); diff --git a/sources/extsysmonsources/extsysmonsensor.cpp b/sources/extsysmonsources/extsysmonsensor.cpp index 74342f9..e8b5c50 100644 --- a/sources/extsysmonsources/extsysmonsensor.cpp +++ b/sources/extsysmonsources/extsysmonsensor.cpp @@ -67,7 +67,6 @@ void ExtSysMonSensor::update() continue; // skip properties which are not explicitly subscribed auto value = m_source->data(source); - qCWarning(LOG_ESS) << source << value; property->setValue(value); } } diff --git a/sources/extsysmonsources/networksource.cpp b/sources/extsysmonsources/networksource.cpp index c8c166e..3d62b42 100644 --- a/sources/extsysmonsources/networksource.cpp +++ b/sources/extsysmonsources/networksource.cpp @@ -52,7 +52,7 @@ NetworkSource::~NetworkSource() QVariant NetworkSource::data(const QString &_source) { - qCWarning(LOG_ESS) << "Source" << _source; + qCDebug(LOG_ESS) << "Source" << _source; if (!m_values.contains(_source)) run(); @@ -82,7 +82,9 @@ KSysGuard::SensorInfo *NetworkSource::initialData(const QString &_source) const void NetworkSource::run() { m_values["device"] = NetworkSource::getCurrentDevice(); - m_process->start("iwgetid", {"-r"}); + if (m_process->state() == QProcess::ProcessState::NotRunning) { + m_process->start("iwgetid", {"-r"}); + } } diff --git a/sources/extsysmonsources/weathersource.cpp b/sources/extsysmonsources/weathersource.cpp index 678fcc9..ee76e56 100644 --- a/sources/extsysmonsources/weathersource.cpp +++ b/sources/extsysmonsources/weathersource.cpp @@ -49,12 +49,10 @@ QVariant WeatherSource::data(const QString &_source) int ind = index(_source); if (!m_values.contains(_source)) { - QVariantHash data = m_extWeather->itemByTagNumber(ind)->run(); - for (auto &key : data.keys()) - m_values[key] = data[key]; + auto data = m_extWeather->itemByTagNumber(ind)->run(); + m_values.insert(data); } - QVariant value = m_values.take(_source); - return value; + return m_values.take(_source); } @@ -94,7 +92,7 @@ KSysGuard::SensorInfo *WeatherSource::initialData(const QString &_source) const data->unit = KSysGuard::UnitCelsius; } else if (_source.startsWith("timestamp")) { data->name = QString("Timestamp for '%1'").arg(m_extWeather->itemByTagNumber(ind)->uniq()); - data->variantType = QVariant::String; + data->variantType = QVariant::DateTime; data->unit = KSysGuard::UnitNone; }