]> git.basschouten.com Git - openhab-addons.git/blob
c8f4cdc3c7bf28d0c0141a83d5a365ae5ffdb25a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.jinja.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 JinjaTransformationservice on an ItemChannelLink
32  *
33  * @author Jochen Klein - initial contribution
34  *
35  */
36 @NonNullByDefault
37 public class JinjaTransformationProfile implements StateProfile {
38
39     public static final ProfileTypeUID PROFILE_TYPE_UID = new ProfileTypeUID(
40             TransformationService.TRANSFORM_PROFILE_SCOPE, "JINJA");
41
42     private final Logger logger = LoggerFactory.getLogger(JinjaTransformationProfile.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 JinjaTransformationProfile(ProfileCallback callback, ProfileContext context, TransformationService service) {
56         this.service = service;
57         this.callback = callback;
58
59         Object paramFunction = context.getConfiguration().get(FUNCTION_PARAM);
60         Object paramSource = context.getConfiguration().get(SOURCE_FORMAT_PARAM);
61
62         logger.debug("Profile configured with '{}'='{}', '{}'={}", FUNCTION_PARAM, paramFunction, SOURCE_FORMAT_PARAM,
63                 paramSource);
64         // SOURCE_FORMAT_PARAM is an advanced parameter and we assume "%s" if it is not set
65         if (paramSource == null) {
66             paramSource = "%s";
67         }
68         if (paramFunction instanceof String && paramSource instanceof String) {
69             function = (String) paramFunction;
70             sourceFormat = (String) paramSource;
71         } else {
72             logger.error("Parameter '{}' and '{}' have to be Strings. Profile will be inactive.", FUNCTION_PARAM,
73                     SOURCE_FORMAT_PARAM);
74             function = null;
75             sourceFormat = null;
76         }
77     }
78
79     @Override
80     public ProfileTypeUID getProfileTypeUID() {
81         return PROFILE_TYPE_UID;
82     }
83
84     @Override
85     public void onStateUpdateFromItem(State state) {
86     }
87
88     @Override
89     public void onCommandFromItem(Command command) {
90         callback.handleCommand(command);
91     }
92
93     @Override
94     public void onCommandFromHandler(Command command) {
95         if (function == null || sourceFormat == null) {
96             logger.warn(
97                     "Please specify a function and a source format for this Profile in the '{}', and '{}' parameters. Returning the original command now.",
98                     FUNCTION_PARAM, SOURCE_FORMAT_PARAM);
99             callback.sendCommand(command);
100             return;
101         }
102         callback.sendCommand((Command) transformState(command));
103     }
104
105     @Override
106     public void onStateUpdateFromHandler(State state) {
107         if (function == null || sourceFormat == null) {
108             logger.warn(
109                     "Please specify a function and a source format for this Profile in the '{}' and '{}' parameters. Returning the original state now.",
110                     FUNCTION_PARAM, SOURCE_FORMAT_PARAM);
111             callback.sendUpdate(state);
112             return;
113         }
114         callback.sendUpdate((State) transformState(state));
115     }
116
117     private Type transformState(Type state) {
118         String result = state.toFullString();
119         try {
120             result = TransformationHelper.transform(service, function, sourceFormat, state.toFullString());
121         } catch (TransformationException e) {
122             logger.warn("Could not transform state '{}' with function '{}' and format '{}'", state, function,
123                     sourceFormat);
124         }
125         StringType resultType = new StringType(result);
126         logger.debug("Transformed '{}' into '{}'", state, resultType);
127         return resultType;
128     }
129 }