]> git.basschouten.com Git - openhab-addons.git/blob
6defbf71a93d82d7332d09a0e138beb613cf77bd
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.hdpowerview.internal.discovery;
14
15 import static org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants.*;
16
17 import java.net.UnknownHostException;
18 import java.util.Collections;
19 import java.util.concurrent.ScheduledFuture;
20 import java.util.concurrent.TimeUnit;
21
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;
33
34 import jcifs.netbios.NbtAddress;
35
36 /**
37  * Discovers HD PowerView hubs by means of NetBios
38  *
39  * @author Andy Lintner - Initial contribution
40  */
41 @NonNullByDefault
42 @Component(service = DiscoveryService.class, configurationPid = "discovery.hdpowerview")
43 public class HDPowerViewHubDiscoveryService extends AbstractDiscoveryService {
44
45     private final Logger logger = LoggerFactory.getLogger(HDPowerViewHubDiscoveryService.class);
46
47     private final Runnable scanner;
48     private @Nullable ScheduledFuture<?> backgroundFuture;
49
50     public HDPowerViewHubDiscoveryService() {
51         super(Collections.singleton(THING_TYPE_HUB), 60, true);
52         scanner = createScanner();
53     }
54
55     @Override
56     protected void startScan() {
57         scheduler.execute(scanner);
58     }
59
60     @Override
61     protected void startBackgroundDiscovery() {
62         ScheduledFuture<?> backgroundFuture = this.backgroundFuture;
63         if (backgroundFuture != null && !backgroundFuture.isDone()) {
64             backgroundFuture.cancel(true);
65         }
66         this.backgroundFuture = scheduler.scheduleWithFixedDelay(scanner, 0, 60, TimeUnit.SECONDS);
67     }
68
69     @Override
70     protected void stopBackgroundDiscovery() {
71         ScheduledFuture<?> backgroundFuture = this.backgroundFuture;
72         if (backgroundFuture != null && !backgroundFuture.isDone()) {
73             backgroundFuture.cancel(true);
74             this.backgroundFuture = null;
75         }
76         super.stopBackgroundDiscovery();
77     }
78
79     private Runnable createScanner() {
80         return () -> {
81             for (String netBiosName : NETBIOS_NAMES) {
82                 try {
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);
92                         thingDiscovered(hub);
93                     }
94                 } catch (UnknownHostException e) {
95                     // Nothing to do here - the host couldn't be found, likely because it doesn't
96                     // exist
97                 }
98             }
99         };
100     }
101 }