Continue implementation of RPC module

This commit is contained in:
Simon Stürz 2025-09-09 16:44:59 +02:00
parent 85e108c93a
commit dada6940d2
7 changed files with 304 additions and 133 deletions

View File

@ -1,6 +1,20 @@
# nymea-plugin-everest # nymea-plugin-everest
This nymea integration plugin allows to connect to any EVerest instance in the network or on localhost with an API module and to add every connector configured on this instance. This nymea integration plugin allows to connect to any EVerest instance in the network or on localhost.
## Using the RpcApi module
By default the integration requires the use of the RpcApi module, which provides a JSON RPC interface based on a websocket communication to the EVerest core.
## Using the MQTT based API module
If required, the MQTT based API module of everest can be used.
By default the MQTT API module will not be initialized unless explictily enabled by creating following file
touch /etc/nymea/everest-mqtt
Each connector has to be added manually using the discovery. Once added, the integration creates an EV charger within nymea which makes it available to the energy manager and the overall nymea eco system. Each connector has to be added manually using the discovery. Once added, the integration creates an EV charger within nymea which makes it available to the energy manager and the overall nymea eco system.
Known issues: Known issues:

View File

@ -35,6 +35,9 @@
#include "jsonrpc/everestevse.h" #include "jsonrpc/everestevse.h"
#include "jsonrpc/everestjsonrpcdiscovery.h" #include "jsonrpc/everestjsonrpcdiscovery.h"
#include <QFileInfo>
#include <nymeasettings.h>
#include <network/networkdevicediscovery.h> #include <network/networkdevicediscovery.h>
IntegrationPluginEverest::IntegrationPluginEverest() IntegrationPluginEverest::IntegrationPluginEverest()
@ -44,11 +47,19 @@ IntegrationPluginEverest::IntegrationPluginEverest()
void IntegrationPluginEverest::init() void IntegrationPluginEverest::init()
{ {
QFileInfo enableMqttFileInfo(NymeaSettings::settingsPath() + "/everest-mqtt");
m_useMqtt = enableMqttFileInfo.exists();
if (m_useMqtt) {
qCDebug(dcEverest()) << "MQTT API module enabled, detected" << enableMqttFileInfo.absoluteFilePath();
} else {
qCDebug(dcEverest()) << "MQTT API module disabled, the file" << enableMqttFileInfo.absoluteFilePath() << "does not exist.";
}
} }
void IntegrationPluginEverest::startMonitoringAutoThings() void IntegrationPluginEverest::startMonitoringAutoThings()
{ {
if (m_useMqtt) {
// Check on localhost if there is any EVerest instance running and if we have to set up a thing for this EV charger // Check on localhost if there is any EVerest instance running and if we have to set up a thing for this EV charger
// Since this integration plugin is most liekly running on an EV charger running EVerest, the local instance should // Since this integration plugin is most liekly running on an EV charger running EVerest, the local instance should
// be set up automatically. Additional instances in the network can still be added by running a normal network discovery // be set up automatically. Additional instances in the network can still be added by running a normal network discovery
@ -109,6 +120,7 @@ void IntegrationPluginEverest::startMonitoringAutoThings()
}); });
mqttDiscovery->startLocalhost(); mqttDiscovery->startLocalhost();
}
} }
void IntegrationPluginEverest::discoverThings(ThingDiscoveryInfo *info) void IntegrationPluginEverest::discoverThings(ThingDiscoveryInfo *info)
@ -120,6 +132,7 @@ void IntegrationPluginEverest::discoverThings(ThingDiscoveryInfo *info)
return; return;
} }
if (m_useMqtt) {
if (info->thingClassId() == everestMqttThingClassId) { if (info->thingClassId() == everestMqttThingClassId) {
EverestMqttDiscovery *mqttDiscovery = new EverestMqttDiscovery(hardwareManager()->networkDeviceDiscovery(), this); EverestMqttDiscovery *mqttDiscovery = new EverestMqttDiscovery(hardwareManager()->networkDeviceDiscovery(), this);
connect(mqttDiscovery, &EverestMqttDiscovery::finished, mqttDiscovery, &EverestMqttDiscovery::deleteLater); connect(mqttDiscovery, &EverestMqttDiscovery::finished, mqttDiscovery, &EverestMqttDiscovery::deleteLater);
@ -197,6 +210,10 @@ void IntegrationPluginEverest::discoverThings(ThingDiscoveryInfo *info)
mqttDiscovery->start(); mqttDiscovery->start();
return; return;
} }
} else {
info->finish(Thing::ThingErrorUnsupportedFeature);
return;
}
if (info->thingClassId() == everestConnectionThingClassId) { if (info->thingClassId() == everestConnectionThingClassId) {
quint16 port = info->params().paramValue(everestConnectionDiscoveryPortParamTypeId).toUInt(); quint16 port = info->params().paramValue(everestConnectionDiscoveryPortParamTypeId).toUInt();

View File

@ -59,6 +59,7 @@ public:
void executeAction(ThingActionInfo *info) override; void executeAction(ThingActionInfo *info) override;
private: private:
bool m_useMqtt = false;
QList<EverestMqttClient *> m_everstMqttClients; QList<EverestMqttClient *> m_everstMqttClients;
QHash<Thing *, EverestMqttClient *> m_thingClients; QHash<Thing *, EverestMqttClient *> m_thingClients;

View File

@ -127,6 +127,8 @@ void EverestConnection::start()
m_client->disconnectFromServer(); m_client->disconnectFromServer();
m_client->connectToServer(url); m_client->connectToServer(url);
} else {
qCDebug(dcEverest()) << "Monitor is not reachable yet" << this;
} }
} else { } else {
qCDebug(dcEverest()) << "Connecting" << this; qCDebug(dcEverest()) << "Connecting" << this;

View File

@ -49,9 +49,27 @@ EverestEvse::EverestEvse(EverestJsonRpcClient *client, Thing *thing, QObject *pa
} }
}); });
if (m_client->available()) { connect(m_client, &EverestJsonRpcClient::evseStatusChanged, this, [this](int evseIndex, const EverestJsonRpcClient::EVSEStatus &evseStatus){
// We are only insterested in our own status
if (m_index != evseIndex)
return;
m_evseStatus = evseStatus;
processEvseStatus();
});
connect(m_client, &EverestJsonRpcClient::hardwareCapabilitiesChanged, this, [this](int evseIndex, const EverestJsonRpcClient::HardwareCapabilities &hardwareCapabilities){
// We are only insterested in our own status
if (m_index != evseIndex)
return;
m_hardwareCapabilities = hardwareCapabilities;
processHardwareCapabilities();
});
if (m_client->available())
qCDebug(dcEverest()) << "Evse: The connection is already available. Initializing the instance..."; qCDebug(dcEverest()) << "Evse: The connection is already available. Initializing the instance...";
}
} }
int EverestEvse::index() const int EverestEvse::index() const

View File

@ -108,8 +108,6 @@ EverestJsonRpcClient::EverestJsonRpcClient(QObject *parent)
} }
// We are done with the init and the client is now available // We are done with the init and the client is now available
if (!m_available) { if (!m_available) {
m_available = true; m_available = true;
@ -298,14 +296,31 @@ EverestJsonRpcClient::EVSEStatus EverestJsonRpcClient::parseEvseStatus(const QVa
evseStatus.chargingDuration = evseStatusMap.value("charging_duration_s").toInt(); evseStatus.chargingDuration = evseStatusMap.value("charging_duration_s").toInt();
evseStatus.chargingAllowed = evseStatusMap.value("charging_allowed").toBool(); evseStatus.chargingAllowed = evseStatusMap.value("charging_allowed").toBool();
evseStatus.available = evseStatusMap.value("available").toBool(); evseStatus.available = evseStatusMap.value("available").toBool();
evseStatus.activeConnectorId = evseStatusMap.value("active_connector_id").toInt(); evseStatus.activeConnectorIndex = evseStatusMap.value("active_connector_index").toInt();
evseStatus.evseError = evseStatusMap.value("evse_error").toString(); evseStatus.errorPresent = evseStatusMap.value("error_present").toBool();
evseStatus.chargeProtocol = parseChargeProtocol(evseStatusMap.value("charge_protocol").toString()); evseStatus.chargeProtocol = parseChargeProtocol(evseStatusMap.value("charge_protocol").toString());
evseStatus.evseState = parseEvseState(evseStatusMap.value("state").toString()); evseStatus.evseState = parseEvseState(evseStatusMap.value("state").toString());
evseStatus.evseStateString = evseStatusMap.value("state").toString(); evseStatus.evseStateString = evseStatusMap.value("state").toString();
evseStatus.acChargeStatus = EverestJsonRpcClient::parseACChargeStatus(evseStatusMap.value("ac_charge_status").toMap());
evseStatus.acChargeParameters = EverestJsonRpcClient::parseACChargeParameters(evseStatusMap.value("ac_charge_param").toMap());
return evseStatus; return evseStatus;
} }
EverestJsonRpcClient::ACChargeStatus EverestJsonRpcClient::parseACChargeStatus(const QVariantMap &acChargeStatusMap)
{
EverestJsonRpcClient::ACChargeStatus status;
status.activePhaseCount = acChargeStatusMap.value("evse_active_phase_count").toInt();
return status;
}
EverestJsonRpcClient::ACChargeParameters EverestJsonRpcClient::parseACChargeParameters(const QVariantMap &acChargeParametersMap)
{
EverestJsonRpcClient::ACChargeParameters params;
params.maxCurrent = acChargeParametersMap.value("evse_max_current").toInt();
params.maxPhaseCount = acChargeParametersMap.value("evse_max_phase_count").toInt();
return params;
}
EverestJsonRpcClient::HardwareCapabilities EverestJsonRpcClient::parseHardwareCapabilities(const QVariantMap &hardwareCapabilitiesMap) EverestJsonRpcClient::HardwareCapabilities EverestJsonRpcClient::parseHardwareCapabilities(const QVariantMap &hardwareCapabilitiesMap)
{ {
HardwareCapabilities hardwareCapabilities; HardwareCapabilities hardwareCapabilities;
@ -321,7 +336,6 @@ EverestJsonRpcClient::HardwareCapabilities EverestJsonRpcClient::parseHardwareCa
return hardwareCapabilities; return hardwareCapabilities;
} }
void EverestJsonRpcClient::connectToServer(const QUrl &serverUrl) void EverestJsonRpcClient::connectToServer(const QUrl &serverUrl)
{ {
m_interface->connectServer(serverUrl); m_interface->connectServer(serverUrl);
@ -358,12 +372,17 @@ void EverestJsonRpcClient::processDataPacket(const QByteArray &data)
} }
QVariantMap dataMap = jsonDoc.toVariant().toMap(); QVariantMap dataMap = jsonDoc.toVariant().toMap();
if (!dataMap.contains("id") || dataMap.value("jsonrpc").toString() != "2.0") { if (dataMap.value("jsonrpc").toString() != "2.0") {
qCWarning(dcEverest()) << "Received valid JSON data but does not seem to be a JSON RPC 2.0 format" << m_interface->serverUrl().toString() << qUtf8Printable(data); qCWarning(dcEverest()) << "Received valid JSON data but does not seem to be a JSON RPC 2.0 format" << m_interface->serverUrl().toString() << qUtf8Printable(data);
return; return;
} }
if (dataMap.contains("id")) {
// Response to a request
int commandId = dataMap.value("id").toInt(); int commandId = dataMap.value("id").toInt();
EverestJsonRpcReply *reply = m_replies.take(commandId); EverestJsonRpcReply *reply = m_replies.take(commandId);
if (reply) { if (reply) {
reply->setResponse(dataMap); reply->setResponse(dataMap);
@ -378,7 +397,31 @@ void EverestJsonRpcClient::processDataPacket(const QByteArray &data)
return; return;
} else { } else {
// Data without reply, check if this is a notification // Data without reply, check if this is a notification
qCDebug(dcEverest()) << "Received data without reply" << qUtf8Printable(data); qCDebug(dcEverest()) << "Received response data without reply" << qUtf8Printable(data);
}
} else {
// A Notification is a Request object without an "id" member.
QString notification = dataMap.value("method").toString();
QVariantMap params = dataMap.value("params").toMap();
qCDebug(dcEverest()) << "Received notification" << notification << params;
if (notification == "EVSE.StatusChanged") {
int evseIndex = params.value("evse_index").toInt();
EVSEStatus evseStatus = EverestJsonRpcClient::parseEvseStatus(params.value("evse_status").toMap());
emit evseStatusChanged(evseIndex, evseStatus);
} else if (notification == "ChargePoint.ActiveErrorsChanged") {
// TODO
} else if (notification == "EVSE.HardwareCapabilitiesChanged") {
int evseIndex = params.value("evse_index").toInt();
HardwareCapabilities hardwareCapabilities = EverestJsonRpcClient::parseHardwareCapabilities(params.value("hardware_capabilities").toMap());
emit hardwareCapabilitiesChanged(evseIndex, hardwareCapabilities);
} else if (notification == "EVSE.MeterDataChanged") {
int evseIndex = params.value("evse_index").toInt();
HardwareCapabilities hardwareCapabilities = EverestJsonRpcClient::parseHardwareCapabilities(params.value("hardware_capabilities").toMap());
emit hardwareCapabilitiesChanged(evseIndex, hardwareCapabilities);
}
} }
} }

View File

@ -129,17 +129,34 @@ public:
QList<ConnectorInfo> availableConnectors; QList<ConnectorInfo> availableConnectors;
} EVSEInfo; } EVSEInfo;
typedef struct ACChargeStatus {
int activePhaseCount = 3;
} ACChargeStatus;
typedef struct ACChargeParameters {
// V 1.0.0 supported values
double maxCurrent = 0; // A
double maxPhaseCount = 0;
// double maxChargePower = 0; // W
// double minChargePower = 0; // W
// double nominalFrequency = 0; // Hz
} ACChargeParameters;
typedef struct EVSEStatus { typedef struct EVSEStatus {
double chargedEnergyWh = 0; double chargedEnergyWh = 0;
double dischargedEnergyWh = 0; double dischargedEnergyWh = 0;
int chargingDuration = 0; // seconds int chargingDuration = 0; // seconds
bool chargingAllowed = false; bool chargingAllowed = false;
bool available = false; bool available = false;
int activeConnectorId = -1; int activeConnectorIndex = -1;
QString evseError; // FIXME: maybe convert to internal enum bool errorPresent = false;
ChargeProtocol chargeProtocol = ChargeProtocolUnknown; ChargeProtocol chargeProtocol = ChargeProtocolUnknown;
EvseState evseState = EvseStateUnplugged; EvseState evseState = EvseStateUnplugged;
QString evseStateString; QString evseStateString;
ACChargeStatus acChargeStatus;
ACChargeParameters acChargeParameters;
// TODO: // TODO:
// o: "ac_charge_param": "$ACChargeParametersObj", // o: "ac_charge_param": "$ACChargeParametersObj",
// o: "dc_charge_param": "$DCChargeParametersObj", // o: "dc_charge_param": "$DCChargeParametersObj",
@ -149,6 +166,7 @@ public:
} EVSEStatus; } EVSEStatus;
typedef struct HardwareCapabilities { typedef struct HardwareCapabilities {
double maxCurrentExport = 0; double maxCurrentExport = 0;
double maxCurrentImport = 0; double maxCurrentImport = 0;
@ -162,6 +180,56 @@ public:
} HardwareCapabilities; } HardwareCapabilities;
/*
MeterDataObj {
o: "current_A": {
"L1": "float",
"L2": "float",
"L3": "float",
"N": "float"
},
"energy_Wh_import": {
"L1": "float",
"L2": "float",
"L3": "float",
"total": "float"
},
o: "energy_Wh_export": {
"L1": "float",
"L2": "float",
"L3": "float",
"total": "float"
},
o: "frequency_Hz": {
"L1": "float",
"L2": "float",
"L3": "float"
},
"meter_id": "string",
o: "serial_number": "string",
o: "phase_seq_error": "bool",
o: "power_W": {
"L1": "float",
"L2": "float",
"L3": "float",
"total": "float"
},
"timestamp": "string",
o: "voltage_V": {
"L1": "float",
"L2": "float",
"L3": "float"
}
*/
typedef struct MeterData {
} MeterData;
explicit EverestJsonRpcClient(QObject *parent = nullptr); explicit EverestJsonRpcClient(QObject *parent = nullptr);
QUrl serverUrl(); QUrl serverUrl();
@ -197,6 +265,8 @@ public:
static EVSEInfo parseEvseInfo(const QVariantMap &evseInfoMap); static EVSEInfo parseEvseInfo(const QVariantMap &evseInfoMap);
static ConnectorInfo parseConnectorInfo(const QVariantMap &connectorInfoMap); static ConnectorInfo parseConnectorInfo(const QVariantMap &connectorInfoMap);
static EVSEStatus parseEvseStatus(const QVariantMap &evseStatusMap); static EVSEStatus parseEvseStatus(const QVariantMap &evseStatusMap);
static ACChargeStatus parseACChargeStatus(const QVariantMap &acChargeStatusMap);
static ACChargeParameters parseACChargeParameters(const QVariantMap &acChargeParametersMap);
static HardwareCapabilities parseHardwareCapabilities(const QVariantMap &hardwareCapabilitiesMap); static HardwareCapabilities parseHardwareCapabilities(const QVariantMap &hardwareCapabilitiesMap);
public slots: public slots:
@ -207,6 +277,12 @@ signals:
void connectionErrorOccurred(); void connectionErrorOccurred();
void availableChanged(bool available); void availableChanged(bool available);
// Notifications
void evseStatusChanged(int evseIndex, const EverestJsonRpcClient::EVSEStatus &evseStatus);
void hardwareCapabilitiesChanged(int evseIndex, const EverestJsonRpcClient::HardwareCapabilities &hardwareCapabilities);
void meterDataChanged(int evseIndex, const EverestJsonRpcClient::HardwareCapabilities &hardwareCapabilities);
// TODO void activeErrorsChanged();
private slots: private slots:
void sendRequest(EverestJsonRpcReply *reply); void sendRequest(EverestJsonRpcReply *reply);
void processDataPacket(const QByteArray &data); void processDataPacket(const QByteArray &data);
@ -214,6 +290,7 @@ private slots:
private: private:
bool m_available = false; bool m_available = false;
int m_commandId = 0; int m_commandId = 0;
EverestJsonRpcInterface *m_interface = nullptr; EverestJsonRpcInterface *m_interface = nullptr;
QHash<int, EverestJsonRpcReply *> m_replies; QHash<int, EverestJsonRpcReply *> m_replies;
@ -224,7 +301,6 @@ private:
bool m_authenticationRequired = false; bool m_authenticationRequired = false;
QList<EVSEInfo> m_evseInfos; QList<EVSEInfo> m_evseInfos;
// API calls // API calls
EverestJsonRpcReply *apiHello(); EverestJsonRpcReply *apiHello();
EverestJsonRpcReply *chargePointGetEVSEInfos(); EverestJsonRpcReply *chargePointGetEVSEInfos();