Add goe discovery and prepare V2 integration

master
Simon Stürz 2022-07-21 10:08:32 +02:00
parent 860fbac0e8
commit 094f2e14ef
6 changed files with 1093 additions and 420 deletions

View File

@ -5,7 +5,9 @@ QT += network
PKGCONFIG += nymea-mqtt
SOURCES += \
goediscovery.cpp \
integrationplugingoecharger.cpp \
HEADERS += \
goediscovery.h \
integrationplugingoecharger.h \

273
goecharger/goediscovery.cpp Normal file
View File

@ -0,0 +1,273 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright 2013 - 2022, nymea GmbH
* Contact: contact@nymea.io
*
* This file is part of nymea.
* This project including source code and documentation is protected by
* copyright law, and remains the property of nymea GmbH. All rights, including
* reproduction, publication, editing and translation, are reserved. The use of
* this project is subject to the terms of a license agreement to be concluded
* with nymea GmbH in accordance with the terms of use of nymea GmbH, available
* under https://nymea.io/license
*
* GNU Lesser General Public License Usage
* Alternatively, this project may be redistributed and/or modified under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; version 3. This project 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this project. If not, see <https://www.gnu.org/licenses/>.
*
* For any further details and any questions please contact us under
* contact@nymea.io or see our FAQ/Licensing Information on
* https://nymea.io/license/faq
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "goediscovery.h"
#include "extern-plugininfo.h"
#include <QJsonDocument>
#include <QJsonParseError>
GoeDiscovery::GoeDiscovery(NetworkAccessManager *networkAccessManager, NetworkDeviceDiscovery *networkDeviceDiscovery, QObject *parent) :
QObject(parent),
m_networkAccessManager(networkAccessManager),
m_networkDeviceDiscovery(networkDeviceDiscovery)
{
m_gracePeriodTimer.setSingleShot(true);
m_gracePeriodTimer.setInterval(3000);
connect(&m_gracePeriodTimer, &QTimer::timeout, this, [this](){
qCDebug(dcGoECharger()) << "Discovery: Grace period timer triggered.";
finishDiscovery();
});
}
GoeDiscovery::~GoeDiscovery()
{
qCDebug(dcGoECharger()) << "Discovery: destroy discovery object";
cleanupPendingReplies();
}
void GoeDiscovery::startDiscovery()
{
// Clean up
m_discoveryResults.clear();
m_verifiedNetworkDeviceInfos.clear();
m_gracePeriodTimer.stop();
m_startDateTime = QDateTime::currentDateTime();
qCInfo(dcGoECharger()) << "Discovery: Start discovering the network...";
m_discoveryReply = m_networkDeviceDiscovery->discover();
// Check if all network device infos which might already be discovered here to save time...
foreach (const NetworkDeviceInfo &networkDeviceInfo, m_discoveryReply->networkDeviceInfos()) {
checkNetworkDevice(networkDeviceInfo);
}
// Test any network device beeing discovered
connect(m_discoveryReply, &NetworkDeviceDiscoveryReply::networkDeviceInfoAdded, this, &GoeDiscovery::checkNetworkDevice);
// When the network discovery has finished, we process the rest and give some time to finish the pending replies
connect(m_discoveryReply, &NetworkDeviceDiscoveryReply::finished, this, [=](){
// The network device discovery is done
m_discoveredNetworkDeviceInfos = m_discoveryReply->networkDeviceInfos();
m_discoveryReply = nullptr;
// Check if all network device infos have been verified
foreach (const NetworkDeviceInfo &networkDeviceInfo, m_discoveredNetworkDeviceInfos) {
if (m_verifiedNetworkDeviceInfos.contains(networkDeviceInfo))
continue;
checkNetworkDevice(networkDeviceInfo);
}
// If there might be some response after the grace period time,
// we don't care any more since there might just waiting for some timeouts...
// If there would be a device, it would have responded.
m_gracePeriodTimer.start();
});
}
QList<GoeDiscovery::Result> GoeDiscovery::discoveryResults() const
{
return m_discoveryResults.values();
}
QNetworkRequest GoeDiscovery::buildRequestV1(const QHostAddress &address)
{
QUrl requestUrl;
requestUrl.setScheme("http");
requestUrl.setHost(address.toString());
requestUrl.setPath("/status");
return QNetworkRequest(requestUrl);
}
QNetworkRequest GoeDiscovery::buildRequestV2(const QHostAddress &address)
{
QUrl requestUrl;
requestUrl.setScheme("http");
requestUrl.setHost(address.toString());
requestUrl.setPath("/api/status");
return QNetworkRequest(requestUrl);
}
void GoeDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo)
{
// Make sure we have not checked this host yet
if (m_verifiedNetworkDeviceInfos.contains(networkDeviceInfo))
return;
qCDebug(dcGoECharger()) << "Discovery: Start inspecting" << networkDeviceInfo.address().toString();
checkNetworkDeviceApiV2(networkDeviceInfo);
checkNetworkDeviceApiV1(networkDeviceInfo);
m_verifiedNetworkDeviceInfos.append(networkDeviceInfo);
}
void GoeDiscovery::checkNetworkDeviceApiV1(const NetworkDeviceInfo &networkDeviceInfo)
{
// First check if API V1 is available: http://<host>/status
QNetworkReply *reply = m_networkAccessManager->get(buildRequestV1(networkDeviceInfo.address()));
m_pendingReplies.append(reply);
connect(reply, &QNetworkReply::finished, this, [=](){
m_pendingReplies.removeAll(reply);
reply->deleteLater();
if (reply->error() != QNetworkReply::NoError) {
qCDebug(dcGoECharger()) << "Discovery:" << networkDeviceInfo.address().toString() << "API V1 verification HTTP error" << reply->errorString() << "Continue...";
return;
}
QByteArray data = reply->readAll();
QJsonParseError error;
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
if (error.error != QJsonParseError::NoError) {
qCDebug(dcGoECharger()) << "Discovery:" << networkDeviceInfo.address().toString() << "API V1 verification invalid JSON data. Continue...";
return;
}
// Verify if we have the required values in the response map
// https://github.com/goecharger/go-eCharger-API-v1/blob/master/go-eCharger%20API%20v1%20EN.md
QVariantMap responseMap = jsonDoc.toVariant().toMap();
if (responseMap.contains("fwv") && responseMap.contains("sse") && responseMap.contains("nrg") && responseMap.contains("amp")) {
// Looks like we have found a go-e V1 api endpoint, nice
qCDebug(dcGoECharger()) << "Discovery: --> Found API V1 on" << networkDeviceInfo.address().toString();
if (m_discoveryResults.contains(networkDeviceInfo.address())) {
// We use the information from API V2 since there are more information available
m_discoveryResults[networkDeviceInfo.address()].apiAvailableV1 = true;
} else {
GoeDiscovery::Result result;
result.serialNumber = responseMap.value("sse").toString();
result.firmwareVersion = responseMap.value("fwv").toString();
result.networkDeviceInfo = networkDeviceInfo;
result.apiAvailableV1 = true;
m_discoveryResults[networkDeviceInfo.address()] = result;
}
} else {
qCDebug(dcGoECharger()) << "Discovery:" << networkDeviceInfo.address().toString() << "API V1 verification returned JSON data but not the right one. Continue...";
}
// Check if we are done with all checks
verifyDiscoveryFinished();
});
}
void GoeDiscovery::checkNetworkDeviceApiV2(const NetworkDeviceInfo &networkDeviceInfo)
{
// Check if API V2 is available: http://<host>/api/status
qCDebug(dcGoECharger()) << "Discovery: verify API V2 on" << networkDeviceInfo.address().toString();
QNetworkReply *reply = m_networkAccessManager->get(buildRequestV2(networkDeviceInfo.address()));
m_pendingReplies.append(reply);
connect(reply, &QNetworkReply::finished, this, [=](){
m_pendingReplies.removeAll(reply);
reply->deleteLater();
if (reply->error() != QNetworkReply::NoError) {
qCDebug(dcGoECharger()) << "Discovery:" << networkDeviceInfo.address().toString() << "API V2 verification HTTP error" << reply->errorString() << "Continue...";
return;
}
QByteArray data = reply->readAll();
QJsonParseError error;
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
if (error.error != QJsonParseError::NoError) {
qCDebug(dcGoECharger()) << "Discovery:" << networkDeviceInfo.address().toString() << "API V2 verification invalid JSON data. Continue...";
return;
}
// Verify if we have the required values in the response map
// https://github.com/goecharger/go-eCharger-API-v2/blob/main/http-en.md
QVariantMap responseMap = jsonDoc.toVariant().toMap();
if (responseMap.contains("fwv") && responseMap.contains("sse") && responseMap.contains("typ") && responseMap.contains("fna")) {
// Looks like we have found a go-e V2 api endpoint, nice
qCDebug(dcGoECharger()) << "Discovery: --> Found API V2 on" << networkDeviceInfo.address().toString();
GoeDiscovery::Result result;
result.serialNumber = responseMap.value("sse").toString();
result.firmwareVersion = responseMap.value("fwv").toString();
result.manufacturer = responseMap.value("oem").toString();
result.product = responseMap.value("typ").toString();
result.friendlyName = responseMap.value("fna").toString();
result.networkDeviceInfo = networkDeviceInfo;
result.apiAvailableV2 = true;
if (m_discoveryResults.contains(networkDeviceInfo.address())) {
result.apiAvailableV1 = m_discoveryResults.value(networkDeviceInfo.address()).apiAvailableV1;
}
// Overwrite result from V1 since V2 contains more information
m_discoveryResults[networkDeviceInfo.address()] = result;
} else {
qCDebug(dcGoECharger()) << "Discovery:" << networkDeviceInfo.address().toString() << "API V2 verification returned JSON data but not the right one. Continue...";
}
// Check if we are done with all checks
verifyDiscoveryFinished();
});
}
void GoeDiscovery::verifyDiscoveryFinished()
{
if (!m_discoveryReply && m_verifiedNetworkDeviceInfos.count() == m_discoveredNetworkDeviceInfos.count()) {
finishDiscovery();
}
}
void GoeDiscovery::cleanupPendingReplies()
{
foreach (QNetworkReply *reply, m_pendingReplies) {
m_pendingReplies.removeAll(reply);
reply->abort();
}
}
void GoeDiscovery::finishDiscovery()
{
qint64 durationMilliSeconds = QDateTime::currentMSecsSinceEpoch() - m_startDateTime.toMSecsSinceEpoch();
qCInfo(dcGoECharger()) << "Discovery: Finished the discovery process. Found" << m_discoveryResults.count() << "go-eChargers in" << QTime::fromMSecsSinceStartOfDay(durationMilliSeconds).toString("mm:ss.zzz");
m_gracePeriodTimer.stop();
cleanupPendingReplies();
emit discoveryFinished();
}
QDebug operator<<(QDebug dbg, const GoeDiscovery::Result &result)
{
dbg.nospace() << "GoeDiscovery:Result(" << result.product;
dbg.nospace() << ", " << result.manufacturer;
dbg.nospace() << ", Version: " << result.firmwareVersion;
dbg.nospace() << ", SN: " << result.serialNumber;
dbg.nospace() << ", V1: " << result.apiAvailableV1;
dbg.nospace() << ", V2: " << result.apiAvailableV2;
dbg.nospace() << ", " << result.networkDeviceInfo.address().toString();
dbg.nospace() << ", " << result.networkDeviceInfo.macAddress();
dbg.nospace() << ") ";
return dbg.maybeSpace();
}

93
goecharger/goediscovery.h Normal file
View File

@ -0,0 +1,93 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright 2013 - 2022, nymea GmbH
* Contact: contact@nymea.io
*
* This file is part of nymea.
* This project including source code and documentation is protected by
* copyright law, and remains the property of nymea GmbH. All rights, including
* reproduction, publication, editing and translation, are reserved. The use of
* this project is subject to the terms of a license agreement to be concluded
* with nymea GmbH in accordance with the terms of use of nymea GmbH, available
* under https://nymea.io/license
*
* GNU Lesser General Public License Usage
* Alternatively, this project may be redistributed and/or modified under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; version 3. This project 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this project. If not, see <https://www.gnu.org/licenses/>.
*
* For any further details and any questions please contact us under
* contact@nymea.io or see our FAQ/Licensing Information on
* https://nymea.io/license/faq
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef GOEDISCOVERY_H
#define GOEDISCOVERY_H
#include <QObject>
#include <QDebug>
#include <network/networkaccessmanager.h>
#include <network/networkdevicediscovery.h>
class GoeDiscovery : public QObject
{
Q_OBJECT
public:
typedef struct Result {
QString product = "go-eCharger";
QString manufacturer = "go-e";
QString friendlyName;
QString serialNumber;
QString firmwareVersion;
NetworkDeviceInfo networkDeviceInfo;
bool apiAvailableV1 = false;
bool apiAvailableV2 = false;
} Result;
explicit GoeDiscovery(NetworkAccessManager *networkAccessManager, NetworkDeviceDiscovery *networkDeviceDiscovery, QObject *parent = nullptr);
~GoeDiscovery();
void startDiscovery();
QList<GoeDiscovery::Result> discoveryResults() const;
static QNetworkRequest buildRequestV1(const QHostAddress &address);
static QNetworkRequest buildRequestV2(const QHostAddress &address);
signals:
void discoveryFinished();
private:
QDateTime m_startDateTime;
QTimer m_gracePeriodTimer;
NetworkAccessManager *m_networkAccessManager = nullptr;
NetworkDeviceDiscovery *m_networkDeviceDiscovery = nullptr;
NetworkDeviceDiscoveryReply *m_discoveryReply = nullptr;
QHash<QHostAddress, GoeDiscovery::Result> m_discoveryResults;
NetworkDeviceInfos m_discoveredNetworkDeviceInfos;
NetworkDeviceInfos m_verifiedNetworkDeviceInfos;
QList<QNetworkReply *> m_pendingReplies;
private slots:
void checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo);
void checkNetworkDeviceApiV1(const NetworkDeviceInfo &networkDeviceInfo);
void checkNetworkDeviceApiV2(const NetworkDeviceInfo &networkDeviceInfo);
void verifyDiscoveryFinished();
void cleanupPendingReplies();
void finishDiscovery();
};
QDebug operator<<(QDebug debug, const GoeDiscovery::Result &result);
#endif // GOEDISCOVERY_H

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright 2013 - 2021, nymea GmbH
* Copyright 2013 - 2022, nymea GmbH
* Contact: contact@nymea.io
*
* This file is part of nymea.
@ -33,11 +33,12 @@
#include <QUuid>
#include <network/networkaccessmanager.h>
#include <plugintimer.h>
#include <network/mqtt/mqttchannel.h>
#include <network/mqtt/mqttprovider.h>
#include <network/networkaccessmanager.h>
#include <network/networkdevicemonitor.h>
#include <integrations/integrationplugin.h>
#include <plugintimer.h>
class IntegrationPluginGoECharger: public IntegrationPlugin
{
@ -47,7 +48,14 @@ class IntegrationPluginGoECharger: public IntegrationPlugin
Q_INTERFACES(IntegrationPlugin)
public:
enum ApiVersion {
ApiVersion1 = 1,
ApiVersion2 = 2
};
Q_ENUM(ApiVersion)
enum CarState {
CarStateUnknown = 0,
CarStateReadyNoCar = 1,
CarStateCharging = 2,
CarStateWaitForCar = 3,
@ -55,6 +63,13 @@ public:
};
Q_ENUM(CarState)
enum ForceState {
ForceStateNeutral = 0,
ForceStateOff = 1,
ForceStateOn = 2
};
Q_ENUM(ForceState)
enum Access {
AccessOpen = 0,
AccessRfid = 1,
@ -83,25 +98,39 @@ public:
void setupThing(ThingSetupInfo *info) override;
void postSetupThing(Thing *thing) override;
void thingRemoved(Thing *thing) override;
void executeAction(ThingActionInfo *info) override;
private:
PluginTimer *m_refreshTimer = nullptr;
QHash<Thing *, MqttChannel *> m_channels;
QHash<Thing *, QNetworkReply *> m_pendingReplies;
void update(Thing *thing, const QVariantMap &statusMap);
QHash<Thing *, MqttChannel *> m_mqttChannelsV1;
QHash<Thing *, MqttChannel *> m_mqttChannelsV2;
QHash<Thing *, QNetworkReply *> m_pendingReplies;
QHash<Thing *, NetworkDeviceMonitor *> m_monitors;
// General methods
void setupGoeHome(ThingSetupInfo *info);
QNetworkRequest buildStatusRequest(Thing *thing);
QNetworkRequest buildConfigurationRequest(const QHostAddress &address, const QString &configuration);
void sendActionRequest(Thing *thing, ThingActionInfo *info, const QString &configuration);
void setupMqttChannel(ThingSetupInfo *info, const QHostAddress &address, const QVariantMap &statusMap);
QHostAddress getHostAddress(Thing *thing);
ApiVersion getApiVersion(Thing *thing);
// API V1
void updateV1(Thing *thing, const QVariantMap &statusMap);
void sendActionRequestV1(Thing *thing, ThingActionInfo *info, const QString &configuration);
QNetworkRequest buildConfigurationRequestV1(const QHostAddress &address, const QString &configuration);
void setupMqttChannelV1(ThingSetupInfo *info, const QHostAddress &address, const QVariantMap &statusMap);
void reconfigureMqttChannelV1(Thing *thing, const QVariantMap &statusMap);
private slots:
void refreshHttp();
void onClientConnected(MqttChannel* channel);
void onClientDisconnected(MqttChannel* channel);
void onPublishReceived(MqttChannel* channel, const QString &topic, const QByteArray &payload);
// API V1
void onMqttClientV1Connected(MqttChannel* channel);
void onMqttClientV1Disconnected(MqttChannel* channel);
void onMqttPublishV1Received(MqttChannel* channel, const QString &topic, const QByteArray &payload);
};

View File

@ -15,24 +15,34 @@
"createMethods": ["Discovery", "User"],
"interfaces": ["evcharger", "smartmeterconsumer", "connectable"],
"paramTypes": [
{
"id": "4342b72c-99d0-41a5-abc6-ea6c1cc1352c",
"name":"ipAddress",
"displayName": "IP address",
"type": "QString"
},
{
"id": "0e30e30f-ad96-417e-b739-cac85f75de39",
"name":"macAddress",
"displayName": "MAC address",
"type": "QString"
},
{
"id": "74abaff3-39e6-40be-b3c3-f41911d17e02",
"name": "serialNumber",
"displayName": "Serial number",
"type": "QString",
"defaultValue": ""
},
{
"id": "848613a6-8a17-4082-ba77-3b4421170a4f",
"name":"useMqtt",
"displayName": "Use MQTT interface",
"type": "bool",
"defaultValue": false
"defaultValue": true
},
{
"id": "3ad014e2-c948-406e-99be-eba1d866ea20",
"name":"apiVersion",
"displayName": "API Version",
"type": "uint",
"minValue": 1,
"maxValue": 2,
"defaultValue": 1
}
],
"stateTypes":[
@ -52,6 +62,7 @@
"displayNameEvent": "Car status changed",
"type": "QString",
"possibleValues": [
"Unknown",
"Ready but no vehicle connected",
"Vehicle loads",
"Waiting for vehicle",
@ -122,24 +133,25 @@
"name": "absoluteMaxAmpere",
"displayName": "Maximal ampere",
"displayNameEvent": "Maximal ampere changed",
"displayNameAction": "Set maximal ampere",
"type": "uint",
"unit": "Ampere",
"minValue": 6,
"maxValue": 32,
"defaultValue": 20,
"writable": true,
"defaultValue": 32,
"suggestLogging": true
},
{
"id": "ac849296-3f70-4b1b-aa30-127d774667bb",
"name": "cloud",
"displayName": "Cloud enabled",
"displayNameAction": "Set cloud enabled",
"displayNameEvent": "Cloud enabled changed",
"type": "bool",
"defaultValue": true,
"writable": true
"id": "ede9251d-662e-4d4b-90e3-db3d33c823d3",
"name": "modelMaxAmpere",
"displayName": "Model maximal ampere",
"displayNameEvent": "Maximal ampere model changed",
"type": "uint",
"unit": "Ampere",
"minValue": 16,
"maxValue": 32,
"defaultValue": 16,
"suggestLogging": true,
"cached": true
},
{
"id": "08b107bc-1284-455d-9e5a-6a1c3adc389f",
@ -166,7 +178,8 @@
"displayNameEvent": "Cable ampere encoding changed",
"type": "uint",
"unit": "Ampere",
"defaultValue": 0
"defaultValue": 0,
"cached": true
},
{
"id": "d8f5abb6-5db3-4040-8829-553b1d881ce4",
@ -175,7 +188,8 @@
"displayNameEvent": "Total energy changed",
"type": "double",
"unit": "KiloWattHour",
"defaultValue": 0.0
"defaultValue": 0.0,
"cached": true
},
{
"id": "e8258831-ad89-4d27-b295-e8c10dd42b76",
@ -277,6 +291,15 @@
"unit": "Volt",
"defaultValue": 0.00
},
{
"id": "28f59f96-4b30-4606-9a04-80c82abc346b",
"name": "frequency",
"displayName": "Frequency",
"displayNameEvent": "Frequency changed",
"type": "double",
"unit": "Hertz",
"defaultValue": 50.0
},
{
"id": "b78d805a-f97c-4c9d-a647-5fc98f8d6dd1",
"name": "phaseCount",
@ -287,28 +310,6 @@
"maxValue": 3,
"defaultValue": 1
},
{
"id": "b06479d5-7a38-4fbd-867e-e55bdb54651b",
"name": "ledBrightness",
"displayName": "Led brightness",
"displayNameAction": "Set led brightness",
"displayNameEvent": "Led brightness changed",
"type": "int",
"minValue": 0,
"maxValue": 255,
"defaultValue": 255,
"writable": true
},
{
"id": "048a4c98-3ee4-4d02-ad48-6d70f31fce8c",
"name": "ledEnergySave",
"displayName": "Led energy saving enabled",
"displayNameAction": "Set led energy saving enabled",
"displayNameEvent": "Led energy saving enabled enabled changed",
"type": "bool",
"defaultValue": true,
"writable": true
},
{
"id": "2bf1ebf1-0d8c-4209-ad35-4114d9861832",
"name": "temperatureSensor1",
@ -353,15 +354,6 @@
"type": "QString",
"defaultValue": "",
"cached": true
},
{
"id": "8ecdf24b-daca-4b7a-98b5-3236f1e6ad85",
"name": "serialNumber",
"displayName": "Serial number",
"displayNameEvent": "Serial number changed",
"type": "QString",
"defaultValue": "",
"cached": true
}
]
}
@ -369,7 +361,3 @@
}
]
}