]> git.basschouten.com Git - openhab-addons.git/blob
da5f773d5b41892d35607ef21f542cc661e1b705
[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.senechome.internal;
14
15 import java.io.IOException;
16 import java.net.MalformedURLException;
17 import java.util.Objects;
18 import java.util.concurrent.ExecutionException;
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.json.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 import com.google.gson.stream.MalformedJsonException;
37
38 /**
39  * The {@link SenecHomeApi} class configures http client and
40  * performs status requests
41  *
42  * @author Steven Schwarznau - Initial contribution
43  *
44  */
45 @NonNullByDefault
46 public class SenecHomeApi {
47     private static final String HTTP_PROTO_PREFIX = "http://";
48
49     private final Logger logger = LoggerFactory.getLogger(SenecHomeApi.class);
50     private final HttpClient httpClient;
51     private final Gson gson = new Gson();
52     private String hostname = "";
53
54     public SenecHomeApi(HttpClient httpClient) {
55         this.httpClient = httpClient;
56     }
57
58     public void setHostname(String hostname) {
59         this.hostname = hostname;
60     }
61
62     /**
63      * POST json with empty, but expected fields, to lala.cgi of Senec webinterface
64      * the response will contain the same fields, but with the corresponding values
65      *
66      * To receive new values, just modify the Json objects and add them to the thing channels
67      *
68      * @return Instance of SenecHomeResponse
69      * @throws MalformedURLException Configuration/URL is wrong
70      * @throws IOException Communication failed
71      */
72     public SenecHomeResponse getStatistics()
73             throws InterruptedException, TimeoutException, ExecutionException, IOException {
74         String location = HTTP_PROTO_PREFIX + hostname;
75
76         Request request = httpClient.newRequest(location);
77         request.header(HttpHeader.ACCEPT, MimeTypes.Type.APPLICATION_JSON.asString());
78         request.header(HttpHeader.CONTENT_TYPE, MimeTypes.Type.FORM_ENCODED.asString());
79         ContentResponse response = null;
80         try {
81             response = request.method(HttpMethod.POST)
82                     .content(new StringContentProvider(gson.toJson(new SenecHomeResponse()))).send();
83             if (response.getStatus() == HttpStatus.OK_200) {
84                 return Objects.requireNonNull(gson.fromJson(response.getContentAsString(), SenecHomeResponse.class));
85             } else {
86                 logger.trace("Got unexpected response code {}", response.getStatus());
87                 throw new IOException("Got unexpected response code " + response.getStatus());
88             }
89         } catch (MalformedJsonException | JsonSyntaxException | InterruptedException | TimeoutException
90                 | ExecutionException e) {
91             String errorMessage = "\nlocation: " + location;
92             errorMessage += "\nrequest: " + request.toString();
93             errorMessage += "\nrequest.getHeaders: " + request.getHeaders();
94             if (response == null) {
95                 errorMessage += "\nresponse: null";
96             } else {
97                 errorMessage += "\nresponse: " + response.toString();
98                 errorMessage += "\nresponse.getHeaders: " + response.getHeaders();
99                 if (response.getContent() == null) {
100                     errorMessage += "\nresponse.getContent is null";
101                 } else {
102                     errorMessage += "\nresponse.getContentAsString: " + response.getContentAsString();
103                 }
104             }
105             logger.trace("Issue with getting SenecHomeResponse\n{}", errorMessage);
106             throw e;
107         }
108     }
109 }