Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@
import org.openhab.core.config.discovery.AbstractDiscoveryService;
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
import org.openhab.core.config.discovery.DiscoveryService;
import org.openhab.core.thing.ThingRegistry;
import org.openhab.core.thing.ThingUID;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -50,9 +53,12 @@ public class KNXnetDiscoveryService extends AbstractDiscoveryService {
private final Logger logger = LoggerFactory.getLogger(KNXnetDiscoveryService.class);

private @Nullable Future<?> scanFuture = null;
private final ThingRegistry thingRegistry;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Afair, discovery services should not have a dependency on the thing registry.

Anyhow, I do not think this is the right approach, see also my comment at #19824.


public KNXnetDiscoveryService() {
@Activate
public KNXnetDiscoveryService(@Reference ThingRegistry thingRegistry) {
super(Set.of(THING_TYPE_IP_BRIDGE), 3, true);
this.thingRegistry = thingRegistry;
}

@Override
Expand Down Expand Up @@ -84,6 +90,15 @@ protected void stopScan() {
}
}

// this filter prevents adding devices that are already registered as Things
private boolean filtered(ThingUID uid) {
if (thingRegistry.get(uid) != null) {
logger.debug("Ignoring device {} as it is already registered", uid);
return true;
}
return false;
}

private synchronized void startDiscovery() {
try {
logger.debug("Starting KNXnet/IP discovery scan");
Expand All @@ -109,16 +124,23 @@ private synchronized void startDiscovery() {
}

if (services.containsKey(ServiceFamiliesDIB.ServiceFamily.Tunneling)) {
thingDiscovered(DiscoveryResultBuilder.create(new ThingUID(THING_TYPE_IP_BRIDGE, serial))
.withLabel(response.getDevice().getName()).withProperty("serialNumber", serial)
.withProperty("type", "TUNNEL")
ThingUID uid = new ThingUID(THING_TYPE_IP_BRIDGE, serial);
if (filtered(uid)) {
continue;
}
thingDiscovered(DiscoveryResultBuilder.create(uid).withLabel(response.getDevice().getName())
.withProperty("serialNumber", serial).withProperty("type", "TUNNEL")
.withProperty("ipAddress",
"" + response.getControlEndpoint().endpoint().getAddress().getHostAddress())
.withProperty("port", "" + response.getControlEndpoint().endpoint().getPort())
.withRepresentationProperty("serialNumber").build());
}
if (services.containsKey(ServiceFamiliesDIB.ServiceFamily.Routing)) {
thingDiscovered(DiscoveryResultBuilder.create(new ThingUID(THING_TYPE_IP_BRIDGE, serial))
ThingUID uid = new ThingUID(THING_TYPE_IP_BRIDGE, serial);
if (filtered(uid)) {
continue;
}
thingDiscovered(DiscoveryResultBuilder.create(uid)
.withLabel(response.getDevice().getName() + " (router mode)")
.withProperty("serialNumber", serial + "-r").withProperty("type", "ROUTER")
.withProperty("ipAddress", "224.0.23.12")
Expand Down