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