]> git.basschouten.com Git - openhab-addons.git/blob
7433d1a14ecc9f849385a6c51741709a291a327d
[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.ventaair.internal.discovery;
14
15 import java.io.IOException;
16 import java.net.DatagramPacket;
17 import java.net.DatagramSocket;
18 import java.net.SocketException;
19 import java.nio.charset.StandardCharsets;
20 import java.time.Duration;
21 import java.util.HashMap;
22 import java.util.Set;
23 import java.util.concurrent.ScheduledFuture;
24 import java.util.concurrent.TimeUnit;
25
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.openhab.binding.ventaair.internal.VentaAirBindingConstants;
29 import org.openhab.binding.ventaair.internal.message.dto.Header;
30 import org.openhab.binding.ventaair.internal.message.dto.Message;
31 import org.openhab.core.config.discovery.AbstractDiscoveryService;
32 import org.openhab.core.config.discovery.DiscoveryResult;
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.openhab.core.util.HexUtils;
38 import org.osgi.service.component.annotations.Component;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 import com.google.gson.Gson;
43 import com.google.gson.JsonSyntaxException;
44
45 /**
46  * Discovers Venta Air humidifier and cleaner devices by listening for UDP messages
47  *
48  * @author Stefan Triller - Initial contribution
49  *
50  */
51 @NonNullByDefault
52 @Component(service = DiscoveryService.class, configurationPid = "discovery.ventaair")
53 public class VentaDeviceDiscovery extends AbstractDiscoveryService {
54     private static final String REPRESENTATION_PROPERTY = "macAddress";
55     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set
56             .of(VentaAirBindingConstants.THING_TYPE_LW60T);
57     // defined as int, because AbstractDiscoveryService wants and int and not long as provided by Duration.getSeconds()
58     private static final int MANUAL_DISCOVERY_TIME = 30;
59     private static final Duration TIME_BETWEEN_SCANS = Duration.ofSeconds(30);
60
61     private final Logger logger = LoggerFactory.getLogger(VentaDeviceDiscovery.class);
62
63     private @Nullable ScheduledFuture<?> scanJob = null;
64
65     public VentaDeviceDiscovery() {
66         super(SUPPORTED_THING_TYPES_UIDS, MANUAL_DISCOVERY_TIME, true);
67     }
68
69     @Override
70     protected void startScan() {
71         findDevices();
72     }
73
74     @Override
75     protected void startBackgroundDiscovery() {
76         super.startBackgroundDiscovery();
77
78         ScheduledFuture<?> localScanJob = scanJob;
79         if (localScanJob != null) {
80             localScanJob.cancel(true);
81         }
82
83         scanJob = scheduler.scheduleWithFixedDelay(this::findDevices, 5, TIME_BETWEEN_SCANS.getSeconds(),
84                 TimeUnit.SECONDS);
85     }
86
87     @Override
88     protected void stopBackgroundDiscovery() {
89         super.stopBackgroundDiscovery();
90
91         ScheduledFuture<?> localScanJob = scanJob;
92         if (localScanJob != null) {
93             localScanJob.cancel(true);
94         }
95         scanJob = null;
96     }
97
98     private void findDevices() {
99         byte[] buf = new byte[512];
100         try (DatagramSocket socket = new DatagramSocket(VentaAirBindingConstants.PORT)) {
101             DatagramPacket packet = new DatagramPacket(buf, buf.length);
102             socket.receive(packet);
103
104             Message m = parseDiscoveryPaket(packet.getData());
105             if (m == null) {
106                 logger.debug("Received broken discovery packet data={}", HexUtils.bytesToHex(packet.getData(), ", "));
107                 return;
108             }
109
110             logger.debug("Found device with: IP={} Mac={} and device type={}", m.getHeader().getIpAdress(),
111                     m.getHeader().getMacAdress(), m.getHeader().getDeviceType());
112
113             ThingTypeUID thingTypeUID;
114             switch (m.getHeader().getDeviceType()) {
115                 case 4:
116                     thingTypeUID = VentaAirBindingConstants.THING_TYPE_LW60T;
117                     break;
118                 default:
119                     thingTypeUID = VentaAirBindingConstants.THING_TYPE_GENERIC;
120                     break;
121             }
122             createDiscoveryResult(thingTypeUID, m.getHeader());
123         } catch (SocketException e) {
124             logger.warn("Could not open port {} to scan for Venta devices in the network.",
125                     VentaAirBindingConstants.PORT);
126         } catch (IOException e) {
127             // swallow, since we already log the broken packet above
128         }
129     }
130
131     private @Nullable Message parseDiscoveryPaket(byte[] packet) {
132         Gson gson = new Gson();
133         Message msg = null;
134
135         String packetAsString = new String(packet, StandardCharsets.UTF_8);
136
137         String[] lines = packetAsString.split("\n");
138         if (lines.length >= 3) {
139             String input = lines[2];
140             int end = input.lastIndexOf("}"); // strip padding bytes added by the device
141             if (end > 0) {
142                 String rawJSONstring = input.substring(0, end + 1);
143                 try {
144                     msg = gson.fromJson(rawJSONstring, Message.class);
145                 } catch (JsonSyntaxException e) {
146                     logger.debug("Received invalid JSON data={}", rawJSONstring, e);
147                 }
148             }
149         }
150         return msg;
151     }
152
153     private void createDiscoveryResult(ThingTypeUID thingTypeUID, Header messageHeader) {
154         String ipAddress = messageHeader.getIpAdress();
155         String macAddress = messageHeader.getMacAdress();
156         int deviceType = messageHeader.getDeviceType();
157
158         ThingUID uid = new ThingUID(thingTypeUID, ipAddress.replace(".", "_"));
159         HashMap<String, Object> properties = new HashMap<>();
160         properties.put("ipAddress", ipAddress);
161         properties.put(REPRESENTATION_PROPERTY, macAddress);
162         properties.put("deviceType", deviceType);
163
164         String typeLabel = thingTypeUID.getId().toUpperCase();
165
166         DiscoveryResult result = DiscoveryResultBuilder.create(uid).withRepresentationProperty(REPRESENTATION_PROPERTY)
167                 .withProperties(properties).withLabel(typeLabel + " (IP=" + ipAddress + ")").build();
168
169         this.thingDiscovered(result);
170     }
171 }