2 * Copyright (c) 2010-2023 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.shelly.internal.api;
15 import static org.openhab.binding.shelly.internal.ShellyBindingConstants.SHELLY_API_TIMEOUT_MS;
16 import static org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.*;
17 import static org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.*;
18 import static org.openhab.binding.shelly.internal.util.ShellyUtils.*;
20 import java.nio.charset.StandardCharsets;
21 import java.text.MessageFormat;
22 import java.util.Base64;
24 import java.util.concurrent.ExecutionException;
25 import java.util.concurrent.TimeUnit;
26 import java.util.concurrent.TimeoutException;
28 import javax.ws.rs.core.HttpHeaders;
30 import org.eclipse.jdt.annotation.NonNullByDefault;
31 import org.eclipse.jdt.annotation.Nullable;
32 import org.eclipse.jetty.client.HttpClient;
33 import org.eclipse.jetty.client.api.ContentResponse;
34 import org.eclipse.jetty.client.api.Request;
35 import org.eclipse.jetty.client.util.StringContentProvider;
36 import org.eclipse.jetty.http.HttpFields;
37 import org.eclipse.jetty.http.HttpHeader;
38 import org.eclipse.jetty.http.HttpMethod;
39 import org.eclipse.jetty.http.HttpStatus;
40 import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2AuthChallenge;
41 import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2AuthRsp;
42 import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2RpcBaseMessage;
43 import org.openhab.binding.shelly.internal.config.ShellyThingConfiguration;
44 import org.openhab.binding.shelly.internal.handler.ShellyThingInterface;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 import com.google.gson.Gson;
51 * {@link ShellyHttpClient} implements basic HTTP access
53 * @author Markus Michels - Initial contribution
56 public class ShellyHttpClient {
57 private final Logger logger = LoggerFactory.getLogger(ShellyHttpClient.class);
59 public static final String HTTP_HEADER_AUTH = HttpHeaders.AUTHORIZATION;
60 public static final String HTTP_AUTH_TYPE_BASIC = "Basic";
61 public static final String HTTP_AUTH_TYPE_DIGEST = "Digest";
62 public static final String CONTENT_TYPE_JSON = "application/json; charset=UTF-8";
63 public static final String CONTENT_TYPE_FORM_URLENC = "application/x-www-form-urlencoded";
65 protected final HttpClient httpClient;
66 protected ShellyThingConfiguration config = new ShellyThingConfiguration();
67 protected String thingName;
68 protected final Gson gson = new Gson();
69 protected int timeoutErrors = 0;
70 protected int timeoutsRecovered = 0;
71 private ShellyDeviceProfile profile;
73 public ShellyHttpClient(String thingName, ShellyThingInterface thing) {
74 this(thingName, thing.getThingConfig(), thing.getHttpClient());
75 this.profile = thing.getProfile();
78 public ShellyHttpClient(String thingName, ShellyThingConfiguration config, HttpClient httpClient) {
79 profile = new ShellyDeviceProfile();
80 this.thingName = thingName;
81 setConfig(thingName, config);
82 this.httpClient = httpClient;
83 this.httpClient.setConnectTimeout(SHELLY_API_TIMEOUT_MS);
86 public void initialize() throws ShellyApiException {
89 public void setConfig(String thingName, ShellyThingConfiguration config) {
90 this.thingName = thingName;
95 * Submit GET request and return response, check for invalid responses
97 * @param uri: URI (e.g. "/settings")
99 public <T> T callApi(String uri, Class<T> classOfT) throws ShellyApiException {
100 String json = httpRequest(uri);
101 return fromJson(gson, json, classOfT);
104 public <T> T postApi(String uri, String data, Class<T> classOfT) throws ShellyApiException {
105 String json = httpPost(uri, data);
106 return fromJson(gson, json, classOfT);
109 protected String httpRequest(String uri) throws ShellyApiException {
110 ShellyApiResult apiResult = new ShellyApiResult();
112 boolean timeout = false;
113 while (retries > 0) {
115 apiResult = innerRequest(HttpMethod.GET, uri, null, "");
117 logger.debug("{}: API timeout #{}/{} recovered ({})", thingName, timeoutErrors, timeoutsRecovered,
121 return apiResult.response; // successful
122 } catch (ShellyApiException e) {
123 if (e.isConnectionError()
124 || (!e.isTimeout() && !apiResult.isHttpServerError()) && !apiResult.isNotFound()
125 || profile.hasBattery || (retries == 0)) {
126 // Sensor in sleep mode or API exception for non-battery device or retry counter expired
127 throw e; // non-timeout exception
132 timeoutErrors++; // count the retries
133 logger.debug("{}: API Timeout, retry #{} ({})", thingName, timeoutErrors, e.toString());
136 throw new ShellyApiException("API Timeout or inconsistent result"); // successful
139 public String httpPost(String uri, String data) throws ShellyApiException {
140 return innerRequest(HttpMethod.POST, uri, null, data).response;
143 public String httpPost(@Nullable Shelly2AuthChallenge auth, String data) throws ShellyApiException {
144 return innerRequest(HttpMethod.POST, SHELLYRPC_ENDPOINT, auth, data).response;
147 private ShellyApiResult innerRequest(HttpMethod method, String uri, @Nullable Shelly2AuthChallenge auth,
148 String data) throws ShellyApiException {
149 Request request = null;
150 String url = "http://" + config.deviceIp + uri;
151 ShellyApiResult apiResult = new ShellyApiResult(method.toString(), url);
154 request = httpClient.newRequest(url).method(method.toString()).timeout(SHELLY_API_TIMEOUT_MS,
155 TimeUnit.MILLISECONDS);
157 if (!uri.equals(SHELLY_URL_DEVINFO) && !config.password.isEmpty()) { // not for /shelly or no password
161 // Gen 2: Digest Auth
162 String authHeader = "";
163 if (auth != null) { // only if we received an Auth challenge
164 authHeader = formatAuthResponse(uri,
165 buildAuthResponse(uri, auth, SHELLY2_AUTHDEF_USER, config.password));
167 if (!uri.equals(SHELLYRPC_ENDPOINT)) {
168 String bearer = config.userId + ":" + config.password;
169 authHeader = HTTP_AUTH_TYPE_BASIC + " " + Base64.getEncoder().encodeToString(bearer.getBytes());
172 if (!authHeader.isEmpty()) {
173 request.header(HTTP_HEADER_AUTH, authHeader);
176 fillPostData(request, data);
177 logger.trace("{}: HTTP {} for {} {}\n{}", thingName, method, url, data, request.getHeaders());
179 // Do request and get response
180 ContentResponse contentResponse = request.send();
181 apiResult = new ShellyApiResult(contentResponse);
182 apiResult.httpCode = contentResponse.getStatus();
183 String response = contentResponse.getContentAsString().replace("\t", "").replace("\r\n", "").trim();
184 logger.trace("{}: HTTP Response {}: {}\n{}", thingName, contentResponse.getStatus(), response,
185 contentResponse.getHeaders());
187 if (response.contains("\"error\":{")) { // Gen2
188 Shelly2RpcBaseMessage message = gson.fromJson(response, Shelly2RpcBaseMessage.class);
189 if (message != null && message.error != null) {
190 apiResult.httpCode = message.error.code;
191 apiResult.response = message.error.message;
192 if (getInteger(message.error.code) == HttpStatus.UNAUTHORIZED_401) {
193 apiResult.authChallenge = getString(message.error.message).replaceAll("\\\"", "\"");
197 HttpFields headers = contentResponse.getHeaders();
198 String authChallenge = headers.get(HttpHeader.WWW_AUTHENTICATE);
199 if (!getString(authChallenge).isEmpty()) {
200 apiResult.authChallenge = authChallenge;
203 // validate response, API errors are reported as Json
204 if (apiResult.httpCode != HttpStatus.OK_200) {
205 throw new ShellyApiException(apiResult);
208 if (response.isEmpty() || !response.startsWith("{") && !response.startsWith("[") && !url.contains("/debug/")
209 && !url.contains("/sta_cache_reset")) {
210 throw new ShellyApiException("Unexpected response: " + response);
212 } catch (ExecutionException | InterruptedException | TimeoutException | IllegalArgumentException e) {
213 ShellyApiException ex = new ShellyApiException(apiResult, e);
214 if (!ex.isConnectionError() && !ex.isTimeout()) { // will be handled by the caller
215 logger.trace("{}: API call returned exception", thingName, ex);
222 protected @Nullable Shelly2AuthRsp buildAuthResponse(String uri, @Nullable Shelly2AuthChallenge challenge,
223 String user, String password) throws ShellyApiException {
224 if (challenge == null) {
225 return null; // not required
227 if (!SHELLY2_AUTHTTYPE_DIGEST.equalsIgnoreCase(challenge.authType)
228 || !SHELLY2_AUTHALG_SHA256.equalsIgnoreCase(challenge.algorithm)) {
229 throw new IllegalArgumentException("Unsupported Auth type/algorithm requested by device");
231 Shelly2AuthRsp response = new Shelly2AuthRsp();
232 response.username = user;
233 response.realm = challenge.realm;
234 response.nonce = challenge.nonce;
235 response.cnonce = Long.toHexString((long) Math.floor(Math.random() * 10e8));
236 response.nc = "00000001";
237 response.authType = challenge.authType;
238 response.algorithm = challenge.algorithm;
239 String ha1 = sha256(response.username + ":" + response.realm + ":" + password);
240 String ha2 = sha256(HttpMethod.POST + ":" + uri);// SHELLY2_AUTH_NOISE;
241 response.response = sha256(
242 ha1 + ":" + response.nonce + ":" + response.nc + ":" + response.cnonce + ":" + "auth" + ":" + ha2);
246 protected String formatAuthResponse(String uri, @Nullable Shelly2AuthRsp rsp) {
247 return rsp != null ? MessageFormat.format(HTTP_AUTH_TYPE_DIGEST
248 + " username=\"{0}\", realm=\"{1}\", uri=\"{2}\", nonce=\"{3}\", cnonce=\"{4}\", nc=\"{5}\", qop=\"auth\",response=\"{6}\", algorithm=\"{7}\", ",
249 rsp.username, rsp.realm, uri, rsp.nonce, rsp.cnonce, rsp.nc, rsp.response, rsp.algorithm) : "";
253 * Fill in POST data, set http headers
255 * @param request HTTP request structure
256 * @param data POST data, might be empty
258 private void fillPostData(Request request, String data) {
259 boolean json = data.startsWith("{") || data.contains("\": {");
260 String type = json ? CONTENT_TYPE_JSON : CONTENT_TYPE_FORM_URLENC;
261 request.header(HttpHeader.CONTENT_TYPE, type);
262 if (!data.isEmpty()) {
263 StringContentProvider postData;
264 postData = new StringContentProvider(type, data, StandardCharsets.UTF_8);
265 request.content(postData);
266 // request.header(HttpHeader.CONTENT_LENGTH, Long.toString(postData.getLength()));
271 * Format POST body depending on content type (JSON or form encoded)
273 * @param dataMap Field list
274 * @param json true=JSON format, false=form encoded
275 * @return formatted body
277 public static String buildPostData(Map<String, String> dataMap, boolean json) {
279 for (Map.Entry<String, String> e : dataMap.entrySet()) {
280 data = data + (data.isEmpty() ? "" : json ? ", " : "&");
282 data = data + e.getKey() + "=" + e.getValue();
284 data = data + "\"" + e.getKey() + "\" : \"" + e.getValue() + "\"";
287 return json ? "{ " + data + " }" : data;
290 public String getControlUriPrefix(Integer id) {
292 if (profile.isLight || profile.isDimmer) {
293 if (profile.isDuo || profile.isDimmer) {
295 uri = SHELLY_URL_CONTROL_LIGHT;
298 uri = "/" + (profile.inColor ? SHELLY_MODE_COLOR : SHELLY_MODE_WHITE);
302 uri = SHELLY_URL_CONTROL_RELEAY;
304 uri = uri + "/" + id;
308 public int getTimeoutErrors() {
309 return timeoutErrors;
312 public int getTimeoutsRecovered() {
313 return timeoutsRecovered;
316 public void postEvent(String device, String index, String event, Map<String, String> parms)
317 throws ShellyApiException {