]> git.basschouten.com Git - openhab-addons.git/blob
6beecb16dd051926fad2fb0f42b4d6fda41f6231
[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.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 a 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 a 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 value = 0;
50         if (readReplyDataPos != null) {
51             int base = 0;
52
53             for (int i = readReplyDataPos.length - 1; i >= 0; i--) {
54                 if (readReplyDataPos[i] < data.length) {
55                     value += data[readReplyDataPos[i]] << base;
56                     base += 8;
57                 } else {
58                     return -1;
59                 }
60             }
61         } else {
62             value = -1;
63         }
64         return value;
65     }
66
67     default String calculateStringValue(int[] data, ComfoAirCommandType commandType) {
68         int[] readReplyDataPos = commandType.getReadReplyDataPos();
69         StringBuilder value = new StringBuilder();
70         if (readReplyDataPos != null) {
71             for (int pos : readReplyDataPos) {
72                 if (pos < data.length) {
73                     value.append((char) data[pos]);
74                 }
75             }
76         }
77         return value.toString();
78     }
79 }