]> git.basschouten.com Git - openhab-addons.git/commitdiff
[tibber] Fix NPE on server issues (#14273)
authorlsiepel <leosiepel@gmail.com>
Sun, 29 Jan 2023 09:24:26 +0000 (10:24 +0100)
committerGitHub <noreply@github.com>
Sun, 29 Jan 2023 09:24:26 +0000 (10:24 +0100)
* Fix NPE

Signed-off-by: lsiepel <leosiepel@gmail.com>
bundles/org.openhab.binding.tibber/src/main/java/org/openhab/binding/tibber/internal/handler/TibberHandler.java

index 241599ee148cf303ff71a9bf35d80b55c3f846ef..b360347aa2834c07a85cbf530b3efb2d6472907d 100644 (file)
@@ -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");
                 }