Skip to content

Commit

Permalink
Applied comment from review
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Weitkamp <[email protected]>
  • Loading branch information
cweitkamp committed Jan 3, 2021
1 parent fe38df1 commit 8b028d3
Showing 1 changed file with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 8b028d3

Please sign in to comment.