2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
14 package org.openhab.binding.wled.internal;
16 import static org.openhab.binding.wled.internal.WLedBindingConstants.*;
18 import java.util.HashMap;
21 import java.util.concurrent.ExecutionException;
22 import java.util.concurrent.TimeUnit;
23 import java.util.concurrent.TimeoutException;
25 import javax.jmdns.ServiceInfo;
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.eclipse.jetty.client.HttpClient;
30 import org.eclipse.jetty.client.api.ContentResponse;
31 import org.eclipse.jetty.client.api.Request;
32 import org.eclipse.jetty.http.HttpHeader;
33 import org.eclipse.jetty.http.HttpMethod;
34 import org.openhab.core.config.discovery.DiscoveryResult;
35 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
36 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
37 import org.openhab.core.io.net.http.HttpClientFactory;
38 import org.openhab.core.thing.Thing;
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;
48 * The {@link WLedDiscoveryService} Discovers and adds any Wled devices found.
50 * @author Matthew Skinner - Initial contribution
53 @Component(service = MDNSDiscoveryParticipant.class)
54 public class WLedDiscoveryService implements MDNSDiscoveryParticipant {
55 private final Logger logger = LoggerFactory.getLogger(WLedDiscoveryService.class);
56 private final HttpClient httpClient;
59 public WLedDiscoveryService(@Reference HttpClientFactory httpClientFactory) {
60 this.httpClient = httpClientFactory.getCommonHttpClient();
63 private String sendGetRequest(String address, String url) {
64 Request request = httpClient.newRequest(address + url);
65 request.timeout(3, TimeUnit.SECONDS);
66 request.method(HttpMethod.GET);
67 request.header(HttpHeader.ACCEPT_ENCODING, "gzip");
68 logger.trace("Sending WLED GET:{}", url);
70 ContentResponse contentResponse = request.send();
71 if (contentResponse.getStatus() == 200) {
72 return contentResponse.getContentAsString();
74 } catch (InterruptedException e) {
75 Thread.currentThread().interrupt();
76 } catch (TimeoutException | ExecutionException e) {
78 "WLED discovery hit a TimeoutException | ExecutionException which may have blocked a device from getting discovered:{}",
85 public @Nullable DiscoveryResult createResult(ServiceInfo service) {
86 String name = service.getName().toLowerCase();
87 if (!name.contains("wled")) {
90 String address[] = service.getURLs();
91 if ((address == null) || address.length < 1) {
92 logger.debug("WLED discovered with empty IP address-{}", service);
95 String response = sendGetRequest(address[0], "/json");
96 // LinkedList<String> segmentIndexList = WLedHelper.listOfResults(response, "{\"id\":", ",");
97 // How to create multiple things from the returned list of segments?
98 String label = WLedHelper.getValue(response, "\"name\":\"", "\"");
99 if (label.isEmpty()) {
100 label = "WLED @ " + address[0];
102 String macAddress = WLedHelper.getValue(response, "\"mac\":\"", "\"");
103 String firmware = WLedHelper.getValue(response, "\"ver\":\"", "\"");
104 ThingTypeUID thingtypeuid = new ThingTypeUID("wled", "wled");
105 ThingUID thingUID = new ThingUID(thingtypeuid, macAddress);
106 Map<String, Object> properties = new HashMap<>();
107 properties.put(Thing.PROPERTY_MAC_ADDRESS, macAddress);
108 properties.put(Thing.PROPERTY_FIRMWARE_VERSION, firmware);
109 return DiscoveryResultBuilder.create(thingUID).withProperty(CONFIG_ADDRESS, address[0])
110 .withProperty(CONFIG_SEGMENT_INDEX, -1).withLabel(label).withProperties(properties)
111 .withRepresentationProperty(Thing.PROPERTY_MAC_ADDRESS).build();
115 public @Nullable ThingUID getThingUID(ServiceInfo service) {
120 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
121 return SUPPORTED_THING_TYPES;
125 public String getServiceType() {
126 return "_http._tcp.local.";