]> git.basschouten.com Git - openhab-addons.git/blob
9c046560a69dae594ea25e3439150c665706fc44
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.opensprinkler.internal.discovery;
14
15 import static org.openhab.binding.opensprinkler.internal.OpenSprinklerBindingConstants.*;
16 import static org.openhab.binding.opensprinkler.internal.api.OpenSprinklerApiConstants.*;
17
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;
30 import java.util.Set;
31 import java.util.concurrent.ExecutorService;
32 import java.util.concurrent.Executors;
33
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;
46
47 /**
48  * The {@link OpenSprinklerDiscoveryService} class allow manual discovery of
49  * OpenSprinkler devices.
50  *
51  * @author Chris Graham - Initial contribution
52  */
53 @Component(service = DiscoveryService.class, immediate = true, 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));
58
59     private ExecutorService discoverySearchPool;
60     private OpenSprinklerApiFactory apiFactory;
61
62     @Activate
63     public OpenSprinklerDiscoveryService(@Reference OpenSprinklerApiFactory apiFactory) {
64         super(SUPPORTED_THING_TYPES_UIDS, DISCOVERY_DEFAULT_TIMEOUT_RATE, DISCOVERY_DEFAULT_AUTO_DISCOVER);
65         this.apiFactory = apiFactory;
66     }
67
68     @Override
69     public Set<ThingTypeUID> getSupportedThingTypes() {
70         return SUPPORTED_THING_TYPES_UIDS;
71     }
72
73     OpenSprinklerApiFactory getApiFactory() {
74         return this.apiFactory;
75     }
76
77     @Override
78     protected void startScan() {
79         logger.debug("Starting discovery of OpenSprinkler devices.");
80
81         try {
82             List<String> ipList = getIpAddressScanList();
83
84             discoverySearchPool = Executors.newFixedThreadPool(DISCOVERY_THREAD_POOL_SIZE);
85
86             for (String ip : ipList) {
87                 discoverySearchPool.execute(new OpenSprinklerDiscoveryJob(this, ip));
88             }
89
90             discoverySearchPool.shutdown();
91         } catch (Exception exp) {
92             logger.debug("OpenSprinkler discovery service encountered an error while scanning for devices: {}",
93                     exp.getMessage());
94         }
95
96         logger.debug("Completed discovery of OpenSprinkler devices.");
97     }
98
99     /**
100      * Create a new Thing with an IP address given. Uses default port and password.
101      *
102      * @param ip IP address of the OpenSprinkler device as a string.
103      */
104     public void submitDiscoveryResults(String ip) {
105         ThingUID uid = new ThingUID(OPENSPRINKLER_HTTP_BRIDGE, ip.replace('.', '_'));
106
107         HashMap<String, Object> properties = new HashMap<>();
108
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);
113
114         thingDiscovered(
115                 DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel("OpenSprinkler").build());
116     }
117
118     /**
119      * Provide a string list of all the IP addresses associated with the network interfaces on
120      * this machine.
121      *
122      * @return String list of IP addresses.
123      * @throws UnknownHostException
124      * @throws SocketException
125      */
126     private List<String> getIpAddressScanList() throws UnknownHostException, SocketException {
127         List<String> results = new ArrayList<>();
128
129         InetAddress localHost = InetAddress.getLocalHost();
130         NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
131
132         for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {
133             InetAddress ipAddress = address.getAddress();
134
135             String cidrSubnet = ipAddress.getHostAddress() + "/" + address.getNetworkPrefixLength();
136
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);
140
141                 SubnetUtils utils = new SubnetUtils(cidrSubnet);
142
143                 results.addAll(Arrays.asList(utils.getInfo().getAllAddresses()));
144             } else if (ipAddress instanceof Inet6Address) {
145                 logger.debug("Found interface IPv6 address to scan: {}", cidrSubnet);
146             } else {
147                 logger.debug("Found interface unknown IP type address to scan: {}", cidrSubnet);
148             }
149         }
150
151         return results;
152     }
153 }