]> git.basschouten.com Git - openhab-addons.git/blob
2505e7fd4fefe3a98ad693cf9c681d2a43afed75
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.kaleidescape.internal.discovery;
14
15 import static org.openhab.binding.kaleidescape.internal.KaleidescapeBindingConstants.*;
16
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;
29 import java.util.Set;
30 import java.util.concurrent.ExecutorService;
31 import java.util.concurrent.Executors;
32 import java.util.stream.Collectors;
33 import java.util.stream.Stream;
34
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;
47
48 /**
49  * The {@link KaleidescapeDiscoveryService} class allow manual discovery of Kaleidescape components.
50  *
51  * @author Chris Graham - Initial contribution
52  * @author Michael Lobstein - Adapted for the Kaleidescape binding
53  * 
54  */
55 @NonNullByDefault
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()));
62
63     @Activate
64     public KaleidescapeDiscoveryService() {
65         super(SUPPORTED_THING_TYPES_UIDS, DISCOVERY_DEFAULT_TIMEOUT_RATE_MS, DISCOVERY_DEFAULT_AUTO_DISCOVER);
66     }
67
68     @Override
69     public Set<ThingTypeUID> getSupportedThingTypes() {
70         return SUPPORTED_THING_TYPES_UIDS;
71     }
72
73     @Override
74     protected void startScan() {
75         logger.debug("Starting discovery of Kaleidescape components.");
76
77         try {
78             List<String> ipList = getIpAddressScanList();
79
80             ExecutorService discoverySearchPool = Executors.newFixedThreadPool(DISCOVERY_THREAD_POOL_SIZE,
81                     new NamedThreadFactory("OH-binding-discovery.kaleidescape", true));
82
83             for (String ip : ipList) {
84                 discoverySearchPool.execute(new KaleidescapeDiscoveryJob(this, ip));
85             }
86
87             discoverySearchPool.shutdown();
88         } catch (Exception exp) {
89             logger.debug("Kaleidescape discovery service encountered an error while scanning for components: {}",
90                     exp.getMessage());
91         }
92
93         logger.debug("Completed discovery of Kaleidescape components.");
94     }
95
96     /**
97      * Create a new Thing with an IP address and Component type given. Uses default port.
98      *
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.
103      */
104     public void submitDiscoveryResults(ThingTypeUID thingTypeUid, String ip, String friendlyName, String serialNumber) {
105         ThingUID uid = new ThingUID(thingTypeUid, serialNumber);
106
107         HashMap<String, Object> properties = new HashMap<>();
108
109         properties.put("host", ip);
110         properties.put("port", DEFAULT_API_PORT);
111
112         thingDiscovered(DiscoveryResultBuilder.create(uid).withProperties(properties).withRepresentationProperty("host")
113                 .withLabel(friendlyName).build());
114     }
115
116     /**
117      * Provide a string list of all the IP addresses associated with the network interfaces on
118      * this machine.
119      *
120      * @return String list of IP addresses.
121      * @throws UnknownHostException
122      * @throws SocketException
123      */
124     private List<String> getIpAddressScanList() throws UnknownHostException, SocketException {
125         List<String> results = new ArrayList<>();
126
127         InetAddress localHost = InetAddress.getLocalHost();
128         NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
129
130         for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {
131             InetAddress ipAddress = address.getAddress();
132
133             String cidrSubnet = ipAddress.getHostAddress() + "/" + address.getNetworkPrefixLength();
134
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);
138
139                 SubnetUtils utils = new SubnetUtils(cidrSubnet);
140
141                 results.addAll(Arrays.asList(utils.getInfo().getAllAddresses())); // not sure how to do this without the
142                                                                                   // Apache libraries
143             } else if (ipAddress instanceof Inet6Address) {
144                 logger.debug("Found interface IPv6 address to scan: {}, ignoring", cidrSubnet);
145             } else {
146                 logger.debug("Found interface unknown IP type address to scan: {}", cidrSubnet);
147             }
148         }
149
150         return results;
151     }
152 }