]> git.basschouten.com Git - openhab-addons.git/blob
d2c4f8fab1d6c6f0d608c42d00b198f460639dca
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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 org.eclipse.jdt.annotation.NonNullByDefault;
16
17 /**
18  * The {@link AirthingsWavePlusDataParser} is responsible for parsing data from Wave Plus device format.
19  *
20  * @author Pauli Anttila - Initial contribution
21  */
22 @NonNullByDefault
23 public class AirthingsWavePlusDataParser {
24     private static final int EXPECTED_DATA_LEN = 20;
25     private static final int EXPECTED_VER = 1;
26
27     private double humidity;
28     private int radonShortTermAvg;
29     private int radonLongTermAvg;
30     private double temperature;
31     private double pressure;
32     private int co2;
33     private int tvoc;
34
35     public AirthingsWavePlusDataParser(int[] data) throws AirthingsParserException {
36         parseData(data);
37     }
38
39     public double getHumidity() {
40         return humidity;
41     }
42
43     public int getRadonShortTermAvg() {
44         return radonShortTermAvg;
45     }
46
47     public int getRadonLongTermAvg() {
48         return radonLongTermAvg;
49     }
50
51     public double getTemperature() {
52         return temperature;
53     }
54
55     public double getPressure() {
56         return pressure;
57     }
58
59     public int getCo2() {
60         return co2;
61     }
62
63     public int getTvoc() {
64         return tvoc;
65     }
66
67     private void parseData(int[] data) throws AirthingsParserException {
68         if (data.length == EXPECTED_DATA_LEN) {
69             final int version = data[0];
70
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]);
79             } else {
80                 throw new AirthingsParserException(String.format("Unsupported data structure version '%d'", version));
81             }
82         } else {
83             throw new AirthingsParserException(String.format("Illegal data structure length '%d'", data.length));
84         }
85     }
86
87     private int intFromBytes(int lowByte, int highByte) {
88         return (highByte & 0xFF) << 8 | (lowByte & 0xFF);
89     }
90
91     @Override
92     public String toString() {
93         return String.format(
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);
96     }
97 }