2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.bluetooth.airthings.internal;
15 import java.math.BigDecimal;
16 import java.math.RoundingMode;
17 import java.util.HashMap;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
23 * The {@link AirthingsDataParser} is responsible for parsing data from Wave Plus device format.
25 * @author Pauli Anttila - Initial contribution
26 * @author Kai Kreuzer - Added Airthings Wave Mini support
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";
38 private static final int EXPECTED_DATA_LEN = 20;
39 private static final int EXPECTED_VER_PLUS = 1;
41 private AirthingsDataParser() {
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<>();
48 final int version = data[0];
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]));
60 throw new AirthingsParserException(String.format("Unsupported data structure version '%d'", version));
63 throw new AirthingsParserException(String.format("Illegal data structure length '%d'", data.length));
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]));
78 throw new AirthingsParserException(String.format("Illegal data structure length '%d'", data.length));
82 private static int intFromBytes(int lowByte, int highByte) {
83 return (highByte & 0xFF) << 8 | (lowByte & 0xFF);