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