Skip to content

Commit

Permalink
Start investiguate refreshing channel state
Browse files Browse the repository at this point in the history
Signed-off-by: Laurent ARNAL <[email protected]>
  • Loading branch information
lo92fr committed Dec 6, 2024
1 parent 11fee0f commit 00ed746
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.JsonObject;

/**
* @author Bob Raker - Initial contribution
*/
Expand Down Expand Up @@ -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<String, String> 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 {
Expand Down

0 comments on commit 00ed746

Please sign in to comment.