-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[boschshc] Add scenario channel (#15752)
Signed-off-by: Patrick Gell <[email protected]>
- Loading branch information
1 parent
1eacf67
commit 1b466fb
Showing
15 changed files
with
609 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...c/src/main/java/org/openhab/binding/boschshc/internal/devices/bridge/ScenarioHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* Copyright (c) 2010-2023 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.boschshc.internal.devices.bridge; | ||
|
||
import java.util.Arrays; | ||
import java.util.Optional; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jetty.client.api.ContentResponse; | ||
import org.eclipse.jetty.client.api.Request; | ||
import org.eclipse.jetty.http.HttpMethod; | ||
import org.eclipse.jetty.http.HttpStatus; | ||
import org.openhab.binding.boschshc.internal.devices.bridge.dto.Scenario; | ||
import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Handler for executing a scenario. | ||
* | ||
* @author Patrick Gell - Initial contribution | ||
* | ||
*/ | ||
@NonNullByDefault | ||
public class ScenarioHandler { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
protected ScenarioHandler() { | ||
} | ||
|
||
public void triggerScenario(final BoschHttpClient httpClient, final String scenarioName) { | ||
|
||
final Scenario[] scenarios; | ||
try { | ||
scenarios = getAvailableScenarios(httpClient); | ||
} catch (BoschSHCException e) { | ||
logger.debug("unable to read the available scenarios from Bosch Smart Home Conteroller", e); | ||
return; | ||
} | ||
final Optional<Scenario> scenario = Arrays.stream(scenarios).filter(s -> s.name.equals(scenarioName)) | ||
.findFirst(); | ||
if (scenario.isPresent()) { | ||
sendPOSTRequest(httpClient.getBoschSmartHomeUrl(String.format("scenarios/%s/triggers", scenario.get().id)), | ||
httpClient); | ||
} else { | ||
if (logger.isDebugEnabled()) { | ||
logger.debug("Scenario '{}' was not found in the list of available scenarios {}", scenarioName, | ||
prettyLogScenarios(scenarios)); | ||
} | ||
} | ||
} | ||
|
||
private Scenario[] getAvailableScenarios(final BoschHttpClient httpClient) throws BoschSHCException { | ||
final Request request = httpClient.createRequest(httpClient.getBoschSmartHomeUrl("scenarios"), HttpMethod.GET); | ||
try { | ||
return httpClient.sendRequest(request, Scenario[].class, Scenario::isValid, null); | ||
} catch (InterruptedException e) { | ||
logger.debug("Scenario call was interrupted", e); | ||
Thread.currentThread().interrupt(); | ||
} catch (TimeoutException e) { | ||
logger.debug("Scenario call timed out", e); | ||
} catch (ExecutionException e) { | ||
logger.debug("Exception occurred during scenario call", e); | ||
} | ||
|
||
return new Scenario[] {}; | ||
} | ||
|
||
private void sendPOSTRequest(final String url, final BoschHttpClient httpClient) { | ||
try { | ||
final Request request = httpClient.createRequest(url, HttpMethod.POST); | ||
final ContentResponse response = request.send(); | ||
if (HttpStatus.ACCEPTED_202 != response.getStatus()) { | ||
logger.debug("{} - {} failed with {}: {}", HttpMethod.POST, url, response.getStatus(), | ||
response.getContentAsString()); | ||
} | ||
} catch (InterruptedException e) { | ||
logger.debug("Scenario call was interrupted", e); | ||
Thread.currentThread().interrupt(); | ||
} catch (TimeoutException e) { | ||
logger.debug("Scenario call timed out", e); | ||
} catch (ExecutionException e) { | ||
logger.debug("Exception occurred during scenario call", e); | ||
} | ||
} | ||
|
||
private String prettyLogScenarios(final Scenario[] scenarios) { | ||
final StringBuilder builder = new StringBuilder(); | ||
builder.append("["); | ||
for (Scenario scenario : scenarios) { | ||
builder.append("\n "); | ||
builder.append(scenario); | ||
} | ||
builder.append("\n]"); | ||
return builder.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...hshc/src/main/java/org/openhab/binding/boschshc/internal/devices/bridge/dto/Scenario.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* Copyright (c) 2010-2023 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.boschshc.internal.devices.bridge.dto; | ||
|
||
import java.util.Arrays; | ||
|
||
import org.openhab.binding.boschshc.internal.services.dto.BoschSHCServiceState; | ||
|
||
/** | ||
* A scenario as represented by the controller. | ||
* | ||
* Json example: | ||
* { | ||
* "@type": "scenarioTriggered", | ||
* "name": "My scenario", | ||
* "id": "509bd737-eed0-40b7-8caa-e8686a714399", | ||
* "lastTimeTriggered": "1693758693032" | ||
* } | ||
* | ||
* @author Patrick Gell - Initial contribution | ||
*/ | ||
public class Scenario extends BoschSHCServiceState { | ||
|
||
public String name; | ||
public String id; | ||
public String lastTimeTriggered; | ||
|
||
public Scenario() { | ||
super("scenarioTriggered"); | ||
} | ||
|
||
public static Scenario createScenario(final String id, final String name, final String lastTimeTriggered) { | ||
final Scenario scenario = new Scenario(); | ||
|
||
scenario.id = id; | ||
scenario.name = name; | ||
scenario.lastTimeTriggered = lastTimeTriggered; | ||
return scenario; | ||
} | ||
|
||
public static Boolean isValid(Scenario[] scenarios) { | ||
return Arrays.stream(scenarios).allMatch(scenario -> (scenario.id != null)); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
final StringBuilder sb = new StringBuilder("Scenario{"); | ||
sb.append("name='").append(name).append("'"); | ||
sb.append(", id='").append(id).append("'"); | ||
sb.append(", lastTimeTriggered='").append(lastTimeTriggered).append("'"); | ||
sb.append('}'); | ||
return sb.toString(); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...ava/org/openhab/binding/boschshc/internal/serialization/BoschServiceDataDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* Copyright (c) 2010-2023 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.boschshc.internal.serialization; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.binding.boschshc.internal.devices.bridge.dto.DeviceServiceData; | ||
import org.openhab.binding.boschshc.internal.devices.bridge.dto.Scenario; | ||
import org.openhab.binding.boschshc.internal.services.dto.BoschSHCServiceState; | ||
|
||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParseException; | ||
|
||
/** | ||
* Utility class for JSON deserialization of device data and triggered scenarios using Google Gson. | ||
* | ||
* @author Patrick Gell - Initial contribution | ||
* | ||
*/ | ||
@NonNullByDefault | ||
public class BoschServiceDataDeserializer implements JsonDeserializer<BoschSHCServiceState> { | ||
|
||
@Nullable | ||
@Override | ||
public BoschSHCServiceState deserialize(JsonElement jsonElement, Type type, | ||
JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { | ||
|
||
JsonObject jsonObject = jsonElement.getAsJsonObject(); | ||
JsonElement dataType = jsonObject.get("@type"); | ||
switch (dataType.getAsString()) { | ||
case "DeviceServiceData" -> { | ||
var deviceServiceData = new DeviceServiceData(); | ||
deviceServiceData.deviceId = jsonObject.get("deviceId").getAsString(); | ||
deviceServiceData.state = jsonObject.get("state"); | ||
deviceServiceData.id = jsonObject.get("id").getAsString(); | ||
deviceServiceData.path = jsonObject.get("path").getAsString(); | ||
return deviceServiceData; | ||
} | ||
case "scenarioTriggered" -> { | ||
var scenario = new Scenario(); | ||
scenario.id = jsonObject.get("id").getAsString(); | ||
scenario.name = jsonObject.get("name").getAsString(); | ||
scenario.lastTimeTriggered = jsonObject.get("lastTimeTriggered").getAsString(); | ||
return scenario; | ||
} | ||
default -> { | ||
return new BoschSHCServiceState(dataType.getAsString()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.