From 14f0519547e08383fed18fa36cf135bdd0259cef Mon Sep 17 00:00:00 2001 From: "bernhard.trinnes" Date: Tue, 9 Jun 2020 10:35:00 +0200 Subject: [PATCH] added mDNS discovery --- philipshue/integrationpluginphilipshue.cpp | 57 +++++++++++++++++++++- philipshue/integrationpluginphilipshue.h | 9 ++-- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/philipshue/integrationpluginphilipshue.cpp b/philipshue/integrationpluginphilipshue.cpp index 1b2fe8ab..3b2d2064 100644 --- a/philipshue/integrationpluginphilipshue.cpp +++ b/philipshue/integrationpluginphilipshue.cpp @@ -36,6 +36,9 @@ #include "network/upnp/upnpdiscovery.h" #include "network/upnp/upnpdiscoveryreply.h" +#include "platform/platformzeroconfcontroller.h" +#include "network/zeroconf/zeroconfservicebrowser.h" + #include #include #include @@ -78,11 +81,29 @@ void IntegrationPluginPhilipsHue::init() } }); + m_zeroConfBrowser = hardwareManager()->zeroConfController()->createServiceBrowser("_hue._tcp._local"); + connect(m_zeroConfBrowser, &ZeroConfServiceBrowser::serviceEntryAdded, this, [=](const ZeroConfServiceEntry &entry){ + foreach (Thing *thing, myThings().filterByThingClassId(bridgeThingClassId)) { + QString thingId = thing->paramValue(bridgeThingIdParamTypeId).toString(); + if (entry.protocol() == QAbstractSocket::IPv4Protocol) { + + foreach (const QString &txtEntry, entry.txt()) { + QStringList parts = txtEntry.split('='); + if (parts.length() == 2 && parts.first() == "uuid" && parts.last() == thingId) { + thing->setParamValue(bridgeThingHostParamTypeId, entry.hostAddress().toString()); + HueBridge *bridge = m_bridges.key(thing); + bridge->setHostAddress(entry.hostAddress()); + //TODO also add upnp to update the address + } + } + } + } + }); } void IntegrationPluginPhilipsHue::discoverThings(ThingDiscoveryInfo *info) { - // We're starting a UpnpDiscovery and a NUpnpDiscovery. + // We're starting a mDNS-, Upnp- and a NUpnpDiscovery. // For that, we create a tracking object holding pointers to both of those discoveries. // Both discoveries add their results to a temporary list. // Once a discovery is finished, it will remove itself from the tracking object. @@ -98,6 +119,38 @@ void IntegrationPluginPhilipsHue::discoverThings(ThingDiscoveryInfo *info) delete m_discoveries.take(info); }); + foreach (const ZeroConfServiceEntry &entry, m_zeroConfBrowser->serviceEntries()) { + + /* + * Philips Hue - xxxxxx._hue._tcp._local + * protocol: tcp + * service: hue + * name: Philips Hue - xxxxxx where xxxxxx are the last 6 digits of the bridge ID. + */ + if (!entry.serviceType().contains("_hue._tcp._local")) { + continue; + } + qCDebug(dcPhilipsHue()) << "Zeroconf entry:" << entry; + + QString name = entry.name().split(" - ").first(); + QString id = entry.name().split(" - ").last(); + QHostAddress address = entry.hostAddress(); + + ParamList params; + params.append(Param(bridgeThingHostParamTypeId, address.toString())); + params.append(Param(bridgeThingIdParamTypeId, id)); + + ThingDescriptor descriptor(bridgeThingClassId, "Philips Hue Bridge", address.toString()); + descriptor.setParams(params); + + foreach (Thing *thing, myThings()) { + if (thing->paramValue(bridgeThingIdParamTypeId).toString() == id) { + descriptor.setThingId(thing->id()); + break; + } + } + discovery->results.append(descriptor); + } qCDebug(dcPhilipsHue()) << "Starting UPnP discovery..."; UpnpDiscoveryReply *upnpReply = hardwareManager()->upnpDiscovery()->discoverDevices("libhue:idl"); @@ -139,7 +192,7 @@ void IntegrationPluginPhilipsHue::discoverThings(ThingDiscoveryInfo *info) qCDebug(dcPhilipsHue) << "Starting N-UPNP discovery..."; - QNetworkRequest request(QUrl("https://www.meethue.com/api/nupnp")); + QNetworkRequest request(QUrl(" https://discovery.meethue.com ")); QNetworkReply *nUpnpReply = hardwareManager()->networkManager()->get(request); discovery->nUpnpReply = nUpnpReply; diff --git a/philipshue/integrationpluginphilipshue.h b/philipshue/integrationpluginphilipshue.h index bbece57d..465863f3 100644 --- a/philipshue/integrationpluginphilipshue.h +++ b/philipshue/integrationpluginphilipshue.h @@ -41,6 +41,9 @@ #include "plugintimer.h" #include "network/networkaccessmanager.h" #include "network/upnp/upnpdiscovery.h" +#include "network/zeroconf/zeroconfservicebrowser.h" +#include "network/zeroconf/zeroconfserviceentry.h" + class QNetworkReply; @@ -90,6 +93,7 @@ private: QNetworkReply* nUpnpReply; ThingDescriptors results; }; + ZeroConfServiceBrowser *m_zeroConfBrowser = nullptr; QHash m_discoveries; void finishDiscovery(DiscoveryJob* job); @@ -108,8 +112,6 @@ private: QHash m_bridgeSensorsDiscoveryRequests; QHash m_bridgeSearchDevicesRequests; - QHash > m_asyncActions; - QHash m_bridges; QHash m_lights; QHash m_remotes; @@ -135,7 +137,6 @@ private: void processLightsRefreshResponse(Thing *thing, const QByteArray &data); void processSensorsRefreshResponse(Thing *thing, const QByteArray &data); void processSetNameResponse(Thing *thing, const QByteArray &data); - void processActionResponse(Thing *thing, const ActionId actionId, const QByteArray &data); void bridgeReachableChanged(Thing *thing, const bool &reachable); @@ -149,4 +150,4 @@ private: void abortRequests(QHash requestList, Thing* thing); }; -#endif // INTEGRATIONPLUGINBOBLIGHT_H +#endif // INTEGRATIONPLUGINPHILIPSHUE_H