From 12ca5b7f480180330dc846eb1ffbd1c58f5040bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Wed, 18 Dec 2024 11:52:36 +0100 Subject: [PATCH] EspSomfyRts: Update to networkdevice interface --- espsomfyrts/espsomfyrts.cpp | 5 +++ espsomfyrts/espsomfyrts.h | 1 + espsomfyrts/espsomfyrtsdiscovery.cpp | 18 ++++++---- espsomfyrts/espsomfyrtsdiscovery.h | 4 ++- espsomfyrts/integrationpluginespsomfyrts.cpp | 36 ++++++++++--------- espsomfyrts/integrationpluginespsomfyrts.json | 23 ++++++++++-- 6 files changed, 61 insertions(+), 26 deletions(-) diff --git a/espsomfyrts/espsomfyrts.cpp b/espsomfyrts/espsomfyrts.cpp index caa8d2f5..b8a7e09d 100644 --- a/espsomfyrts/espsomfyrts.cpp +++ b/espsomfyrts/espsomfyrts.cpp @@ -80,6 +80,11 @@ EspSomfyRts::EspSomfyRts(NetworkDeviceMonitor *monitor, QObject *parent) }); } +NetworkDeviceMonitor *EspSomfyRts::monitor() const +{ + return m_monitor; +} + QHostAddress EspSomfyRts::address() const { return QHostAddress(m_websocketUrl.host()); diff --git a/espsomfyrts/espsomfyrts.h b/espsomfyrts/espsomfyrts.h index 2820b200..23fe2971 100644 --- a/espsomfyrts/espsomfyrts.h +++ b/espsomfyrts/espsomfyrts.h @@ -95,6 +95,7 @@ public: explicit EspSomfyRts(NetworkDeviceMonitor *monitor, QObject *parent = nullptr); + NetworkDeviceMonitor *monitor() const; QHostAddress address() const; bool connected() const; diff --git a/espsomfyrts/espsomfyrtsdiscovery.cpp b/espsomfyrts/espsomfyrtsdiscovery.cpp index 3bcf7481..0497896f 100644 --- a/espsomfyrts/espsomfyrtsdiscovery.cpp +++ b/espsomfyrts/espsomfyrtsdiscovery.cpp @@ -53,9 +53,10 @@ void EspSomfyRtsDiscovery::startDiscovery() m_startDateTime = QDateTime::currentDateTime(); NetworkDeviceDiscoveryReply *discoveryReply = m_networkDeviceDiscovery->discover(); - connect(discoveryReply, &NetworkDeviceDiscoveryReply::networkDeviceInfoAdded, this, &EspSomfyRtsDiscovery::checkNetworkDevice); + connect(discoveryReply, &NetworkDeviceDiscoveryReply::hostAddressDiscovered, this, &EspSomfyRtsDiscovery::checkNetworkDevice); connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, this, [=](){ qCDebug(dcESPSomfyRTS()) << "Discovery: Network discovery finished. Found" << discoveryReply->networkDeviceInfos().count() << "network devices"; + m_networkDeviceInfos = discoveryReply->networkDeviceInfos(); m_gracePeriodTimer.start(); discoveryReply->deleteLater(); }); @@ -66,18 +67,18 @@ QList EspSomfyRtsDiscovery::results() const return m_results; } -void EspSomfyRtsDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo) +void EspSomfyRtsDiscovery::checkNetworkDevice(const QHostAddress &address) { - qCDebug(dcESPSomfyRTS()) << "Discovery: Verifying" << networkDeviceInfo; + qCDebug(dcESPSomfyRTS()) << "Discovery: Verifying" << address; QUrl url; url.setScheme("http"); - url.setHost(networkDeviceInfo.address().toString()); + url.setHost(address.toString()); url.setPort(8081); url.setPath("/discovery"); QNetworkReply *reply = m_networkManager->get(QNetworkRequest(url)); connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater); - connect(reply, &QNetworkReply::finished, this, [this, reply, networkDeviceInfo](){ + connect(reply, &QNetworkReply::finished, this, [this, reply, address](){ if (reply->error() != QNetworkReply::NoError) { qCDebug(dcESPSomfyRTS()) << "Discovery: Reply finished with error" << reply->errorString() << "Continue..."; return; @@ -95,7 +96,7 @@ void EspSomfyRtsDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDe if (responseMap.contains("model") && responseMap.value("model").toString().toLower().contains("espsomfyrts")) { Result result; - result.networkDeviceInfo = networkDeviceInfo; + result.address = address; result.name = responseMap.value("serverId").toString(); result.firmwareVersion = responseMap.value("version").toString(); m_results.append(result); @@ -109,6 +110,11 @@ void EspSomfyRtsDiscovery::checkNetworkDevice(const NetworkDeviceInfo &networkDe void EspSomfyRtsDiscovery::finishDiscovery() { qint64 durationMilliSeconds = QDateTime::currentMSecsSinceEpoch() - m_startDateTime.toMSecsSinceEpoch(); + + // Fill in all network device infos we have + for (int i = 0; i < m_results.count(); i++) + m_results[i].networkDeviceInfo = m_networkDeviceInfos.get(m_results.at(i).address); + qCDebug(dcESPSomfyRTS()) << "Discovery: Finished the discovery process. Found" << m_results.count() << "ESPSomfy-RTS devices in" << QTime::fromMSecsSinceStartOfDay(durationMilliSeconds).toString("mm:ss.zzz"); m_gracePeriodTimer.stop(); diff --git a/espsomfyrts/espsomfyrtsdiscovery.h b/espsomfyrts/espsomfyrtsdiscovery.h index 8b69c196..cb279f48 100644 --- a/espsomfyrts/espsomfyrtsdiscovery.h +++ b/espsomfyrts/espsomfyrtsdiscovery.h @@ -46,6 +46,7 @@ public: typedef struct Result { QString name; QString firmwareVersion; + QHostAddress address; NetworkDeviceInfo networkDeviceInfo; } Result; @@ -63,9 +64,10 @@ private: QTimer m_gracePeriodTimer; QDateTime m_startDateTime; + NetworkDeviceInfos m_networkDeviceInfos; QList m_results; - void checkNetworkDevice(const NetworkDeviceInfo &networkDeviceInfo); + void checkNetworkDevice(const QHostAddress &address); void finishDiscovery(); }; diff --git a/espsomfyrts/integrationpluginespsomfyrts.cpp b/espsomfyrts/integrationpluginespsomfyrts.cpp index cd77d48a..235dea8b 100644 --- a/espsomfyrts/integrationpluginespsomfyrts.cpp +++ b/espsomfyrts/integrationpluginespsomfyrts.cpp @@ -66,24 +66,25 @@ void IntegrationPluginEspSomfyRts::discoverThings(ThingDiscoveryInfo *info) qCInfo(dcESPSomfyRTS()) << "Discovery finished. Found" << discovery->results().count() << "devices"; foreach (const EspSomfyRtsDiscovery::Result &result, discovery->results()) { qCInfo(dcESPSomfyRTS()) << "Discovered device on" << result.networkDeviceInfo; - if (result.networkDeviceInfo.macAddress().isNull()) - continue; QString title = "ESP Somfy RTS (" + result.name + ")"; - QString description = result.networkDeviceInfo.address().toString() + " (" + result.networkDeviceInfo.macAddress() + ")"; + QString description = result.networkDeviceInfo.address().toString(); ThingDescriptor descriptor(espSomfyRtsThingClassId, title, description); + ParamList params; + params << Param(espSomfyRtsThingMacAddressParamTypeId, result.networkDeviceInfo.thingParamValueMacAddress()); + params << Param(espSomfyRtsThingHostNameParamTypeId, result.networkDeviceInfo.thingParamValueHostName()); + params << Param(espSomfyRtsThingAddressParamTypeId, result.networkDeviceInfo.thingParamValueAddress()); + descriptor.setParams(params); + // Check if we already have set up this device - Things existingThings = myThings().filterByParam(espSomfyRtsThingMacAddressParamTypeId, result.networkDeviceInfo.macAddress()); - if (existingThings.count() == 1) { - qCDebug(dcESPSomfyRTS()) << "This thing already exists in the system." << existingThings.first() << result.networkDeviceInfo; - descriptor.setThingId(existingThings.first()->id()); + Thing *existingThing = myThings().findByParams(params); + if (existingThing) { + qCDebug(dcESPSomfyRTS()) << "This thing already exists in the system:" << result.networkDeviceInfo; + descriptor.setThingId(existingThing->id()); } - ParamList params; - params << Param(espSomfyRtsThingMacAddressParamTypeId, result.networkDeviceInfo.macAddress()); - descriptor.setParams(params); info->addThingDescriptor(descriptor); } @@ -104,15 +105,13 @@ void IntegrationPluginEspSomfyRts::setupThing(ThingSetupInfo *info) return; } - MacAddress macAddress(thing->paramValue(espSomfyRtsThingMacAddressParamTypeId).toString()); - if (!macAddress.isValid()) { - qCWarning(dcESPSomfyRTS()) << "Invalid MAC address, cannot set up thing" << thing << thing->params(); - info->finish(Thing::ThingErrorHardwareNotAvailable); + NetworkDeviceMonitor *monitor = hardwareManager()->networkDeviceDiscovery()->registerMonitor(thing); + if (!monitor) { + qCWarning(dcESPSomfyRTS()) << "Could not register monitor with the given parameters" << thing << thing->params(); + info->finish(Thing::ThingErrorInvalidParameter); return; } - NetworkDeviceMonitor *monitor = hardwareManager()->networkDeviceDiscovery()->registerMonitor(macAddress); - EspSomfyRts *somfy = new EspSomfyRts(monitor, thing); m_somfys.insert(thing, somfy); @@ -171,7 +170,10 @@ void IntegrationPluginEspSomfyRts::postSetupThing(Thing *thing) void IntegrationPluginEspSomfyRts::thingRemoved(Thing *thing) { - Q_UNUSED(thing) + if (thing->thingClassId() == espSomfyRtsThingClassId) { + EspSomfyRts *somfy = m_somfys.take(thing); + hardwareManager()->networkDeviceDiscovery()->unregisterMonitor(somfy->monitor()); + } } void IntegrationPluginEspSomfyRts::executeAction(ThingActionInfo *info) diff --git a/espsomfyrts/integrationpluginespsomfyrts.json b/espsomfyrts/integrationpluginespsomfyrts.json index d96be5f3..1c5105d0 100644 --- a/espsomfyrts/integrationpluginespsomfyrts.json +++ b/espsomfyrts/integrationpluginespsomfyrts.json @@ -13,13 +13,32 @@ "displayName": "ESPSomfy-RTS", "id": "9a477bbe-81f0-46ad-ae62-715c2bba2f1f", "createMethods": ["Discovery", "User"], - "interfaces": ["gateway", "wirelessconnectable" ], + "interfaces": ["gateway", "wirelessconnectable", "networkdevice"], "paramTypes": [ + { + "id": "3e473059-dc06-4da6-93e5-b27db497a887", + "name": "address", + "displayName": "Host address", + "type": "QString", + "inputType": "IPv4Address", + "defaultValue": "" + }, + { + "id": "6426dbbd-978f-4e69-bc07-2d35fd8be88b", + "name": "hostName", + "displayName": "Host name", + "type": "QString", + "inputType": "TextLine", + "defaultValue": "" + }, { "id": "0e30e30f-ad96-417e-b739-cac85f75de39", "name":"macAddress", "displayName": "MAC address", - "type": "QString" + "type": "QString", + "inputType": "MacAddress", + "readOnly": true, + "defaultValue": "" } ], "stateTypes": [