added mDNS discovery

This commit is contained in:
bernhard.trinnes 2020-06-09 10:35:00 +02:00
parent 5c97828655
commit 14f0519547
2 changed files with 60 additions and 6 deletions

View File

@ -36,6 +36,9 @@
#include "network/upnp/upnpdiscovery.h" #include "network/upnp/upnpdiscovery.h"
#include "network/upnp/upnpdiscoveryreply.h" #include "network/upnp/upnpdiscoveryreply.h"
#include "platform/platformzeroconfcontroller.h"
#include "network/zeroconf/zeroconfservicebrowser.h"
#include <QDebug> #include <QDebug>
#include <QColor> #include <QColor>
#include <QDateTime> #include <QDateTime>
@ -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) 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. // For that, we create a tracking object holding pointers to both of those discoveries.
// Both discoveries add their results to a temporary list. // Both discoveries add their results to a temporary list.
// Once a discovery is finished, it will remove itself from the tracking object. // 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); 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..."; qCDebug(dcPhilipsHue()) << "Starting UPnP discovery...";
UpnpDiscoveryReply *upnpReply = hardwareManager()->upnpDiscovery()->discoverDevices("libhue:idl"); UpnpDiscoveryReply *upnpReply = hardwareManager()->upnpDiscovery()->discoverDevices("libhue:idl");
@ -139,7 +192,7 @@ void IntegrationPluginPhilipsHue::discoverThings(ThingDiscoveryInfo *info)
qCDebug(dcPhilipsHue) << "Starting N-UPNP discovery..."; 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); QNetworkReply *nUpnpReply = hardwareManager()->networkManager()->get(request);
discovery->nUpnpReply = nUpnpReply; discovery->nUpnpReply = nUpnpReply;

View File

@ -41,6 +41,9 @@
#include "plugintimer.h" #include "plugintimer.h"
#include "network/networkaccessmanager.h" #include "network/networkaccessmanager.h"
#include "network/upnp/upnpdiscovery.h" #include "network/upnp/upnpdiscovery.h"
#include "network/zeroconf/zeroconfservicebrowser.h"
#include "network/zeroconf/zeroconfserviceentry.h"
class QNetworkReply; class QNetworkReply;
@ -90,6 +93,7 @@ private:
QNetworkReply* nUpnpReply; QNetworkReply* nUpnpReply;
ThingDescriptors results; ThingDescriptors results;
}; };
ZeroConfServiceBrowser *m_zeroConfBrowser = nullptr;
QHash<ThingDiscoveryInfo*, DiscoveryJob*> m_discoveries; QHash<ThingDiscoveryInfo*, DiscoveryJob*> m_discoveries;
void finishDiscovery(DiscoveryJob* job); void finishDiscovery(DiscoveryJob* job);
@ -108,8 +112,6 @@ private:
QHash<QNetworkReply *, Thing *> m_bridgeSensorsDiscoveryRequests; QHash<QNetworkReply *, Thing *> m_bridgeSensorsDiscoveryRequests;
QHash<QNetworkReply *, Thing *> m_bridgeSearchDevicesRequests; QHash<QNetworkReply *, Thing *> m_bridgeSearchDevicesRequests;
QHash<QNetworkReply *, QPair<Thing *, ActionId> > m_asyncActions;
QHash<HueBridge *, Thing *> m_bridges; QHash<HueBridge *, Thing *> m_bridges;
QHash<HueLight *, Thing *> m_lights; QHash<HueLight *, Thing *> m_lights;
QHash<HueRemote *, Thing *> m_remotes; QHash<HueRemote *, Thing *> m_remotes;
@ -135,7 +137,6 @@ private:
void processLightsRefreshResponse(Thing *thing, const QByteArray &data); void processLightsRefreshResponse(Thing *thing, const QByteArray &data);
void processSensorsRefreshResponse(Thing *thing, const QByteArray &data); void processSensorsRefreshResponse(Thing *thing, const QByteArray &data);
void processSetNameResponse(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); void bridgeReachableChanged(Thing *thing, const bool &reachable);
@ -149,4 +150,4 @@ private:
void abortRequests(QHash<QNetworkReply *, Thing *> requestList, Thing* thing); void abortRequests(QHash<QNetworkReply *, Thing *> requestList, Thing* thing);
}; };
#endif // INTEGRATIONPLUGINBOBLIGHT_H #endif // INTEGRATIONPLUGINPHILIPSHUE_H