Add network discovery to the modbus TCP commander

pull/26/head
Simon Stürz 2021-06-07 16:06:43 +02:00 committed by Michael Zanetti
parent acb5dbe9a2
commit 6b8469177f
2 changed files with 40 additions and 1 deletions

View File

@ -32,6 +32,7 @@
#include "plugininfo.h"
#include "hardwaremanager.h"
#include "network/networkdevicediscovery.h"
#include "hardware/modbus/modbusrtumaster.h"
#include "hardware/modbus/modbusrtuhardwareresource.h"
@ -109,6 +110,44 @@ void IntegrationPluginModbusCommander::discoverThings(ThingDiscoveryInfo *info)
info->finish(Thing::ThingErrorNoError);
return;
} else if (thingClassId == modbusTCPClientThingClassId) {
NetworkDeviceDiscoveryReply *discoveryReply = hardwareManager()->networkDeviceDiscovery()->discover();
connect(discoveryReply, &NetworkDeviceDiscoveryReply::finished, this, [=](){
ThingDescriptors descriptors;
qCDebug(dcModbusCommander()) << "Discovery finished. Found" << discoveryReply->networkDevices().count() << "devices";
foreach (const NetworkDevice &networkDevice, discoveryReply->networkDevices()) {
qCDebug(dcModbusCommander()) << 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(modbusTCPClientThingClassId, title, description);
// Check if we already have set up this device
Things existingThings = myThings().filterByParam(modbusTCPClientThingIpAddressParamTypeId, networkDevice.address().toString());
if (existingThings.count() == 1) {
qCDebug(dcModbusCommander()) << "This thing already exists in the system." << existingThings.first() << networkDevice;
descriptor.setThingId(existingThings.first()->id());
}
ParamList params;
params << Param(modbusTCPClientThingIpAddressParamTypeId, networkDevice.address().toString());
descriptor.setParams(params);
info->addThingDescriptor(descriptor);
}
info->finish(Thing::ThingErrorNoError);
});
return;
} else if (thingClassId == discreteInputThingClassId) {
Q_FOREACH(Thing *clientThing, myThings()){
if (clientThing->thingClassId() == modbusTCPClientThingClassId) {

View File

@ -22,7 +22,7 @@
"id": "35d3e7dc-1f33-4b8c-baa3-eb10b4f157a7",
"name": "modbusTCPClient",
"displayName": "Modbus TCP client",
"createMethods": ["user"],
"createMethods": ["user", "discovery"],
"interfaces": ["connectable"],
"settingsTypes": [
{