]> git.basschouten.com Git - openhab-addons.git/blob
74c62e1961ca5af21bc2282fee9f3bd142e0e22e
[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.StringType;
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 error messages
26  *
27  * @author Holger Hees - Initial Contribution
28  * @author Hans Böhm - Refactoring
29  */
30 @NonNullByDefault
31 public class DataTypeMessage implements ComfoAirDataType {
32     private static final DataTypeMessage SINGLETON_INSTANCE = new DataTypeMessage();
33
34     private DataTypeMessage() {
35     }
36
37     private final Logger logger = LoggerFactory.getLogger(DataTypeMessage.class);
38
39     public static DataTypeMessage 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("\"DataTypeMessage\" class \"convertToState\" method parameter: null");
47             return UnDefType.NULL;
48         } else {
49             int[] readReplyDataPos = commandType.getReadReplyDataPos();
50             if (readReplyDataPos != null) {
51                 int errorAlo = data[readReplyDataPos[0]];
52                 int errorE = data[readReplyDataPos[1]];
53                 int errorEA = (data.length > 9) ? data[readReplyDataPos[2]] : -1;
54                 int errorAhi = (data.length > 9) ? data[readReplyDataPos[3]] : -1;
55
56                 StringBuilder errorCode = new StringBuilder();
57
58                 if (errorAlo > 0) {
59                     errorCode.append("A");
60                     errorCode.append(convertToCode(errorAlo));
61                 } else if (errorAhi > 0) {
62                     if (errorAhi == 0x80) {
63                         errorCode.append("A0");
64                     } else {
65                         errorCode.append("A");
66                         errorCode.append(convertToCode(errorAhi) + 8);
67                     }
68                 }
69
70                 if (errorE > 0) {
71                     if (errorCode.length() > 0) {
72                         errorCode.append(" ");
73                     }
74                     errorCode.append("E");
75                     errorCode.append(convertToCode(errorE));
76                 } else if (errorEA > 0) {
77                     if (errorCode.length() > 0) {
78                         errorCode.append(" ");
79                     }
80                     errorCode.append("EA");
81                     errorCode.append(convertToCode(errorEA));
82                 }
83                 return new StringType(errorCode.length() > 0 ? errorCode.toString() : "No Errors");
84             } else {
85                 return UnDefType.UNDEF;
86             }
87         }
88     }
89
90     @Override
91     public int @Nullable [] convertFromState(State value, ComfoAirCommandType commandType) {
92         return null;
93     }
94
95     private int convertToCode(int code) {
96         switch (code) {
97             case 0x01:
98                 return 1;
99             case 0x02:
100                 return 2;
101             case 0x04:
102                 return 3;
103             case 0x08:
104                 return 4;
105             case 0x10:
106                 return 5;
107             case 0x20:
108                 return 6;
109             case 0x40:
110                 return 7;
111             case 0x80:
112                 return 8;
113             default:
114                 return -1;
115         }
116     }
117 }