]> git.basschouten.com Git - openhab-addons.git/blob
c285337a8dfdbe46e54dcfb6a515c4f589a278ca
[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.wled.internal;
14
15 import static org.openhab.binding.wled.internal.WLedBindingConstants.*;
16
17 import java.util.Map;
18 import java.util.Set;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
22
23 import javax.jmdns.ServiceInfo;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.eclipse.jetty.client.HttpClient;
28 import org.eclipse.jetty.client.api.ContentResponse;
29 import org.eclipse.jetty.client.api.Request;
30 import org.eclipse.jetty.http.HttpHeader;
31 import org.eclipse.jetty.http.HttpMethod;
32 import org.openhab.core.config.discovery.DiscoveryResult;
33 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
34 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
35 import org.openhab.core.io.net.http.HttpClientFactory;
36 import org.openhab.core.thing.Thing;
37 import org.openhab.core.thing.ThingTypeUID;
38 import org.openhab.core.thing.ThingUID;
39 import org.osgi.service.component.annotations.Activate;
40 import org.osgi.service.component.annotations.Component;
41 import org.osgi.service.component.annotations.Reference;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * The {@link WLedDiscoveryService} Discovers and adds any Wled devices found.
47  *
48  * @author Matthew Skinner - Initial contribution
49  */
50 @NonNullByDefault
51 @Component(service = MDNSDiscoveryParticipant.class)
52 public class WLedDiscoveryService implements MDNSDiscoveryParticipant {
53     private final Logger logger = LoggerFactory.getLogger(WLedDiscoveryService.class);
54     private final HttpClient httpClient;
55
56     @Activate
57     public WLedDiscoveryService(@Reference HttpClientFactory httpClientFactory) {
58         this.httpClient = httpClientFactory.getCommonHttpClient();
59     }
60
61     private String sendGetRequest(String address, String url) {
62         Request request = httpClient.newRequest(address + url);
63         request.timeout(3, TimeUnit.SECONDS);
64         request.method(HttpMethod.GET);
65         request.header(HttpHeader.ACCEPT_ENCODING, "gzip");
66         logger.trace("Sending WLED GET:{}", url);
67         try {
68             ContentResponse contentResponse = request.send();
69             if (contentResponse.getStatus() == 200) {
70                 return contentResponse.getContentAsString();
71             }
72         } catch (InterruptedException e) {
73             Thread.currentThread().interrupt();
74         } catch (TimeoutException | ExecutionException e) {
75             logger.debug(
76                     "WLED discovery hit a TimeoutException | ExecutionException which may have blocked a device from getting discovered:{}",
77                     e.getMessage());
78         }
79         return "";
80     }
81
82     @Override
83     public @Nullable DiscoveryResult createResult(ServiceInfo service) {
84         String name = service.getName().toLowerCase();
85         if (!name.contains("wled")) {
86             return null;
87         }
88         String[] address = service.getURLs();
89         if ((address == null) || address.length < 1) {
90             logger.debug("WLED discovered with empty IP address-{}", service);
91             return null;
92         }
93         String response = sendGetRequest(address[0], "/json");
94         String label = WLedHelper.getValue(response, "\"name\":\"", "\"");
95         if (label.isEmpty()) {
96             label = "WLED @ " + address[0];
97         }
98         String macAddress = WLedHelper.getValue(response, "\"mac\":\"", "\"");
99         if (!macAddress.isBlank()) {
100             String firmware = WLedHelper.getValue(response, "\"ver\":\"", "\"");
101             ThingUID thingUID = new ThingUID(THING_TYPE_JSON, macAddress);
102             Map<String, Object> properties = Map.of(Thing.PROPERTY_MAC_ADDRESS, macAddress,
103                     Thing.PROPERTY_FIRMWARE_VERSION, firmware, CONFIG_ADDRESS, address[0]);
104             return DiscoveryResultBuilder.create(thingUID).withLabel(label).withProperties(properties)
105                     .withRepresentationProperty(Thing.PROPERTY_MAC_ADDRESS).build();
106         }
107         return null;
108     }
109
110     @Override
111     public @Nullable ThingUID getThingUID(ServiceInfo service) {
112         return null;
113     }
114
115     @Override
116     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
117         return SUPPORTED_THING_TYPES;
118     }
119
120     @Override
121     public String getServiceType() {
122         return "_http._tcp.local.";
123     }
124 }