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 "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<Host> &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<Host> &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<Host> &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);
}

View File

@ -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 <QObject>
@ -59,7 +57,6 @@ public:
void thingRemoved(Thing *thing) override;
private:
Discovery *m_discovery = nullptr;
PluginTimer *m_pluginTimer = nullptr;
QHash<Thing *, Webasto *> m_webastoConnections;
QHash<QUuid, ThingActionInfo *> m_asyncActions;

View File

@ -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;
}

View File

@ -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