2 * Copyright (c) 2010-2021 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 org.eclipse.jdt.annotation.NonNullByDefault;
18 * The {@link AirthingsWavePlusDataParser} is responsible for parsing data from Wave Plus device format.
20 * @author Pauli Anttila - Initial contribution
23 public class AirthingsWavePlusDataParser {
24 private static final int EXPECTED_DATA_LEN = 20;
25 private static final int EXPECTED_VER = 1;
27 private double humidity;
28 private int radonShortTermAvg;
29 private int radonLongTermAvg;
30 private double temperature;
31 private double pressure;
35 public AirthingsWavePlusDataParser(int[] data) throws AirthingsParserException {
39 public double getHumidity() {
43 public int getRadonShortTermAvg() {
44 return radonShortTermAvg;
47 public int getRadonLongTermAvg() {
48 return radonLongTermAvg;
51 public double getTemperature() {
55 public double getPressure() {
63 public int getTvoc() {
67 private void parseData(int[] data) throws AirthingsParserException {
68 if (data.length == EXPECTED_DATA_LEN) {
69 final int version = data[0];
71 if (version == EXPECTED_VER) {
72 humidity = data[1] / 2D;
73 radonShortTermAvg = intFromBytes(data[4], data[5]);
74 radonLongTermAvg = intFromBytes(data[6], data[7]);
75 temperature = intFromBytes(data[8], data[9]) / 100D;
76 pressure = intFromBytes(data[10], data[11]) / 50D;
77 co2 = intFromBytes(data[12], data[13]);
78 tvoc = intFromBytes(data[14], data[15]);
80 throw new AirthingsParserException(String.format("Unsupported data structure version '%d'", version));
83 throw new AirthingsParserException(String.format("Illegal data structure length '%d'", data.length));
87 private int intFromBytes(int lowByte, int highByte) {
88 return (highByte & 0xFF) << 8 | (lowByte & 0xFF);
92 public String toString() {
94 "[humidity=%.1f %%rH, radonShortTermAvg=%d Bq/m3, radonLongTermAvg=%d Bq/m3, temperature=%.1f °C, air pressure=%.2f mbar, co2=%d ppm, tvoc=%d ppb]",
95 humidity, radonShortTermAvg, radonLongTermAvg, temperature, pressure, co2, tvoc);