]> git.basschouten.com Git - openhab-addons.git/blob
c8878c96960cf279d0a73dd33b65672ae190cbb5
[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.airthings.internal;
14
15 import java.math.BigDecimal;
16 import java.math.RoundingMode;
17 import java.util.HashMap;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21
22 /**
23  * The {@link AirthingsDataParser} is responsible for parsing data from Wave Plus device format.
24  *
25  * @author Pauli Anttila - Initial contribution
26  * @author Kai Kreuzer - Added Airthings Wave Mini support
27  */
28 @NonNullByDefault
29 public class AirthingsDataParser {
30     public static final String TVOC = "tvoc";
31     public static final String CO2 = "co2";
32     public static final String PRESSURE = "pressure";
33     public static final String TEMPERATURE = "temperature";
34     public static final String RADON_LONG_TERM_AVG = "radonLongTermAvg";
35     public static final String RADON_SHORT_TERM_AVG = "radonShortTermAvg";
36     public static final String HUMIDITY = "humidity";
37
38     private static final int EXPECTED_DATA_LEN = 20;
39     private static final int EXPECTED_VER_PLUS = 1;
40
41     private AirthingsDataParser() {
42     }
43
44     public static Map<String, Number> parseWavePlusData(int[] data) throws AirthingsParserException {
45         if (data.length == EXPECTED_DATA_LEN) {
46             final Map<String, Number> result = new HashMap<>();
47
48             final int version = data[0];
49
50             if (version == EXPECTED_VER_PLUS) {
51                 result.put(HUMIDITY, data[1] / 2D);
52                 result.put(RADON_SHORT_TERM_AVG, intFromBytes(data[4], data[5]));
53                 result.put(RADON_LONG_TERM_AVG, intFromBytes(data[6], data[7]));
54                 result.put(TEMPERATURE, intFromBytes(data[8], data[9]) / 100D);
55                 result.put(PRESSURE, intFromBytes(data[10], data[11]) / 50D);
56                 result.put(CO2, intFromBytes(data[12], data[13]));
57                 result.put(TVOC, intFromBytes(data[14], data[15]));
58                 return result;
59             } else {
60                 throw new AirthingsParserException(String.format("Unsupported data structure version '%d'", version));
61             }
62         } else {
63             throw new AirthingsParserException(String.format("Illegal data structure length '%d'", data.length));
64         }
65     }
66
67     public static Map<String, Number> parseWaveMiniData(int[] data) throws AirthingsParserException {
68         if (data.length == EXPECTED_DATA_LEN) {
69             final Map<String, Number> result = new HashMap<>();
70             result.put(TEMPERATURE,
71                     new BigDecimal(intFromBytes(data[2], data[3]))
72                             .divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP)
73                             .subtract(BigDecimal.valueOf(273.15)).doubleValue());
74             result.put(HUMIDITY, intFromBytes(data[6], data[7]) / 100D);
75             result.put(TVOC, intFromBytes(data[8], data[9]));
76             return result;
77         } else {
78             throw new AirthingsParserException(String.format("Illegal data structure length '%d'", data.length));
79         }
80     }
81
82     private static int intFromBytes(int lowByte, int highByte) {
83         return (highByte & 0xFF) << 8 | (lowByte & 0xFF);
84     }
85 }