]> git.basschouten.com Git - openhab-addons.git/blob
042ca1bd558bdf34dc24693837d5411875f0a7e1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.HashMap;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.concurrent.ExecutionException;
21 import java.util.concurrent.TimeUnit;
22 import java.util.concurrent.TimeoutException;
23
24 import javax.jmdns.ServiceInfo;
25
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;
45
46 /**
47  * The {@link WLedDiscoveryService} Discovers and adds any Wled devices found.
48  *
49  * @author Matthew Skinner - Initial contribution
50  */
51 @NonNullByDefault
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;
56
57     @Activate
58     public WLedDiscoveryService(@Reference HttpClientFactory httpClientFactory) {
59         this.httpClient = httpClientFactory.getCommonHttpClient();
60     }
61
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);
68         try {
69             ContentResponse contentResponse = request.send();
70             if (contentResponse.getStatus() == 200) {
71                 return contentResponse.getContentAsString();
72             }
73         } catch (InterruptedException e) {
74             Thread.currentThread().interrupt();
75         } catch (TimeoutException | ExecutionException e) {
76             logger.debug(
77                     "WLED discovery hit a TimeoutException | ExecutionException which may have blocked a device from getting discovered:{}",
78                     e.getMessage());
79         }
80         return "";
81     }
82
83     @Override
84     public @Nullable DiscoveryResult createResult(ServiceInfo service) {
85         String name = service.getName().toLowerCase();
86         if (!name.contains("wled")) {
87             return null;
88         }
89         String address[] = service.getURLs();
90         if ((address == null) || address.length < 1) {
91             logger.debug("WLED discovered with empty IP address-{}", service);
92             return null;
93         }
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];
100         }
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();
111     }
112
113     @Override
114     public @Nullable ThingUID getThingUID(ServiceInfo service) {
115         return null;
116     }
117
118     @Override
119     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
120         return SUPPORTED_THING_TYPES;
121     }
122
123     @Override
124     public String getServiceType() {
125         return "_http._tcp.local.";
126     }
127 }