From dead28437f2610144997624c0012cdd7b883c526 Mon Sep 17 00:00:00 2001 From: Laurent ARNAL Date: Fri, 13 Dec 2024 16:50:40 +0100 Subject: [PATCH] start adding dynamic update on event handling Signed-off-by: Laurent ARNAL --- .../internal/SmartthingsServlet.java | 49 ++++++++++++++++-- .../internal/api/SmartthingsApi.java | 2 +- .../handler/SmartthingsThingHandler.java | 50 +++++++++++-------- 3 files changed, 75 insertions(+), 26 deletions(-) diff --git a/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/SmartthingsServlet.java b/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/SmartthingsServlet.java index 872d764c469fe..6756e2e42fab3 100644 --- a/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/SmartthingsServlet.java +++ b/bundles/org.openhab.binding.smartthings/src/main/java/org/openhab/binding/smartthings/internal/SmartthingsServlet.java @@ -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; @@ -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; @@ -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 things = bridge.getThings(); + + Optional 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); @@ -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"; 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 5297bfd240a9b..7882bf7181bd6 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 @@ -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"; 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 3ae95cef0dafa..98404fcdde9da 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 @@ -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(); @@ -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(""); } @@ -276,6 +284,8 @@ private void pollingCode() { return; } } + + // refreshDevice(); } private @Nullable SmartthingsConverter getConverter(String converterName) {