]> git.basschouten.com Git - openhab-addons.git/blob
ccb0246e7a9648a600f47295847a30a963282499
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.senechome.internal;
14
15 import java.io.IOException;
16 import java.util.Objects;
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.HttpClient;
23 import org.eclipse.jetty.client.api.ContentResponse;
24 import org.eclipse.jetty.client.api.Request;
25 import org.eclipse.jetty.client.util.StringContentProvider;
26 import org.eclipse.jetty.http.HttpHeader;
27 import org.eclipse.jetty.http.HttpMethod;
28 import org.eclipse.jetty.http.HttpStatus;
29 import org.eclipse.jetty.http.MimeTypes;
30 import org.openhab.binding.senechome.internal.dto.SenecHomeResponse;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import com.google.gson.Gson;
35 import com.google.gson.JsonSyntaxException;
36
37 /**
38  * The {@link SenecHomeApi} class configures http client and
39  * performs status requests
40  *
41  * @author Steven Schwarznau - Initial contribution
42  * @author Robert Delbrück - Update for Senec API changes
43  *
44  */
45 @NonNullByDefault
46 public class SenecHomeApi {
47     private final Logger logger = LoggerFactory.getLogger(SenecHomeApi.class);
48     private final HttpClient httpClient;
49     private final Gson gson = new Gson();
50     private String hostname = "";
51
52     public SenecHomeApi(HttpClient httpClient) {
53         this.httpClient = httpClient;
54     }
55
56     public void setHostname(String hostname) {
57         this.hostname = hostname;
58     }
59
60     /**
61      * POST json with empty, but expected fields, to lala.cgi of Senec webinterface
62      * the response will contain the same fields, but with the corresponding values
63      *
64      * To receive new values, just modify the Json objects and add them to the thing channels
65      *
66      * @return Instance of SenecHomeResponse
67      * @throws TimeoutException Communication failed (Timeout)
68      * @throws ExecutionException Communication failed
69      * @throws IOException Communication failed
70      * @throws InterruptedException Communication failed (Interrupted)
71      * @throws JsonSyntaxException Received response has an invalid json syntax
72      */
73     public SenecHomeResponse getStatistics()
74             throws TimeoutException, ExecutionException, IOException, InterruptedException, JsonSyntaxException {
75         String location = hostname + "/lala.cgi";
76         logger.trace("sending request to: {}", location);
77
78         Request request = httpClient.newRequest(location);
79         request.header(HttpHeader.ACCEPT, MimeTypes.Type.APPLICATION_JSON.asString());
80         request.header(HttpHeader.CONTENT_TYPE, MimeTypes.Type.APPLICATION_JSON.asString());
81         ContentResponse response = null;
82         try {
83             String dataToSend = gson.toJson(new SenecHomeResponse());
84             logger.trace("data to send: {}", dataToSend);
85             response = request.method(HttpMethod.POST).content(new StringContentProvider(dataToSend))
86                     .timeout(15, TimeUnit.SECONDS).send();
87             if (response.getStatus() == HttpStatus.OK_200) {
88                 String responseString = response.getContentAsString();
89                 return Objects.requireNonNull(gson.fromJson(responseString, SenecHomeResponse.class));
90             } else {
91                 logger.trace("Got unexpected response code {}", response.getStatus());
92                 throw new IOException("Got unexpected response code " + response.getStatus());
93             }
94         } catch (JsonSyntaxException | InterruptedException | TimeoutException | ExecutionException e) {
95             String errorMessage = "\nlocation: " + location;
96             errorMessage += "\nrequest: " + request.toString();
97             errorMessage += "\nrequest.getHeaders: " + request.getHeaders();
98             if (response == null) {
99                 errorMessage += "\nresponse: null";
100             } else {
101                 errorMessage += "\nresponse: " + response.toString();
102                 errorMessage += "\nresponse.getHeaders: " + response.getHeaders();
103                 if (response.getContent() == null) {
104                     errorMessage += "\nresponse.getContent is null";
105                 } else {
106                     errorMessage += "\nresponse.getContentAsString: " + response.getContentAsString();
107                 }
108             }
109             logger.trace("Issue with getting SenecHomeResponse\n{}", errorMessage);
110             throw e;
111         }
112     }
113 }