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.comfoair.internal.datatypes;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.comfoair.internal.ComfoAirCommandType;
18 import org.openhab.core.library.types.DecimalType;
19 import org.openhab.core.types.State;
20 import org.openhab.core.types.UnDefType;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * Class to handle numeric values
27 * @author Holger Hees - Initial Contribution
28 * @author Hans Böhm - Refactoring
31 public class DataTypeNumber implements ComfoAirDataType {
32 private static final DataTypeNumber SINGLETON_INSTANCE = new DataTypeNumber();
34 private DataTypeNumber() {
37 private final Logger logger = LoggerFactory.getLogger(DataTypeNumber.class);
39 public static DataTypeNumber getInstance() {
40 return SINGLETON_INSTANCE;
44 public State convertToState(int @Nullable [] data, ComfoAirCommandType commandType) {
46 logger.trace("\"DataTypeNumber\" class \"convertToState\" method parameter: null");
47 return UnDefType.NULL;
49 int value = calculateNumberValue(data, commandType);
52 return UnDefType.NULL;
55 int[] possibleValues = commandType.getPossibleValues();
56 if (possibleValues != null) {
57 // fix for unexpected value for "level" value. got a 0x33. valid was
58 // the 0x03. 0x30 was to much.
59 // send DATA: 07 f0 00 cd 00 7a 07 0f
60 // receive CMD: ce DATA: 0f 20 32 00 0f 21 33 2d 33 03 01 5a 5b 00
61 for (int possibleValue : possibleValues) {
62 if ((value & possibleValue) == possibleValue) {
63 return new DecimalType(value);
66 return UnDefType.NULL;
68 return new DecimalType(value);
73 public int @Nullable [] convertFromState(State value, ComfoAirCommandType commandType) {
74 if (value instanceof UnDefType) {
75 logger.trace("\"DataTypeNumber\" class \"convertFromState\" undefined state");
78 int[] template = commandType.getChangeDataTemplate();
79 int[] possibleValues = commandType.getPossibleValues();
80 int position = commandType.getChangeDataPos();
82 int intValue = ((DecimalType) value).intValue();
84 if (possibleValues == null) {
85 template[position] = intValue;
87 for (int i = 0; i < possibleValues.length; i++) {
88 if (possibleValues[i] == intValue) {
89 template[position] = intValue;