]> git.basschouten.com Git - openhab-addons.git/commitdiff
Enhancement: Query for subscription url every reconnect attempt (#14573)
authorStian Kjoglum <47720690+kjoglum@users.noreply.github.com>
Sat, 11 Mar 2023 16:55:42 +0000 (17:55 +0100)
committerGitHub <noreply@github.com>
Sat, 11 Mar 2023 16:55:42 +0000 (17:55 +0100)
Signed-off-by: kjoglum <stiankj@online.no>
bundles/org.openhab.binding.tibber/src/main/java/org/openhab/binding/tibber/internal/handler/TibberHandler.java

index b360347aa2834c07a85cbf530b3efb2d6472907d..59e8e1b8cdb742817803db25f784954a17c89237 100644 (file)
@@ -146,28 +146,14 @@ public class TibberHandler extends BaseThingHandler {
 
                 if ("true".equals(rtEnabled)) {
                     logger.debug("Pulse associated with HomeId: Live stream will be started");
+                    getSubscriptionUrl();
 
-                    InputStream wsURL = tibberQuery.getWebsocketUrl();
-                    String wsResponse = HttpUtil.executeUrl("POST", BASE_URL, httpHeader, wsURL, null, REQUEST_TIMEOUT);
-
-                    JsonObject wsobject = (JsonObject) JsonParser.parseString(wsResponse);
-                    JsonObject dataObject = wsobject.getAsJsonObject("data");
-                    if (dataObject != null) {
-                        JsonObject viewerObject = dataObject.getAsJsonObject("viewer");
-                        if (viewerObject != null) {
-                            JsonElement subscriptionElement = viewerObject.get("websocketSubscriptionUrl");
-                            if (subscriptionElement != null) {
-                                subscriptionURL = subscriptionElement.toString().replaceAll("^\"|\"$", "");
-                            }
-                        }
-                    }
-                    String url = subscriptionURL;
-                    if (url == null || url.isBlank()) {
-                        logger.trace("Unexpected result from the server: {}", jsonResponse);
+                    if (subscriptionURL == null || subscriptionURL.isBlank()) {
+                        logger.debug("Unexpected subscription result from the server");
                         updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
-                                "Unexpected result from the server");
+                                "Unexpected subscription result from the server");
                     } else {
-                        logger.debug("Subscribing to: {}", subscriptionURL);
+                        logger.debug("Reconnecting Subscription to: {}", subscriptionURL);
                         open();
                     }
                 } else {
@@ -297,7 +283,34 @@ public class TibberHandler extends BaseThingHandler {
         getURLInput(BASE_URL);
         if ("true".equals(rtEnabled) && !isConnected()) {
             logger.debug("Attempting to reopen Websocket connection");
-            open();
+            getSubscriptionUrl();
+
+            if (subscriptionURL == null || subscriptionURL.isBlank()) {
+                logger.debug("Unexpected subscription result from the server");
+                updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
+                        "Unexpected subscription result from the server");
+            } else {
+                logger.debug("Reconnecting Subscription to: {}", subscriptionURL);
+                open();
+            }
+        }
+    }
+
+    private void getSubscriptionUrl() throws IOException {
+        TibberPriceConsumptionHandler tibberQuery = new TibberPriceConsumptionHandler();
+        InputStream wsURL = tibberQuery.getWebsocketUrl();
+        String wsResponse = HttpUtil.executeUrl("POST", BASE_URL, httpHeader, wsURL, null, REQUEST_TIMEOUT);
+
+        JsonObject wsobject = (JsonObject) JsonParser.parseString(wsResponse);
+        JsonObject dataObject = wsobject.getAsJsonObject("data");
+        if (dataObject != null) {
+            JsonObject viewerObject = dataObject.getAsJsonObject("viewer");
+            if (viewerObject != null) {
+                JsonElement subscriptionElement = viewerObject.get("websocketSubscriptionUrl");
+                if (subscriptionElement != null) {
+                    subscriptionURL = subscriptionElement.toString().replaceAll("^\"|\"$", "");
+                }
+            }
         }
     }