]> git.basschouten.com Git - openhab-addons.git/blob
2ffa6e2b2e9d821500708bd0b8edc4cdd8f00e67
[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.liquidcheck.internal.httpclient;
14
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.TimeoutException;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jetty.client.HttpClient;
21 import org.eclipse.jetty.client.api.ContentResponse;
22 import org.eclipse.jetty.client.api.Request;
23 import org.eclipse.jetty.client.util.StringContentProvider;
24 import org.eclipse.jetty.http.HttpHeader;
25 import org.eclipse.jetty.http.HttpMethod;
26 import org.openhab.binding.liquidcheck.internal.LiquidCheckConfiguration;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * The {@link LiquidCheckHttpClient} sets up the jetty client for the connection to the device.
32  *
33  * @author Marcel Goerentz - Initial contribution
34  */
35 @NonNullByDefault
36 public class LiquidCheckHttpClient {
37
38     private final Logger logger = LoggerFactory.getLogger(LiquidCheckHttpClient.class);
39     private final HttpClient client;
40     private final LiquidCheckConfiguration config;
41
42     public boolean isClosed = false;
43
44     /**
45      * The Constructor of the LiquidCheckHttpClient class will set up a jetty client
46      * 
47      * @param config
48      */
49     public LiquidCheckHttpClient(LiquidCheckConfiguration config, HttpClient client) {
50         this.config = config;
51         this.client = client;
52     }
53
54     /**
55      * The pollData method will poll the data from device
56      * 
57      * @return String with the response of the request
58      * @throws InterruptedException
59      * @throws TimeoutException
60      * @throws ExecutionException
61      */
62     public String pollData() throws InterruptedException, TimeoutException, ExecutionException {
63         String uri = "http://" + config.hostname + "/infos.json";
64         Request request = client.newRequest(uri).method(HttpMethod.GET)
65                 .timeout(config.connectionTimeout, TimeUnit.SECONDS).followRedirects(false);
66         logger.debug("Polling for data");
67         ContentResponse response = request.send();
68         return response.getContentAsString();
69     }
70
71     /**
72      * The measureCommand method will start a measurement
73      * 
74      * @return String with response of the request
75      * @throws InterruptedException
76      * @throws TimeoutException
77      * @throws ExecutionException
78      */
79     public String measureCommand() throws InterruptedException, TimeoutException, ExecutionException {
80         String uri = "http://" + config.hostname + "/command";
81         Request request = client.newRequest(uri);
82         request.method(HttpMethod.POST);
83         request.header(HttpHeader.CONTENT_TYPE, "applicaton/json");
84         request.content(new StringContentProvider(
85                 "{\"header\":{\"namespace\":\"Device.Control\",\"name\":\"StartMeasure\",\"messageId\":\"1\",\"payloadVersion\":\"1\"},\"payload\":null}"));
86         ContentResponse response = request.send();
87         return response.getContentAsString();
88     }
89
90     /**
91      * The isConnected method will return the state of the http client
92      * 
93      * @return
94      */
95     public boolean isConnected() {
96         String state = this.client.getState();
97         return "STARTED".equals(state);
98     }
99 }