]> git.basschouten.com Git - openhab-addons.git/blob
4cf9a771f8b69d9e0281e128b7ac42eb72915c91
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.comfoair.internal.datatypes;
14
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;
24
25 /**
26  * Class to handle boolean values which are handled as decimal 0/1 states
27  *
28  * @author Holger Hees - Initial Contribution
29  * @author Hans Böhm - Refactoring
30  */
31 @NonNullByDefault
32 public class DataTypeBoolean implements ComfoAirDataType {
33     private static final DataTypeBoolean SINGLETON_INSTANCE = new DataTypeBoolean();
34
35     private DataTypeBoolean() {
36     }
37
38     private final Logger logger = LoggerFactory.getLogger(DataTypeBoolean.class);
39
40     public static DataTypeBoolean getInstance() {
41         return SINGLETON_INSTANCE;
42     }
43
44     @Override
45     public State convertToState(int @Nullable [] data, ComfoAirCommandType commandType) {
46         if (data == null) {
47             logger.trace("\"DataTypeBoolean\" class \"convertToState\" method parameter: null");
48             return UnDefType.NULL;
49         } else {
50             int[] readReplyDataPos = commandType.getReadReplyDataPos();
51             int readReplyDataBits = commandType.getReadReplyDataBits();
52             boolean result;
53
54             if (readReplyDataPos != null && readReplyDataPos[0] < data.length) {
55                 if (readReplyDataBits == 0) {
56                     result = data[readReplyDataPos[0]] == 1;
57                 } else {
58                     result = (data[readReplyDataPos[0]] & readReplyDataBits) == readReplyDataBits;
59                 }
60                 return OnOffType.from(result);
61             } else {
62                 return UnDefType.NULL;
63             }
64         }
65     }
66
67     @Override
68     public int @Nullable [] convertFromState(State value, ComfoAirCommandType commandType) {
69         if (value instanceof UnDefType) {
70             logger.trace("\"DataTypeBoolean\" class \"convertFromState\" undefined state");
71             return null;
72         } else {
73             DecimalType decimalValue = value.as(DecimalType.class);
74             int[] possibleValues = commandType.getPossibleValues();
75             int returnValue = 0x01;
76
77             if (possibleValues != null) {
78                 returnValue = possibleValues[0];
79             }
80
81             if (decimalValue != null) {
82                 int[] template = commandType.getChangeDataTemplate();
83
84                 template[commandType.getChangeDataPos()] = decimalValue.intValue() == 1 ? returnValue : 0x00;
85
86                 return template;
87             } else {
88                 logger.trace(
89                         "\"DataTypeBoolean\" class \"convertFromState\" method: State value conversion returned null");
90                 return null;
91             }
92         }
93     }
94 }