]> git.basschouten.com Git - openhab-addons.git/commitdiff
[smartmeter] Prevent NumberFormatException (#16183)
authorlsiepel <leosiepel@gmail.com>
Sat, 27 Jan 2024 19:48:59 +0000 (20:48 +0100)
committerGitHub <noreply@github.com>
Sat, 27 Jan 2024 19:48:59 +0000 (20:48 +0100)
* Fix NumberFormatException
* Fix logger comment

---------

Signed-off-by: Leo Siepel <leosiepel@gmail.com>
bundles/org.openhab.binding.smartmeter/src/main/java/org/openhab/binding/smartmeter/internal/conformity/negate/NegateHandler.java
bundles/org.openhab.binding.smartmeter/src/test/java/org/openhab/binding/smartmeter/TestNegateBit.java

index 0ba18391d7a90771d11db7a091306b7ee7e7dd16..64393645f1b34645f729735e84b3f20f25d04ecd 100644 (file)
@@ -17,6 +17,8 @@ import java.util.function.Function;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.binding.smartmeter.internal.MeterValue;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Handles the Negate Bit property for a specific meter value.
@@ -26,6 +28,7 @@ import org.openhab.binding.smartmeter.internal.MeterValue;
  */
 @NonNullByDefault
 public class NegateHandler {
+    private static final Logger LOGGER = LoggerFactory.getLogger(NegateHandler.class);
 
     /**
      * Gets whether negation should be applied for the given <code>negateProperty</code> and the {@link MeterValue}
@@ -64,7 +67,13 @@ public class NegateHandler {
      * @return Whether the given bit is set or not
      */
     public static boolean isNegateSet(String value, int negatePosition) {
-        long longValue = Long.parseLong(value);
+        long longValue = 0;
+        try {
+            longValue = (long) Double.parseDouble(value);
+        } catch (NumberFormatException e) {
+            LOGGER.warn("Failed to parse value: {} when determining isNegateSet, assuming false", value);
+            return false;
+        }
         return (longValue & (1L << negatePosition)) != 0;
     }
 }
index 1b59dceb618e688beb1845f8cc554f96da8b37d4..4b6de5643c5cbbe65b557cd8df96909607a6dda6 100644 (file)
@@ -46,6 +46,16 @@ public class TestNegateBit {
         assertTrue(negateState);
     }
 
+    @Test
+    public void testNegateHandlingDecimalTrue() {
+        String negateProperty = "1-0_16-7-0:31:0";
+
+        boolean negateStateDot = NegateHandler.shouldNegateState(negateProperty,
+                obis -> new MeterValue<>(obis, "49.0", null));
+
+        assertTrue(negateStateDot);
+    }
+
     @Test
     public void testNegateHandlingFalse() {
         String negateProperty = "1-0_1-8-0:5:1";