Update webasto to make use of nymea internal network discovery

master
Simon Stürz 2021-06-07 16:48:09 +02:00 committed by Michael Zanetti
parent d482fe3418
commit cb43bc0797
4 changed files with 64 additions and 55 deletions

View File

@ -28,6 +28,7 @@
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "network/networkdevicediscovery.h"
#include "integrationpluginwebasto.h" #include "integrationpluginwebasto.h"
#include "plugininfo.h" #include "plugininfo.h"
@ -45,61 +46,78 @@ IntegrationPluginWebasto::IntegrationPluginWebasto()
void IntegrationPluginWebasto::init() void IntegrationPluginWebasto::init()
{ {
m_discovery = new Discovery(this); // FIXME: make use of the internal network discovery if the device gets unavailable. For now, commented out since it has not been used
connect(m_discovery, &Discovery::finished, this, [this](const QList<Host> &hosts) { // at the moment of changing this.
foreach (const Host &host, hosts) { // m_discovery = new Discovery(this);
if (!host.hostName().contains("webasto", Qt::CaseSensitivity::CaseInsensitive)) // connect(m_discovery, &Discovery::finished, this, [this](const QList<Host> &hosts) {
continue;
foreach (Thing *existingThing, myThings()) { // foreach (const Host &host, hosts) {
if (existingThing->paramValue(liveWallboxThingMacAddressParamTypeId).toString().isEmpty()) { // if (!host.hostName().contains("webasto", Qt::CaseSensitivity::CaseInsensitive))
//This device got probably manually setup, to enable auto rediscovery the MAC address needs to setup // continue;
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 (Thing *existingThing, myThings()) {
} else if (existingThing->paramValue(liveWallboxThingMacAddressParamTypeId).toString() == host.macAddress()) { // if (existingThing->paramValue(liveWallboxThingMacAddressParamTypeId).toString().isEmpty()) {
if (existingThing->paramValue(liveWallboxThingIpAddressParamTypeId).toString() != host.address()) { // //This device got probably manually setup, to enable auto rediscovery the MAC address needs to setup
qCDebug(dcWebasto()) << "Wallbox IP Address has changed, from" << existingThing->paramValue(liveWallboxThingIpAddressParamTypeId).toString() << "to" << host.address(); // if (existingThing->paramValue(liveWallboxThingIpAddressParamTypeId).toString() == host.address()) {
existingThing->setParamValue(liveWallboxThingIpAddressParamTypeId, 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(); // } else if (existingThing->paramValue(liveWallboxThingMacAddressParamTypeId).toString() == host.macAddress()) {
} // if (existingThing->paramValue(liveWallboxThingIpAddressParamTypeId).toString() != host.address()) {
break; // 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) void IntegrationPluginWebasto::discoverThings(ThingDiscoveryInfo *info)
{ {
qCDebug(dcWebasto()) << "Discover things";
if (info->thingClassId() == liveWallboxThingClassId) { if (info->thingClassId() == liveWallboxThingClassId) {
m_discovery->discoverHosts(25); qCDebug(dcWebasto()) << "Discover things";
connect(m_discovery, &Discovery::finished, info, [this, info] (const QList<Host> &hosts) { NetworkDeviceDiscoveryReply *discoveryReply = hardwareManager()->networkDeviceDiscovery()->discover();
connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, this, [=](){
foreach (const Host &host, hosts) { ThingDescriptors descriptors;
if (!host.hostName().contains("webasto", Qt::CaseSensitivity::CaseInsensitive)) 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; continue;
qCDebug(dcWebasto()) << " - " << host.hostName() << host.address() << host.macAddress(); QString title = "Wallbox ";
ThingDescriptor descriptor(liveWallboxThingClassId, "Wallbox", host.address() + " (" + host.macAddress() + ")"); if (networkDevice.hostName().isEmpty()) {
title += networkDevice.address().toString();
// Rediscovery } else {
foreach (Thing *existingThing, myThings()) { title += networkDevice.address().toString() + " (" + networkDevice.hostName() + ")";
if (existingThing->paramValue(liveWallboxThingMacAddressParamTypeId).toString() == host.macAddress()) {
qCDebug(dcWebasto()) << " - Device is already added";
descriptor.setThingId(existingThing->id());
break;
}
} }
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; ParamList params;
params << Param(liveWallboxThingMacAddressParamTypeId, host.macAddress()); params << Param(liveWallboxThingIpAddressParamTypeId, networkDevice.address().toString());
params << Param(liveWallboxThingIpAddressParamTypeId, host.address()); params << Param(liveWallboxThingMacAddressParamTypeId, networkDevice.macAddress());
descriptor.setParams(params); descriptor.setParams(params);
info->addThingDescriptor(descriptor); info->addThingDescriptor(descriptor);
} }

View File

@ -34,8 +34,6 @@
#include "integrations/integrationplugin.h" #include "integrations/integrationplugin.h"
#include "plugintimer.h" #include "plugintimer.h"
#include "webasto.h" #include "webasto.h"
#include "../discovery/discovery.h"
#include "../discovery/host.h"
#include "../modbus/modbustcpmaster.h" #include "../modbus/modbustcpmaster.h"
#include <QObject> #include <QObject>
@ -59,7 +57,6 @@ public:
void thingRemoved(Thing *thing) override; void thingRemoved(Thing *thing) override;
private: private:
Discovery *m_discovery = nullptr;
PluginTimer *m_pluginTimer = nullptr; PluginTimer *m_pluginTimer = nullptr;
QHash<Thing *, Webasto *> m_webastoConnections; QHash<Thing *, Webasto *> m_webastoConnections;
QHash<QUuid, ThingActionInfo *> m_asyncActions; QHash<QUuid, ThingActionInfo *> m_asyncActions;

View File

@ -80,7 +80,7 @@ void Webasto::setLivebitInterval(uint seconds)
void Webasto::getRegister(Webasto::TqModbusRegister modbusRegister, uint length) void Webasto::getRegister(Webasto::TqModbusRegister modbusRegister, uint length)
{ {
qCDebug(dcWebasto()) << "Webasto: Get register" << modbusRegister << 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]"; qCWarning(dcWebasto()) << "Invalide register length, allowed values [1,10]";
return; return;
} }

View File

@ -1,19 +1,13 @@
include(../plugins.pri) include(../plugins.pri)
QT += \ QT += serialbus network
serialbus \
network
SOURCES += \ SOURCES += \
integrationpluginwebasto.cpp \ integrationpluginwebasto.cpp \
webasto.cpp \ webasto.cpp \
../modbus/modbustcpmaster.cpp \ ../modbus/modbustcpmaster.cpp
../discovery/discovery.cpp \
../discovery/host.cpp
HEADERS += \ HEADERS += \
integrationpluginwebasto.h \ integrationpluginwebasto.h \
webasto.h \ webasto.h \
../modbus/modbustcpmaster.h \ ../modbus/modbustcpmaster.h
../discovery/discovery.h \
../discovery/host.h