From a58f1269ec2a0a347a5c191c7594f80b66a08210 Mon Sep 17 00:00:00 2001 From: lsiepel Date: Sun, 29 Jan 2023 10:24:26 +0100 Subject: [PATCH] [tibber] Fix NPE on server issues (#14273) * Fix NPE Signed-off-by: lsiepel --- .../internal/handler/TibberHandler.java | 40 +++++++++++++++---- 1 file changed, 33 insertions(+), 7 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 241599ee1..b360347aa 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 @@ -57,6 +57,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.gson.JsonArray; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.JsonSyntaxException; @@ -129,8 +130,19 @@ public class TibberHandler extends BaseThingHandler { REQUEST_TIMEOUT); JsonObject object = (JsonObject) JsonParser.parseString(jsonResponse); - rtEnabled = object.getAsJsonObject("data").getAsJsonObject("viewer").getAsJsonObject("home") - .getAsJsonObject("features").get("realTimeConsumptionEnabled").toString(); + JsonObject dObject = object.getAsJsonObject("data"); + if (dObject != null) { + JsonObject viewerObject = dObject.getAsJsonObject("viewer"); + if (viewerObject != null) { + JsonObject homeObject = viewerObject.getAsJsonObject("home"); + if (homeObject != null) { + JsonObject featuresObject = homeObject.getAsJsonObject("features"); + if (featuresObject != null) { + rtEnabled = featuresObject.get("realTimeConsumptionEnabled").toString(); + } + } + } + } if ("true".equals(rtEnabled)) { logger.debug("Pulse associated with HomeId: Live stream will be started"); @@ -139,11 +151,25 @@ public class TibberHandler extends BaseThingHandler { String wsResponse = HttpUtil.executeUrl("POST", BASE_URL, httpHeader, wsURL, null, REQUEST_TIMEOUT); JsonObject wsobject = (JsonObject) JsonParser.parseString(wsResponse); - subscriptionURL = wsobject.getAsJsonObject("data").getAsJsonObject("viewer") - .get("websocketSubscriptionUrl").toString().replaceAll("^\"|\"$", ""); - logger.debug("Subscribing to: {}", subscriptionURL); - - open(); + 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); + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, + "Unexpected result from the server"); + } else { + logger.debug("Subscribing to: {}", subscriptionURL); + open(); + } } else { logger.debug("No Pulse associated with HomeId: No live stream will be started"); }