2 * Copyright (c) 2010-2024 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 static org.openhab.binding.bluetooth.airthings.internal.AirthingsBindingConstants.*;
17 import java.util.UUID;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.core.library.types.QuantityType;
21 import org.openhab.core.library.unit.SIUnits;
22 import org.openhab.core.library.unit.Units;
23 import org.openhab.core.thing.Thing;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * The {@link AirthingsWaveGen1Handler} is responsible for handling commands, which are
29 * sent to one of the channels.
31 * @author Davy Wong - Added Airthings Wave Gen 1 support
34 public class AirthingsWaveGen1Handler extends AbstractAirthingsHandler {
36 private static final String HUMIDITY_UUID = "00002a6f-0000-1000-8000-00805f9b34fb"; // 0x2A6F
37 private static final String TEMPERATURE_UUID = "00002a6e-0000-1000-8000-00805f9b34fb"; // 0x2A6E
38 private static final String RADON_STA_UUID = "b42e01aa-ade7-11e4-89d3-123b93f75cba";
39 private static final String RADON_LTA_UUID = "b42e0a4c-ade7-11e4-89d3-123b93f75cba";
41 private int intResult;
42 private double dblResult;
43 private volatile ReadSensor readSensor = ReadSensor.RADON_STA;
45 private enum ReadSensor {
52 public AirthingsWaveGen1Handler(Thing thing) {
56 private final Logger logger = LoggerFactory.getLogger(AirthingsWaveGen1Handler.class);
59 protected void updateChannels(int[] is) {
62 if (rawdata.length == 2) {
65 dblResult = intFromBytes(rawdata[0], rawdata[1]) / 100D;
66 logger.debug("Parsed data 1: {}", String.format("[temperature=%.1f °C]", dblResult));
67 readSensor = ReadSensor.HUMIDITY;
68 logger.debug("Change next readSensor to: {}", readSensor);
69 logger.debug("Update channel 1");
70 updateState(CHANNEL_ID_TEMPERATURE,
71 QuantityType.valueOf(Double.valueOf(dblResult), SIUnits.CELSIUS));
72 logger.debug("Update channel 1 done");
75 dblResult = intFromBytes(rawdata[0], rawdata[1]) / 100D;
76 logger.debug("Parsed data 2: {}", String.format("[humidity=%.1f %%rH]", dblResult));
77 readSensor = ReadSensor.RADON_STA;
78 logger.debug("Change next readSensor to: {}", readSensor);
79 logger.debug("Update channel 2");
80 updateState(CHANNEL_ID_HUMIDITY, QuantityType.valueOf(Double.valueOf(dblResult), Units.PERCENT));
81 logger.debug("Update channel 2 done");
84 intResult = intFromBytes(rawdata[0], rawdata[1]);
85 logger.debug("Parsed data 3: {}", String.format("[radonShortTermAvg=%d Bq/m3]", intResult));
86 readSensor = ReadSensor.RADON_LTA;
87 logger.debug("Change next readSensor to: {}", readSensor);
88 logger.debug("Update channel 3");
89 updateState(CHANNEL_ID_RADON_ST_AVG,
90 QuantityType.valueOf(Double.valueOf(intResult), Units.BECQUEREL_PER_CUBIC_METRE));
91 logger.debug("Update channel 3 done");
94 intResult = intFromBytes(rawdata[0], rawdata[1]);
95 logger.debug("Parsed data 4: {}", String.format("[radonLongTermAvg=%d Bq/m3]", intResult));
96 readSensor = ReadSensor.TEMPERATURE;
97 logger.debug("Change next readSensor to: {}", readSensor);
98 logger.debug("Update channel 4");
99 updateState(CHANNEL_ID_RADON_LT_AVG,
100 QuantityType.valueOf(Double.valueOf(intResult), Units.BECQUEREL_PER_CUBIC_METRE));
101 logger.debug("Update channel 4 done");
105 logger.debug("Illegal data structure length '{}'", String.valueOf(rawdata).length());
110 protected UUID getDataUUID() {
111 switch (readSensor) {
113 logger.debug("Return UUID Temperature");
114 return UUID.fromString(TEMPERATURE_UUID);
116 logger.debug("Return UUID Humidity");
117 return UUID.fromString(HUMIDITY_UUID);
119 logger.debug("Return UUID Radon STA");
120 return UUID.fromString(RADON_STA_UUID);
122 logger.debug("Return UUID Radon LTA");
123 return UUID.fromString(RADON_LTA_UUID);
125 logger.debug("Return UUID Default");
126 return UUID.fromString(RADON_STA_UUID);
130 private int intFromBytes(int lowByte, int highByte) {
131 return (highByte & 0xFF) << 8 | (lowByte & 0xFF);