]> git.basschouten.com Git - openhab-addons.git/commitdiff
[goecharger] Avoid potential NullPointerException (#14933)
authorlolodomo <lg.hc@free.fr>
Sat, 13 May 2023 10:26:58 +0000 (12:26 +0200)
committerGitHub <noreply@github.com>
Sat, 13 May 2023 10:26:58 +0000 (12:26 +0200)
Also include change from PR #14632

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
bundles/org.openhab.binding.goecharger/src/main/java/org/openhab/binding/goecharger/internal/handler/GoEChargerHandler.java
bundles/org.openhab.binding.goecharger/src/main/java/org/openhab/binding/goecharger/internal/handler/GoEChargerV2Handler.java

index c426d10d780d1c735d2dc6f52e41b5ce81944559..1ec1bb9cd8b68fd2b5bce239b9b7d42cca0816c4 100644 (file)
@@ -154,13 +154,13 @@ public class GoEChargerHandler extends GoEChargerBaseHandler {
                     return UnDefType.UNDEF;
                 }
                 int count = 0;
-                if (goeResponse.energy[4] > 0) { // current P1
+                if (goeResponse.energy.length >= 5 && goeResponse.energy[4] > 0) { // current P1
                     count++;
                 }
-                if (goeResponse.energy[5] > 0) { // current P2
+                if (goeResponse.energy.length >= 6 && goeResponse.energy[5] > 0) { // current P2
                     count++;
                 }
-                if (goeResponse.energy[6] > 0) { // current P3
+                if (goeResponse.energy.length >= 7 && goeResponse.energy[6] > 0) { // current P3
                     count++;
                 }
                 return new DecimalType(count);
@@ -173,68 +173,66 @@ public class GoEChargerHandler extends GoEChargerBaseHandler {
                 if (goeResponse.sessionChargeConsumption == null) {
                     return UnDefType.UNDEF;
                 }
-                return new QuantityType<>((Double) (goeResponse.sessionChargeConsumption / 360000d),
-                        Units.KILOWATT_HOUR);
+                return new QuantityType<>(goeResponse.sessionChargeConsumption / 360000d, Units.KILOWATT_HOUR);
             case SESSION_CHARGE_CONSUMPTION_LIMIT:
                 if (goeResponse.sessionChargeConsumptionLimit == null) {
                     return UnDefType.UNDEF;
                 }
-                return new QuantityType<>((Double) (goeResponse.sessionChargeConsumptionLimit / 10d),
-                        Units.KILOWATT_HOUR);
+                return new QuantityType<>(goeResponse.sessionChargeConsumptionLimit / 10d, Units.KILOWATT_HOUR);
             case TOTAL_CONSUMPTION:
                 if (goeResponse.totalChargeConsumption == null) {
                     return UnDefType.UNDEF;
                 }
-                return new QuantityType<>((Double) (goeResponse.totalChargeConsumption / 10d), Units.KILOWATT_HOUR);
+                return new QuantityType<>(goeResponse.totalChargeConsumption / 10d, Units.KILOWATT_HOUR);
             case CURRENT_L1:
-                if (goeResponse.energy == null) {
+                if (goeResponse.energy == null || goeResponse.energy.length < 5) {
                     return UnDefType.UNDEF;
                 }
                 // values come in as A*10, 41 means 4.1A
-                return new QuantityType<>((Double) (goeResponse.energy[4] / 10d), Units.AMPERE);
+                return new QuantityType<>(goeResponse.energy[4] / 10d, Units.AMPERE);
             case CURRENT_L2:
-                if (goeResponse.energy == null) {
+                if (goeResponse.energy == null || goeResponse.energy.length < 6) {
                     return UnDefType.UNDEF;
                 }
-                return new QuantityType<>((Double) (goeResponse.energy[5] / 10d), Units.AMPERE);
+                return new QuantityType<>(goeResponse.energy[5] / 10d, Units.AMPERE);
             case CURRENT_L3:
-                if (goeResponse.energy == null) {
+                if (goeResponse.energy == null || goeResponse.energy.length < 7) {
                     return UnDefType.UNDEF;
                 }
-                return new QuantityType<>((Double) (goeResponse.energy[6] / 10d), Units.AMPERE);
+                return new QuantityType<>(goeResponse.energy[6] / 10d, Units.AMPERE);
             case POWER_L1:
-                if (goeResponse.energy == null) {
+                if (goeResponse.energy == null || goeResponse.energy.length < 8) {
                     return UnDefType.UNDEF;
                 }
                 // values come in as kW*10, 41 means 4.1kW
                 return new QuantityType<>(goeResponse.energy[7] * 100, Units.WATT);
             case POWER_L2:
-                if (goeResponse.energy == null) {
+                if (goeResponse.energy == null || goeResponse.energy.length < 9) {
                     return UnDefType.UNDEF;
                 }
                 return new QuantityType<>(goeResponse.energy[8] * 100, Units.WATT);
             case POWER_L3:
-                if (goeResponse.energy == null) {
+                if (goeResponse.energy == null || goeResponse.energy.length < 10) {
                     return UnDefType.UNDEF;
                 }
                 return new QuantityType<>(goeResponse.energy[9] * 100, Units.WATT);
             case VOLTAGE_L1:
-                if (goeResponse.energy == null) {
+                if (goeResponse.energy == null || goeResponse.energy.length < 1) {
                     return UnDefType.UNDEF;
                 }
                 return new QuantityType<>(goeResponse.energy[0], Units.VOLT);
             case VOLTAGE_L2:
-                if (goeResponse.energy == null) {
+                if (goeResponse.energy == null || goeResponse.energy.length < 2) {
                     return UnDefType.UNDEF;
                 }
                 return new QuantityType<>(goeResponse.energy[1], Units.VOLT);
             case VOLTAGE_L3:
-                if (goeResponse.energy == null) {
+                if (goeResponse.energy == null || goeResponse.energy.length < 3) {
                     return UnDefType.UNDEF;
                 }
                 return new QuantityType<>(goeResponse.energy[2], Units.VOLT);
             case POWER_ALL:
-                if (goeResponse.energy == null) {
+                if (goeResponse.energy == null || goeResponse.energy.length < 12) {
                     return UnDefType.UNDEF;
                 }
                 return new QuantityType<>(goeResponse.energy[11] * 10, Units.WATT);
@@ -392,7 +390,7 @@ public class GoEChargerHandler extends GoEChargerBaseHandler {
             allChannels.forEach(channel -> updateState(channel, UnDefType.UNDEF));
         } else {
             updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE);
-            allChannels.forEach(channel -> updateState(channel, getValue(channel, (GoEStatusResponseDTO) goeResponse)));
+            allChannels.forEach(channel -> updateState(channel, getValue(channel, goeResponse)));
         }
     }
 }
index 027af1eaffe3f61472f2d5bab7a823b657f403f2..1056663cf6f9a0df24df39acf5d419b3728c22ed 100644 (file)
@@ -145,12 +145,14 @@ public class GoEChargerV2Handler extends GoEChargerBaseHandler {
             case ALLOW_CHARGING:
                 return goeResponse.allowCharging == true ? OnOffType.ON : OnOffType.OFF;
             case TEMPERATURE_TYPE2_PORT:
-                if (goeResponse.temperatures == null) {
+                // It was reported that the temperature is invalid when only one value is returned
+                // That's why it is checked that at least 2 values are returned
+                if (goeResponse.temperatures == null || goeResponse.temperatures.length < 2) {
                     return UnDefType.UNDEF;
                 }
                 return new QuantityType<>(goeResponse.temperatures[0], SIUnits.CELSIUS);
             case TEMPERATURE_CIRCUIT_BOARD:
-                if (goeResponse.temperatures == null) {
+                if (goeResponse.temperatures == null || goeResponse.temperatures.length < 2) {
                     return UnDefType.UNDEF;
                 }
                 return new QuantityType<>(goeResponse.temperatures[1], SIUnits.CELSIUS);
@@ -168,54 +170,54 @@ public class GoEChargerV2Handler extends GoEChargerBaseHandler {
                 if (goeResponse.totalChargeConsumption == null) {
                     return UnDefType.UNDEF;
                 }
-                return new QuantityType<>((Double) (goeResponse.totalChargeConsumption / 1000d), Units.KILOWATT_HOUR);
+                return new QuantityType<>(goeResponse.totalChargeConsumption / 1000d, Units.KILOWATT_HOUR);
             case VOLTAGE_L1:
-                if (goeResponse.energy == null) {
+                if (goeResponse.energy == null || goeResponse.energy.length < 1) {
                     return UnDefType.UNDEF;
                 }
                 return new QuantityType<>(goeResponse.energy[0], Units.VOLT);
             case VOLTAGE_L2:
-                if (goeResponse.energy == null) {
+                if (goeResponse.energy == null || goeResponse.energy.length < 2) {
                     return UnDefType.UNDEF;
                 }
                 return new QuantityType<>(goeResponse.energy[1], Units.VOLT);
             case VOLTAGE_L3:
-                if (goeResponse.energy == null) {
+                if (goeResponse.energy == null || goeResponse.energy.length < 3) {
                     return UnDefType.UNDEF;
                 }
                 return new QuantityType<>(goeResponse.energy[2], Units.VOLT);
             case CURRENT_L1:
-                if (goeResponse.energy == null) {
+                if (goeResponse.energy == null || goeResponse.energy.length < 5) {
                     return UnDefType.UNDEF;
                 }
                 return new QuantityType<>(goeResponse.energy[4], Units.AMPERE);
             case CURRENT_L2:
-                if (goeResponse.energy == null) {
+                if (goeResponse.energy == null || goeResponse.energy.length < 6) {
                     return UnDefType.UNDEF;
                 }
                 return new QuantityType<>(goeResponse.energy[5], Units.AMPERE);
             case CURRENT_L3:
-                if (goeResponse.energy == null) {
+                if (goeResponse.energy == null || goeResponse.energy.length < 7) {
                     return UnDefType.UNDEF;
                 }
                 return new QuantityType<>(goeResponse.energy[6], Units.AMPERE);
             case POWER_L1:
-                if (goeResponse.energy == null) {
+                if (goeResponse.energy == null || goeResponse.energy.length < 8) {
                     return UnDefType.UNDEF;
                 }
                 return new QuantityType<>(goeResponse.energy[7], Units.WATT);
             case POWER_L2:
-                if (goeResponse.energy == null) {
+                if (goeResponse.energy == null || goeResponse.energy.length < 9) {
                     return UnDefType.UNDEF;
                 }
                 return new QuantityType<>(goeResponse.energy[8], Units.WATT);
             case POWER_L3:
-                if (goeResponse.energy == null) {
+                if (goeResponse.energy == null || goeResponse.energy.length < 10) {
                     return UnDefType.UNDEF;
                 }
                 return new QuantityType<>(goeResponse.energy[9], Units.WATT);
             case POWER_ALL:
-                if (goeResponse.energy == null) {
+                if (goeResponse.energy == null || goeResponse.energy.length < 12) {
                     return UnDefType.UNDEF;
                 }
                 return new QuantityType<>(goeResponse.energy[11], Units.WATT);
@@ -374,14 +376,14 @@ public class GoEChargerV2Handler extends GoEChargerBaseHandler {
         return gson.fromJson(response, GoEStatusResponseV2DTO.class);
     }
 
+    @Override
     protected void updateChannelsAndStatus(@Nullable GoEStatusResponseBaseDTO goeResponse, @Nullable String message) {
         if (goeResponse == null) {
             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, message);
             allChannels.forEach(channel -> updateState(channel, UnDefType.UNDEF));
         } else {
             updateStatus(ThingStatus.ONLINE);
-            allChannels
-                    .forEach(channel -> updateState(channel, getValue(channel, (GoEStatusResponseV2DTO) goeResponse)));
+            allChannels.forEach(channel -> updateState(channel, getValue(channel, goeResponse)));
         }
     }
 }