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.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;
32 * The {@link BsbLanParameterHandler} is responsible for updating the data, which are
33 * sent to one of the channels.
35 * @author Peter Schraffl - Initial contribution
38 public class BsbLanParameterConverter {
40 private static final Logger LOGGER = LoggerFactory.getLogger(BsbLanParameterConverter.class);
42 public static @Nullable State getState(String channelId, BsbLanApiParameterDTO parameter) {
44 case PARAMETER_CHANNEL_NAME:
45 return getStateForNameChannel(parameter);
47 case PARAMETER_CHANNEL_DESCRIPTION:
48 return getStateForDescriptionChannel(parameter);
50 case PARAMETER_CHANNEL_DATATYPE:
51 return getStateForDatatypeChannel(parameter);
53 case PARAMETER_CHANNEL_NUMBER_VALUE:
54 return getStateForNumberValueChannel(parameter);
56 case PARAMETER_CHANNEL_STRING_VALUE:
57 return getStateForStringValueChannel(parameter);
59 case PARAMETER_CHANNEL_SWITCH_VALUE:
60 return getStateForSwitchValueChannel(parameter);
62 case PARAMETER_CHANNEL_UNIT:
63 return getStateForUnitChannel(parameter);
66 LOGGER.debug("unsupported channel '{}' while updating state", channelId);
70 private static State getStateForNameChannel(BsbLanApiParameterDTO parameter) {
71 return new StringType(parameter.name);
74 private static State getStateForDescriptionChannel(BsbLanApiParameterDTO parameter) {
75 return new StringType(parameter.description);
78 private static State getStateForUnitChannel(BsbLanApiParameterDTO parameter) {
79 String value = StringEscapeUtils.unescapeHtml4(parameter.unit);
80 return new StringType(value);
83 private static State getStateForDatatypeChannel(BsbLanApiParameterDTO parameter) {
84 int value = parameter.dataType.getValue();
85 return new DecimalType(value);
88 private static @Nullable State getStateForNumberValueChannel(BsbLanApiParameterDTO parameter) {
90 switch (parameter.dataType) {
91 // parse enum data type as integer
93 return new DecimalType(Integer.parseInt(parameter.value));
96 return new DecimalType(Double.parseDouble(parameter.value));
98 } catch (NumberFormatException e) {
99 // silently ignore - there is not "tryParse"
104 private static State getStateForStringValueChannel(BsbLanApiParameterDTO parameter) {
105 return new StringType(parameter.value);
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;
114 * Converts a Command back to a value which is sent to the BSB-LAN device afterwards.
118 * @return null if conversion fails or channel is readonly.
120 public static @Nullable String getValue(String channelId, Command command) {
122 case PARAMETER_CHANNEL_NUMBER_VALUE:
123 return getValueForNumberValueChannel(command);
125 case PARAMETER_CHANNEL_STRING_VALUE:
126 return getValueForStringValueChannel(command);
128 case PARAMETER_CHANNEL_SWITCH_VALUE:
129 return getValueForSwitchValueChannel(command);
132 LOGGER.debug("Channel '{}' is read only. Ignoring command", channelId);
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());
144 else if (command.toString().matches("-?\\d+(\\.\\d+)?")) {
145 return command.toString();
147 LOGGER.warn("Command '{}' is not a valid number value", command);
151 private static String getValueForStringValueChannel(Command command) {
152 // special OnOffType handling
153 if (command.equals(OnOffType.ON)) {
155 } else if (command.equals(OnOffType.OFF)) {
158 return command.toString();
161 private static @Nullable String getValueForSwitchValueChannel(Command command) {
162 if (command.equals(OnOffType.ON)) {
164 } else if (command.equals(OnOffType.OFF)) {
167 LOGGER.warn("Command '{}' is not a valid switch value", command);