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.homematic.internal.communicator.parser;
15 import java.io.IOException;
16 import java.util.Objects;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.openhab.binding.homematic.internal.misc.HomematicConstants;
20 import org.openhab.binding.homematic.internal.misc.MiscUtils;
21 import org.openhab.binding.homematic.internal.model.HmDatapoint;
22 import org.openhab.binding.homematic.internal.model.HmParamsetType;
23 import org.openhab.binding.homematic.internal.model.HmValueType;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Abstract base class for all parsers with common methods.
30 * @author Gerhard Riegler - Initial contribution
32 public abstract class CommonRpcParser<M, R> implements RpcParser<M, R> {
34 private final Logger logger = LoggerFactory.getLogger(CommonRpcParser.class);
37 * Converts the object to a string.
39 protected String toString(Object object) {
40 String value = Objects.toString(object, "").trim();
41 return value.isEmpty() ? null : value;
45 * Converts the object to an integer.
47 protected Integer toInteger(Object object) {
48 if (object == null || object instanceof Integer) {
49 return (Integer) object;
52 return Double.valueOf(object.toString()).intValue();
53 } catch (NumberFormatException ex) {
54 logger.debug("Failed converting {} to a Double", object, ex);
60 * Converts the object to a double.
62 protected Double toDouble(Object object) {
63 if (object == null || object instanceof Double) {
64 return (Double) object;
67 return Double.valueOf(object.toString());
68 } catch (NumberFormatException ex) {
69 logger.debug("Failed converting {} to a Double", object, ex);
75 * Converts the object to a number.
77 protected Number toNumber(Object object) {
78 if (object == null || object instanceof Number) {
79 return (Number) object;
82 String value = object.toString();
83 if (value.contains(".")) {
84 return Float.parseFloat(value);
86 return Integer.parseInt(value);
88 } catch (NumberFormatException ex) {
89 logger.debug("Failed converting {} to a Number", object, ex);
95 * Converts the object to a boolean.
97 protected Boolean toBoolean(Object object) {
98 if (object == null || object instanceof Boolean) {
99 return (Boolean) object;
101 return "true".equals(object.toString().toLowerCase());
105 * Converts the object to a string array.
107 protected String[] toOptionList(Object optionList) {
108 if (optionList != null && optionList instanceof Object[] vl) {
109 String[] stringArray = new String[vl.length];
110 for (int i = 0; i < vl.length; i++) {
111 stringArray[i] = vl[i].toString();
119 * Returns the address of a device, replacing group address identifier and illegal characters.
122 protected String getSanitizedAddress(Object object) {
123 String address = Objects.toString(object, "").trim().replaceFirst("\\*", "T-");
124 return MiscUtils.validateCharacters(address.isEmpty() ? null : address, "Address", "_");
128 * Adjust uninitialized rssi values to zero.
130 protected void adjustRssiValue(HmDatapoint dp) {
131 if (dp.getValue() != null && dp.getName().startsWith("RSSI_") && dp.isIntegerType()) {
132 int rssiValue = ((Number) dp.getValue()).intValue();
133 dp.setValue(getAdjustedRssiValue(rssiValue));
138 * Adjust a rssi value if it is out of range.
140 protected Integer getAdjustedRssiValue(Integer rssiValue) {
141 if (rssiValue == null || rssiValue >= 255 || rssiValue <= -255) {
148 * Converts the value to the correct type if necessary.
150 protected Object convertToType(HmDatapoint dp, Object value) {
153 } else if (dp.isBooleanType()) {
154 return toBoolean(value);
155 } else if (dp.isIntegerType()) {
156 return toInteger(value);
157 } else if (dp.isFloatType()) {
158 return toNumber(value);
159 } else if (dp.isStringType()) {
160 return toString(value);
167 * Assembles a datapoint with the given parameters.
169 protected HmDatapoint assembleDatapoint(String name, String unit, String type, String[] options, Object min,
170 Object max, Integer operations, Object defaultValue, HmParamsetType paramsetType, boolean isHmIpDevice)
172 HmDatapoint dp = new HmDatapoint();
174 dp.setDescription(name);
176 unit = unit.trim().replace("\ufffd", "°");
178 dp.setUnit(unit == null || unit.isEmpty() ? null : unit);
180 // Bypass: For several devices the CCU does not send a unit together with the value in the data point definition
181 if (dp.getUnit() == null && dp.getName() != null) {
182 if (dp.getName().startsWith("RSSI_")) {
184 } else if (dp.getName().startsWith(HomematicConstants.DATAPOINT_NAME_OPERATING_VOLTAGE)) {
186 } else if (HomematicConstants.DATAPOINT_NAME_HUMIDITY.equals(dp.getName())) {
188 } else if (HomematicConstants.DATAPOINT_NAME_LEVEL.equals(dp.getName())) {
193 HmValueType valueType = HmValueType.parse(type);
194 if (valueType == null || valueType == HmValueType.UNKNOWN) {
195 throw new IOException("Unknown datapoint type: " + type);
196 } else if (valueType == HmValueType.FLOAT && dp.getUnit() == null
197 && dp.getName().matches("\\w*_TEMPERATURE(_\\w.*|$)")) {
198 logger.debug("No unit information found for temperature datapoint {}, assuming Number:Temperature",
200 dp.setUnit("°C"); // Bypass for a problem with HMIP devices where unit of temperature channels is sometimes
203 dp.setType(valueType);
205 dp.setOptions(options);
206 if (dp.isNumberType() || dp.isEnumType()) {
207 if (isHmIpDevice && dp.isEnumType()) {
208 dp.setMinValue(dp.getOptionIndex(toString(min)));
209 dp.setMaxValue(dp.getOptionIndex(toString(max)));
211 dp.setMinValue(toNumber(min));
212 dp.setMaxValue(toNumber(max));
215 dp.setReadOnly((operations & 2) != 2);
216 dp.setReadable((operations & 1) == 1);
217 dp.setParamsetType(paramsetType);
218 if (isHmIpDevice && dp.isEnumType()) {
219 dp.setDefaultValue(dp.getOptionIndex(toString(defaultValue)));
221 dp.setDefaultValue(convertToType(dp, defaultValue));
223 dp.setValue(dp.getDefaultValue());
228 * Converts a string value to the type.
230 protected Object convertToType(String value) {
231 if (value == null || value.isBlank()) {
234 if ("true".equalsIgnoreCase(value) || "on".equalsIgnoreCase(value)) {
235 return (Boolean.TRUE);
236 } else if ("false".equalsIgnoreCase(value) || "off".equalsIgnoreCase(value)) {
237 return (Boolean.FALSE);
238 } else if (value.matches("(-|\\+)?[0-9]+")) {
239 return (Integer.valueOf(value));
240 } else if (value.matches("[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?")) {
241 return (Double.valueOf(value));