]> git.basschouten.com Git - openhab-addons.git/commitdiff
[sensebox] Fix NPE (#14879)
authorChristoph Weitkamp <github@christophweitkamp.de>
Mon, 1 May 2023 17:44:08 +0000 (19:44 +0200)
committerGitHub <noreply@github.com>
Mon, 1 May 2023 17:44:08 +0000 (19:44 +0200)
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
bundles/org.openhab.binding.sensebox/src/main/java/org/openhab/binding/sensebox/internal/SenseBoxAPIConnection.java

index bb82fd44808126cc7aa8d299add3ec72c45114cf..340ed123882c5f40e45fc6aeb65354430576a582 100644 (file)
@@ -71,86 +71,92 @@ public class SenseBoxAPIConnection {
             logger.trace("Fetched Data: {}", body);
             SenseBoxData parsedData = gson.fromJson(body, SenseBoxData.class);
 
-            // Assume all is well at first
-            parsedData.setStatus(ThingStatus.ONLINE);
-
-            // Could perhaps be simplified via triply-nested arrays
-            // http://stackoverflow.com/questions/36946875/how-can-i-parse-geojson-with-gson
-            for (SenseBoxLoc loc : parsedData.getLocs()) {
-                if (loc.getGeometry() != null) {
-                    List<Double> locationData = loc.getGeometry().getData();
-                    if (locationData != null) {
-                        int locationDataCount = locationData.size();
-                        SenseBoxLocation location = new SenseBoxLocation();
-
-                        if (locationDataCount > 0) {
-                            location.setLongitude(locationData.get(0));
+            if (parsedData != null) {
+                // Assume all is well at first
+                parsedData.setStatus(ThingStatus.ONLINE);
+
+                // Could perhaps be simplified via triply-nested arrays
+                // http://stackoverflow.com/questions/36946875/how-can-i-parse-geojson-with-gson
+                for (SenseBoxLoc loc : parsedData.getLocs()) {
+                    if (loc.getGeometry() != null) {
+                        List<Double> locationData = loc.getGeometry().getData();
+                        if (locationData != null) {
+                            int locationDataCount = locationData.size();
+                            SenseBoxLocation location = new SenseBoxLocation();
+
+                            if (locationDataCount > 0) {
+                                location.setLongitude(locationData.get(0));
+                            }
+
+                            if (locationDataCount > 1) {
+                                location.setLatitude(locationData.get(1));
+                            }
+
+                            if (locationDataCount > 2) {
+                                location.setHeight(locationData.get(2));
+                            }
+
+                            parsedData.setLocation(location);
                         }
-
-                        if (locationDataCount > 1) {
-                            location.setLatitude(locationData.get(1));
-                        }
-
-                        if (locationDataCount > 2) {
-                            location.setHeight(locationData.get(2));
-                        }
-
-                        parsedData.setLocation(location);
                     }
                 }
-            }
 
-            for (SenseBoxSensor sensor : parsedData.getSensors()) {
-                if ("VEML6070".equals(sensor.getSensorType())) {
-                    // "unit" is not nicely comparable, so use sensor type for now
-                    parsedData.setUvIntensity(sensor);
-                } else if ("SDS 011".equals(sensor.getSensorType())) {
-                    // "unit" is not nicely comparable, neither is type, so use sensor title for now
-                    if ("PM2.5".equals(sensor.getTitle())) {
-                        parsedData.setParticulateMatter2dot5(sensor);
-                    } else if ("PM10".equals(sensor.getTitle())) {
-                        parsedData.setParticulateMatter10(sensor);
-                    } else {
-                        logger.debug("SDS 011 sensor title is {}", sensor.getTitle());
-                    }
-                } else if ("lx".equals(sensor.getUnit())) {
-                    if (sensor.getLastMeasurement() != null) {
-                        if (!(INVALID_BRIGHTNESS.equals(sensor.getLastMeasurement().getValue()))) {
-                            parsedData.setLuminance(sensor);
+                for (SenseBoxSensor sensor : parsedData.getSensors()) {
+                    if ("VEML6070".equals(sensor.getSensorType())) {
+                        // "unit" is not nicely comparable, so use sensor type for now
+                        parsedData.setUvIntensity(sensor);
+                    } else if ("SDS 011".equals(sensor.getSensorType())) {
+                        // "unit" is not nicely comparable, neither is type, so use sensor title for now
+                        if ("PM2.5".equals(sensor.getTitle())) {
+                            parsedData.setParticulateMatter2dot5(sensor);
+                        } else if ("PM10".equals(sensor.getTitle())) {
+                            parsedData.setParticulateMatter10(sensor);
+                        } else {
+                            logger.debug("SDS 011 sensor title is {}", sensor.getTitle());
                         }
-                    }
-                } else if ("Pa".equals(sensor.getUnit()) || "hPa".equals(sensor.getUnit())) {
-                    parsedData.setPressure(sensor);
-                } else if ("%".equals(sensor.getUnit())) {
-                    parsedData.setHumidity(sensor);
-                } else if ("°C".equals(sensor.getUnit())) {
-                    parsedData.setTemperature(sensor);
-                } else {
-                    if (logger.isDebugEnabled()) {
-                        logger.debug("    Sensor: {}", sensor);
-                        logger.debug("    Sensor unit: {}", sensor.getUnit());
-                        logger.debug("    Sensor type: {}", sensor.getSensorType());
-                        logger.debug("    Sensor LM: {}", sensor.getLastMeasurement());
+                    } else if ("lx".equals(sensor.getUnit())) {
                         if (sensor.getLastMeasurement() != null) {
-                            logger.debug("    Sensor LM value: {}", sensor.getLastMeasurement().getValue());
-                            logger.debug("    Sensor LM date: '{}'", sensor.getLastMeasurement().getCreatedAt());
+                            if (!(INVALID_BRIGHTNESS.equals(sensor.getLastMeasurement().getValue()))) {
+                                parsedData.setLuminance(sensor);
+                            }
+                        }
+                    } else if ("Pa".equals(sensor.getUnit()) || "hPa".equals(sensor.getUnit())) {
+                        parsedData.setPressure(sensor);
+                    } else if ("%".equals(sensor.getUnit())) {
+                        parsedData.setHumidity(sensor);
+                    } else if ("°C".equals(sensor.getUnit())) {
+                        parsedData.setTemperature(sensor);
+                    } else {
+                        if (logger.isDebugEnabled()) {
+                            logger.debug("    Sensor: {}", sensor);
+                            logger.debug("    Sensor unit: {}", sensor.getUnit());
+                            logger.debug("    Sensor type: {}", sensor.getSensorType());
+                            logger.debug("    Sensor LM: {}", sensor.getLastMeasurement());
+                            if (sensor.getLastMeasurement() != null) {
+                                logger.debug("    Sensor LM value: {}", sensor.getLastMeasurement().getValue());
+                                logger.debug("    Sensor LM date: '{}'", sensor.getLastMeasurement().getCreatedAt());
+                            }
                         }
                     }
                 }
-            }
 
-            SenseBoxDescriptor descriptor = new SenseBoxDescriptor();
-            descriptor.setApiUrl(query);
-            String image = parsedData.getImage();
-            if (image != null && !image.isEmpty()) {
-                descriptor.setImageUrl(SENSEMAP_IMAGE_URL_BASE + "/" + image);
-            }
-            descriptor.setMapUrl(SENSEMAP_MAP_URL_BASE + "/explore/" + senseBoxId);
-            parsedData.setDescriptor(descriptor);
+                SenseBoxDescriptor descriptor = new SenseBoxDescriptor();
+                descriptor.setApiUrl(query);
+                String image = parsedData.getImage();
+                if (image != null && !image.isEmpty()) {
+                    descriptor.setImageUrl(SENSEMAP_IMAGE_URL_BASE + "/" + image);
+                }
+                descriptor.setMapUrl(SENSEMAP_MAP_URL_BASE + "/explore/" + senseBoxId);
+                parsedData.setDescriptor(descriptor);
 
-            logger.trace("=================================");
+                logger.trace("=================================");
 
-            result = parsedData;
+                result = parsedData;
+            } else {
+                logger.debug("An error occurred while parsing the data into desired class: {} / {}", body,
+                        SenseBoxData.class.getName());
+                result.setStatus(ThingStatus.OFFLINE);
+            }
         } catch (JsonSyntaxException e) {
             logger.debug("An error occurred while parsing the data into desired class: {} / {} / {}", body,
                     SenseBoxData.class.getName(), e.getMessage());
@@ -159,7 +165,6 @@ public class SenseBoxAPIConnection {
             logger.debug("IO problems while fetching data: {} / {}", query, e.getMessage());
             result.setStatus(ThingStatus.OFFLINE);
         }
-
         return result;
     }
 }