]> git.basschouten.com Git - openhab-addons.git/blob
b1f72e8701ea3710bd4bf1b91dcfacaaac6a0839
[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.types.State;
20 import org.openhab.core.types.UnDefType;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Class to handle numeric values
26  *
27  * @author Holger Hees - Initial Contribution
28  * @author Hans Böhm - Refactoring
29  */
30 @NonNullByDefault
31 public class DataTypeNumber implements ComfoAirDataType {
32     private static final DataTypeNumber SINGLETON_INSTANCE = new DataTypeNumber();
33
34     private DataTypeNumber() {
35     }
36
37     private final Logger logger = LoggerFactory.getLogger(DataTypeNumber.class);
38
39     public static DataTypeNumber getInstance() {
40         return SINGLETON_INSTANCE;
41     }
42
43     @Override
44     public State convertToState(int @Nullable [] data, ComfoAirCommandType commandType) {
45         if (data == null) {
46             logger.trace("\"DataTypeNumber\" class \"convertToState\" method parameter: null");
47             return UnDefType.NULL;
48         } else {
49             int value = calculateNumberValue(data, commandType);
50
51             if (value < 0) {
52                 return UnDefType.NULL;
53             }
54
55             int[] possibleValues = commandType.getPossibleValues();
56             if (possibleValues != null) {
57                 // fix for unexpected value for "level" value. got a 0x33. valid was
58                 // the 0x03. 0x30 was to much.
59                 // send DATA: 07 f0 00 cd 00 7a 07 0f
60                 // receive CMD: ce DATA: 0f 20 32 00 0f 21 33 2d 33 03 01 5a 5b 00
61                 for (int possibleValue : possibleValues) {
62                     if ((value & possibleValue) == possibleValue) {
63                         return new DecimalType(value);
64                     }
65                 }
66                 return UnDefType.NULL;
67             }
68             return new DecimalType(value);
69         }
70     }
71
72     @Override
73     public int @Nullable [] convertFromState(State value, ComfoAirCommandType commandType) {
74         if (value instanceof UnDefType) {
75             logger.trace("\"DataTypeNumber\" class \"convertFromState\" undefined state");
76             return null;
77         } else {
78             int[] template = commandType.getChangeDataTemplate();
79             int[] possibleValues = commandType.getPossibleValues();
80             int position = commandType.getChangeDataPos();
81
82             int intValue = ((DecimalType) value).intValue();
83
84             if (possibleValues == null) {
85                 template[position] = intValue;
86             } else {
87                 for (int i = 0; i < possibleValues.length; i++) {
88                     if (possibleValues[i] == intValue) {
89                         template[position] = intValue;
90                         break;
91                     }
92                 }
93             }
94             return template;
95         }
96     }
97 }