mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-10 15:11:59 +01:00
[modbus] Discard data if transformation failed (#17457)
Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au> Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
parent
53937d0c08
commit
cd35a2bd0f
@ -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);
|
||||
|
@ -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"));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user