From d44d84eb4359b5cc5038104a8487bc92faf82672 Mon Sep 17 00:00:00 2001 From: jimtng <2554958+jimtng@users.noreply.github.com> Date: Fri, 27 Sep 2024 05:28:38 +1000 Subject: [PATCH] [modbus] Discard data if transformation failed (#17457) Signed-off-by: Jimmy Tanagra --- .../modbus/internal/ModbusTransformation.java | 13 +++++++++++-- .../modbus/internal/ModbusTransformationTest.java | 7 +++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/internal/ModbusTransformation.java b/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/internal/ModbusTransformation.java index 9c0050f60a..10181ce7c6 100644 --- a/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/internal/ModbusTransformation.java +++ b/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/internal/ModbusTransformation.java @@ -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); diff --git a/bundles/org.openhab.binding.modbus/src/test/java/org/openhab/binding/modbus/internal/ModbusTransformationTest.java b/bundles/org.openhab.binding.modbus/src/test/java/org/openhab/binding/modbus/internal/ModbusTransformationTest.java index 30480e39e7..e59f457f54 100644 --- a/bundles/org.openhab.binding.modbus/src/test/java/org/openhab/binding/modbus/internal/ModbusTransformationTest.java +++ b/bundles/org.openhab.binding.modbus/src/test/java/org/openhab/binding/modbus/internal/ModbusTransformationTest.java @@ -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")); + } } -- 2.47.3