-
-
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.
Signed-off-by: Patrick Gell <[email protected]>
- Loading branch information
1 parent
2d92fda
commit cd078d9
Showing
16 changed files
with
587 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
101 changes: 101 additions & 0 deletions
101
...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,101 @@ | ||
/** | ||
* 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.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
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.serialization.GsonUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.google.gson.JsonSyntaxException; | ||
|
||
/** | ||
* Handler for executing a scenario. | ||
* | ||
* @author Patrick Gell - Initial contribution | ||
* | ||
*/ | ||
@NonNullByDefault | ||
public class ScenarioHandler { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
private final Map<String, Scenario> availableScenarios; | ||
|
||
protected ScenarioHandler(Map<String, Scenario> availableScenarios) { | ||
this.availableScenarios = Objects.requireNonNullElseGet(availableScenarios, HashMap::new); | ||
} | ||
|
||
public void executeScenario(final @Nullable BoschHttpClient httpClient, final String scenarioName) { | ||
assert httpClient != null; | ||
if (!availableScenarios.containsKey(scenarioName)) { | ||
updateScenarios(httpClient); | ||
} | ||
final Scenario scenario = this.availableScenarios.get(scenarioName); | ||
if (scenario != null) { | ||
sendRequest(HttpMethod.POST, | ||
httpClient.getBoschSmartHomeUrl(String.format("scenarios/%s/triggers", scenario.id)), httpClient); | ||
} else { | ||
logger.debug("scenario '{}' not found on the Bosch Controller", scenarioName); | ||
} | ||
} | ||
|
||
private void updateScenarios(final @Nullable BoschHttpClient httpClient) { | ||
if (httpClient != null) { | ||
final String result = sendRequest(HttpMethod.GET, httpClient.getBoschSmartHomeUrl("scenarios"), httpClient); | ||
try { | ||
Scenario[] scenarios = GsonUtils.DEFAULT_GSON_INSTANCE.fromJson(result, Scenario[].class); | ||
if (scenarios != null) { | ||
for (Scenario scenario : scenarios) { | ||
availableScenarios.put(scenario.name, scenario); | ||
} | ||
} | ||
} catch (JsonSyntaxException e) { | ||
logger.debug("response from SHC could not be parsed: {}", result, e); | ||
} | ||
} | ||
} | ||
|
||
private String sendRequest(final HttpMethod method, final String url, final BoschHttpClient httpClient) { | ||
try { | ||
final Request request = httpClient.createRequest(url, method); | ||
final ContentResponse response = request.send(); | ||
switch (HttpStatus.getCode(response.getStatus())) { | ||
case OK -> { | ||
return response.getContentAsString(); | ||
} | ||
case NOT_FOUND, METHOD_NOT_ALLOWED -> logger.debug("{} - {} failed with {}: {}", method, url, | ||
response.getStatus(), response.getContentAsString()); | ||
} | ||
} catch (InterruptedException e) { | ||
logger.debug("scenario call was interrupted", e); | ||
} catch (TimeoutException e) { | ||
logger.debug("scenarion call timed out", e); | ||
} catch (ExecutionException e) { | ||
logger.debug("exception occurred during scenario call", e); | ||
} | ||
return ""; | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
...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,39 @@ | ||
/** | ||
* 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 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"); | ||
} | ||
} |
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
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.