diff --git a/plugin/chargingsessionsdbusinterfaceclient.cpp b/plugin/chargingsessionsdbusinterfaceclient.cpp new file mode 100644 index 0000000..03dd5be --- /dev/null +++ b/plugin/chargingsessionsdbusinterfaceclient.cpp @@ -0,0 +1,123 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +* +* Copyright (C) 2013 - 2024, nymea GmbH +* Copyright (C) 2024 - 2025, chargebyte austria GmbH +* +* This file is part of nymea-energy-plugin-chargingsessions. +* +* nymea-energy-plugin-chargingsessions is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* nymea-energy-plugin-chargingsessions is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with nymea-energy-plugin-chargingsessions. If not, see . +* +* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include "chargingsessionsdbusinterfaceclient.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +Q_DECLARE_LOGGING_CATEGORY(dcChargingSessions) + +static const QString kDbusService = QStringLiteral("io.nymea.energy.chargingsessions"); +static const QString kDbusPath = QStringLiteral("/io/nymea/energy/chargingsessions"); +static const QString kDbusInterface = QStringLiteral("io.nymea.energy.chargingsessions"); + +ChargingSessionsDBusInterfaceClient::ChargingSessionsDBusInterfaceClient(QObject *parent) : + QObject(parent), + m_connection(QDBusConnection::systemBus()) +{ + if (!m_connection.isConnected()) { + qCWarning(dcChargingSessions()) << "DBus system bus not connected"; + } +} + +ChargingSessionsDBusInterfaceClient::~ChargingSessionsDBusInterfaceClient() +{ + delete m_interface; +} + +QList ChargingSessionsDBusInterfaceClient::sessions() const +{ + return m_sessions; +} + +void ChargingSessionsDBusInterfaceClient::getSessions(const QStringList &carThingIds) +{ + if (!ensureInterface()) { + emit errorOccurred(QStringLiteral("Charging sessions DBus interface is not available")); + return; + } + + QDBusPendingCall call = m_interface->asyncCall(QStringLiteral("GetSessions"), carThingIds); + QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(call, this); + connect(watcher, &QDBusPendingCallWatcher::finished, this, &ChargingSessionsDBusInterfaceClient::onCallFinished); +} + +void ChargingSessionsDBusInterfaceClient::onCallFinished(QDBusPendingCallWatcher *watcher) +{ + QDBusPendingReply reply = *watcher; + watcher->deleteLater(); + + if (reply.isError()) { + qCWarning(dcChargingSessions()) << "GetSessions DBus call failed:" << reply.error().message(); + emit errorOccurred(reply.error().message()); + return; + } + + QList sessions; + const QVariantList values = reply.value(); + for (const QVariant &value : values) { + if (value.canConvert()) { + sessions.append(value.toMap()); + continue; + } + + const QDBusArgument arg = value.value(); + sessions.append(qdbus_cast(arg)); + } + + m_sessions = sessions; + emit sessionsReceived(m_sessions); +} + +bool ChargingSessionsDBusInterfaceClient::ensureInterface() +{ + if (m_interface && m_interface->isValid()) { + return true; + } + + delete m_interface; + m_interface = nullptr; + + if (!m_connection.isConnected()) { + qCWarning(dcChargingSessions()) << "DBus system bus not connected"; + return false; + } + + m_interface = new QDBusInterface(kDbusService, kDbusPath, kDbusInterface, m_connection, this); + if (!m_interface->isValid()) { + qCWarning(dcChargingSessions()) << "Charging sessions DBus interface is not available:" << m_connection.lastError().message(); + delete m_interface; + m_interface = nullptr; + return false; + } + + return true; +} diff --git a/plugin/chargingsessionsdbusinterfaceclient.h b/plugin/chargingsessionsdbusinterfaceclient.h new file mode 100644 index 0000000..aacd161 --- /dev/null +++ b/plugin/chargingsessionsdbusinterfaceclient.h @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +* +* Copyright (C) 2013 - 2024, nymea GmbH +* Copyright (C) 2024 - 2025, chargebyte austria GmbH +* +* This file is part of nymea-energy-plugin-chargingsessions. +* +* nymea-energy-plugin-chargingsessions is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* nymea-energy-plugin-chargingsessions is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with nymea-energy-plugin-chargingsessions. If not, see . +* +* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#pragma once + +#include +#include +#include +#include +#include + +class QDBusInterface; +class QDBusPendingCallWatcher; + +class ChargingSessionsDBusInterfaceClient : public QObject +{ + Q_OBJECT +public: + explicit ChargingSessionsDBusInterfaceClient(QObject *parent = nullptr); + ~ChargingSessionsDBusInterfaceClient(); + + QList sessions() const; + +public slots: + void getSessions(const QStringList &carThingIds = QStringList()); + +signals: + void sessionsReceived(const QList &sessions); + void errorOccurred(const QString &message); + +private slots: + void onCallFinished(QDBusPendingCallWatcher *watcher); + +private: + bool ensureInterface(); + + QDBusConnection m_connection; + QDBusInterface *m_interface = nullptr; + QList m_sessions; +}; diff --git a/plugin/evdashengine.cpp b/plugin/evdashengine.cpp index 8ec50cc..4f90df4 100644 --- a/plugin/evdashengine.cpp +++ b/plugin/evdashengine.cpp @@ -32,6 +32,7 @@ #include "evdashsettings.h" #include "evdashwebserverresource.h" #include "energymanagerdbusclient.h" +#include "chargingsessionsdbusinterfaceclient.h" #include @@ -102,7 +103,21 @@ EvDashEngine::EvDashEngine(ThingManager *thingManager, EvDashWebServerResource * bool enabled = settings.value("enabled", false).toBool(); settings.endGroup(); + // ChargingSessions client for fetching charging sessions + m_chargingSessionsClient = new ChargingSessionsDBusInterfaceClient(this); + connect(m_chargingSessionsClient, &ChargingSessionsDBusInterfaceClient::sessionsReceived, this, [](const QList &chargingSessions){ + qCDebug(dcEvDashExperience()) << "ChargingSessions :"; + foreach (const QVariant &ciVariant, chargingSessions) { + qCDebug(dcEvDashExperience()) << "-->" << ciVariant.toMap(); + } + }); + connect(m_chargingSessionsClient, &ChargingSessionsDBusInterfaceClient::errorOccurred, this, [](const QString &errorMessage){ + qCWarning(dcEvDashExperience()) << "Charging sessions DBus client error occurred:" << errorMessage; + }); + + + // Energy manager client for associated cars and current mode m_energyManagerClient = new EnergyManagerDbusClient(this); connect(m_energyManagerClient, &EnergyManagerDbusClient::chargingInfosUpdated, this, [](const QVariantList &chargingInfos){ qCDebug(dcEvDashExperience()) << "ChargingInfos:"; @@ -132,7 +147,7 @@ EvDashEngine::EvDashEngine(ThingManager *thingManager, EvDashWebServerResource * }); connect(m_energyManagerClient, &EnergyManagerDbusClient::errorOccurred, this, [](const QString &errorMessage){ - qCWarning(dcEvDashExperience()) << "Energy manager DBus client error:" << errorMessage; + qCWarning(dcEvDashExperience()) << "Energy manager DBus client error occurred:" << errorMessage; }); qCDebug(dcEvDashExperience()) << "ChargingInfos:" << m_energyManagerClient->chargingInfos(); diff --git a/plugin/evdashengine.h b/plugin/evdashengine.h index 0c9e436..9366528 100644 --- a/plugin/evdashengine.h +++ b/plugin/evdashengine.h @@ -44,6 +44,7 @@ class Thing; class ThingManager; class EnergyManagerDbusClient; class EvDashWebServerResource; +class ChargingSessionsDBusInterfaceClient; class EvDashEngine : public QObject { @@ -79,6 +80,7 @@ private: bool m_enabled = false; EnergyManagerDbusClient *m_energyManagerClient = nullptr; + ChargingSessionsDBusInterfaceClient *m_chargingSessionsClient = nullptr; QWebSocketServer *m_webSocketServer = nullptr; quint16 m_webSocketPort = 4449; diff --git a/plugin/plugin.pro b/plugin/plugin.pro index e235dc6..d7bc534 100644 --- a/plugin/plugin.pro +++ b/plugin/plugin.pro @@ -13,6 +13,7 @@ QT += network sql websockets dbus HEADERS += experiencepluginevdash.h \ energymanagerdbusclient.h \ + chargingsessionsdbusinterfaceclient.h \ evdashengine.h \ evdashjsonhandler.h \ evdashsettings.h \ @@ -20,6 +21,7 @@ HEADERS += experiencepluginevdash.h \ SOURCES += experiencepluginevdash.cpp \ energymanagerdbusclient.cpp \ + chargingsessionsdbusinterfaceclient.cpp \ evdashengine.cpp \ evdashjsonhandler.cpp \ evdashsettings.cpp \