]> git.basschouten.com Git - openhab-addons.git/blob
4be67f8ec58d7f3cecfd35be77c26a9d8f1afe8c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.bluetooth.radoneye.internal;
14
15 import java.math.BigDecimal;
16 import java.util.Arrays;
17 import java.util.HashMap;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * The {@link RadoneyeDataParser} is responsible for parsing data from Wave Plus device format.
26  *
27  * @author Peter Obel - Initial contribution
28  */
29 @NonNullByDefault
30 public class RadoneyeDataParser {
31     public static final String RADON = "radon";
32
33     private static final int EXPECTED_DATA_LEN_V1 = 20;
34     private static final int EXPECTED_DATA_LEN_V2 = 12;
35     private static final int EXPECTED_VER_PLUS = 1;
36
37     private static final Logger logger = LoggerFactory.getLogger(RadoneyeDataParser.class);
38
39     private RadoneyeDataParser() {
40     }
41
42     public static Map<String, Number> parseRd200Data(int fwVersion, int[] data) throws RadoneyeParserException {
43         logger.debug("Parsed data length: {}", data.length);
44         logger.debug("Parsed data: {}", data);
45
46         final Map<String, Number> result = new HashMap<>();
47
48         switch (fwVersion) {
49             case 1:
50                 if (data.length != EXPECTED_DATA_LEN_V1) {
51                     throw new RadoneyeParserException(String.format("Illegal data structure length '%d'", data.length));
52                 }
53
54                 int[] radonArray = subArray(data, 2, 6);
55                 result.put(RADON, new BigDecimal(readFloat(radonArray) * 37));
56                 break;
57             case 2:
58                 if (data.length != EXPECTED_DATA_LEN_V2) {
59                     throw new RadoneyeParserException(String.format("Illegal data structure length '%d'", data.length));
60                 }
61
62                 result.put(RADON, intFromBytes(data[2], data[3]));
63                 break;
64             default:
65                 throw new UnsupportedOperationException("fwVersion: " + fwVersion + " is not implemented");
66         }
67         return result;
68     }
69
70     private static int intFromBytes(int lowByte, int highByte) {
71         return (highByte & 0xFF) << 8 | (lowByte & 0xFF);
72     }
73
74     // Little endian
75     private static int fromByteArrayLE(int[] bytes) {
76         int result = 0;
77         for (int i = 0; i < bytes.length; i++) {
78             result |= (bytes[i] & 0xFF) << (8 * i);
79         }
80         return result;
81     }
82
83     private static float readFloat(int[] bytes) {
84         int i = fromByteArrayLE(bytes);
85         return Float.intBitsToFloat(i);
86     }
87
88     private static int[] subArray(int[] array, int beg, int end) {
89         return Arrays.copyOfRange(array, beg, end + 1);
90     }
91 }