]> git.basschouten.com Git - openhab-addons.git/blob
27a4cf5b7094b80b08df95f291355a934dd5f526
[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.types.State;
19
20 /**
21  * Abstract class to convert binary hex values into openHAB states and vice
22  * versa
23  *
24  * @author Holger Hees - Initial Contribution
25  * @author Hans Böhm - Refactoring
26  */
27 @NonNullByDefault
28 public interface ComfoAirDataType {
29     /**
30      * Generate an openHAB State object based on response data.
31      *
32      * @param response
33      * @param commandType
34      * @return converted State object
35      */
36     State convertToState(int[] response, ComfoAirCommandType commandType);
37
38     /**
39      * Generate byte array based on an openHAB State.
40      *
41      * @param value
42      * @param commandType
43      * @return converted byte array
44      */
45     int @Nullable [] convertFromState(State value, ComfoAirCommandType commandType);
46
47     default int calculateNumberValue(int[] data, ComfoAirCommandType commandType) {
48         int[] readReplyDataPos = commandType.getReadReplyDataPos();
49         int readReplyDataBits = commandType.getReadReplyDataBits();
50
51         int value = 0;
52         if (readReplyDataPos != null) {
53             if (readReplyDataBits == 0) {
54                 int base = 0;
55
56                 for (int i = readReplyDataPos.length - 1; i >= 0; i--) {
57                     if (readReplyDataPos[i] < data.length) {
58                         value += data[readReplyDataPos[i]] << base;
59                         base += 8;
60                     } else {
61                         return -1;
62                     }
63                 }
64             } else {
65                 value = (data[readReplyDataPos[0]] & readReplyDataBits) == readReplyDataBits ? 1 : 0;
66             }
67         } else {
68             value = -1;
69         }
70         return value;
71     }
72
73     default String calculateStringValue(int[] data, ComfoAirCommandType commandType) {
74         int[] readReplyDataPos = commandType.getReadReplyDataPos();
75         StringBuilder value = new StringBuilder();
76         if (readReplyDataPos != null) {
77             for (int pos : readReplyDataPos) {
78                 if (pos < data.length) {
79                     value.append((char) data[pos]);
80                 }
81             }
82         }
83         return value.toString();
84     }
85 }