]> git.basschouten.com Git - openhab-addons.git/blob
730834a40aea3caeeffc3c4e1c76c5264906e273
[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.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 = 20;
34     private static final int EXPECTED_VER_PLUS = 1;
35
36     private static final Logger logger = LoggerFactory.getLogger(RadoneyeDataParser.class);
37
38     private RadoneyeDataParser() {
39     }
40
41     public static Map<String, Number> parseRd200Data(int[] data) throws RadoneyeParserException {
42         logger.debug("Parsed data length: {}", data.length);
43         logger.debug("Parsed data: {}", data);
44         if (data.length == EXPECTED_DATA_LEN) {
45             final Map<String, Number> result = new HashMap<>();
46
47             int[] radonArray = subArray(data, 2, 6);
48             result.put(RADON, new BigDecimal(readFloat(radonArray) * 37));
49             return result;
50         } else {
51             throw new RadoneyeParserException(String.format("Illegal data structure length '%d'", data.length));
52         }
53     }
54
55     private static int intFromBytes(int lowByte, int highByte) {
56         return (highByte & 0xFF) << 8 | (lowByte & 0xFF);
57     }
58
59     // Little endian
60     private static int fromByteArrayLE(int[] bytes) {
61         int result = 0;
62         for (int i = 0; i < bytes.length; i++) {
63             result |= (bytes[i] & 0xFF) << (8 * i);
64         }
65         return result;
66     }
67
68     private static float readFloat(int[] bytes) {
69         int i = fromByteArrayLE(bytes);
70         return Float.intBitsToFloat(i);
71     }
72
73     private static int[] subArray(int[] array, int beg, int end) {
74         return Arrays.copyOfRange(array, beg, end + 1);
75     }
76 }