2 * Copyright (c) 2010-2024 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.boschshc.internal.devices.bridge;
15 import static org.eclipse.jetty.http.HttpMethod.GET;
17 import java.nio.charset.StandardCharsets;
18 import java.security.KeyStoreException;
19 import java.security.cert.Certificate;
20 import java.security.cert.CertificateEncodingException;
21 import java.util.Base64;
22 import java.util.HashMap;
24 import java.util.concurrent.ExecutionException;
25 import java.util.concurrent.TimeUnit;
26 import java.util.concurrent.TimeoutException;
27 import java.util.function.BiFunction;
28 import java.util.function.Predicate;
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.HttpMethod;
37 import org.eclipse.jetty.http.HttpStatus;
38 import org.eclipse.jetty.util.ssl.SslContextFactory;
39 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
40 import org.openhab.binding.boschshc.internal.serialization.GsonUtils;
41 import org.openhab.binding.boschshc.internal.services.dto.BoschSHCServiceState;
42 import org.openhab.binding.boschshc.internal.services.userstate.dto.UserStateServiceState;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 import com.google.gson.JsonSyntaxException;
49 * HTTP client using own context with private and Bosch Certs
50 * to pair and connect to the Bosch Smart Home Controller.
52 * @author Gerd Zanker - Initial contribution
55 public class BoschHttpClient extends HttpClient {
57 private final Logger logger = LoggerFactory.getLogger(getClass());
59 private final String ipAddress;
60 private final String systemPassword;
62 public BoschHttpClient(String ipAddress, String systemPassword, SslContextFactory sslContextFactory) {
63 super(sslContextFactory);
64 this.ipAddress = ipAddress;
65 this.systemPassword = systemPassword;
69 * Returns the public information URL for the Bosch SHC client addressed with the given IP address, using port 8446
70 * See https://github.com/BoschSmartHome/bosch-shc-api-docs/blob/master/postman/README.md
72 * @return URL for public information
74 public static String getPublicInformationUrl(String ipAddress) {
75 return String.format("https://%s:8446/smarthome/public/information", ipAddress);
79 * Returns the public information URL for the current Bosch SHC client.
81 * @return URL for public information
83 public String getPublicInformationUrl() {
84 return getPublicInformationUrl(this.ipAddress);
88 * Returns the pairing URL for the Bosch SHC clients, using port 8443.
89 * See https://github.com/BoschSmartHome/bosch-shc-api-docs/blob/master/postman/README.md
91 * @return URL for pairing
93 public String getPairingUrl() {
94 return String.format("https://%s:8443/smarthome/clients", this.ipAddress);
98 * Returns a Bosch SHC URL for the endpoint, using port 8444.
100 * @param endpoint an endpoint, see https://apidocs.bosch-smarthome.com/local/index.html
101 * @return Bosch SHC URL for passed endpoint
103 public String getBoschShcUrl(String endpoint) {
104 return String.format("https://%s:8444/%s", this.ipAddress, endpoint);
108 * Returns a SmartHome URL for the endpoint - shortcut of {@link #getBoschShcUrl(String)}
110 * @param endpoint an endpoint, see https://apidocs.bosch-smarthome.com/local/index.html
111 * @return SmartHome URL for passed endpoint
113 public String getBoschSmartHomeUrl(String endpoint) {
114 return this.getBoschShcUrl(String.format("smarthome/%s", endpoint));
118 * Returns a URL to get or put a service state.
123 * https://localhost:8444/smarthome/devices/hdm:ZigBee:000d6f0016d1cdae/services/AirQualityLevel/state
126 * see https://apidocs.bosch-smarthome.com/local/index.html
128 * @param serviceName the name of the service
129 * @param deviceId the device identifier
130 * @return a URL to get or put a service state
132 public String getServiceStateUrl(String serviceName, String deviceId) {
133 return this.getBoschSmartHomeUrl(String.format("devices/%s/services/%s/state", deviceId, serviceName));
136 public <T extends BoschSHCServiceState> String getServiceStateUrl(String serviceName, String deviceId,
137 Class<T> serviceClass) {
138 if (serviceClass.isAssignableFrom(UserStateServiceState.class)) {
139 return this.getBoschSmartHomeUrl(String.format("userdefinedstates/%s/state", deviceId));
141 return getServiceStateUrl(serviceName, deviceId);
146 * Returns a URL to get general information about a service.
151 * https://localhost:8444/smarthome/devices/hdm:ZigBee:000d6f0016d1cdae/services/BatteryLevel
154 * In some cases this URL has to be used to get the service state, for example for battery levels.
156 * @param serviceName the name of the service
157 * @param deviceId the device identifier
158 * @return a URL to retrieve general service information
160 public String getServiceUrl(String serviceName, String deviceId) {
161 return this.getBoschSmartHomeUrl(String.format("devices/%s/services/%s", deviceId, serviceName));
165 * Checks if the Bosch SHC is online.
167 * The HTTP server could be offline (Timeout of request).
168 * Or during boot-up the server can response e.g. with SERVICE_UNAVAILABLE_503
170 * Will return true, if the server responds with the "public information".
173 * @return true if HTTP server is online
174 * @throws InterruptedException in case of an interrupt
176 public boolean isOnline() throws InterruptedException {
178 String url = this.getPublicInformationUrl();
179 Request request = this.createRequest(url, GET);
180 ContentResponse contentResponse = request.send();
181 if (HttpStatus.getCode(contentResponse.getStatus()).isSuccess()) {
182 String content = contentResponse.getContentAsString();
183 logger.debug("Online check completed with success: {} - status code: {}", content,
184 contentResponse.getStatus());
187 logger.debug("Online check failed with status code: {}", contentResponse.getStatus());
190 } catch (InterruptedException e) {
192 } catch (Exception e) {
193 logger.debug("Online check failed because of {}!", e.getMessage(), e);
199 * Checks if the Bosch SHC can be accessed.
201 * @return true if HTTP access to SHC devices was successful
202 * @throws InterruptedException in case of an interrupt
204 public boolean isAccessPossible() throws InterruptedException {
206 String url = this.getBoschSmartHomeUrl("devices");
207 Request request = this.createRequest(url, GET);
208 ContentResponse contentResponse = request.send();
209 if (HttpStatus.getCode(contentResponse.getStatus()).isSuccess()) {
210 String content = contentResponse.getContentAsString();
211 logger.debug("Access check completed with success: {} - status code: {}", content,
212 contentResponse.getStatus());
215 logger.debug("Access check failed with status code: {}", contentResponse.getStatus());
218 } catch (InterruptedException e) {
220 } catch (Exception e) {
221 logger.debug("Access check failed because of {}!", e.getMessage(), e);
227 * Pairs this client with the Bosch SHC.
228 * Press pairing button on the Bosch Smart Home Controller!
230 * @return true if pairing was successful, otherwise false
231 * @throws InterruptedException in case of an interrupt
233 public boolean doPairing() throws InterruptedException {
234 logger.trace("Starting pairing openHAB Client with Bosch Smart Home Controller!");
235 logger.trace("Please press the Bosch Smart Home Controller button until LED starts blinking");
237 ContentResponse contentResponse;
239 String publicCert = getCertFromSslContextFactory();
240 logger.trace("Pairing with SHC {}", ipAddress);
243 Map<String, String> items = new HashMap<>();
244 items.put("@type", "client");
245 items.put("id", BoschSslUtil.getBoschShcClientId()); // Client Id contains the unique OpenHab instance Id
246 items.put("name", "oss_OpenHAB_Binding"); // Client name according to
247 // https://github.com/BoschSmartHome/bosch-shc-api-docs#terms-and-conditions
248 items.put("primaryRole", "ROLE_RESTRICTED_CLIENT");
249 items.put("certificate", "-----BEGIN CERTIFICATE-----\r" + publicCert + "\r-----END CERTIFICATE-----");
251 String url = this.getPairingUrl();
252 Request request = this.createRequest(url, HttpMethod.POST, items).header("Systempassword",
253 Base64.getEncoder().encodeToString(this.systemPassword.getBytes(StandardCharsets.UTF_8)));
255 contentResponse = request.send();
257 logger.trace("Pairing response complete: {} - return code: {}", contentResponse.getContentAsString(),
258 contentResponse.getStatus());
259 if (201 == contentResponse.getStatus()) {
260 logger.debug("Pairing successful.");
263 logger.info("Pairing failed with response status {}.", contentResponse.getStatus());
266 } catch (TimeoutException | CertificateEncodingException | KeyStoreException | RuntimeException e) {
267 logger.warn("Pairing failed with exception {}", e.getMessage(), e);
269 } catch (ExecutionException e) {
270 // javax.net.ssl.SSLHandshakeException: General SSLEngine problem
271 // => usually the pairing failed, because hardware button was not pressed.
272 logger.trace("Pairing failed - Details: {}", e.getMessage());
273 logger.warn("Pairing failed. Was the Bosch Smart Home Controller button pressed?");
279 * Creates a HTTP request.
281 * @param url for the HTTP request
282 * @param method for the HTTP request
283 * @return created HTTP request instance
285 public Request createRequest(String url, HttpMethod method) {
286 return this.createRequest(url, method, null);
290 * Creates a HTTP request.
292 * @param url for the HTTP request
293 * @param method for the HTTP request
294 * @param content for the HTTP request
295 * @return created HTTP request instance
297 public Request createRequest(String url, HttpMethod method, @Nullable Object content) {
298 logger.trace("Create request for http client {}", this);
300 Request request = this.newRequest(url).method(method).header("Content-Type", "application/json")
301 .header("api-version", "3.2") // see https://github.com/BoschSmartHome/bosch-shc-api-docs/issues/80
302 .timeout(10, TimeUnit.SECONDS); // Set default timeout
304 if (content != null) {
306 if (content.getClass().isAssignableFrom(UserStateServiceState.class)) {
307 body = ((UserStateServiceState) content).getStateAsString();
309 body = GsonUtils.DEFAULT_GSON_INSTANCE.toJson(content);
311 logger.trace("create request for {} and content {}", url, body);
312 request = request.content(new StringContentProvider(body));
314 logger.trace("create request for {}", url);
321 * Sends a request and expects a response of the specified type.
323 * @param request Request to send
324 * @param responseContentClass Type of expected response
325 * @param contentValidator Checks if the parsed response is valid
326 * @param errorResponseHandler Optional ustom error response handling. If not provided a generic exception is thrown
327 * @throws ExecutionException in case of invalid HTTP request result
328 * @throws TimeoutException in case of an HTTP request timeout
329 * @throws InterruptedException in case of an interrupt
330 * @throws BoschSHCException in case of a custom handled error response
332 public <TContent> TContent sendRequest(Request request, Class<TContent> responseContentClass,
333 Predicate<TContent> contentValidator,
334 @Nullable BiFunction<Integer, String, BoschSHCException> errorResponseHandler)
335 throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
336 logger.trace("Send request: {}", request);
338 ContentResponse contentResponse = request.send();
340 String textContent = contentResponse.getContentAsString();
342 Integer statusCode = contentResponse.getStatus();
343 if (!HttpStatus.getCode(statusCode).isSuccess()) {
344 if (errorResponseHandler != null) {
345 throw errorResponseHandler.apply(statusCode, textContent);
347 throw new ExecutionException(String.format("Send request failed with status code %s", statusCode),
352 logger.debug("Send request completed with success: {} - status code: {}", textContent, statusCode);
356 TContent content = GsonUtils.DEFAULT_GSON_INSTANCE.fromJson(textContent, responseContentClass);
357 if (content == null) {
358 throw new ExecutionException(String.format("Received no content in response, expected type %s",
359 responseContentClass.getName()), null);
361 if (!contentValidator.test(content)) {
362 throw new ExecutionException(String.format("Received invalid content for type %s: %s",
363 responseContentClass.getName(), content), null);
366 } catch (JsonSyntaxException e) {
367 throw new ExecutionException(String.format("Received invalid content in response, expected type %s: %s",
368 responseContentClass.getName(), e.getMessage()), e);
372 private String getCertFromSslContextFactory() throws KeyStoreException, CertificateEncodingException {
373 Certificate cert = this.getSslContextFactory().getKeyStore()
374 .getCertificate(BoschSslUtil.getBoschShcServerId(ipAddress));
375 return Base64.getEncoder().encodeToString(cert.getEncoded());