diff --git a/sunspec/integrationpluginsunspec.cpp b/sunspec/integrationpluginsunspec.cpp index c6a95ed..e353d12 100644 --- a/sunspec/integrationpluginsunspec.cpp +++ b/sunspec/integrationpluginsunspec.cpp @@ -30,6 +30,7 @@ #include "plugininfo.h" #include "integrationpluginsunspec.h" +#include "network/networkdevicediscovery.h" #include @@ -98,6 +99,46 @@ void IntegrationPluginSunSpec::init() m_frequencyStateTypeIds.insert(sunspecThreePhaseMeterThingClassId, sunspecThreePhaseMeterFrequencyStateTypeId); } +void IntegrationPluginSunSpec::discoverThings(ThingDiscoveryInfo *info) +{ + NetworkDeviceDiscoveryReply *discoveryReply = hardwareManager()->networkDeviceDiscovery()->discover(); + connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, this, [=](){ + ThingDescriptors descriptors; + qCDebug(dcSunSpec()) << "Discovery finished. Found" << discoveryReply->networkDevices().count() << "devices"; + foreach (const NetworkDevice &networkDevice, discoveryReply->networkDevices()) { + qCDebug(dcSunSpec()) << networkDevice; + QString title; + 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(sunspecConnectionThingClassId, title, description); + + // Check if we already have set up this device + Things existingThings = myThings().filterByParam(sunspecConnectionThingIpAddressParamTypeId, networkDevice.address().toString()); + if (existingThings.count() == 1) { + qCDebug(dcSunSpec()) << "This thing already exists in the system." << existingThings.first() << networkDevice; + descriptor.setThingId(existingThings.first()->id()); + } + + ParamList params; + params << Param(sunspecConnectionThingIpAddressParamTypeId, networkDevice.address().toString()); + descriptor.setParams(params); + info->addThingDescriptor(descriptor); + } + info->finish(Thing::ThingErrorNoError); + }); +} + void IntegrationPluginSunSpec::setupThing(ThingSetupInfo *info) { Thing *thing = info->thing(); @@ -132,7 +173,7 @@ void IntegrationPluginSunSpec::setupThing(ThingSetupInfo *info) }); connect(info, &ThingSetupInfo::aborted, sunSpec, &SunSpec::deleteLater); - connect(sunSpec, &SunSpec::destroyed, [this, thing] { + connect(sunSpec, &SunSpec::destroyed, thing, [this, thing] { m_sunSpecConnections.remove(thing->id()); }); connect(sunSpec, &SunSpec::foundSunSpecModel, this, &IntegrationPluginSunSpec::onFoundSunSpecModel); diff --git a/sunspec/integrationpluginsunspec.h b/sunspec/integrationpluginsunspec.h index bdb8252..dd9b2f5 100644 --- a/sunspec/integrationpluginsunspec.h +++ b/sunspec/integrationpluginsunspec.h @@ -52,6 +52,7 @@ class IntegrationPluginSunSpec: public IntegrationPlugin public: explicit IntegrationPluginSunSpec(); void init() override; + void discoverThings(ThingDiscoveryInfo *info) override; void setupThing(ThingSetupInfo *info) override; void postSetupThing(Thing *thing) override; void thingRemoved(Thing *thing) override; diff --git a/sunspec/integrationpluginsunspec.json b/sunspec/integrationpluginsunspec.json index df83b8b..8de1498 100644 --- a/sunspec/integrationpluginsunspec.json +++ b/sunspec/integrationpluginsunspec.json @@ -39,7 +39,7 @@ "name": "sunspecConnection", "displayName": "SunSpec connection", "id": "f51853f3-8815-4cf3-b337-45cda1f3e6d5", - "createMethods": [ "User" ], + "createMethods": [ "User", "Discovery" ], "interfaces": ["gateway"], "paramTypes": [ {