]> git.basschouten.com Git - openhab-addons.git/blob
2d1dd78be5d43418a097d6cf5376c26222788f3f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.ipobserver.internal;
14
15 import static org.openhab.binding.ipobserver.internal.IpObserverBindingConstants.LIVE_DATA_URL;
16
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.TimeUnit;
19 import java.util.concurrent.TimeoutException;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jetty.client.api.ContentResponse;
23 import org.eclipse.jetty.client.api.Request;
24 import org.eclipse.jetty.http.HttpHeader;
25 import org.eclipse.jetty.http.HttpMethod;
26
27 /**
28  * The {@link IpObserverDiscoveryJob} class allows auto discovery of
29  * devices for a single IP address. This is used
30  * for threading to make discovery faster.
31  *
32  * @author Matthew Skinner - Initial contribution
33  */
34 @NonNullByDefault
35 public class IpObserverDiscoveryJob implements Runnable {
36     private IpObserverDiscoveryService discoveryClass;
37     private String ipAddress;
38
39     public IpObserverDiscoveryJob(IpObserverDiscoveryService service, String ip) {
40         this.discoveryClass = service;
41         this.ipAddress = ip;
42     }
43
44     @Override
45     public void run() {
46         if (isIpObserverDevice(this.ipAddress)) {
47             discoveryClass.submitDiscoveryResults(this.ipAddress);
48         }
49     }
50
51     private boolean isIpObserverDevice(String ip) {
52         Request request = discoveryClass.getHttpClient().newRequest("http://" + ip + LIVE_DATA_URL);
53         request.method(HttpMethod.GET).timeout(5, TimeUnit.SECONDS).header(HttpHeader.ACCEPT_ENCODING, "gzip");
54         ContentResponse contentResponse;
55         try {
56             contentResponse = request.send();
57             if (contentResponse.getStatus() == 200 && contentResponse.getContentAsString().contains("livedata.htm")) {
58                 return true;
59             }
60         } catch (InterruptedException | TimeoutException | ExecutionException e) {
61         }
62         return false;
63     }
64 }