]> git.basschouten.com Git - openhab-addons.git/blob
6123bcf3fe9e3bca8097f70edab3147448099f80
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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 static org.openhab.binding.bluetooth.airthings.internal.AirthingsBindingConstants.*;
16
17 import java.util.UUID;
18
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;
26
27 /**
28  * The {@link AirthingsWaveGen1Handler} is responsible for handling commands, which are
29  * sent to one of the channels.
30  *
31  * @author Davy Wong - Added Airthings Wave Gen 1 support
32  */
33 @NonNullByDefault
34 public class AirthingsWaveGen1Handler extends AbstractAirthingsHandler {
35
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";
40
41     private int intResult;
42     private double dblResult;
43     private volatile ReadSensor readSensor = ReadSensor.RADON_STA;
44
45     private enum ReadSensor {
46         TEMPERATURE,
47         HUMIDITY,
48         RADON_STA,
49         RADON_LTA,
50     }
51
52     public AirthingsWaveGen1Handler(Thing thing) {
53         super(thing);
54     }
55
56     private final Logger logger = LoggerFactory.getLogger(AirthingsWaveGen1Handler.class);
57
58     @Override
59     protected void updateChannels(int[] is) {
60         int[] rawdata;
61         rawdata = is;
62         if (rawdata.length == 2) {
63             switch (readSensor) {
64                 case TEMPERATURE:
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");
73                     break;
74                 case HUMIDITY:
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");
82                     break;
83                 case RADON_STA:
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");
92                     break;
93                 case RADON_LTA:
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");
102                     break;
103             }
104         } else {
105             logger.debug("Illegal data structure length '{}'", String.valueOf(rawdata).length());
106         }
107     }
108
109     @Override
110     protected UUID getDataUUID() {
111         switch (readSensor) {
112             case TEMPERATURE:
113                 logger.debug("Return UUID Temperature");
114                 return UUID.fromString(TEMPERATURE_UUID);
115             case HUMIDITY:
116                 logger.debug("Return UUID Humidity");
117                 return UUID.fromString(HUMIDITY_UUID);
118             case RADON_STA:
119                 logger.debug("Return UUID Radon STA");
120                 return UUID.fromString(RADON_STA_UUID);
121             case RADON_LTA:
122                 logger.debug("Return UUID Radon LTA");
123                 return UUID.fromString(RADON_LTA_UUID);
124             default:
125                 logger.debug("Return UUID Default");
126                 return UUID.fromString(RADON_STA_UUID);
127         }
128     }
129
130     private int intFromBytes(int lowByte, int highByte) {
131         return (highByte & 0xFF) << 8 | (lowByte & 0xFF);
132     }
133 }