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.ipobserver.internal;
15 import static org.openhab.binding.ipobserver.internal.IpObserverBindingConstants.*;
17 import java.io.IOException;
18 import java.net.InetAddress;
19 import java.net.InterfaceAddress;
20 import java.net.NetworkInterface;
21 import java.net.SocketException;
22 import java.nio.ByteBuffer;
23 import java.util.Enumeration;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.concurrent.ExecutorService;
28 import java.util.concurrent.Executors;
30 import org.eclipse.jdt.annotation.NonNullByDefault;
31 import org.eclipse.jetty.client.HttpClient;
32 import org.openhab.core.config.discovery.AbstractDiscoveryService;
33 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
34 import org.openhab.core.config.discovery.DiscoveryService;
35 import org.openhab.core.io.net.http.HttpClientFactory;
36 import org.openhab.core.thing.ThingTypeUID;
37 import org.openhab.core.thing.ThingUID;
38 import org.osgi.service.component.annotations.Activate;
39 import org.osgi.service.component.annotations.Component;
40 import org.osgi.service.component.annotations.Reference;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * The {@link IpObserverDiscoveryService} is responsible for finding ipObserver devices.
47 * @author Matthew Skinner - Initial contribution.
49 @Component(service = DiscoveryService.class, configurationPid = "discovery.ipobserver")
51 public class IpObserverDiscoveryService extends AbstractDiscoveryService {
52 private final Logger logger = LoggerFactory.getLogger(this.getClass());
53 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_WEATHER_STATION);
54 private ExecutorService discoverySearchPool = Executors.newFixedThreadPool(DISCOVERY_THREAD_POOL_SIZE);
55 private HttpClient httpClient;
58 public IpObserverDiscoveryService(@Reference HttpClientFactory httpClientFactory) {
59 super(SUPPORTED_THING_TYPES_UIDS, 240);
60 httpClient = httpClientFactory.getCommonHttpClient();
64 public Set<ThingTypeUID> getSupportedThingTypes() {
65 return SUPPORTED_THING_TYPES_UIDS;
68 protected HttpClient getHttpClient() {
72 public void submitDiscoveryResults(String ip) {
73 ThingUID thingUID = new ThingUID(THING_WEATHER_STATION, ip.replace('.', '_'));
74 HashMap<String, Object> properties = new HashMap<>();
75 properties.put("address", ip);
76 thingDiscovered(DiscoveryResultBuilder.create(thingUID).withProperties(properties).withLabel("Weather Station")
77 .withRepresentationProperty("address").build());
80 private void scanSingleSubnet(InterfaceAddress hostAddress) {
81 byte[] broadcastAddress = hostAddress.getBroadcast().getAddress();
82 // Create subnet mask from length
83 int shft = 0xffffffff << (32 - hostAddress.getNetworkPrefixLength());
84 byte oct1 = (byte) (((byte) ((shft & 0xff000000) >> 24)) & 0xff);
85 byte oct2 = (byte) (((byte) ((shft & 0x00ff0000) >> 16)) & 0xff);
86 byte oct3 = (byte) (((byte) ((shft & 0x0000ff00) >> 8)) & 0xff);
87 byte oct4 = (byte) (((byte) (shft & 0x000000ff)) & 0xff);
88 byte[] subnetMask = new byte[] { oct1, oct2, oct3, oct4 };
89 // calc first IP to start scanning from on this subnet
90 byte[] startAddress = new byte[4];
91 startAddress[0] = (byte) (broadcastAddress[0] & subnetMask[0]);
92 startAddress[1] = (byte) (broadcastAddress[1] & subnetMask[1]);
93 startAddress[2] = (byte) (broadcastAddress[2] & subnetMask[2]);
94 startAddress[3] = (byte) (broadcastAddress[3] & subnetMask[3]);
95 // Loop from start of subnet to the broadcast address.
96 for (int i = ByteBuffer.wrap(startAddress).getInt(); i < ByteBuffer.wrap(broadcastAddress).getInt(); i++) {
98 InetAddress currentIP = InetAddress.getByAddress(ByteBuffer.allocate(4).putInt(i).array());
99 // Try to reach each IP with a timeout of 500ms which is enough for local network
100 if (currentIP.isReachable(500)) {
101 String host = currentIP.getHostAddress().toString();
102 logger.debug("Unknown device was found at: {}", host);
103 discoverySearchPool.execute(new IpObserverDiscoveryJob(this, host));
105 } catch (IOException e) {
111 protected void startScan() {
112 discoverySearchPool = Executors.newFixedThreadPool(DISCOVERY_THREAD_POOL_SIZE);
115 } catch (Exception exp) {
116 logger.debug("IpObserver discovery service encountered an error while scanning for devices: {}",
122 protected void stopScan() {
123 discoverySearchPool.shutdown();
127 private void ipAddressScan() {
129 for (Enumeration<NetworkInterface> enumNetworks = NetworkInterface.getNetworkInterfaces(); enumNetworks
130 .hasMoreElements();) {
131 NetworkInterface networkInterface = enumNetworks.nextElement();
132 List<InterfaceAddress> list = networkInterface.getInterfaceAddresses();
133 for (InterfaceAddress hostAddress : list) {
134 InetAddress inetAddress = hostAddress.getAddress();
135 if (!inetAddress.isLoopbackAddress() && inetAddress.isSiteLocalAddress()) {
136 logger.debug("Scanning all IP address's that IP {}/{} is on", hostAddress.getAddress(),
137 hostAddress.getNetworkPrefixLength());
138 scanSingleSubnet(hostAddress);
142 } catch (SocketException ex) {