]> git.basschouten.com Git - openhab-addons.git/blob
6e14d78e1f87c7383090e19f086a87036b56d9f9
[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.exec.internal.profiles;
14
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;
30
31 /**
32  * Profile to offer the ExecTransformationservice on a ItemChannelLink
33  *
34  * @author Stefan Triller - initial contribution
35  */
36 @NonNullByDefault
37 public class ExecTransformationProfile implements StateProfile {
38
39     public static final ProfileTypeUID PROFILE_TYPE_UID = new ProfileTypeUID(
40             TransformationService.TRANSFORM_PROFILE_SCOPE, "EXEC");
41
42     private final Logger logger = LoggerFactory.getLogger(ExecTransformationProfile.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     private @Nullable String function;
51     private @Nullable String sourceFormat;
52
53     public ExecTransformationProfile(ProfileCallback callback, ProfileContext context, TransformationService service) {
54         this.service = service;
55         this.callback = callback;
56
57         Object paramFunction = context.getConfiguration().get(FUNCTION_PARAM);
58         Object paramSource = context.getConfiguration().get(SOURCE_FORMAT_PARAM);
59
60         logger.debug("Profile configured with '{}'='{}', '{}'={}", FUNCTION_PARAM, paramFunction, SOURCE_FORMAT_PARAM,
61                 paramSource);
62
63         // SOURCE_FORMAT_PARAM is an advanced parameter and we assume "%s" if it is not set
64         if (paramSource == null) {
65             paramSource = "%s";
66         }
67         if (paramFunction instanceof String && paramSource instanceof String) {
68             function = (String) paramFunction;
69             sourceFormat = (String) paramSource;
70         } else {
71             logger.error("Parameter '{}' and '{}' have to be Strings. Profile will be inactive.", FUNCTION_PARAM,
72                     SOURCE_FORMAT_PARAM);
73             function = null;
74             sourceFormat = null;
75         }
76     }
77
78     @Override
79     public ProfileTypeUID getProfileTypeUID() {
80         return PROFILE_TYPE_UID;
81     }
82
83     @Override
84     public void onStateUpdateFromItem(State state) {
85         callback.handleUpdate(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         String function = this.function;
120         String sourceFormat = this.sourceFormat;
121
122         if (function == null || sourceFormat == null) {
123             logger.warn("Could not transform state '{}' with function '{}' and format '{}'", state, function,
124                     sourceFormat);
125         } else {
126             try {
127                 result = TransformationHelper.transform(service, function, sourceFormat, state.toFullString());
128             } catch (TransformationException e) {
129                 logger.warn("Could not transform state '{}' with function '{}' and format '{}'", state, function,
130                         sourceFormat);
131             }
132         }
133         StringType resultType = new StringType(result);
134         logger.debug("Transformed '{}' into '{}'", state, resultType);
135         return resultType;
136     }
137 }