Skip to content

Commit

Permalink
start adding dynamic update on event handling
Browse files Browse the repository at this point in the history
Signed-off-by: Laurent ARNAL <[email protected]>
  • Loading branch information
lo92fr committed Dec 13, 2024
1 parent 3f6a06a commit dead284
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.servlet.ServletConfig;
Expand All @@ -40,7 +42,11 @@
import org.openhab.binding.smartthings.internal.dto.SMEvent.device;
import org.openhab.binding.smartthings.internal.dto.SmartthingsLocation;
import org.openhab.binding.smartthings.internal.handler.SmartthingsBridgeHandler;
import org.openhab.binding.smartthings.internal.handler.SmartthingsThingHandler;
import org.openhab.binding.smartthings.internal.type.SmartthingsException;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.binding.ThingHandler;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.http.HttpService;
Expand Down Expand Up @@ -207,14 +213,31 @@ protected void service(@Nullable HttpServletRequest req, @Nullable HttpServletRe
String deviceId = data.events[0].deviceEvent.deviceId;
String componentId = data.events[0].deviceEvent.componentId;
String capa = data.events[0].deviceEvent.capability;
String atttr = data.events[0].deviceEvent.attribute;
String attr = data.events[0].deviceEvent.attribute;
String value = data.events[0].deviceEvent.value;

logger.info("EVENT: {} {} {} {} {}", deviceId, componentId, capa, atttr, value);
Bridge bridge = bridgeHandler.getThing();
List<Thing> things = bridge.getThings();

Optional<Thing> theThingOpt = things.stream().filter(x -> x.getProperties().containsValue(deviceId))
.findFirst();
if (theThingOpt.isPresent()) {
Thing theThing = theThingOpt.get();

ThingHandler handler = theThing.getHandler();
SmartthingsThingHandler smarthingsHandler = (SmartthingsThingHandler) handler;
smarthingsHandler.refreshDevice(componentId, capa, attr, value);

logger.info("aa");
}

logger.info("EVENT: {} {} {} {} {}", deviceId, componentId, capa, attr, value);
} else if (resultObj.lifecycle.equals("INSTALL")) {
logger.info("");
// String token = resultObj.installData.authToken;
String token = resultObj.installData.authToken;
installedAppId = resultObj.installData.installedApp.installedAppId;
String subscriptionUri = "https://api.smartthings.com/v1/installedapps/" + installedAppId
+ "/subscriptions";

try {
SmartthingsLocation loc = api.getLocation(resultObj.installData.installedApp.locationId);
Expand All @@ -223,11 +246,27 @@ protected void service(@Nullable HttpServletRequest req, @Nullable HttpServletRe
installedLocation = "Unable to retrieve location!!";
}

networkConnector.doRequest(JsonObject.class, subscriptionUri, null, token, "", HttpMethod.GET);

SMEvent evt = new SMEvent();
evt.sourceType = "DEVICE";
evt.device = new device("97806abc-ce85-4b28-9df2-31e33323cf62", "main", true, null);

String body = gson.toJson(evt);
networkConnector.doRequest(JsonObject.class, subscriptionUri, null, token, body, HttpMethod.POST);

evt = new SMEvent();
evt.sourceType = "DEVICE";
evt.device = new device("ee87617f-0c84-40a3-be25-e70e53f3fc6a", "main", true, null);

body = gson.toJson(evt);
networkConnector.doRequest(JsonObject.class, subscriptionUri, null, token, body, HttpMethod.POST);

setupInProgress = false;
logger.info("");
logger.info("INSTALL");
} else if (resultObj.lifecycle.equals("UPDATE")) {
String token = resultObj.updateData.authToken;
String installedAppId = resultObj.updateData.installedApp.installedAppId;
installedAppId = resultObj.updateData.installedApp.installedAppId;
String subscriptionUri = "https://api.smartthings.com/v1/installedapps/" + installedAppId
+ "/subscriptions";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class SmartthingsApi {
private final SmartthingsNetworkConnector networkConnector;
private final String token;

private static final String APP_NAME = "openhabnew024";
private static final String APP_NAME = "openhabnew025";
private Gson gson = new Gson();
private String baseUrl = "https://api.smartthings.com/v1";
private String deviceEndPoint = "/devices";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,33 @@ else if (command instanceof PercentType) {
}
}

public void refreshDevice(String componentId, String capa, String attr, Object value) {
String channelName = (StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(attr), '-') + "-channel")
.toLowerCase();

ChannelUID chanUid = new ChannelUID(this.getThing().getUID(), "light_default", channelName);
Channel chan = thing.getChannel(chanUid);

if (chan != null) {
if (attr.equals("switch")) {
if (("on".equals(value))) {
updateState(chanUid, OnOffType.ON);
} else {
updateState(chanUid, OnOffType.OFF);
}
}
if (attr.equals("level")) {
if (value instanceof String) {
int val = java.lang.Integer.parseInt(((String) value));
updateState(chanUid, new PercentType(val));
} else {
updateState(chanUid, new PercentType(((Double) value).intValue()));
}
}
}

}

public void refreshDevice() {
Bridge bridge = getBridge();

Expand Down Expand Up @@ -176,26 +203,7 @@ public void refreshDevice() {
Object value = props.value;
String timestamp = props.timestamp;

String channelName = (StringUtils
.join(StringUtils.splitByCharacterTypeCamelCase(propertyKey), '-') + "-channel")
.toLowerCase();

ChannelUID chanUid = new ChannelUID(this.getThing().getUID(), "light_default",
channelName);
Channel chan = thing.getChannel(chanUid);

if (chan != null) {
if (propertyKey.equals("switch")) {
if (("on".equals(value))) {
updateState(chanUid, OnOffType.ON);
} else {
updateState(chanUid, OnOffType.OFF);
}
}
if (propertyKey.equals("level")) {
updateState(chanUid, new PercentType(((Double) value).intValue()));
}
}
refreshDevice("main", "capa", propertyKey, value);
logger.info("");
}

Expand Down Expand Up @@ -276,6 +284,8 @@ private void pollingCode() {
return;
}
}

// refreshDevice();
}

private @Nullable SmartthingsConverter getConverter(String converterName) {
Expand Down

0 comments on commit dead284

Please sign in to comment.