2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.hdpowerview.internal.discovery;
15 import static org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants.*;
17 import java.net.UnknownHostException;
18 import java.util.Collections;
19 import java.util.concurrent.ScheduledFuture;
20 import java.util.concurrent.TimeUnit;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.hdpowerview.internal.config.HDPowerViewHubConfiguration;
25 import org.openhab.core.config.discovery.AbstractDiscoveryService;
26 import org.openhab.core.config.discovery.DiscoveryResult;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.config.discovery.DiscoveryService;
29 import org.openhab.core.thing.ThingUID;
30 import org.osgi.service.component.annotations.Component;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import jcifs.netbios.NbtAddress;
37 * Discovers HD PowerView hubs by means of NetBios
39 * @author Andy Lintner - Initial contribution
42 @Component(service = DiscoveryService.class, configurationPid = "discovery.hdpowerview")
43 public class HDPowerViewHubDiscoveryService extends AbstractDiscoveryService {
45 private final Logger logger = LoggerFactory.getLogger(HDPowerViewHubDiscoveryService.class);
47 private final Runnable scanner;
48 private @Nullable ScheduledFuture<?> backgroundFuture;
50 public HDPowerViewHubDiscoveryService() {
51 super(Collections.singleton(THING_TYPE_HUB), 60, true);
52 scanner = createScanner();
56 protected void startScan() {
57 scheduler.execute(scanner);
61 protected void startBackgroundDiscovery() {
62 ScheduledFuture<?> backgroundFuture = this.backgroundFuture;
63 if (backgroundFuture != null && !backgroundFuture.isDone()) {
64 backgroundFuture.cancel(true);
66 this.backgroundFuture = scheduler.scheduleWithFixedDelay(scanner, 0, 60, TimeUnit.SECONDS);
70 protected void stopBackgroundDiscovery() {
71 ScheduledFuture<?> backgroundFuture = this.backgroundFuture;
72 if (backgroundFuture != null && !backgroundFuture.isDone()) {
73 backgroundFuture.cancel(true);
74 this.backgroundFuture = null;
76 super.stopBackgroundDiscovery();
79 private Runnable createScanner() {
81 for (String netBiosName : NETBIOS_NAMES) {
83 NbtAddress address = NbtAddress.getByName(netBiosName);
84 if (address != null) {
85 String host = address.getInetAddress().getHostAddress();
86 ThingUID thingUID = new ThingUID(THING_TYPE_HUB, host.replace('.', '_'));
87 DiscoveryResult hub = DiscoveryResultBuilder.create(thingUID)
88 .withProperty(HDPowerViewHubConfiguration.HOST, host)
89 .withRepresentationProperty(HDPowerViewHubConfiguration.HOST)
90 .withLabel("PowerView Hub (" + host + ")").build();
91 logger.debug("NetBios discovered hub on host '{}'", host);
94 } catch (UnknownHostException e) {
95 // Nothing to do here - the host couldn't be found, likely because it doesn't