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.library.types.OnOffType;
20 import org.openhab.core.types.State;
21 import org.openhab.core.types.UnDefType;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * Class to handle boolean values which are handled as decimal 0/1 states
28 * @author Holger Hees - Initial Contribution
29 * @author Hans Böhm - Refactoring
32 public class DataTypeBoolean implements ComfoAirDataType {
33 private static final DataTypeBoolean SINGLETON_INSTANCE = new DataTypeBoolean();
35 private DataTypeBoolean() {
38 private final Logger logger = LoggerFactory.getLogger(DataTypeBoolean.class);
40 public static DataTypeBoolean getInstance() {
41 return SINGLETON_INSTANCE;
45 public State convertToState(int @Nullable [] data, ComfoAirCommandType commandType) {
47 logger.trace("\"DataTypeBoolean\" class \"convertToState\" method parameter: null");
48 return UnDefType.NULL;
50 int[] readReplyDataPos = commandType.getReadReplyDataPos();
51 int readReplyDataBits = commandType.getReadReplyDataBits();
54 if (readReplyDataPos != null && readReplyDataPos[0] < data.length) {
55 if (readReplyDataBits == 0) {
56 result = data[readReplyDataPos[0]] == 1;
58 result = (data[readReplyDataPos[0]] & readReplyDataBits) == readReplyDataBits;
60 return OnOffType.from(result);
62 return UnDefType.NULL;
68 public int @Nullable [] convertFromState(State value, ComfoAirCommandType commandType) {
69 if (value instanceof UnDefType) {
70 logger.trace("\"DataTypeBoolean\" class \"convertFromState\" undefined state");
73 DecimalType decimalValue = value.as(DecimalType.class);
74 int[] possibleValues = commandType.getPossibleValues();
75 int returnValue = 0x01;
77 if (possibleValues != null) {
78 returnValue = possibleValues[0];
81 if (decimalValue != null) {
82 int[] template = commandType.getChangeDataTemplate();
84 template[commandType.getChangeDataPos()] = decimalValue.intValue() == 1 ? returnValue : 0x00;
89 "\"DataTypeBoolean\" class \"convertFromState\" method: State value conversion returned null");