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