2 * Copyright (c) 2010-2020 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.*;
16 import static org.openhab.binding.opensprinkler.internal.api.OpenSprinklerApiConstants.*;
18 import java.net.Inet4Address;
19 import java.net.Inet6Address;
20 import java.net.InetAddress;
21 import java.net.InterfaceAddress;
22 import java.net.NetworkInterface;
23 import java.net.SocketException;
24 import java.net.UnknownHostException;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.List;
31 import java.util.concurrent.ExecutorService;
32 import java.util.concurrent.Executors;
34 import org.apache.commons.net.util.SubnetUtils;
35 import org.openhab.binding.opensprinkler.internal.api.OpenSprinklerApiFactory;
36 import org.openhab.core.config.discovery.AbstractDiscoveryService;
37 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
38 import org.openhab.core.config.discovery.DiscoveryService;
39 import org.openhab.core.thing.ThingTypeUID;
40 import org.openhab.core.thing.ThingUID;
41 import org.osgi.service.component.annotations.Activate;
42 import org.osgi.service.component.annotations.Component;
43 import org.osgi.service.component.annotations.Reference;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * The {@link OpenSprinklerDiscoveryService} class allow manual discovery of
49 * OpenSprinkler devices.
51 * @author Chris Graham - Initial contribution
53 @Component(service = DiscoveryService.class, configurationPid = "discovery.opensprinkler")
54 public class OpenSprinklerDiscoveryService extends AbstractDiscoveryService {
55 private final Logger logger = LoggerFactory.getLogger(OpenSprinklerDiscoveryService.class);
56 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = new HashSet<>(
57 Arrays.asList(OPENSPRINKLER_HTTP_BRIDGE));
59 private ExecutorService discoverySearchPool;
60 private OpenSprinklerApiFactory apiFactory;
63 public OpenSprinklerDiscoveryService(@Reference OpenSprinklerApiFactory apiFactory) {
64 super(SUPPORTED_THING_TYPES_UIDS, DISCOVERY_DEFAULT_TIMEOUT_RATE, DISCOVERY_DEFAULT_AUTO_DISCOVER);
65 this.apiFactory = apiFactory;
69 public Set<ThingTypeUID> getSupportedThingTypes() {
70 return SUPPORTED_THING_TYPES_UIDS;
73 OpenSprinklerApiFactory getApiFactory() {
74 return this.apiFactory;
78 protected void startScan() {
79 logger.debug("Starting discovery of OpenSprinkler devices.");
82 List<String> ipList = getIpAddressScanList();
84 discoverySearchPool = Executors.newFixedThreadPool(DISCOVERY_THREAD_POOL_SIZE);
86 for (String ip : ipList) {
87 discoverySearchPool.execute(new OpenSprinklerDiscoveryJob(this, ip));
90 discoverySearchPool.shutdown();
91 } catch (Exception exp) {
92 logger.debug("OpenSprinkler discovery service encountered an error while scanning for devices: {}",
96 logger.debug("Completed discovery of OpenSprinkler devices.");
100 * Create a new Thing with an IP address given. Uses default port and password.
102 * @param ip IP address of the OpenSprinkler device as a string.
104 public void submitDiscoveryResults(String ip) {
105 ThingUID uid = new ThingUID(OPENSPRINKLER_HTTP_BRIDGE, ip.replace('.', '_'));
107 HashMap<String, Object> properties = new HashMap<>();
109 properties.put("hostname", ip);
110 properties.put("port", DEFAULT_API_PORT);
111 properties.put("password", DEFAULT_ADMIN_PASSWORD);
112 properties.put("refresh", DEFAULT_REFRESH_RATE);
115 DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel("OpenSprinkler").build());
119 * Provide a string list of all the IP addresses associated with the network interfaces on
122 * @return String list of IP addresses.
123 * @throws UnknownHostException
124 * @throws SocketException
126 private List<String> getIpAddressScanList() throws UnknownHostException, SocketException {
127 List<String> results = new ArrayList<>();
129 InetAddress localHost = InetAddress.getLocalHost();
130 NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
132 for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {
133 InetAddress ipAddress = address.getAddress();
135 String cidrSubnet = ipAddress.getHostAddress() + "/" + address.getNetworkPrefixLength();
137 /* Apache Subnet Utils only supports IP v4 for creating string list of IP's */
138 if (ipAddress instanceof Inet4Address) {
139 logger.debug("Found interface IPv4 address to scan: {}", cidrSubnet);
141 SubnetUtils utils = new SubnetUtils(cidrSubnet);
143 results.addAll(Arrays.asList(utils.getInfo().getAllAddresses()));
144 } else if (ipAddress instanceof Inet6Address) {
145 logger.debug("Found interface IPv6 address to scan: {}", cidrSubnet);
147 logger.debug("Found interface unknown IP type address to scan: {}", cidrSubnet);