2 * Copyright (c) 2010-2021 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.kaleidescape.internal.discovery;
15 import static org.openhab.binding.kaleidescape.internal.KaleidescapeBindingConstants.*;
17 import java.net.Inet4Address;
18 import java.net.Inet6Address;
19 import java.net.InetAddress;
20 import java.net.InterfaceAddress;
21 import java.net.NetworkInterface;
22 import java.net.SocketException;
23 import java.net.UnknownHostException;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.List;
30 import java.util.concurrent.ExecutorService;
31 import java.util.concurrent.Executors;
32 import java.util.stream.Collectors;
33 import java.util.stream.Stream;
35 import org.apache.commons.net.util.SubnetUtils;
36 import org.eclipse.jdt.annotation.NonNullByDefault;
37 import org.openhab.core.common.NamedThreadFactory;
38 import org.openhab.core.config.discovery.AbstractDiscoveryService;
39 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
40 import org.openhab.core.config.discovery.DiscoveryService;
41 import org.openhab.core.thing.ThingTypeUID;
42 import org.openhab.core.thing.ThingUID;
43 import org.osgi.service.component.annotations.Activate;
44 import org.osgi.service.component.annotations.Component;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * The {@link KaleidescapeDiscoveryService} class allow manual discovery of Kaleidescape components.
51 * @author Chris Graham - Initial contribution
52 * @author Michael Lobstein - Adapted for the Kaleidescape binding
56 @Component(service = DiscoveryService.class, configurationPid = "discovery.kaleidescape")
57 public class KaleidescapeDiscoveryService extends AbstractDiscoveryService {
58 private final Logger logger = LoggerFactory.getLogger(KaleidescapeDiscoveryService.class);
59 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
60 .unmodifiableSet(Stream.of(THING_TYPE_PLAYER, THING_TYPE_CINEMA_ONE, THING_TYPE_ALTO, THING_TYPE_STRATO)
61 .collect(Collectors.toSet()));
64 public KaleidescapeDiscoveryService() {
65 super(SUPPORTED_THING_TYPES_UIDS, DISCOVERY_DEFAULT_TIMEOUT_RATE_MS, DISCOVERY_DEFAULT_AUTO_DISCOVER);
69 public Set<ThingTypeUID> getSupportedThingTypes() {
70 return SUPPORTED_THING_TYPES_UIDS;
74 protected void startScan() {
75 logger.debug("Starting discovery of Kaleidescape components.");
78 List<String> ipList = getIpAddressScanList();
80 ExecutorService discoverySearchPool = Executors.newFixedThreadPool(DISCOVERY_THREAD_POOL_SIZE,
81 new NamedThreadFactory("OH-binding-discovery.kaleidescape", true));
83 for (String ip : ipList) {
84 discoverySearchPool.execute(new KaleidescapeDiscoveryJob(this, ip));
87 discoverySearchPool.shutdown();
88 } catch (Exception exp) {
89 logger.debug("Kaleidescape discovery service encountered an error while scanning for components: {}",
93 logger.debug("Completed discovery of Kaleidescape components.");
97 * Create a new Thing with an IP address and Component type given. Uses default port.
99 * @param thingTypeUid ThingTypeUID of detected Kaleidescape component.
100 * @param ip IP address of the Kaleidescape component as a string.
101 * @param friendlyName Name of Kaleidescape component as a string.
102 * @param serialNumber Serial Number of Kaleidescape component as a string.
104 public void submitDiscoveryResults(ThingTypeUID thingTypeUid, String ip, String friendlyName, String serialNumber) {
105 ThingUID uid = new ThingUID(thingTypeUid, serialNumber);
107 HashMap<String, Object> properties = new HashMap<>();
109 properties.put("host", ip);
110 properties.put("port", DEFAULT_API_PORT);
112 thingDiscovered(DiscoveryResultBuilder.create(uid).withProperties(properties).withRepresentationProperty("host")
113 .withLabel(friendlyName).build());
117 * Provide a string list of all the IP addresses associated with the network interfaces on
120 * @return String list of IP addresses.
121 * @throws UnknownHostException
122 * @throws SocketException
124 private List<String> getIpAddressScanList() throws UnknownHostException, SocketException {
125 List<String> results = new ArrayList<>();
127 InetAddress localHost = InetAddress.getLocalHost();
128 NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
130 for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {
131 InetAddress ipAddress = address.getAddress();
133 String cidrSubnet = ipAddress.getHostAddress() + "/" + address.getNetworkPrefixLength();
135 /* Apache Subnet Utils only supports IP v4 for creating string list of IP's */
136 if (ipAddress instanceof Inet4Address) {
137 logger.debug("Found interface IPv4 address to scan: {}", cidrSubnet);
139 SubnetUtils utils = new SubnetUtils(cidrSubnet);
141 results.addAll(Arrays.asList(utils.getInfo().getAllAddresses())); // not sure how to do this without the
143 } else if (ipAddress instanceof Inet6Address) {
144 logger.debug("Found interface IPv6 address to scan: {}, ignoring", cidrSubnet);
146 logger.debug("Found interface unknown IP type address to scan: {}", cidrSubnet);