]> git.basschouten.com Git - openhab-addons.git/blob
e13ecc1d269e20c62cd85cd947173da051222646
[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.lcn.internal.pchkdiscovery;
14
15 import java.io.IOException;
16 import java.net.DatagramPacket;
17 import java.net.InetAddress;
18 import java.net.MulticastSocket;
19 import java.net.NetworkInterface;
20 import java.net.SocketException;
21 import java.net.UnknownHostException;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.LinkedList;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28 import java.util.stream.Collectors;
29 import java.util.stream.Stream;
30
31 import org.eclipse.jdt.annotation.NonNullByDefault;
32 import org.openhab.binding.lcn.internal.LcnBindingConstants;
33 import org.openhab.binding.lcn.internal.common.LcnDefs;
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.Component;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 import com.thoughtworks.xstream.XStream;
44 import com.thoughtworks.xstream.io.xml.StaxDriver;
45
46 /**
47  * Discovers LCN-PCK gateways, such as LCN-PCHK.
48  *
49  * Scan approach:
50  * 1. Determines all local network interfaces
51  * 2. Send a multicast message on each interface to the PCHK multicast address 234.5.6.7 (not configurable by user).
52  * 3. Evaluate multicast responses of PCK gateways in the network
53  *
54  * @author Fabian Wolter - Initial Contribution
55  */
56 @NonNullByDefault
57 @Component(service = DiscoveryService.class, configurationPid = "discovery.lcn")
58 public class LcnPchkDiscoveryService extends AbstractDiscoveryService {
59     private final Logger logger = LoggerFactory.getLogger(LcnPchkDiscoveryService.class);
60     private static final String HOSTNAME = "hostname";
61     private static final String PORT = "port";
62     private static final String MAC_ADDRESS = "macAddress";
63     private static final String PCHK_DISCOVERY_MULTICAST_ADDRESS = "234.5.6.7";
64     private static final int PCHK_DISCOVERY_PORT = 4220;
65     private static final int INTERFACE_TIMEOUT_SEC = 2;
66     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
67             .unmodifiableSet(Stream.of(LcnBindingConstants.THING_TYPE_PCK_GATEWAY).collect(Collectors.toSet()));
68     private static final String DISCOVER_REQUEST = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ServicesRequest xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"servicesrequest.xsd\"><Version major=\"1\" minor=\"0\" /><Requester requestId=\"1\" type=\"openHAB\" major=\"1\" minor=\"0\">openHAB</Requester><Requests><Request xsi:type=\"EnumServices\" major=\"1\" minor=\"0\" name=\"LcnPchkBus\" /></Requests></ServicesRequest>";
69
70     public LcnPchkDiscoveryService() throws IllegalArgumentException {
71         super(SUPPORTED_THING_TYPES_UIDS, 0, false);
72     }
73
74     private List<InetAddress> getLocalAddresses() {
75         List<InetAddress> result = new LinkedList<>();
76         try {
77             for (NetworkInterface networkInterface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
78                 try {
79                     if (networkInterface.isUp() && !networkInterface.isLoopback()
80                             && !networkInterface.isPointToPoint()) {
81                         result.addAll(Collections.list(networkInterface.getInetAddresses()));
82                     }
83                 } catch (SocketException exception) {
84                     // ignore
85                 }
86             }
87         } catch (SocketException exception) {
88             return Collections.emptyList();
89         }
90         return result;
91     }
92
93     @Override
94     protected void startScan() {
95         try {
96             InetAddress multicastAddress = InetAddress.getByName(PCHK_DISCOVERY_MULTICAST_ADDRESS);
97
98             getLocalAddresses().forEach(localInterfaceAddress -> {
99                 logger.debug("Searching on {} ...", localInterfaceAddress.getHostAddress());
100                 try (MulticastSocket socket = new MulticastSocket(PCHK_DISCOVERY_PORT)) {
101                     socket.setInterface(localInterfaceAddress);
102                     socket.setReuseAddress(true);
103                     socket.setSoTimeout(INTERFACE_TIMEOUT_SEC * 1000);
104                     socket.joinGroup(multicastAddress);
105
106                     byte[] requestData = DISCOVER_REQUEST.getBytes(LcnDefs.LCN_ENCODING);
107                     DatagramPacket request = new DatagramPacket(requestData, requestData.length, multicastAddress,
108                             PCHK_DISCOVERY_PORT);
109                     socket.send(request);
110
111                     do {
112                         byte[] rxbuf = new byte[8192];
113                         DatagramPacket packet = new DatagramPacket(rxbuf, rxbuf.length);
114                         socket.receive(packet);
115
116                         InetAddress addr = packet.getAddress();
117                         String response = new String(packet.getData(), LcnDefs.LCN_ENCODING);
118
119                         if (response.contains("ServicesRequest")) {
120                             continue;
121                         }
122
123                         ServicesResponse deserialized = xmlToServiceResponse(response);
124
125                         String macAddress = deserialized.getServer().getMachineId().replace(":", "");
126                         ThingUID thingUid = new ThingUID(LcnBindingConstants.THING_TYPE_PCK_GATEWAY, macAddress);
127
128                         Map<String, Object> properties = new HashMap<>(3);
129                         properties.put(HOSTNAME, addr.getHostAddress());
130                         properties.put(PORT, deserialized.getExtServices().getExtService().getLocalPort());
131                         properties.put(MAC_ADDRESS, macAddress);
132
133                         DiscoveryResultBuilder discoveryResult = DiscoveryResultBuilder.create(thingUid)
134                                 .withProperties(properties).withRepresentationProperty(MAC_ADDRESS)
135                                 .withLabel(deserialized.getServer().getContent() + " ("
136                                         + deserialized.getServer().getMachineName() + ")");
137
138                         thingDiscovered(discoveryResult.build());
139                     } while (true); // left by SocketTimeoutException
140                 } catch (IOException e) {
141                     logger.debug("Discovery failed for {}: {}", localInterfaceAddress, e.getMessage());
142                 }
143             });
144         } catch (UnknownHostException e) {
145             logger.warn("Discovery failed: {}", e.getMessage());
146         }
147     }
148
149     ServicesResponse xmlToServiceResponse(String response) {
150         XStream xstream = new XStream(new StaxDriver());
151         XStream.setupDefaultSecurity(xstream);
152         xstream.allowTypesByWildcard(new String[] { ServicesResponse.class.getPackageName() + ".**" });
153         xstream.setClassLoader(getClass().getClassLoader());
154         xstream.autodetectAnnotations(true);
155         xstream.alias("ServicesResponse", ServicesResponse.class);
156         xstream.alias("Server", Server.class);
157         xstream.alias("Version", Server.class);
158         xstream.alias("ExtServices", ExtServices.class);
159         xstream.alias("ExtService", ExtService.class);
160
161         return (ServicesResponse) xstream.fromXML(response);
162     }
163 }