2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.liquidcheck.internal.httpclient;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.TimeoutException;
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;
31 * The {@link LiquidCheckHttpClient} sets up the jetty client for the connection to the device.
33 * @author Marcel Goerentz - Initial contribution
36 public class LiquidCheckHttpClient {
38 private final Logger logger = LoggerFactory.getLogger(LiquidCheckHttpClient.class);
39 private final HttpClient client;
40 private final LiquidCheckConfiguration config;
42 public boolean isClosed = false;
45 * The Constructor of the LiquidCheckHttpClient class will set up a jetty client
49 public LiquidCheckHttpClient(LiquidCheckConfiguration config, HttpClient client) {
55 * The pollData method will poll the data from device
57 * @return String with the response of the request
58 * @throws InterruptedException
59 * @throws TimeoutException
60 * @throws ExecutionException
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();
72 * The measureCommand method will start a measurement
74 * @return String with response of the request
75 * @throws InterruptedException
76 * @throws TimeoutException
77 * @throws ExecutionException
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();
91 * The isConnected method will return the state of the http client
95 public boolean isConnected() {
96 String state = this.client.getState();
97 return "STARTED".equals(state);