]> git.basschouten.com Git - openhab-addons.git/blob
a0d285a3e3348ba980cdd16c77979372d540242e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.binding.bsblan.internal.helper;
14
15 import static org.openhab.binding.bsblan.internal.BsbLanBindingConstants.*;
16
17 import org.apache.commons.lang.StringEscapeUtils;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.bsblan.internal.api.dto.BsbLanApiParameterDTO;
21 import org.openhab.core.library.types.DecimalType;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.library.types.StringType;
24 import org.openhab.core.types.Command;
25 import org.openhab.core.types.State;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * The {@link BsbLanParameterHandler} is responsible for updating the data, which are
31  * sent to one of the channels.
32  *
33  * @author Peter Schraffl - Initial contribution
34  */
35 @NonNullByDefault
36 public class BsbLanParameterConverter {
37
38     private static final Logger LOGGER = LoggerFactory.getLogger(BsbLanParameterConverter.class);
39
40     public static @Nullable State getState(String channelId, BsbLanApiParameterDTO parameter) {
41         switch (channelId) {
42             case PARAMETER_CHANNEL_NAME:
43                 return getStateForNameChannel(parameter);
44
45             case PARAMETER_CHANNEL_DESCRIPTION:
46                 return getStateForDescriptionChannel(parameter);
47
48             case PARAMETER_CHANNEL_DATATYPE:
49                 return getStateForDatatypeChannel(parameter);
50
51             case PARAMETER_CHANNEL_NUMBER_VALUE:
52                 return getStateForNumberValueChannel(parameter);
53
54             case PARAMETER_CHANNEL_STRING_VALUE:
55                 return getStateForStringValueChannel(parameter);
56
57             case PARAMETER_CHANNEL_SWITCH_VALUE:
58                 return getStateForSwitchValueChannel(parameter);
59
60             case PARAMETER_CHANNEL_UNIT:
61                 return getStateForUnitChannel(parameter);
62         }
63
64         LOGGER.debug("unsupported channel '{}' while updating state", channelId);
65         return null;
66     }
67
68     private static State getStateForNameChannel(BsbLanApiParameterDTO parameter) {
69         return new StringType(parameter.name);
70     }
71
72     private static State getStateForDescriptionChannel(BsbLanApiParameterDTO parameter) {
73         return new StringType(parameter.description);
74     }
75
76     private static State getStateForUnitChannel(BsbLanApiParameterDTO parameter) {
77         String value = StringEscapeUtils.unescapeHtml(parameter.unit);
78         return new StringType(value);
79     }
80
81     private static State getStateForDatatypeChannel(BsbLanApiParameterDTO parameter) {
82         int value = parameter.dataType.getValue();
83         return new DecimalType(value);
84     }
85
86     private static @Nullable State getStateForNumberValueChannel(BsbLanApiParameterDTO parameter) {
87         try {
88             switch (parameter.dataType) {
89                 // parse enum data type as integer
90                 case DT_ENUM:
91                     return new DecimalType(Integer.parseInt(parameter.value));
92
93                 default:
94                     return new DecimalType(Double.parseDouble(parameter.value));
95             }
96         } catch (NumberFormatException e) {
97             // silently ignore - there is not "tryParse"
98         }
99         return null;
100     }
101
102     private static State getStateForStringValueChannel(BsbLanApiParameterDTO parameter) {
103         return new StringType(parameter.value);
104     }
105
106     private static State getStateForSwitchValueChannel(BsbLanApiParameterDTO parameter) {
107         // treat "0" as OFF and everything else as ON
108         return parameter.value.equals("0") ? OnOffType.OFF : OnOffType.ON;
109     }
110
111     /**
112      * Converts a Command back to a value which is sent to the BSB-LAN device afterwards.
113      * 
114      * @param channelId
115      * @param command
116      * @return null if conversion fails or channel is readonly.
117      */
118     public static @Nullable String getValue(String channelId, Command command) {
119         switch (channelId) {
120             case PARAMETER_CHANNEL_NUMBER_VALUE:
121                 return getValueForNumberValueChannel(command);
122
123             case PARAMETER_CHANNEL_STRING_VALUE:
124                 return getValueForStringValueChannel(command);
125
126             case PARAMETER_CHANNEL_SWITCH_VALUE:
127                 return getValueForSwitchValueChannel(command);
128
129             default:
130                 LOGGER.debug("Channel '{}' is read only. Ignoring command", channelId);
131                 return null;
132         }
133     }
134
135     private static @Nullable String getValueForNumberValueChannel(Command command) {
136         // check if numeric
137         if (command.toString().matches("-?\\d+(\\.\\d+)?")) {
138             return command.toString();
139         }
140         LOGGER.warn("Command '{}' is not a valid number value", command);
141         return null;
142     }
143
144     private static String getValueForStringValueChannel(Command command) {
145         // special OnOffType handling
146         if (command.equals(OnOffType.ON)) {
147             return "1";
148         } else if (command.equals(OnOffType.OFF)) {
149             return "0";
150         }
151         return command.toString();
152     }
153
154     private static @Nullable String getValueForSwitchValueChannel(Command command) {
155         if (command.equals(OnOffType.ON)) {
156             return "1";
157         } else if (command.equals(OnOffType.OFF)) {
158             return "0";
159         }
160         LOGGER.warn("Command '{}' is not a valid switch value", command);
161         return null;
162     }
163 }