]> git.basschouten.com Git - openhab-addons.git/blob
2f2b3a5904b69c4a5738115f7693fbbc361a090b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.transform.javascript.internal.profiles;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.core.library.types.StringType;
17 import org.openhab.core.thing.profiles.ProfileCallback;
18 import org.openhab.core.thing.profiles.ProfileContext;
19 import org.openhab.core.thing.profiles.ProfileTypeUID;
20 import org.openhab.core.thing.profiles.StateProfile;
21 import org.openhab.core.transform.TransformationException;
22 import org.openhab.core.transform.TransformationHelper;
23 import org.openhab.core.transform.TransformationService;
24 import org.openhab.core.types.Command;
25 import org.openhab.core.types.State;
26 import org.openhab.core.types.Type;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Profile to offer the JavascriptTransformationservice on a ItemChannelLink
32  *
33  * @author Stefan Triller - initial contribution
34  *
35  */
36 @NonNullByDefault
37 public class JavascriptTransformationProfile implements StateProfile {
38
39     public static final ProfileTypeUID PROFILE_TYPE_UID = new ProfileTypeUID(
40             TransformationService.TRANSFORM_PROFILE_SCOPE, "JS");
41
42     private final Logger logger = LoggerFactory.getLogger(JavascriptTransformationProfile.class);
43
44     private final TransformationService service;
45     private final ProfileCallback callback;
46
47     private static final String FUNCTION_PARAM = "function";
48     private static final String SOURCE_FORMAT_PARAM = "sourceFormat";
49
50     @NonNullByDefault({})
51     private final String function;
52     @NonNullByDefault({})
53     private final String sourceFormat;
54
55     public JavascriptTransformationProfile(ProfileCallback callback, ProfileContext context,
56             TransformationService service) {
57         this.service = service;
58         this.callback = callback;
59
60         Object paramFunction = context.getConfiguration().get(FUNCTION_PARAM);
61         Object paramSource = context.getConfiguration().get(SOURCE_FORMAT_PARAM);
62
63         logger.debug("Profile configured with '{}'='{}', '{}'={}", FUNCTION_PARAM, paramFunction, SOURCE_FORMAT_PARAM,
64                 paramSource);
65         // SOURCE_FORMAT_PARAM is an advanced parameter and we assume "%s" if it is not set
66         if (paramSource == null) {
67             paramSource = "%s";
68         }
69         if (paramFunction instanceof String && paramSource instanceof String) {
70             function = (String) paramFunction;
71             sourceFormat = (String) paramSource;
72         } else {
73             logger.error("Parameter '{}' and '{}' have to be Strings. Profile will be inactive.", FUNCTION_PARAM,
74                     SOURCE_FORMAT_PARAM);
75             function = null;
76             sourceFormat = null;
77         }
78     }
79
80     @Override
81     public ProfileTypeUID getProfileTypeUID() {
82         return PROFILE_TYPE_UID;
83     }
84
85     @Override
86     public void onStateUpdateFromItem(State state) {
87         callback.handleUpdate(state);
88     }
89
90     @Override
91     public void onCommandFromItem(Command command) {
92         callback.handleCommand(command);
93     }
94
95     @Override
96     public void onCommandFromHandler(Command command) {
97         if (function == null || sourceFormat == null) {
98             logger.warn(
99                     "Please specify a function and a source format for this Profile in the '{}', and '{}' parameters. Returning the original command now.",
100                     FUNCTION_PARAM, SOURCE_FORMAT_PARAM);
101             callback.sendCommand(command);
102             return;
103         }
104         callback.sendCommand((Command) transformState(command));
105     }
106
107     @Override
108     public void onStateUpdateFromHandler(State state) {
109         if (function == null || sourceFormat == null) {
110             logger.warn(
111                     "Please specify a function and a source format for this Profile in the '{}' and '{}' parameters. Returning the original state now.",
112                     FUNCTION_PARAM, SOURCE_FORMAT_PARAM);
113             callback.sendUpdate(state);
114             return;
115         }
116         callback.sendUpdate((State) transformState(state));
117     }
118
119     private Type transformState(Type state) {
120         String result = state.toFullString();
121         try {
122             result = TransformationHelper.transform(service, function, sourceFormat, state.toFullString());
123         } catch (TransformationException e) {
124             logger.warn("Could not transform state '{}' with function '{}' and format '{}'", state, function,
125                     sourceFormat);
126         }
127         StringType resultType = new StringType(result);
128         logger.debug("Transformed '{}' into '{}'", state, resultType);
129         return resultType;
130     }
131 }