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.opensprinkler.internal.discovery;
15 import static org.openhab.binding.opensprinkler.internal.OpenSprinklerBindingConstants.*;
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.Arrays;
24 import java.util.Enumeration;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.List;
29 import java.util.concurrent.ExecutorService;
30 import java.util.concurrent.Executors;
32 import org.eclipse.jdt.annotation.NonNullByDefault;
33 import org.openhab.binding.opensprinkler.internal.api.OpenSprinklerApiFactory;
34 import org.openhab.core.config.discovery.AbstractDiscoveryService;
35 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
36 import org.openhab.core.config.discovery.DiscoveryService;
37 import org.openhab.core.thing.ThingTypeUID;
38 import org.openhab.core.thing.ThingUID;
39 import org.osgi.service.component.annotations.Activate;
40 import org.osgi.service.component.annotations.Component;
41 import org.osgi.service.component.annotations.Reference;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * The {@link OpenSprinklerDiscoveryService} class allow manual discovery of
47 * OpenSprinkler devices.
49 * @author Chris Graham - Initial contribution
51 @Component(service = DiscoveryService.class, configurationPid = "discovery.opensprinkler")
53 public class OpenSprinklerDiscoveryService extends AbstractDiscoveryService {
54 private final Logger logger = LoggerFactory.getLogger(OpenSprinklerDiscoveryService.class);
55 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = new HashSet<>(
56 Arrays.asList(OPENSPRINKLER_HTTP_BRIDGE));
57 private ExecutorService discoverySearchPool = scheduler;
58 private OpenSprinklerApiFactory apiFactory;
61 public OpenSprinklerDiscoveryService(@Reference OpenSprinklerApiFactory apiFactory) {
62 super(SUPPORTED_THING_TYPES_UIDS, DISCOVERY_DEFAULT_TIMEOUT_RATE, DISCOVERY_DEFAULT_AUTO_DISCOVER);
63 this.apiFactory = apiFactory;
67 public Set<ThingTypeUID> getSupportedThingTypes() {
68 return SUPPORTED_THING_TYPES_UIDS;
71 OpenSprinklerApiFactory getApiFactory() {
72 return this.apiFactory;
76 protected void startScan() {
77 discoverySearchPool = Executors.newFixedThreadPool(DISCOVERY_THREAD_POOL_SIZE);
80 } catch (Exception exp) {
81 logger.debug("OpenSprinkler discovery service encountered an error while scanning for devices: {}",
84 logger.debug("Completed discovery of OpenSprinkler devices.");
88 * Create a new Thing with an IP address given. Uses default port and password.
90 * @param ip IP address of the OpenSprinkler device as a string.
92 public void submitDiscoveryResults(String ip) {
93 ThingUID bridgeUID = new ThingUID(OPENSPRINKLER_HTTP_BRIDGE, ip.replace('.', '_'));
94 HashMap<String, Object> properties = new HashMap<>();
95 properties.put("hostname", ip);
96 properties.put("port", 80);
97 properties.put("password", DEFAULT_ADMIN_PASSWORD);
98 properties.put("refresh", 60);
99 thingDiscovered(DiscoveryResultBuilder.create(bridgeUID).withProperties(properties)
100 .withLabel("OpenSprinkler HTTP Bridge").withRepresentationProperty("hostname").build());
101 // Now create the Device thing
103 properties.put("hostname", ip);
104 ThingUID uid = new ThingUID(OPENSPRINKLER_DEVICE, bridgeUID, ip.replace('.', '_'));
105 thingDiscovered(DiscoveryResultBuilder.create(uid).withBridge(bridgeUID).withProperties(properties)
106 .withRepresentationProperty("hostname").withLabel("OpenSprinkler Device").build());
109 private void scanSingleSubnet(InterfaceAddress hostAddress) {
110 byte[] broadcastAddress = hostAddress.getBroadcast().getAddress();
111 // Create subnet mask from length
112 int shft = 0xffffffff << (32 - hostAddress.getNetworkPrefixLength());
113 byte oct1 = (byte) (((byte) ((shft & 0xff000000) >> 24)) & 0xff);
114 byte oct2 = (byte) (((byte) ((shft & 0x00ff0000) >> 16)) & 0xff);
115 byte oct3 = (byte) (((byte) ((shft & 0x0000ff00) >> 8)) & 0xff);
116 byte oct4 = (byte) (((byte) (shft & 0x000000ff)) & 0xff);
117 byte[] subnetMask = new byte[] { oct1, oct2, oct3, oct4 };
118 // calc first IP to start scanning from on this subnet
119 byte[] startAddress = new byte[4];
120 startAddress[0] = (byte) (broadcastAddress[0] & subnetMask[0]);
121 startAddress[1] = (byte) (broadcastAddress[1] & subnetMask[1]);
122 startAddress[2] = (byte) (broadcastAddress[2] & subnetMask[2]);
123 startAddress[3] = (byte) (broadcastAddress[3] & subnetMask[3]);
124 // Loop from start of subnet to the broadcast address.
125 for (int i = ByteBuffer.wrap(startAddress).getInt(); i < ByteBuffer.wrap(broadcastAddress).getInt(); i++) {
127 InetAddress currentIP = InetAddress.getByAddress(ByteBuffer.allocate(4).putInt(i).array());
128 // Try to reach each IP with a timeout of 500ms which is enough for local network
129 if (currentIP.isReachable(500)) {
130 String host = currentIP.getHostAddress().toString();
131 logger.debug("Unknown device was found at: {}", host);
132 discoverySearchPool.execute(new OpenSprinklerDiscoveryJob(this, host));
134 } catch (IOException e) {
139 private void ipAddressScan() {
141 for (Enumeration<NetworkInterface> enumNetworks = NetworkInterface.getNetworkInterfaces(); enumNetworks
142 .hasMoreElements();) {
143 NetworkInterface networkInterface = enumNetworks.nextElement();
144 List<InterfaceAddress> list = networkInterface.getInterfaceAddresses();
145 for (InterfaceAddress hostAddress : list) {
146 InetAddress inetAddress = hostAddress.getAddress();
147 if (!inetAddress.isLoopbackAddress() && inetAddress.isSiteLocalAddress()) {
148 logger.debug("Scanning all IP address's that IP {}/{} is on", hostAddress.getAddress(),
149 hostAddress.getNetworkPrefixLength());
150 scanSingleSubnet(hostAddress);
154 } catch (SocketException ex) {