From 839f3a9c36998e5197cbb955069cd768ccd01f5c Mon Sep 17 00:00:00 2001 From: Christoph Weitkamp Date: Sun, 3 Jan 2021 14:56:39 +0100 Subject: [PATCH] Applied comment from review Signed-off-by: Christoph Weitkamp --- .../JavaScriptTransformationProfile.java | 41 +++++++++++-------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/bundles/org.openhab.transform.javascript/src/main/java/org/openhab/transform/javascript/internal/profiles/JavaScriptTransformationProfile.java b/bundles/org.openhab.transform.javascript/src/main/java/org/openhab/transform/javascript/internal/profiles/JavaScriptTransformationProfile.java index f64cffcfe6a3c..9cfc79f848c30 100644 --- a/bundles/org.openhab.transform.javascript/src/main/java/org/openhab/transform/javascript/internal/profiles/JavaScriptTransformationProfile.java +++ b/bundles/org.openhab.transform.javascript/src/main/java/org/openhab/transform/javascript/internal/profiles/JavaScriptTransformationProfile.java @@ -13,6 +13,7 @@ package org.openhab.transform.javascript.internal.profiles; import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.library.types.StringType; import org.openhab.core.thing.profiles.ProfileCallback; import org.openhab.core.thing.profiles.ProfileContext; @@ -35,21 +36,19 @@ @NonNullByDefault public class JavaScriptTransformationProfile implements StateProfile { + private final Logger logger = LoggerFactory.getLogger(JavaScriptTransformationProfile.class); + public static final ProfileTypeUID PROFILE_TYPE_UID = new ProfileTypeUID( TransformationService.TRANSFORM_PROFILE_SCOPE, "JS"); - private final Logger logger = LoggerFactory.getLogger(JavaScriptTransformationProfile.class); + private static final String FUNCTION_PARAM = "function"; + private static final String SOURCE_FORMAT_PARAM = "sourceFormat"; private final TransformationService service; private final ProfileCallback callback; - private static final String FUNCTION_PARAM = "function"; - private static final String SOURCE_FORMAT_PARAM = "sourceFormat"; - - @NonNullByDefault({}) - private final String function; - @NonNullByDefault({}) - private final String sourceFormat; + private final @Nullable String function; + private final @Nullable String sourceFormat; public JavaScriptTransformationProfile(ProfileCallback callback, ProfileContext context, TransformationService service) { @@ -115,15 +114,23 @@ public void onStateUpdateFromHandler(State state) { } private Type transformState(Type state) { - String result = state.toFullString(); - try { - result = TransformationHelper.transform(service, function, sourceFormat, state.toFullString()); - } catch (TransformationException e) { - logger.warn("Could not transform state '{}' with function '{}' and format '{}'", state, function, - sourceFormat); + String localFunction = function, localSourceFormat = sourceFormat; + if (localFunction != null && localSourceFormat != null) { + String result = state.toFullString(); + try { + result = TransformationHelper.transform(service, localFunction, localSourceFormat, result); + } catch (TransformationException e) { + logger.warn("Could not transform state '{}' with function '{}' and format '{}'", state, function, + sourceFormat); + } + StringType resultType = new StringType(result); + logger.debug("Transformed '{}' into '{}'", state, resultType); + return resultType; + } else { + logger.warn( + "Please specify a function and a source format for this Profile in the '{}' and '{}' parameters. Returning the original state now.", + FUNCTION_PARAM, SOURCE_FORMAT_PARAM); + return state; } - StringType resultType = new StringType(result); - logger.debug("Transformed '{}' into '{}'", state, resultType); - return resultType; } }