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.types.State;
21 * Abstract class to convert binary hex values into openHAB states and vice
24 * @author Holger Hees - Initial Contribution
25 * @author Hans Böhm - Refactoring
28 public interface ComfoAirDataType {
30 * Generate an openHAB State object based on response data.
34 * @return converted State object
36 State convertToState(int[] response, ComfoAirCommandType commandType);
39 * Generate byte array based on an openHAB State.
43 * @return converted byte array
45 int @Nullable [] convertFromState(State value, ComfoAirCommandType commandType);
47 default int calculateNumberValue(int[] data, ComfoAirCommandType commandType) {
48 int[] readReplyDataPos = commandType.getReadReplyDataPos();
49 int readReplyDataBits = commandType.getReadReplyDataBits();
52 if (readReplyDataPos != null) {
53 if (readReplyDataBits == 0) {
56 for (int i = readReplyDataPos.length - 1; i >= 0; i--) {
57 if (readReplyDataPos[i] < data.length) {
58 value += data[readReplyDataPos[i]] << base;
65 value = (data[readReplyDataPos[0]] & readReplyDataBits) == readReplyDataBits ? 1 : 0;
73 default String calculateStringValue(int[] data, ComfoAirCommandType commandType) {
74 int[] readReplyDataPos = commandType.getReadReplyDataPos();
75 StringBuilder value = new StringBuilder();
76 if (readReplyDataPos != null) {
77 for (int pos : readReplyDataPos) {
78 if (pos < data.length) {
79 value.append((char) data[pos]);
83 return value.toString();