]> git.basschouten.com Git - openhab-addons.git/blob
77bd446706d7e828cb79818d33aa238b37d2b83a
[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.intesis.internal.api;
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.jdt.annotation.Nullable;
21 import org.eclipse.jetty.client.HttpClient;
22 import org.eclipse.jetty.client.api.ContentResponse;
23 import org.eclipse.jetty.client.api.Request;
24 import org.eclipse.jetty.client.util.StringContentProvider;
25 import org.eclipse.jetty.http.HttpHeader;
26 import org.openhab.binding.intesis.internal.config.IntesisHomeConfiguration;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * {@link IntesisHomeHttpApi} wraps the IntesisHome REST API and provides various low level function to access the
32  * device api (not cloud api).
33  *
34  * @author Hans-Jörg Merk - Initial contribution
35  */
36 @NonNullByDefault
37 public class IntesisHomeHttpApi {
38     public static final String CONTENT_TYPE_JSON = "application/json; charset=UTF-8";
39
40     private final Logger logger = LoggerFactory.getLogger(IntesisHomeHttpApi.class);
41     private final HttpClient httpClient;
42
43     public IntesisHomeHttpApi(IntesisHomeConfiguration config, HttpClient httpClient) {
44         this.httpClient = httpClient;
45     }
46
47     /**
48      * Used to post a request to the device
49      *
50      * @param ipAddress of the device
51      * @param contentString string
52      * @return JSON string as response
53      */
54     @Nullable
55     public String postRequest(String ipAddress, String contentString) {
56         String url = "http://" + ipAddress + "/api.cgi";
57         try {
58             Request request = httpClient.POST(url);
59             request.header(HttpHeader.CONTENT_TYPE, "application/json");
60             request.content(new StringContentProvider(contentString), "application/json");
61
62             ContentResponse contentResponse = request.timeout(5, TimeUnit.SECONDS).send();
63
64             String response = contentResponse.getContentAsString().replace("\t", "").replace("\r\n", "").trim();
65
66             if (!response.isEmpty()) {
67                 return response;
68             } else {
69                 return null;
70             }
71         } catch (TimeoutException | InterruptedException | ExecutionException e) {
72             logger.debug("Could not make HTTP Post request");
73         }
74         return null;
75     }
76 }