]> git.basschouten.com Git - openhab-addons.git/blob
8f92743467f1609c94acb2e51c8903dea033fad8
[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     private final Logger logger = LoggerFactory.getLogger(LiquidCheckHttpClient.class);
38     private final HttpClient client;
39     private final LiquidCheckConfiguration config;
40
41     public boolean isClosed = false;
42
43     /**
44      * The Constructor of the LiquidCheckHttpClient class will set up a jetty client
45      * 
46      * @param config
47      */
48     public LiquidCheckHttpClient(LiquidCheckConfiguration config, HttpClient client) {
49         this.config = config;
50         this.client = client;
51     }
52
53     /**
54      * The pollData method will poll the data from device
55      * 
56      * @return String with the response of the request
57      * @throws InterruptedException
58      * @throws TimeoutException
59      * @throws ExecutionException
60      */
61     public String pollData() throws InterruptedException, TimeoutException, ExecutionException {
62         String uri = "http://" + config.hostname + "/infos.json";
63         Request request = client.newRequest(uri).method(HttpMethod.GET)
64                 .timeout(config.connectionTimeout, TimeUnit.SECONDS).followRedirects(false);
65         logger.debug("Polling for data");
66         ContentResponse response = request.send();
67         return response.getContentAsString();
68     }
69
70     /**
71      * The measureCommand method will start a measurement
72      * 
73      * @return String with response of the request
74      * @throws InterruptedException
75      * @throws TimeoutException
76      * @throws ExecutionException
77      */
78     public String measureCommand() throws InterruptedException, TimeoutException, ExecutionException {
79         String uri = "http://" + config.hostname + "/command";
80         Request request = client.newRequest(uri).timeout(config.connectionTimeout, TimeUnit.SECONDS);
81         request.method(HttpMethod.POST);
82         request.header(HttpHeader.CONTENT_TYPE, "applicaton/json");
83         request.content(new StringContentProvider(
84                 "{\"header\":{\"namespace\":\"Device.Control\",\"name\":\"StartMeasure\",\"messageId\":\"1\",\"payloadVersion\":\"1\"},\"payload\":null}"));
85         ContentResponse response = request.send();
86         return response.getContentAsString();
87     }
88
89     /**
90      * The isConnected method will return the state of the http client
91      * 
92      * @return
93      */
94     public boolean isConnected() {
95         String state = this.client.getState();
96         return "STARTED".equals(state);
97     }
98 }