2 * Copyright (c) 2010-2022 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
13 package org.openhab.binding.wled.internal;
15 import static org.openhab.binding.wled.internal.WLedBindingConstants.*;
17 import java.util.HashMap;
20 import java.util.concurrent.ExecutionException;
21 import java.util.concurrent.TimeUnit;
22 import java.util.concurrent.TimeoutException;
24 import javax.jmdns.ServiceInfo;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.eclipse.jetty.client.HttpClient;
29 import org.eclipse.jetty.client.api.ContentResponse;
30 import org.eclipse.jetty.client.api.Request;
31 import org.eclipse.jetty.http.HttpHeader;
32 import org.eclipse.jetty.http.HttpMethod;
33 import org.openhab.core.config.discovery.DiscoveryResult;
34 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
35 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
36 import org.openhab.core.io.net.http.HttpClientFactory;
37 import org.openhab.core.thing.Thing;
38 import org.openhab.core.thing.ThingTypeUID;
39 import org.openhab.core.thing.ThingUID;
40 import org.osgi.service.component.annotations.Activate;
41 import org.osgi.service.component.annotations.Component;
42 import org.osgi.service.component.annotations.Reference;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * The {@link WLedDiscoveryService} Discovers and adds any Wled devices found.
49 * @author Matthew Skinner - Initial contribution
52 @Component(service = MDNSDiscoveryParticipant.class)
53 public class WLedDiscoveryService implements MDNSDiscoveryParticipant {
54 private final Logger logger = LoggerFactory.getLogger(WLedDiscoveryService.class);
55 private final HttpClient httpClient;
58 public WLedDiscoveryService(@Reference HttpClientFactory httpClientFactory) {
59 this.httpClient = httpClientFactory.getCommonHttpClient();
62 private String sendGetRequest(String address, String url) {
63 Request request = httpClient.newRequest(address + url);
64 request.timeout(3, TimeUnit.SECONDS);
65 request.method(HttpMethod.GET);
66 request.header(HttpHeader.ACCEPT_ENCODING, "gzip");
67 logger.trace("Sending WLED GET:{}", url);
69 ContentResponse contentResponse = request.send();
70 if (contentResponse.getStatus() == 200) {
71 return contentResponse.getContentAsString();
73 } catch (InterruptedException e) {
74 Thread.currentThread().interrupt();
75 } catch (TimeoutException | ExecutionException e) {
77 "WLED discovery hit a TimeoutException | ExecutionException which may have blocked a device from getting discovered:{}",
84 public @Nullable DiscoveryResult createResult(ServiceInfo service) {
85 String name = service.getName().toLowerCase();
86 if (!name.contains("wled")) {
89 String address[] = service.getURLs();
90 if ((address == null) || address.length < 1) {
91 logger.debug("WLED discovered with empty IP address-{}", service);
94 String response = sendGetRequest(address[0], "/json");
95 // LinkedList<String> segmentIndexList = WLedHelper.listOfResults(response, "{\"id\":", ",");
96 // How to create multiple things from the returned list of segments?
97 String label = WLedHelper.getValue(response, "\"name\":\"", "\"");
98 if (label.isEmpty()) {
99 label = "WLED @ " + address[0];
101 String macAddress = WLedHelper.getValue(response, "\"mac\":\"", "\"");
102 String firmware = WLedHelper.getValue(response, "\"ver\":\"", "\"");
103 ThingTypeUID thingtypeuid = new ThingTypeUID("wled", "wled");
104 ThingUID thingUID = new ThingUID(thingtypeuid, macAddress);
105 Map<String, Object> properties = new HashMap<>();
106 properties.put(Thing.PROPERTY_MAC_ADDRESS, macAddress);
107 properties.put(Thing.PROPERTY_FIRMWARE_VERSION, firmware);
108 return DiscoveryResultBuilder.create(thingUID).withProperty(CONFIG_ADDRESS, address[0])
109 .withProperty(CONFIG_SEGMENT_INDEX, 0).withLabel(label).withProperties(properties)
110 .withRepresentationProperty(Thing.PROPERTY_MAC_ADDRESS).build();
114 public @Nullable ThingUID getThingUID(ServiceInfo service) {
119 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
120 return SUPPORTED_THING_TYPES;
124 public String getServiceType() {
125 return "_http._tcp.local.";