2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.bsblan.internal.helper;
15 import static org.openhab.binding.bsblan.internal.BsbLanBindingConstants.*;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.bsblan.internal.api.dto.BsbLanApiParameterDTO;
20 import org.openhab.core.library.types.DecimalType;
21 import org.openhab.core.library.types.OnOffType;
22 import org.openhab.core.library.types.QuantityType;
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 import org.unbescape.html.HtmlEscape;
31 * The {@link BsbLanParameterHandler} is responsible for updating the data, which are
32 * sent to one of the channels.
34 * @author Peter Schraffl - Initial contribution
37 public class BsbLanParameterConverter {
39 private static final Logger LOGGER = LoggerFactory.getLogger(BsbLanParameterConverter.class);
41 public static @Nullable State getState(String channelId, BsbLanApiParameterDTO parameter) {
43 case PARAMETER_CHANNEL_NAME:
44 return getStateForNameChannel(parameter);
46 case PARAMETER_CHANNEL_DESCRIPTION:
47 return getStateForDescriptionChannel(parameter);
49 case PARAMETER_CHANNEL_DATATYPE:
50 return getStateForDatatypeChannel(parameter);
52 case PARAMETER_CHANNEL_NUMBER_VALUE:
53 return getStateForNumberValueChannel(parameter);
55 case PARAMETER_CHANNEL_STRING_VALUE:
56 return getStateForStringValueChannel(parameter);
58 case PARAMETER_CHANNEL_SWITCH_VALUE:
59 return getStateForSwitchValueChannel(parameter);
61 case PARAMETER_CHANNEL_UNIT:
62 return getStateForUnitChannel(parameter);
65 LOGGER.debug("unsupported channel '{}' while updating state", channelId);
69 private static State getStateForNameChannel(BsbLanApiParameterDTO parameter) {
70 return new StringType(parameter.name);
73 private static State getStateForDescriptionChannel(BsbLanApiParameterDTO parameter) {
74 return new StringType(parameter.description);
77 private static State getStateForUnitChannel(BsbLanApiParameterDTO parameter) {
78 String value = HtmlEscape.unescapeHtml(parameter.unit);
79 return new StringType(value);
82 private static State getStateForDatatypeChannel(BsbLanApiParameterDTO parameter) {
83 int value = parameter.dataType.getValue();
84 return new DecimalType(value);
87 private static @Nullable State getStateForNumberValueChannel(BsbLanApiParameterDTO parameter) {
89 switch (parameter.dataType) {
90 // parse enum data type as integer
92 return new DecimalType(Integer.parseInt(parameter.value));
95 return new DecimalType(Double.parseDouble(parameter.value));
97 } catch (NumberFormatException e) {
98 // silently ignore - there is not "tryParse"
103 private static State getStateForStringValueChannel(BsbLanApiParameterDTO parameter) {
104 return new StringType(parameter.value);
107 private static State getStateForSwitchValueChannel(BsbLanApiParameterDTO parameter) {
108 // treat "0" as OFF and everything else as ON
109 return "0".equals(parameter.value) ? OnOffType.OFF : OnOffType.ON;
113 * Converts a Command back to a value which is sent to the BSB-LAN device afterwards.
117 * @return null if conversion fails or channel is readonly.
119 public static @Nullable String getValue(String channelId, Command command) {
121 case PARAMETER_CHANNEL_NUMBER_VALUE:
122 return getValueForNumberValueChannel(command);
124 case PARAMETER_CHANNEL_STRING_VALUE:
125 return getValueForStringValueChannel(command);
127 case PARAMETER_CHANNEL_SWITCH_VALUE:
128 return getValueForSwitchValueChannel(command);
131 LOGGER.debug("Channel '{}' is read only. Ignoring command", channelId);
136 private static @Nullable String getValueForNumberValueChannel(Command command) {
137 if (command instanceof QuantityType<?> quantity) {
138 return String.valueOf(quantity.doubleValue());
141 else if (command.toString().matches("-?\\d+(\\.\\d+)?")) {
142 return command.toString();
144 LOGGER.warn("Command '{}' is not a valid number value", command);
148 private static String getValueForStringValueChannel(Command command) {
149 // special OnOffType handling
150 if (command.equals(OnOffType.ON)) {
152 } else if (command.equals(OnOffType.OFF)) {
155 return command.toString();
158 private static @Nullable String getValueForSwitchValueChannel(Command command) {
159 if (command.equals(OnOffType.ON)) {
161 } else if (command.equals(OnOffType.OFF)) {
164 LOGGER.warn("Command '{}' is not a valid switch value", command);