2 * Copyright (c) 2010-2020 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.senechome.internal;
15 import java.io.IOException;
16 import java.net.MalformedURLException;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.TimeoutException;
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;
33 import com.google.gson.Gson;
36 * The {@link SenecHomeApi} class configures http client and
37 * performs status requests
39 * @author Steven Schwarznau - Initial contribution
43 public class SenecHomeApi {
44 private static final String HTTP_PROTO_PREFIX = "http://";
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 = "";
51 public SenecHomeApi(HttpClient httpClient) {
52 this.httpClient = httpClient;
55 public void setHostname(String hostname) {
56 this.hostname = hostname;
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
63 * To receive new values, just modify the Json objects and add them to the thing channels
65 * @param hostname Hostname or ip address of senec battery
66 * @return Instance of SenecHomeResponse
67 * @throws MalformedURLException Configuration/URL is wrong
68 * @throws IOException Communication failed
70 public SenecHomeResponse getStatistics()
71 throws InterruptedException, TimeoutException, ExecutionException, IOException {
72 String location = HTTP_PROTO_PREFIX + hostname;
74 Request request = httpClient.newRequest(location);
75 request.header(HttpHeader.ACCEPT, MimeTypes.Type.APPLICATION_JSON.asString());
76 request.header(HttpHeader.CONTENT_TYPE, MimeTypes.Type.FORM_ENCODED.asString());
77 ContentResponse response = request.method(HttpMethod.POST)
78 .content(new StringContentProvider(gson.toJson(new SenecHomeResponse()))).send();
80 if (response.getStatus() == HttpStatus.OK_200) {
81 return gson.fromJson(response.getContentAsString(), SenecHomeResponse.class);
83 logger.trace("Got unexpected response code {}", response.getStatus());
84 throw new IOException("Got unexpected response code " + response.getStatus());