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.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 a 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 a 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();
50 if (readReplyDataPos != null) {
53 for (int i = readReplyDataPos.length - 1; i >= 0; i--) {
54 if (readReplyDataPos[i] < data.length) {
55 value += data[readReplyDataPos[i]] << base;
67 default String calculateStringValue(int[] data, ComfoAirCommandType commandType) {
68 int[] readReplyDataPos = commandType.getReadReplyDataPos();
69 StringBuilder value = new StringBuilder();
70 if (readReplyDataPos != null) {
71 for (int pos : readReplyDataPos) {
72 if (pos < data.length) {
73 value.append((char) data[pos]);
77 return value.toString();