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