]> git.basschouten.com Git - openhab-addons.git/blob
0ac240791b86bcc4494cec604b7f0636ec0b7463
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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             int readCommand = commandType.getReadCommand();
53             boolean result;
54
55             if (readReplyDataPos != null && readReplyDataPos[0] < data.length) {
56                 if (readReplyDataBits == 0) {
57                     result = data[readReplyDataPos[0]] == 1;
58                 } else {
59                     result = (data[readReplyDataPos[0]] & readReplyDataBits) == readReplyDataBits;
60                 }
61                 return OnOffType.from(result);
62             } else if (readCommand == 0) {
63                 return OnOffType.OFF; // handle write-only commands (resets)
64             } else {
65                 return UnDefType.NULL;
66             }
67         }
68     }
69
70     @Override
71     public int @Nullable [] convertFromState(State value, ComfoAirCommandType commandType) {
72         if (value instanceof UnDefType) {
73             logger.trace("\"DataTypeBoolean\" class \"convertFromState\" undefined state");
74             return null;
75         } else {
76             DecimalType decimalValue = value.as(DecimalType.class);
77             int[] possibleValues = commandType.getPossibleValues();
78             int returnValue = 0x01;
79
80             if (possibleValues != null) {
81                 returnValue = possibleValues[0];
82             }
83
84             if (decimalValue != null) {
85                 int[] template = commandType.getChangeDataTemplate();
86
87                 template[commandType.getChangeDataPos()] = decimalValue.intValue() == 1 ? returnValue : 0x00;
88
89                 return template;
90             } else {
91                 logger.trace(
92                         "\"DataTypeBoolean\" class \"convertFromState\" method: State value conversion returned null");
93                 return null;
94             }
95         }
96     }
97 }