From cb43bc07975b30703017882068c7a86b85c703e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Mon, 7 Jun 2021 16:48:09 +0200 Subject: [PATCH] Update webasto to make use of nymea internal network discovery --- webasto/integrationpluginwebasto.cpp | 102 ++++++++++++++++----------- webasto/integrationpluginwebasto.h | 3 - webasto/webasto.cpp | 2 +- webasto/webasto.pro | 12 +--- 4 files changed, 64 insertions(+), 55 deletions(-) diff --git a/webasto/integrationpluginwebasto.cpp b/webasto/integrationpluginwebasto.cpp index f7e05af..6f720e5 100644 --- a/webasto/integrationpluginwebasto.cpp +++ b/webasto/integrationpluginwebasto.cpp @@ -28,6 +28,7 @@ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ +#include "network/networkdevicediscovery.h" #include "integrationpluginwebasto.h" #include "plugininfo.h" @@ -45,61 +46,78 @@ IntegrationPluginWebasto::IntegrationPluginWebasto() void IntegrationPluginWebasto::init() { - m_discovery = new Discovery(this); - connect(m_discovery, &Discovery::finished, this, [this](const QList &hosts) { + // FIXME: make use of the internal network discovery if the device gets unavailable. For now, commented out since it has not been used + // at the moment of changing this. - foreach (const Host &host, hosts) { - if (!host.hostName().contains("webasto", Qt::CaseSensitivity::CaseInsensitive)) - continue; +// m_discovery = new Discovery(this); +// connect(m_discovery, &Discovery::finished, this, [this](const QList &hosts) { - foreach (Thing *existingThing, myThings()) { - if (existingThing->paramValue(liveWallboxThingMacAddressParamTypeId).toString().isEmpty()) { - //This device got probably manually setup, to enable auto rediscovery the MAC address needs to setup - if (existingThing->paramValue(liveWallboxThingIpAddressParamTypeId).toString() == host.address()) { - qCDebug(dcWebasto()) << "Wallbox MAC Address has been discovered" << existingThing->name() << host.macAddress(); - existingThing->setParamValue(liveWallboxThingMacAddressParamTypeId, host.macAddress()); +// foreach (const Host &host, hosts) { +// if (!host.hostName().contains("webasto", Qt::CaseSensitivity::CaseInsensitive)) +// continue; - } - } else if (existingThing->paramValue(liveWallboxThingMacAddressParamTypeId).toString() == host.macAddress()) { - if (existingThing->paramValue(liveWallboxThingIpAddressParamTypeId).toString() != host.address()) { - qCDebug(dcWebasto()) << "Wallbox IP Address has changed, from" << existingThing->paramValue(liveWallboxThingIpAddressParamTypeId).toString() << "to" << host.address(); - existingThing->setParamValue(liveWallboxThingIpAddressParamTypeId, host.address()); +// foreach (Thing *existingThing, myThings()) { +// if (existingThing->paramValue(liveWallboxThingMacAddressParamTypeId).toString().isEmpty()) { +// //This device got probably manually setup, to enable auto rediscovery the MAC address needs to setup +// if (existingThing->paramValue(liveWallboxThingIpAddressParamTypeId).toString() == host.address()) { +// qCDebug(dcWebasto()) << "Wallbox MAC Address has been discovered" << existingThing->name() << host.macAddress(); +// existingThing->setParamValue(liveWallboxThingMacAddressParamTypeId, host.macAddress()); - } else { - qCDebug(dcWebasto()) << "Wallbox" << existingThing->name() << "IP address has not changed" << host.address(); - } - break; - } - } - } - }); +// } +// } else if (existingThing->paramValue(liveWallboxThingMacAddressParamTypeId).toString() == host.macAddress()) { +// if (existingThing->paramValue(liveWallboxThingIpAddressParamTypeId).toString() != host.address()) { +// qCDebug(dcWebasto()) << "Wallbox IP Address has changed, from" << existingThing->paramValue(liveWallboxThingIpAddressParamTypeId).toString() << "to" << host.address(); +// existingThing->setParamValue(liveWallboxThingIpAddressParamTypeId, host.address()); + +// } else { +// qCDebug(dcWebasto()) << "Wallbox" << existingThing->name() << "IP address has not changed" << host.address(); +// } +// break; +// } +// } +// } +// }); } void IntegrationPluginWebasto::discoverThings(ThingDiscoveryInfo *info) { - qCDebug(dcWebasto()) << "Discover things"; if (info->thingClassId() == liveWallboxThingClassId) { - m_discovery->discoverHosts(25); - connect(m_discovery, &Discovery::finished, info, [this, info] (const QList &hosts) { - - foreach (const Host &host, hosts) { - if (!host.hostName().contains("webasto", Qt::CaseSensitivity::CaseInsensitive)) + qCDebug(dcWebasto()) << "Discover things"; + NetworkDeviceDiscoveryReply *discoveryReply = hardwareManager()->networkDeviceDiscovery()->discover(); + connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, this, [=](){ + ThingDescriptors descriptors; + qCDebug(dcWebasto()) << "Discovery finished. Found" << discoveryReply->networkDevices().count() << "devices"; + foreach (const NetworkDevice &networkDevice, discoveryReply->networkDevices()) { + qCDebug(dcWebasto()) << networkDevice; + if (!networkDevice.hostName().contains("webasto", Qt::CaseSensitivity::CaseInsensitive)) continue; - qCDebug(dcWebasto()) << " - " << host.hostName() << host.address() << host.macAddress(); - ThingDescriptor descriptor(liveWallboxThingClassId, "Wallbox", host.address() + " (" + host.macAddress() + ")"); - - // Rediscovery - foreach (Thing *existingThing, myThings()) { - if (existingThing->paramValue(liveWallboxThingMacAddressParamTypeId).toString() == host.macAddress()) { - qCDebug(dcWebasto()) << " - Device is already added"; - descriptor.setThingId(existingThing->id()); - break; - } + QString title = "Wallbox "; + if (networkDevice.hostName().isEmpty()) { + title += networkDevice.address().toString(); + } else { + title += networkDevice.address().toString() + " (" + networkDevice.hostName() + ")"; } + + QString description; + if (networkDevice.macAddressManufacturer().isEmpty()) { + description = networkDevice.macAddress(); + } else { + description = networkDevice.macAddress() + " (" + networkDevice.macAddressManufacturer() + ")"; + } + + ThingDescriptor descriptor(liveWallboxThingClassId, title, description); + + // Check if we already have set up this device + Things existingThings = myThings().filterByParam(liveWallboxThingIpAddressParamTypeId, networkDevice.address().toString()); + if (existingThings.count() == 1) { + qCDebug(dcWebasto()) << "This thing already exists in the system." << existingThings.first() << networkDevice; + descriptor.setThingId(existingThings.first()->id()); + } + ParamList params; - params << Param(liveWallboxThingMacAddressParamTypeId, host.macAddress()); - params << Param(liveWallboxThingIpAddressParamTypeId, host.address()); + params << Param(liveWallboxThingIpAddressParamTypeId, networkDevice.address().toString()); + params << Param(liveWallboxThingMacAddressParamTypeId, networkDevice.macAddress()); descriptor.setParams(params); info->addThingDescriptor(descriptor); } diff --git a/webasto/integrationpluginwebasto.h b/webasto/integrationpluginwebasto.h index b1d765b..199cf1a 100644 --- a/webasto/integrationpluginwebasto.h +++ b/webasto/integrationpluginwebasto.h @@ -34,8 +34,6 @@ #include "integrations/integrationplugin.h" #include "plugintimer.h" #include "webasto.h" -#include "../discovery/discovery.h" -#include "../discovery/host.h" #include "../modbus/modbustcpmaster.h" #include @@ -59,7 +57,6 @@ public: void thingRemoved(Thing *thing) override; private: - Discovery *m_discovery = nullptr; PluginTimer *m_pluginTimer = nullptr; QHash m_webastoConnections; QHash m_asyncActions; diff --git a/webasto/webasto.cpp b/webasto/webasto.cpp index 9c980ad..94c6d77 100644 --- a/webasto/webasto.cpp +++ b/webasto/webasto.cpp @@ -80,7 +80,7 @@ void Webasto::setLivebitInterval(uint seconds) void Webasto::getRegister(Webasto::TqModbusRegister modbusRegister, uint length) { qCDebug(dcWebasto()) << "Webasto: Get register" << modbusRegister << length; - if (length < 1 && length > 10) { + if (length < 1 || length > 10) { qCWarning(dcWebasto()) << "Invalide register length, allowed values [1,10]"; return; } diff --git a/webasto/webasto.pro b/webasto/webasto.pro index 3d94e5f..c39fc78 100644 --- a/webasto/webasto.pro +++ b/webasto/webasto.pro @@ -1,19 +1,13 @@ include(../plugins.pri) -QT += \ - serialbus \ - network +QT += serialbus network SOURCES += \ integrationpluginwebasto.cpp \ webasto.cpp \ - ../modbus/modbustcpmaster.cpp \ - ../discovery/discovery.cpp \ - ../discovery/host.cpp + ../modbus/modbustcpmaster.cpp HEADERS += \ integrationpluginwebasto.h \ webasto.h \ - ../modbus/modbustcpmaster.h \ - ../discovery/discovery.h \ - ../discovery/host.h + ../modbus/modbustcpmaster.h