Add DBus client for charging sessions
parent
441705087f
commit
b36323c2ed
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
|
||||
#include "chargingsessionsdbusinterfaceclient.h"
|
||||
|
||||
#include <QDBusArgument>
|
||||
#include <QDBusInterface>
|
||||
#include <QDBusPendingCall>
|
||||
#include <QDBusPendingCallWatcher>
|
||||
#include <QDBusPendingReply>
|
||||
#include <QDBusReply>
|
||||
#include <QDBusError>
|
||||
#include <QLoggingCategory>
|
||||
|
||||
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<QVariantMap> 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<QVariantList> reply = *watcher;
|
||||
watcher->deleteLater();
|
||||
|
||||
if (reply.isError()) {
|
||||
qCWarning(dcChargingSessions()) << "GetSessions DBus call failed:" << reply.error().message();
|
||||
emit errorOccurred(reply.error().message());
|
||||
return;
|
||||
}
|
||||
|
||||
QList<QVariantMap> sessions;
|
||||
const QVariantList values = reply.value();
|
||||
for (const QVariant &value : values) {
|
||||
if (value.canConvert<QVariantMap>()) {
|
||||
sessions.append(value.toMap());
|
||||
continue;
|
||||
}
|
||||
|
||||
const QDBusArgument arg = value.value<QDBusArgument>();
|
||||
sessions.append(qdbus_cast<QVariantMap>(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;
|
||||
}
|
||||
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
#include <QDBusConnection>
|
||||
#include <QList>
|
||||
#include <QVariantMap>
|
||||
#include <QStringList>
|
||||
|
||||
class QDBusInterface;
|
||||
class QDBusPendingCallWatcher;
|
||||
|
||||
class ChargingSessionsDBusInterfaceClient : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit ChargingSessionsDBusInterfaceClient(QObject *parent = nullptr);
|
||||
~ChargingSessionsDBusInterfaceClient();
|
||||
|
||||
QList<QVariantMap> sessions() const;
|
||||
|
||||
public slots:
|
||||
void getSessions(const QStringList &carThingIds = QStringList());
|
||||
|
||||
signals:
|
||||
void sessionsReceived(const QList<QVariantMap> &sessions);
|
||||
void errorOccurred(const QString &message);
|
||||
|
||||
private slots:
|
||||
void onCallFinished(QDBusPendingCallWatcher *watcher);
|
||||
|
||||
private:
|
||||
bool ensureInterface();
|
||||
|
||||
QDBusConnection m_connection;
|
||||
QDBusInterface *m_interface = nullptr;
|
||||
QList<QVariantMap> m_sessions;
|
||||
};
|
||||
|
|
@ -32,6 +32,7 @@
|
|||
#include "evdashsettings.h"
|
||||
#include "evdashwebserverresource.h"
|
||||
#include "energymanagerdbusclient.h"
|
||||
#include "chargingsessionsdbusinterfaceclient.h"
|
||||
|
||||
#include <integrations/thingmanager.h>
|
||||
|
||||
|
|
@ -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<QVariantMap> &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();
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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 \
|
||||
|
|
|
|||
Loading…
Reference in New Issue