]> git.basschouten.com Git - openhab-addons.git/blob
31af32e6094d80b71fb166b046a90033bb5f38c4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.map.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 MapTransformationservice on an ItemChannelLink
32  *
33  * @author Stefan Triller - Initial contribution
34  */
35 @NonNullByDefault
36 public class MapTransformationProfile implements StateProfile {
37
38     public static final ProfileTypeUID PROFILE_TYPE_UID = new ProfileTypeUID(
39             TransformationService.TRANSFORM_PROFILE_SCOPE, "MAP");
40
41     private final Logger logger = LoggerFactory.getLogger(MapTransformationProfile.class);
42
43     private final TransformationService service;
44     private final ProfileCallback callback;
45
46     private static final String FUNCTION_PARAM = "function";
47     private static final String SOURCE_FORMAT_PARAM = "sourceFormat";
48
49     @NonNullByDefault({})
50     private final String function;
51     @NonNullByDefault({})
52     private final String sourceFormat;
53
54     public MapTransformationProfile(ProfileCallback callback, ProfileContext context, TransformationService service) {
55         this.service = service;
56         this.callback = callback;
57
58         Object paramFunction = context.getConfiguration().get(FUNCTION_PARAM);
59         Object paramSource = context.getConfiguration().get(SOURCE_FORMAT_PARAM);
60
61         logger.debug("Profile configured with '{}'='{}', '{}'={}", FUNCTION_PARAM, paramFunction, SOURCE_FORMAT_PARAM,
62                 paramSource);
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 pFunction && paramSource instanceof String pFormat) {
68             function = pFunction;
69             sourceFormat = pFormat;
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     }
86
87     @Override
88     public void onCommandFromItem(Command command) {
89         callback.handleCommand(command);
90     }
91
92     @Override
93     public void onCommandFromHandler(Command command) {
94         if (function == null || sourceFormat == null) {
95             logger.warn(
96                     "Please specify a function and a source format for this Profile in the '{}', and '{}' parameters, e.g \"translation.map\"  and \"%s\". Returning the original command now.",
97                     FUNCTION_PARAM, SOURCE_FORMAT_PARAM);
98             callback.sendCommand(command);
99             return;
100         }
101         callback.sendCommand((Command) transformState(command));
102     }
103
104     @Override
105     public void onStateUpdateFromHandler(State state) {
106         if (function == null || sourceFormat == null) {
107             logger.warn(
108                     "Please specify a function and a source format for this Profile in the '{}' and '{}' parameters, e.g \"translation.map\" and \"%s\". Returning the original state now.",
109                     FUNCTION_PARAM, SOURCE_FORMAT_PARAM);
110             callback.sendUpdate(state);
111             return;
112         }
113         callback.sendUpdate((State) transformState(state));
114     }
115
116     private Type transformState(Type state) {
117         String result = state.toFullString();
118         try {
119             result = TransformationHelper.transform(service, function, sourceFormat, state.toFullString());
120             if (result != null && result.isEmpty()) {
121                 // map transformation service returns an empty string if the entry is not found in the map, we will use
122                 // the original value
123                 result = state.toFullString();
124             }
125         } catch (TransformationException e) {
126             logger.warn("Could not transform state '{}' with function '{}' and format '{}'", state, function,
127                     sourceFormat);
128         }
129         StringType resultType = new StringType(result);
130         logger.debug("Transformed '{}' into '{}'", state, resultType);
131         return resultType;
132     }
133 }