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.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();
52 int readCommand = commandType.getReadCommand();
55 if (readReplyDataPos != null && readReplyDataPos[0] < data.length) {
56 if (readReplyDataBits == 0) {
57 result = data[readReplyDataPos[0]] == 1;
59 result = (data[readReplyDataPos[0]] & readReplyDataBits) == readReplyDataBits;
61 return OnOffType.from(result);
62 } else if (readCommand == 0) {
63 return OnOffType.OFF; // handle write-only commands (resets)
65 return UnDefType.NULL;
71 public int @Nullable [] convertFromState(State value, ComfoAirCommandType commandType) {
72 if (value instanceof UnDefType) {
73 logger.trace("\"DataTypeBoolean\" class \"convertFromState\" undefined state");
76 DecimalType decimalValue = value.as(DecimalType.class);
77 int[] possibleValues = commandType.getPossibleValues();
78 int returnValue = 0x01;
80 if (possibleValues != null) {
81 returnValue = possibleValues[0];
84 if (decimalValue != null) {
85 int[] template = commandType.getChangeDataTemplate();
87 template[commandType.getChangeDataPos()] = decimalValue.intValue() == 1 ? returnValue : 0x00;
92 "\"DataTypeBoolean\" class \"convertFromState\" method: State value conversion returned null");