Add network discovery to the sunspec plugin

pull/26/head
Simon Stürz 2021-06-07 16:20:31 +02:00 committed by Michael Zanetti
parent 6b8469177f
commit d9138ff01d
3 changed files with 44 additions and 2 deletions

View File

@ -30,6 +30,7 @@
#include "plugininfo.h"
#include "integrationpluginsunspec.h"
#include "network/networkdevicediscovery.h"
#include <QHostAddress>
@ -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);

View File

@ -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;

View File

@ -39,7 +39,7 @@
"name": "sunspecConnection",
"displayName": "SunSpec connection",
"id": "f51853f3-8815-4cf3-b337-45cda1f3e6d5",
"createMethods": [ "User" ],
"createMethods": [ "User", "Discovery" ],
"interfaces": ["gateway"],
"paramTypes": [
{