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 java.util.Arrays;
16 import java.util.Optional;
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.api.ContentResponse;
22 import org.eclipse.jetty.client.api.Request;
23 import org.eclipse.jetty.http.HttpMethod;
24 import org.eclipse.jetty.http.HttpStatus;
25 import org.openhab.binding.boschshc.internal.devices.bridge.dto.Scenario;
26 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Handler for executing a scenario.
33 * @author Patrick Gell - Initial contribution
37 public class ScenarioHandler {
39 private final Logger logger = LoggerFactory.getLogger(getClass());
41 protected ScenarioHandler() {
44 public void triggerScenario(final BoschHttpClient httpClient, final String scenarioName) {
45 final Scenario[] scenarios;
47 scenarios = getAvailableScenarios(httpClient);
48 } catch (BoschSHCException e) {
49 logger.debug("unable to read the available scenarios from Bosch Smart Home Conteroller", e);
52 final Optional<Scenario> scenario = Arrays.stream(scenarios).filter(s -> s.name.equals(scenarioName))
54 if (scenario.isPresent()) {
55 sendPOSTRequest(httpClient.getBoschSmartHomeUrl(String.format("scenarios/%s/triggers", scenario.get().id)),
58 if (logger.isDebugEnabled()) {
59 logger.debug("Scenario '{}' was not found in the list of available scenarios {}", scenarioName,
60 prettyLogScenarios(scenarios));
65 private Scenario[] getAvailableScenarios(final BoschHttpClient httpClient) throws BoschSHCException {
66 final Request request = httpClient.createRequest(httpClient.getBoschSmartHomeUrl("scenarios"), HttpMethod.GET);
68 return httpClient.sendRequest(request, Scenario[].class, Scenario::isValid, null);
69 } catch (InterruptedException e) {
70 logger.debug("Scenario call was interrupted", e);
71 Thread.currentThread().interrupt();
72 } catch (TimeoutException e) {
73 logger.debug("Scenario call timed out", e);
74 } catch (ExecutionException e) {
75 logger.debug("Exception occurred during scenario call", e);
78 return new Scenario[] {};
81 private void sendPOSTRequest(final String url, final BoschHttpClient httpClient) {
83 final Request request = httpClient.createRequest(url, HttpMethod.POST);
84 final ContentResponse response = request.send();
85 if (HttpStatus.ACCEPTED_202 != response.getStatus()) {
86 logger.debug("{} - {} failed with {}: {}", HttpMethod.POST, url, response.getStatus(),
87 response.getContentAsString());
89 } catch (InterruptedException e) {
90 logger.debug("Scenario call was interrupted", e);
91 Thread.currentThread().interrupt();
92 } catch (TimeoutException e) {
93 logger.debug("Scenario call timed out", e);
94 } catch (ExecutionException e) {
95 logger.debug("Exception occurred during scenario call", e);
99 private String prettyLogScenarios(final Scenario[] scenarios) {
100 final StringBuilder builder = new StringBuilder();
102 for (Scenario scenario : scenarios) {
103 builder.append("\n ");
104 builder.append(scenario);
106 builder.append("\n]");
107 return builder.toString();