diff --git a/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/api/SmartthingsApi.java b/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/api/SmartthingsApi.java index 2bdcc08ccbc53..903944257966a 100644 --- a/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/api/SmartthingsApi.java +++ b/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/api/SmartthingsApi.java @@ -85,6 +85,29 @@ public void SendCommand(String deviceId, String jsonMsg) { } + public @Nullable JsonObject SendStatus(String deviceId, String jsonMsg) { + try { + final AccessTokenResponse accessTokenResponse = oAuthClientService.getAccessTokenResponse(); + final String accessToken = accessTokenResponse == null ? null : accessTokenResponse.getAccessToken(); + + String uri = "https://api.smartthings.com/v1/devices/" + deviceId + "/status"; + + if (accessToken == null || accessToken.isEmpty()) { + throw new RuntimeException( + "No Smartthings accesstoken. Did you authorize Smartthings via /connectsmartthings ?"); + } else { + + JsonObject res = networkConnector.DoRequest(uri, null, accessToken, jsonMsg, HttpMethod.GET); + return res; + } + } catch (final IOException e) { + throw new RuntimeException(e.getMessage(), e); + } catch (OAuthException | OAuthResponseException e) { + throw new RuntimeException(e.getMessage(), e); + } + + } + private @Nullable JsonElement DoRequest(String uri) { try { final AccessTokenResponse accessTokenResponse = oAuthClientService.getAccessTokenResponse(); diff --git a/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/handler/SmartthingsThingHandler.java b/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/handler/SmartthingsThingHandler.java index dbfda0c2f6028..e9f425022a501 100644 --- a/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/handler/SmartthingsThingHandler.java +++ b/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/handler/SmartthingsThingHandler.java @@ -42,6 +42,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.gson.JsonObject; + /** * @author Bob Raker - Initial contribution */ @@ -92,21 +94,34 @@ public void handleCommand(ChannelUID channelUID, Command command) { SmartthingsConverter converter = converters.get(channelUID); String path; - String jsonMsg; + String jsonMsg = ""; if (command instanceof RefreshType) { - path = "/state"; - // Go to ST hub and ask for current state - jsonMsg = String.format( - "{\"capabilityKey\": \"%s\", " + "\"deviceDisplayName\": \"%s\", " - + "\"capabilityAttribute\": \"%s\", \"openHabStartTime\": %d}", - thingTypeId, smartthingsName, smartthingsType, System.currentTimeMillis()); + SmartthingsCloudBridgeHandler cloudBridge = (SmartthingsCloudBridgeHandler) bridge.getHandler(); + SmartthingsApi api = cloudBridge.getSmartthingsApi(); + Map properties = this.getThing().getProperties(); + String deviceId = properties.get("deviceId"); + + if (deviceId != null) { + JsonObject res = api.SendStatus(deviceId, jsonMsg); + if (res != null) { + JsonObject cp = res.get("components").getAsJsonObject(); + JsonObject main = cp.get("main").getAsJsonObject(); + JsonObject sw = main.get("switch").getAsJsonObject(); + JsonObject sw2 = sw.get("switch").getAsJsonObject(); + String value = sw2.get("value").getAsString(); + if (value.equals("on")) { + updateState(channelUID, OnOffType.ON); + } else { + updateState(channelUID, OnOffType.OFF); + } + + logger.trace(""); + } + } + } else { - // Send update to ST hub - path = "/update"; + // @todo : review this jsonMsg = converter.convertToSmartthings(channelUID, command); - - // The smartthings hub won't (can't) return a response to this call. But, it will send a separate - // message back to the SmartthingBridgeHandler.receivedPushMessage handler } // try {