]> git.basschouten.com Git - openhab-addons.git/blob
e78fd2fc59690928f29da105aa3be7dc57fb1f2a
[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.boschshc.internal.devices.bridge;
14
15 import java.util.Arrays;
16 import java.util.Optional;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.TimeoutException;
19
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;
29
30 /**
31  * Handler for executing a scenario.
32  *
33  * @author Patrick Gell - Initial contribution
34  *
35  */
36 @NonNullByDefault
37 public class ScenarioHandler {
38
39     private final Logger logger = LoggerFactory.getLogger(getClass());
40
41     protected ScenarioHandler() {
42     }
43
44     public void triggerScenario(final BoschHttpClient httpClient, final String scenarioName) {
45         final Scenario[] scenarios;
46         try {
47             scenarios = getAvailableScenarios(httpClient);
48         } catch (BoschSHCException e) {
49             logger.debug("unable to read the available scenarios from Bosch Smart Home Conteroller", e);
50             return;
51         }
52         final Optional<Scenario> scenario = Arrays.stream(scenarios).filter(s -> s.name.equals(scenarioName))
53                 .findFirst();
54         if (scenario.isPresent()) {
55             sendPOSTRequest(httpClient.getBoschSmartHomeUrl(String.format("scenarios/%s/triggers", scenario.get().id)),
56                     httpClient);
57         } else {
58             if (logger.isDebugEnabled()) {
59                 logger.debug("Scenario '{}' was not found in the list of available scenarios {}", scenarioName,
60                         prettyLogScenarios(scenarios));
61             }
62         }
63     }
64
65     private Scenario[] getAvailableScenarios(final BoschHttpClient httpClient) throws BoschSHCException {
66         final Request request = httpClient.createRequest(httpClient.getBoschSmartHomeUrl("scenarios"), HttpMethod.GET);
67         try {
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);
76         }
77
78         return new Scenario[] {};
79     }
80
81     private void sendPOSTRequest(final String url, final BoschHttpClient httpClient) {
82         try {
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());
88             }
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);
96         }
97     }
98
99     private String prettyLogScenarios(final Scenario[] scenarios) {
100         final StringBuilder builder = new StringBuilder();
101         builder.append("[");
102         for (Scenario scenario : scenarios) {
103             builder.append("\n  ");
104             builder.append(scenario);
105         }
106         builder.append("\n]");
107         return builder.toString();
108     }
109 }