Skip to content

Commit

Permalink
Merge PR #755: go-eCharger: Update to networkdevice interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins committed Jan 14, 2025
2 parents 0d94b68 + 344b7b8 commit d2da165
Show file tree
Hide file tree
Showing 6 changed files with 620 additions and 134 deletions.
90 changes: 44 additions & 46 deletions goecharger/goediscovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void GoeDiscovery::startDiscovery()
{
// Clean up
m_discoveryResults.clear();
m_verifiedNetworkDeviceInfos.clear();
m_verifiedHostAddresses.clear();

m_startDateTime = QDateTime::currentDateTime();

Expand All @@ -69,23 +69,15 @@ void GoeDiscovery::startDiscovery()
m_discoveryReply = m_networkDeviceDiscovery->discover();

// Test any network device beeing discovered
connect(m_discoveryReply, &NetworkDeviceDiscoveryReply::networkDeviceInfoAdded, this, &GoeDiscovery::checkNetworkDevice);
connect(m_discoveryReply, &NetworkDeviceDiscoveryReply::hostAddressDiscovered, this, &GoeDiscovery::checkHostAddress);

// 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, [=](){
connect(m_discoveryReply, &NetworkDeviceDiscoveryReply::finished, this, [this](){
// The network device discovery is done
m_discoveredNetworkDeviceInfos = m_discoveryReply->networkDeviceInfos();
m_discoveryReply->deleteLater();
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.
Expand Down Expand Up @@ -126,38 +118,38 @@ bool GoeDiscovery::isGoeCharger(const ZeroConfServiceEntry &serviceEntry)
return serviceEntry.name().toLower().contains("go-echarger");
}

void GoeDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo)
void GoeDiscovery::checkHostAddress(const QHostAddress &address)
{
// Make sure we have not checked this host yet
if (m_verifiedNetworkDeviceInfos.contains(networkDeviceInfo))
if (m_verifiedHostAddresses.contains(address))
return;

qCDebug(dcGoECharger()) << "Discovery: Start inspecting" << networkDeviceInfo.address().toString();
checkNetworkDeviceApiV2(networkDeviceInfo);
checkNetworkDeviceApiV1(networkDeviceInfo);
qCDebug(dcGoECharger()) << "Discovery: Start inspecting" << address.toString();
checkHostAddressApiV2(address);
checkHostAddressApiV1(address);

m_verifiedNetworkDeviceInfos.append(networkDeviceInfo);
m_verifiedHostAddresses.append(address);
}

void GoeDiscovery::checkNetworkDeviceApiV1(const NetworkDeviceInfo &networkDeviceInfo)
void GoeDiscovery::checkHostAddressApiV1(const QHostAddress &address)
{
// First check if API V1 is available: http://<host>/status
QNetworkReply *reply = m_networkAccessManager->get(buildRequestV1(networkDeviceInfo.address()));
// Check if API V1 is available: http://<host>/status
QNetworkReply *reply = m_networkAccessManager->get(buildRequestV1(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...";
qCDebug(dcGoECharger()) << "Discovery:" << 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...";
qCDebug(dcGoECharger()) << "Discovery:" << address.toString() << "API V1 verification invalid JSON data. Continue...";
return;
}

Expand All @@ -166,52 +158,52 @@ void GoeDiscovery::checkNetworkDeviceApiV1(const NetworkDeviceInfo &networkDevic
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();
qCDebug(dcGoECharger()) << "Discovery: --> Found API V1 on" << address.toString();

if (m_discoveryResults.contains(networkDeviceInfo.address()) && m_discoveryResults.value(networkDeviceInfo.address()).discoveryMethod == DiscoveryMethodZeroConf) {
qCDebug(dcGoECharger()) << "Discovery: Network discovery found API V1 go-eCharger on" << networkDeviceInfo.address().toString()
<< "but this host has already been discovered using ZeroConf. Prefering ZeroConf over MAC address due to Repeater missbehaviours.";
if (m_discoveryResults.contains(address) && m_discoveryResults.value(address).discoveryMethod == DiscoveryMethodZeroConf) {
qCDebug(dcGoECharger()) << "Discovery: Network discovery found API V1 go-eCharger on" << address.toString()
<< "but this host has already been discovered using ZeroConf. Prefering ZeroConf over MAC address due to Repeater missbehaviours.";
return;
}

if (m_discoveryResults.contains(networkDeviceInfo.address())) {
if (m_discoveryResults.contains(address)) {
// We use the information from API V2 since there are more information available
m_discoveryResults[networkDeviceInfo.address()].apiAvailableV1 = true;
m_discoveryResults[address].apiAvailableV1 = true;
} else {
GoeDiscovery::Result result;
result.serialNumber = responseMap.value("sse").toString();
result.firmwareVersion = responseMap.value("fwv").toString();
result.networkDeviceInfo = networkDeviceInfo;
//result.networkDeviceInfo = networkDeviceInfo;
result.apiAvailableV1 = true;
m_discoveryResults[networkDeviceInfo.address()] = result;
m_discoveryResults[address] = result;
}
} else {
qCDebug(dcGoECharger()) << "Discovery:" << networkDeviceInfo.address().toString() << "API V1 verification returned JSON data but not the right one. Continue...";
qCDebug(dcGoECharger()) << "Discovery:" << address.toString() << "API V1 verification returned JSON data but not the right one. Continue...";
}

});
}

void GoeDiscovery::checkNetworkDeviceApiV2(const NetworkDeviceInfo &networkDeviceInfo)
void GoeDiscovery::checkHostAddressApiV2(const QHostAddress &address)
{
// 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()));
qCDebug(dcGoECharger()) << "Discovery: verify API V2 on" << address.toString();
QNetworkReply *reply = m_networkAccessManager->get(buildRequestV2(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...";
qCDebug(dcGoECharger()) << "Discovery:" << 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...";
qCDebug(dcGoECharger()) << "Discovery:" << address.toString() << "API V2 verification invalid JSON data. Continue...";
return;
}

Expand All @@ -220,32 +212,32 @@ void GoeDiscovery::checkNetworkDeviceApiV2(const NetworkDeviceInfo &networkDevic
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();
qCDebug(dcGoECharger()) << "Discovery: --> Found API V2 on" << 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.networkDeviceInfo = networkDeviceInfo;
result.discoveryMethod = DiscoveryMethodNetwork;
result.apiAvailableV2 = true;

if (m_discoveryResults.contains(networkDeviceInfo.address()) && m_discoveryResults.value(networkDeviceInfo.address()).discoveryMethod == DiscoveryMethodZeroConf) {
qCDebug(dcGoECharger()) << "Discovery: Network discovery found API V2 go-eCharger on" << networkDeviceInfo.address().toString()
<< "but this host has already been discovered using ZeroConf. Prefering ZeroConf over MAC address due to Repeater missbehaviours.";
if (m_discoveryResults.contains(address) && m_discoveryResults.value(address).discoveryMethod == DiscoveryMethodZeroConf) {
qCDebug(dcGoECharger()) << "Discovery: Network discovery found API V2 go-eCharger on" << address.toString()
<< "but this host has already been discovered using ZeroConf. Prefering ZeroConf over MAC address due to Repeater missbehaviours.";
return;
}

if (m_discoveryResults.contains(networkDeviceInfo.address())) {
result.apiAvailableV1 = m_discoveryResults.value(networkDeviceInfo.address()).apiAvailableV1;
if (m_discoveryResults.contains(address)) {
result.apiAvailableV1 = m_discoveryResults.value(address).apiAvailableV1;
}

// Overwrite result from V1 since V2 contains more information
m_discoveryResults[networkDeviceInfo.address()] = result;
m_discoveryResults[address] = result;
} else {
qCDebug(dcGoECharger()) << "Discovery:" << networkDeviceInfo.address().toString() << "API V2 verification returned JSON data but not the right one. Continue...";
qCDebug(dcGoECharger()) << "Discovery:" << address.toString() << "API V2 verification returned JSON data but not the right one. Continue...";
}
});
}
Expand Down Expand Up @@ -274,6 +266,7 @@ void GoeDiscovery::onServiceEntryAdded(const ZeroConfServiceEntry &serviceEntry)

// Overwrite any already discovered result for this host, we always prefere ZeroConf over Networkdiscovery...
m_discoveryResults[result.address] = result;
m_verifiedHostAddresses.append(result.address);
}
}

Expand All @@ -289,6 +282,12 @@ void GoeDiscovery::finishDiscovery()
{
disconnect(m_serviceBrowser, &ZeroConfServiceBrowser::serviceEntryAdded, this, &GoeDiscovery::onServiceEntryAdded);

foreach (const Result &result, m_discoveryResults) {
int index = m_discoveredNetworkDeviceInfos.indexFromHostAddress(result.address);
if (index >= 0) {
m_discoveryResults[result.address].networkDeviceInfo = m_discoveredNetworkDeviceInfos.at(index);
}
}

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");
Expand All @@ -309,7 +308,6 @@ QDebug operator<<(QDebug dbg, const GoeDiscovery::Result &result)
dbg.nospace() << ", " << result.address.toString();
} else {
dbg.nospace() << ", " << result.networkDeviceInfo.address().toString();
dbg.nospace() << ", " << result.networkDeviceInfo.macAddress();
}
dbg.nospace() << ") ";
return dbg.maybeSpace();
Expand Down
8 changes: 4 additions & 4 deletions goecharger/goediscovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ class GoeDiscovery : public QObject

QHash<QHostAddress, GoeDiscovery::Result> m_discoveryResults;
NetworkDeviceInfos m_discoveredNetworkDeviceInfos;
NetworkDeviceInfos m_verifiedNetworkDeviceInfos;
QList<QHostAddress> m_verifiedHostAddresses;

QList<QNetworkReply *> m_pendingReplies;

private slots:
void checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo);
void checkNetworkDeviceApiV1(const NetworkDeviceInfo &networkDeviceInfo);
void checkNetworkDeviceApiV2(const NetworkDeviceInfo &networkDeviceInfo);
void checkHostAddress(const QHostAddress &address);
void checkHostAddressApiV1(const QHostAddress &address);
void checkHostAddressApiV2(const QHostAddress &address);

void onServiceEntryAdded(const ZeroConfServiceEntry &serviceEntry);

Expand Down
21 changes: 14 additions & 7 deletions goecharger/integrationplugingoecharger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ void IntegrationPluginGoECharger::init()
m_serviceBrowser = hardwareManager()->zeroConfController()->createServiceBrowser();
connect(m_serviceBrowser, &ZeroConfServiceBrowser::serviceEntryAdded, this, &IntegrationPluginGoECharger::onServiceEntryAdded);
}

}

void IntegrationPluginGoECharger::discoverThings(ThingDiscoveryInfo *info)
Expand Down Expand Up @@ -86,17 +85,20 @@ void IntegrationPluginGoECharger::discoverThings(ThingDiscoveryInfo *info)
title += " (" + result.manufacturer + ")";
}

ParamList params;

QString description = "Serial: " + result.serialNumber + ", V: " + result.firmwareVersion;
if (result.discoveryMethod == GoeDiscovery::DiscoveryMethodNetwork) {
params << Param(goeHomeThingMacAddressParamTypeId, result.networkDeviceInfo.thingParamValueMacAddress());
params << Param(goeHomeThingHostNameParamTypeId, result.networkDeviceInfo.thingParamValueHostName());
params << Param(goeHomeThingAddressParamTypeId, result.networkDeviceInfo.thingParamValueAddress());
description.append(" - " + result.networkDeviceInfo.address().toString());
} else {
description.append(" - " + result.address.toString());
}

qCDebug(dcGoECharger()) << "-->" << title << description;
ThingDescriptor descriptor(goeHomeThingClassId, title, description);
ParamList params;
params << Param(goeHomeThingMacAddressParamTypeId, result.networkDeviceInfo.macAddress());
params << Param(goeHomeThingSerialNumberParamTypeId, result.serialNumber);
params << Param(goeHomeThingApiVersionParamTypeId, result.apiAvailableV2 ? 2 : 1); // always use v2 if available...
descriptor.setParams(params);
Expand All @@ -123,8 +125,12 @@ void IntegrationPluginGoECharger::setupThing(ThingSetupInfo *info)
Thing *thing = info->thing();
qCDebug(dcGoECharger()) << "Setting up" << thing << thing->params();

MacAddress macAddress = MacAddress(thing->paramValue(goeHomeThingMacAddressParamTypeId).toString());
if (!macAddress.isValid()) {

MacAddress macAddress(thing->paramValue(goeHomeThingMacAddressParamTypeId).toString());
QHostAddress address(thing->paramValue(goeHomeThingAddressParamTypeId).toString());
QString hostName(thing->paramValue(goeHomeThingHostNameParamTypeId).toString());

if (!macAddress.isValid() && address.isNull() && hostName.isEmpty()) {
// ZeroConf
QHostAddress address = getHostAddress(thing);
if (address.isNull()) {
Expand All @@ -143,8 +149,9 @@ void IntegrationPluginGoECharger::setupThing(ThingSetupInfo *info)
hardwareManager()->networkDeviceDiscovery()->unregisterMonitor(m_monitors.take(thing));

// Create the monitor
NetworkDeviceMonitor *monitor = hardwareManager()->networkDeviceDiscovery()->registerMonitor(macAddress);
NetworkDeviceMonitor *monitor = hardwareManager()->networkDeviceDiscovery()->registerMonitor(thing);
m_monitors.insert(thing, monitor);

QHostAddress address = getHostAddress(thing);
if (address.isNull()) {
qCWarning(dcGoECharger()) << "Cannot set up go-eCharger. The host address is not known yet. Maybe it will be available in the next run...";
Expand Down Expand Up @@ -222,7 +229,7 @@ void IntegrationPluginGoECharger::setupThing(ThingSetupInfo *info)
setupGoeHome(info);
} else {
qCDebug(dcGoECharger()) << "Wait for the network monitor to get reachable";
connect(monitor, &NetworkDeviceMonitor::reachableChanged, info, [=](bool reachable){
connect(monitor, &NetworkDeviceMonitor::reachableChanged, info, [this, info](bool reachable){
if (reachable) {
setupGoeHome(info);
}
Expand Down
14 changes: 13 additions & 1 deletion goecharger/integrationplugingoecharger.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,26 @@
"displayName": "go-eCharger Home",
"id": "3b663d51-fdb5-4944-b409-c07f7933877e",
"createMethods": ["Discovery", "User"],
"interfaces": ["evcharger", "smartmeterconsumer", "connectable"],
"interfaces": ["evcharger", "smartmeterconsumer", "connectable", "networkdevice"],
"paramTypes": [
{
"id": "0e30e30f-ad96-417e-b739-cac85f75de39",
"name":"macAddress",
"displayName": "MAC address",
"type": "QString"
},
{
"id": "b70db8c3-b380-4c9a-8954-c02126b057b3",
"name":"hostName",
"displayName": "Host name",
"type": "QString"
},
{
"id": "9492e39a-0300-47b9-aeda-8a2b14f9ff2b",
"name":"address",
"displayName": "IP address",
"type": "QString"
},
{
"id": "74abaff3-39e6-40be-b3c3-f41911d17e02",
"name": "serialNumber",
Expand Down
Loading

0 comments on commit d2da165

Please sign in to comment.