2 * Copyright (c) 2010-2020 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.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;
30 * The {@link BsbLanParameterHandler} is responsible for updating the data, which are
31 * sent to one of the channels.
33 * @author Peter Schraffl - Initial contribution
36 public class BsbLanParameterConverter {
38 private static final Logger LOGGER = LoggerFactory.getLogger(BsbLanParameterConverter.class);
40 public static @Nullable State getState(String channelId, BsbLanApiParameterDTO parameter) {
42 case PARAMETER_CHANNEL_NAME:
43 return getStateForNameChannel(parameter);
45 case PARAMETER_CHANNEL_DESCRIPTION:
46 return getStateForDescriptionChannel(parameter);
48 case PARAMETER_CHANNEL_DATATYPE:
49 return getStateForDatatypeChannel(parameter);
51 case PARAMETER_CHANNEL_NUMBER_VALUE:
52 return getStateForNumberValueChannel(parameter);
54 case PARAMETER_CHANNEL_STRING_VALUE:
55 return getStateForStringValueChannel(parameter);
57 case PARAMETER_CHANNEL_SWITCH_VALUE:
58 return getStateForSwitchValueChannel(parameter);
60 case PARAMETER_CHANNEL_UNIT:
61 return getStateForUnitChannel(parameter);
64 LOGGER.debug("unsupported channel '{}' while updating state", channelId);
68 private static State getStateForNameChannel(BsbLanApiParameterDTO parameter) {
69 return new StringType(parameter.name);
72 private static State getStateForDescriptionChannel(BsbLanApiParameterDTO parameter) {
73 return new StringType(parameter.description);
76 private static State getStateForUnitChannel(BsbLanApiParameterDTO parameter) {
77 String value = StringEscapeUtils.unescapeHtml(parameter.unit);
78 return new StringType(value);
81 private static State getStateForDatatypeChannel(BsbLanApiParameterDTO parameter) {
82 int value = parameter.dataType.getValue();
83 return new DecimalType(value);
86 private static @Nullable State getStateForNumberValueChannel(BsbLanApiParameterDTO parameter) {
88 switch (parameter.dataType) {
89 // parse enum data type as integer
91 return new DecimalType(Integer.parseInt(parameter.value));
94 return new DecimalType(Double.parseDouble(parameter.value));
96 } catch (NumberFormatException e) {
97 // silently ignore - there is not "tryParse"
102 private static State getStateForStringValueChannel(BsbLanApiParameterDTO parameter) {
103 return new StringType(parameter.value);
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;
112 * Converts a Command back to a value which is sent to the BSB-LAN device afterwards.
116 * @return null if conversion fails or channel is readonly.
118 public static @Nullable String getValue(String channelId, Command command) {
120 case PARAMETER_CHANNEL_NUMBER_VALUE:
121 return getValueForNumberValueChannel(command);
123 case PARAMETER_CHANNEL_STRING_VALUE:
124 return getValueForStringValueChannel(command);
126 case PARAMETER_CHANNEL_SWITCH_VALUE:
127 return getValueForSwitchValueChannel(command);
130 LOGGER.debug("Channel '{}' is read only. Ignoring command", channelId);
135 private static @Nullable String getValueForNumberValueChannel(Command command) {
137 if (command.toString().matches("-?\\d+(\\.\\d+)?")) {
138 return command.toString();
140 LOGGER.warn("Command '{}' is not a valid number value", command);
144 private static String getValueForStringValueChannel(Command command) {
145 // special OnOffType handling
146 if (command.equals(OnOffType.ON)) {
148 } else if (command.equals(OnOffType.OFF)) {
151 return command.toString();
154 private static @Nullable String getValueForSwitchValueChannel(Command command) {
155 if (command.equals(OnOffType.ON)) {
157 } else if (command.equals(OnOffType.OFF)) {
160 LOGGER.warn("Command '{}' is not a valid switch value", command);