]> git.basschouten.com Git - openhab-addons.git/commitdiff
[modbus] Discard data if transformation failed (#17457)
authorjimtng <2554958+jimtng@users.noreply.github.com>
Thu, 26 Sep 2024 19:28:38 +0000 (05:28 +1000)
committerGitHub <noreply@github.com>
Thu, 26 Sep 2024 19:28:38 +0000 (21:28 +0200)
Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/internal/ModbusTransformation.java
bundles/org.openhab.binding.modbus/src/test/java/org/openhab/binding/modbus/internal/ModbusTransformationTest.java

index 9c0050f60a4689bff385e3b769b69c0591a46715..10181ce7c67e0c386f07b3a74b9b4461204a4ada 100644 (file)
@@ -111,10 +111,19 @@ public class ModbusTransformation {
         }
     }
 
+    /**
+     * Transform the given value using the configured transformations.
+     * 
+     * @param value the value to transform
+     * @return the transformed value. If the transformation failed, return a blank string.
+     *         This could happen in one of these situations:
+     *         - The transformation service is not available.
+     *         - An error occurred when performing transformations.
+     *         - The transformation service intentionally returned null.
+     */
     public String transform(String value) {
         if (transformation != null) {
-            // return input if transformation failed
-            return Objects.requireNonNull(transformation.apply(value).orElse(value));
+            return Objects.requireNonNull(transformation.apply(value).orElse(""));
         }
 
         return Objects.requireNonNullElse(constantOutput, value);
index 30480e39e7ec78f9f79e6efca9ecb7835ccbbd04..e59f457f5493d6274bc00e36168539f56d01c124 100644 (file)
@@ -51,4 +51,11 @@ public class ModbusTransformationTest {
         assertFalse(transformation.isIdentityTransform());
         assertEquals("constant", transformation.transform("xx"));
     }
+
+    @Test
+    public void testTransformationFailed() {
+        ModbusTransformation transformation = new ModbusTransformation(List.of("NONEXISTENT(test)"));
+        assertFalse(transformation.isIdentityTransform());
+        assertEquals("", transformation.transform("xx"));
+    }
 }