From ac7993d329313f56cf567d976a2190881bbbfea0 Mon Sep 17 00:00:00 2001 From: Stian Kjoglum <47720690+kjoglum@users.noreply.github.com> Date: Sat, 11 Mar 2023 17:55:42 +0100 Subject: [PATCH] Enhancement: Query for subscription url every reconnect attempt (#14573) Signed-off-by: kjoglum --- .../internal/handler/TibberHandler.java | 53 ++++++++++++------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/bundles/org.openhab.binding.tibber/src/main/java/org/openhab/binding/tibber/internal/handler/TibberHandler.java b/bundles/org.openhab.binding.tibber/src/main/java/org/openhab/binding/tibber/internal/handler/TibberHandler.java index b360347aa..59e8e1b8c 100644 --- a/bundles/org.openhab.binding.tibber/src/main/java/org/openhab/binding/tibber/internal/handler/TibberHandler.java +++ b/bundles/org.openhab.binding.tibber/src/main/java/org/openhab/binding/tibber/internal/handler/TibberHandler.java @@ -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("^\"|\"$", ""); + } + } } }