2 * Copyright (c) 2010-2023 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.*;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
23 import javax.jmdns.ServiceInfo;
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;
46 * The {@link WLedDiscoveryService} Discovers and adds any Wled devices found.
48 * @author Matthew Skinner - Initial contribution
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;
57 public WLedDiscoveryService(@Reference HttpClientFactory httpClientFactory) {
58 this.httpClient = httpClientFactory.getCommonHttpClient();
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);
68 ContentResponse contentResponse = request.send();
69 if (contentResponse.getStatus() == 200) {
70 return contentResponse.getContentAsString();
72 } catch (InterruptedException e) {
73 Thread.currentThread().interrupt();
74 } catch (TimeoutException | ExecutionException e) {
76 "WLED discovery hit a TimeoutException | ExecutionException which may have blocked a device from getting discovered:{}",
83 public @Nullable DiscoveryResult createResult(ServiceInfo service) {
84 String name = service.getName().toLowerCase();
85 if (!name.contains("wled")) {
88 String address[] = service.getURLs();
89 if ((address == null) || address.length < 1) {
90 logger.debug("WLED discovered with empty IP address-{}", service);
93 String response = sendGetRequest(address[0], "/json");
94 String label = WLedHelper.getValue(response, "\"name\":\"", "\"");
95 if (label.isEmpty()) {
96 label = "WLED @ " + address[0];
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();
111 public @Nullable ThingUID getThingUID(ServiceInfo service) {
116 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
117 return SUPPORTED_THING_TYPES;
121 public String getServiceType() {
122 return "_http._tcp.local.";