From 7fcfca3c1df84de59c81fddeba04daa008383a85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Sat, 5 Jun 2021 00:26:10 +0200 Subject: [PATCH] Add device discovery for wol plugin --- wakeonlan/integrationpluginwakeonlan.cpp | 40 +++++++++++++++++++++-- wakeonlan/integrationpluginwakeonlan.h | 4 ++- wakeonlan/integrationpluginwakeonlan.json | 2 +- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/wakeonlan/integrationpluginwakeonlan.cpp b/wakeonlan/integrationpluginwakeonlan.cpp index 5b85d3c4..f454bf40 100644 --- a/wakeonlan/integrationpluginwakeonlan.cpp +++ b/wakeonlan/integrationpluginwakeonlan.cpp @@ -33,6 +33,8 @@ #include "integrations/thing.h" #include "plugininfo.h" +#include "network/networkdevicediscovery.h" + #include #include #include @@ -41,6 +43,40 @@ IntegrationPluginWakeOnLan::IntegrationPluginWakeOnLan() { } +void IntegrationPluginWakeOnLan::discoverThings(ThingDiscoveryInfo *info) +{ + if (!hardwareManager()->networkDeviceDiscovery()->available()) { + qCWarning(dcWakeOnLan()) << "Failed to discover network devices. The network device discovery is not available."; + info->finish(Thing::ThingErrorHardwareNotAvailable, QT_TR_NOOP("The discovery is not available.")); + return; + } + + qCDebug(dcWakeOnLan()) << "Start discovering network devices..."; + NetworkDeviceDiscoveryReply *discoveryReply = hardwareManager()->networkDeviceDiscovery()->discover(); + connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, this, [=](){ + ThingDescriptors descriptors; + qCDebug(dcWakeOnLan()) << "Discovery finished. Found" << discoveryReply->networkDevices().count() << "devices"; + foreach (const NetworkDevice &networkDevice, discoveryReply->networkDevices()) { + // We need the mac address... + if (networkDevice.macAddress().isEmpty()) + continue; + + // Filter out already added network devices, rediscovery is in this case no option + if (myThings().filterByParam(wolThingMacParamTypeId, networkDevice.macAddress()).count() != 0) + continue; + + QString title = networkDevice.address().toString() + " (" + networkDevice.hostName() + ")"; + ThingDescriptor descriptor(wolThingClassId, title, networkDevice.macAddress()); + ParamList params; + params.append(Param(wolThingMacParamTypeId, networkDevice.macAddress())); + descriptor.setParams(params); + descriptors.append(descriptor); + } + info->addThingDescriptors(descriptors); + info->finish(Thing::ThingErrorNoError); + }); +} + void IntegrationPluginWakeOnLan::executeAction(ThingActionInfo *info) { qCDebug(dcWakeOnLan) << "Wake up" << info->thing()->name(); @@ -48,12 +84,12 @@ void IntegrationPluginWakeOnLan::executeAction(ThingActionInfo *info) return info->finish(Thing::ThingErrorNoError); } -void IntegrationPluginWakeOnLan::wakeup(QString mac) +void IntegrationPluginWakeOnLan::wakeup(const QString &macAddress) { const char header[] = {char(0xff), char(0xff), char(0xff), char(0xff), char(0xff), char(0xff)}; QByteArray packet = QByteArray::fromRawData(header, sizeof(header)); for(int i = 0; i < 16; ++i) { - packet.append(QByteArray::fromHex(mac.remove(':').toLocal8Bit())); + packet.append(QByteArray::fromHex(QString(macAddress).remove(':').toLocal8Bit())); } qCDebug(dcWakeOnLan) << "Created magic packet:" << packet.toHex(); QUdpSocket udpSocket; diff --git a/wakeonlan/integrationpluginwakeonlan.h b/wakeonlan/integrationpluginwakeonlan.h index fb63302c..8116612a 100644 --- a/wakeonlan/integrationpluginwakeonlan.h +++ b/wakeonlan/integrationpluginwakeonlan.h @@ -45,10 +45,12 @@ class IntegrationPluginWakeOnLan : public IntegrationPlugin public: explicit IntegrationPluginWakeOnLan(); + void discoverThings(ThingDiscoveryInfo *info) override; + void executeAction(ThingActionInfo *info) override; private slots: - void wakeup(QString mac); + void wakeup(const QString &macAddress); }; diff --git a/wakeonlan/integrationpluginwakeonlan.json b/wakeonlan/integrationpluginwakeonlan.json index d79ca7ff..88ca3373 100644 --- a/wakeonlan/integrationpluginwakeonlan.json +++ b/wakeonlan/integrationpluginwakeonlan.json @@ -12,7 +12,7 @@ "id": "3c8f2447-dcd0-4882-8c09-99e579e4d24c", "name": "wol", "displayName": "Wake On Lan", - "createMethods": ["user"], + "createMethods": ["user", "discovery"], "interfaces": ["outputtrigger"], "paramTypes": [ {