2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.transform.javascript.internal.profiles;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.core.library.types.StringType;
18 import org.openhab.core.thing.profiles.ProfileCallback;
19 import org.openhab.core.thing.profiles.ProfileContext;
20 import org.openhab.core.thing.profiles.ProfileTypeUID;
21 import org.openhab.core.thing.profiles.StateProfile;
22 import org.openhab.core.transform.TransformationException;
23 import org.openhab.core.transform.TransformationHelper;
24 import org.openhab.core.transform.TransformationService;
25 import org.openhab.core.types.Command;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.Type;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Profile to offer the JavascriptTransformationservice on a ItemChannelLink
34 * @author Stefan Triller - Initial contribution
37 public class JavaScriptTransformationProfile implements StateProfile {
39 private final Logger logger = LoggerFactory.getLogger(JavaScriptTransformationProfile.class);
41 public static final ProfileTypeUID PROFILE_TYPE_UID = new ProfileTypeUID(
42 TransformationService.TRANSFORM_PROFILE_SCOPE, "JS");
44 private static final String FUNCTION_PARAM = "function";
45 private static final String SOURCE_FORMAT_PARAM = "sourceFormat";
47 private final TransformationService service;
48 private final ProfileCallback callback;
50 private final @Nullable String function;
51 private final @Nullable String sourceFormat;
53 public JavaScriptTransformationProfile(ProfileCallback callback, ProfileContext context,
54 TransformationService service) {
55 this.service = service;
56 this.callback = callback;
58 Object paramFunction = context.getConfiguration().get(FUNCTION_PARAM);
59 Object paramSource = context.getConfiguration().get(SOURCE_FORMAT_PARAM);
61 logger.debug("Profile configured with '{}'='{}', '{}'={}", FUNCTION_PARAM, paramFunction, SOURCE_FORMAT_PARAM,
63 // SOURCE_FORMAT_PARAM is an advanced parameter and we assume "%s" if it is not set
64 if (paramSource == null) {
67 if (paramFunction instanceof String && paramSource instanceof String) {
68 function = (String) paramFunction;
69 sourceFormat = (String) paramSource;
71 logger.error("Parameter '{}' and '{}' have to be Strings. Profile will be inactive.", FUNCTION_PARAM,
79 public ProfileTypeUID getProfileTypeUID() {
80 return PROFILE_TYPE_UID;
84 public void onStateUpdateFromItem(State state) {
88 public void onCommandFromItem(Command command) {
89 callback.handleCommand(command);
93 public void onCommandFromHandler(Command command) {
94 if (function == null || sourceFormat == null) {
96 "Please specify a function and a source format for this Profile in the '{}', and '{}' parameters. Returning the original command now.",
97 FUNCTION_PARAM, SOURCE_FORMAT_PARAM);
98 callback.sendCommand(command);
101 callback.sendCommand((Command) transformState(command));
105 public void onStateUpdateFromHandler(State state) {
106 if (function == null || sourceFormat == null) {
108 "Please specify a function and a source format for this Profile in the '{}' and '{}' parameters. Returning the original state now.",
109 FUNCTION_PARAM, SOURCE_FORMAT_PARAM);
110 callback.sendUpdate(state);
113 callback.sendUpdate((State) transformState(state));
116 private Type transformState(Type state) {
117 String localFunction = function, localSourceFormat = sourceFormat;
118 if (localFunction != null && localSourceFormat != null) {
119 String result = state.toFullString();
121 result = TransformationHelper.transform(service, localFunction, localSourceFormat, result);
122 } catch (TransformationException e) {
123 logger.warn("Could not transform state '{}' with function '{}' and format '{}'", state, function,
126 StringType resultType = new StringType(result);
127 logger.debug("Transformed '{}' into '{}'", state, resultType);
131 "Please specify a function and a source format for this Profile in the '{}' and '{}' parameters. Returning the original state now.",
132 FUNCTION_PARAM, SOURCE_FORMAT_PARAM);