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