forked from J-N-K/openhab-addons
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[airthings] Add support for Airthings Wave Mini (openhab#10456)
Signed-off-by: Kai Kreuzer <[email protected]>
- Loading branch information
1 parent
8c1ec3e
commit 38a532a
Showing
12 changed files
with
586 additions
and
382 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
233 changes: 233 additions & 0 deletions
233
.../main/java/org/openhab/binding/bluetooth/airthings/internal/AbstractAirthingsHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,233 @@ | ||
/** | ||
* Copyright (c) 2010-2021 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.bluetooth.airthings.internal; | ||
|
||
import java.util.Optional; | ||
import java.util.UUID; | ||
import java.util.concurrent.ScheduledFuture; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.binding.bluetooth.BeaconBluetoothHandler; | ||
import org.openhab.binding.bluetooth.BluetoothCharacteristic; | ||
import org.openhab.binding.bluetooth.BluetoothDevice.ConnectionState; | ||
import org.openhab.binding.bluetooth.BluetoothUtils; | ||
import org.openhab.binding.bluetooth.notification.BluetoothConnectionStatusNotification; | ||
import org.openhab.core.thing.Thing; | ||
import org.openhab.core.thing.ThingStatus; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* The {@link AbstractAirthingsHandler} is responsible for handling commands, which are | ||
* sent to one of the channels. | ||
* | ||
* @author Pauli Anttila - Initial contribution | ||
* @author Kai Kreuzer - Added Airthings Wave Mini support | ||
*/ | ||
@NonNullByDefault | ||
abstract public class AbstractAirthingsHandler extends BeaconBluetoothHandler { | ||
|
||
private static final int CHECK_PERIOD_SEC = 10; | ||
|
||
private final Logger logger = LoggerFactory.getLogger(AbstractAirthingsHandler.class); | ||
|
||
private AtomicInteger sinceLastReadSec = new AtomicInteger(); | ||
private Optional<AirthingsConfiguration> configuration = Optional.empty(); | ||
private @Nullable ScheduledFuture<?> scheduledTask; | ||
|
||
private volatile int refreshInterval; | ||
|
||
private volatile ServiceState serviceState = ServiceState.NOT_RESOLVED; | ||
private volatile ReadState readState = ReadState.IDLE; | ||
|
||
private enum ServiceState { | ||
NOT_RESOLVED, | ||
RESOLVING, | ||
RESOLVED, | ||
} | ||
|
||
private enum ReadState { | ||
IDLE, | ||
READING, | ||
} | ||
|
||
public AbstractAirthingsHandler(Thing thing) { | ||
super(thing); | ||
} | ||
|
||
@Override | ||
public void initialize() { | ||
logger.debug("Initialize"); | ||
super.initialize(); | ||
configuration = Optional.of(getConfigAs(AirthingsConfiguration.class)); | ||
logger.debug("Using configuration: {}", configuration.get()); | ||
cancelScheduledTask(); | ||
configuration.ifPresent(cfg -> { | ||
refreshInterval = cfg.refreshInterval; | ||
logger.debug("Start scheduled task to read device in every {} seconds", refreshInterval); | ||
scheduledTask = scheduler.scheduleWithFixedDelay(this::executePeridioc, CHECK_PERIOD_SEC, CHECK_PERIOD_SEC, | ||
TimeUnit.SECONDS); | ||
}); | ||
sinceLastReadSec.set(refreshInterval); // update immediately | ||
} | ||
|
||
@Override | ||
public void dispose() { | ||
logger.debug("Dispose"); | ||
cancelScheduledTask(); | ||
serviceState = ServiceState.NOT_RESOLVED; | ||
readState = ReadState.IDLE; | ||
super.dispose(); | ||
} | ||
|
||
private void cancelScheduledTask() { | ||
if (scheduledTask != null) { | ||
scheduledTask.cancel(true); | ||
scheduledTask = null; | ||
} | ||
} | ||
|
||
private void executePeridioc() { | ||
sinceLastReadSec.addAndGet(CHECK_PERIOD_SEC); | ||
execute(); | ||
} | ||
|
||
private synchronized void execute() { | ||
ConnectionState connectionState = device.getConnectionState(); | ||
logger.debug("Device {} state is {}, serviceState {}, readState {}", address, connectionState, serviceState, | ||
readState); | ||
|
||
switch (connectionState) { | ||
case DISCOVERING: | ||
case DISCOVERED: | ||
case DISCONNECTED: | ||
if (isTimeToRead()) { | ||
connect(); | ||
} | ||
break; | ||
case CONNECTED: | ||
read(); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
private void connect() { | ||
logger.debug("Connect to device {}...", address); | ||
if (!device.connect()) { | ||
logger.debug("Connecting to device {} failed", address); | ||
} | ||
} | ||
|
||
private void disconnect() { | ||
logger.debug("Disconnect from device {}...", address); | ||
if (!device.disconnect()) { | ||
logger.debug("Disconnect from device {} failed", address); | ||
} | ||
} | ||
|
||
private void read() { | ||
switch (serviceState) { | ||
case NOT_RESOLVED: | ||
discoverServices(); | ||
break; | ||
case RESOLVED: | ||
switch (readState) { | ||
case IDLE: | ||
logger.debug("Read data from device {}...", address); | ||
BluetoothCharacteristic characteristic = device.getCharacteristic(getDataUUID()); | ||
if (characteristic != null) { | ||
readState = ReadState.READING; | ||
device.readCharacteristic(characteristic).whenComplete((data, ex) -> { | ||
try { | ||
logger.debug("Characteristic {} from device {}: {}", characteristic.getUuid(), | ||
address, data); | ||
updateStatus(ThingStatus.ONLINE); | ||
sinceLastReadSec.set(0); | ||
updateChannels(BluetoothUtils.toIntArray(data)); | ||
} finally { | ||
readState = ReadState.IDLE; | ||
disconnect(); | ||
} | ||
}); | ||
} else { | ||
logger.debug("Read data from device {} failed", address); | ||
disconnect(); | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
private void discoverServices() { | ||
logger.debug("Discover services for device {}", address); | ||
serviceState = ServiceState.RESOLVING; | ||
device.discoverServices(); | ||
} | ||
|
||
@Override | ||
public void onServicesDiscovered() { | ||
serviceState = ServiceState.RESOLVED; | ||
logger.debug("Service discovery completed for device {}", address); | ||
printServices(); | ||
execute(); | ||
} | ||
|
||
private void printServices() { | ||
device.getServices().forEach(service -> logger.debug("Device {} Service '{}'", address, service)); | ||
} | ||
|
||
@Override | ||
public void onConnectionStateChange(BluetoothConnectionStatusNotification connectionNotification) { | ||
switch (connectionNotification.getConnectionState()) { | ||
case DISCONNECTED: | ||
if (serviceState == ServiceState.RESOLVING) { | ||
serviceState = ServiceState.NOT_RESOLVED; | ||
} | ||
readState = ReadState.IDLE; | ||
break; | ||
default: | ||
break; | ||
|
||
} | ||
execute(); | ||
} | ||
|
||
private boolean isTimeToRead() { | ||
int sinceLastRead = sinceLastReadSec.get(); | ||
logger.debug("Time since last update: {} sec", sinceLastRead); | ||
return sinceLastRead >= refreshInterval; | ||
} | ||
|
||
/** | ||
* Provides the UUID of the characteristic, which holds the sensor data | ||
* | ||
* @return the UUID of the data characteristic | ||
*/ | ||
protected abstract UUID getDataUUID(); | ||
|
||
/** | ||
* This method parses the content of the bluetooth characteristic and updates the Thing channels accordingly. | ||
* | ||
* @param is the content of the bluetooth characteristic | ||
*/ | ||
abstract protected void updateChannels(int[] is); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...s/src/main/java/org/openhab/binding/bluetooth/airthings/internal/AirthingsDataParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* Copyright (c) 2010-2021 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.bluetooth.airthings.internal; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.RoundingMode; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
|
||
/** | ||
* The {@link AirthingsDataParser} is responsible for parsing data from Wave Plus device format. | ||
* | ||
* @author Pauli Anttila - Initial contribution | ||
* @author Kai Kreuzer - Added Airthings Wave Mini support | ||
*/ | ||
@NonNullByDefault | ||
public class AirthingsDataParser { | ||
public static final String TVOC = "tvoc"; | ||
public static final String CO2 = "co2"; | ||
public static final String PRESSURE = "pressure"; | ||
public static final String TEMPERATURE = "temperature"; | ||
public static final String RADON_LONG_TERM_AVG = "radonLongTermAvg"; | ||
public static final String RADON_SHORT_TERM_AVG = "radonShortTermAvg"; | ||
public static final String HUMIDITY = "humidity"; | ||
|
||
private static final int EXPECTED_DATA_LEN = 20; | ||
private static final int EXPECTED_VER_PLUS = 1; | ||
|
||
private AirthingsDataParser() { | ||
} | ||
|
||
public static Map<String, Number> parseWavePlusData(int[] data) throws AirthingsParserException { | ||
if (data.length == EXPECTED_DATA_LEN) { | ||
final Map<String, Number> result = new HashMap<>(); | ||
|
||
final int version = data[0]; | ||
|
||
if (version == EXPECTED_VER_PLUS) { | ||
result.put(HUMIDITY, data[1] / 2D); | ||
result.put(RADON_SHORT_TERM_AVG, intFromBytes(data[4], data[5])); | ||
result.put(RADON_LONG_TERM_AVG, intFromBytes(data[6], data[7])); | ||
result.put(TEMPERATURE, intFromBytes(data[8], data[9]) / 100D); | ||
result.put(PRESSURE, intFromBytes(data[10], data[11]) / 50D); | ||
result.put(CO2, intFromBytes(data[12], data[13])); | ||
result.put(TVOC, intFromBytes(data[14], data[15])); | ||
return result; | ||
} else { | ||
throw new AirthingsParserException(String.format("Unsupported data structure version '%d'", version)); | ||
} | ||
} else { | ||
throw new AirthingsParserException(String.format("Illegal data structure length '%d'", data.length)); | ||
} | ||
} | ||
|
||
public static Map<String, Number> parseWaveMiniData(int[] data) throws AirthingsParserException { | ||
if (data.length == EXPECTED_DATA_LEN) { | ||
final Map<String, Number> result = new HashMap<>(); | ||
result.put(TEMPERATURE, | ||
new BigDecimal(intFromBytes(data[2], data[3])) | ||
.divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP) | ||
.subtract(BigDecimal.valueOf(273.15)).doubleValue()); | ||
result.put(HUMIDITY, intFromBytes(data[6], data[7]) / 100D); | ||
result.put(TVOC, intFromBytes(data[8], data[9])); | ||
return result; | ||
} else { | ||
throw new AirthingsParserException(String.format("Illegal data structure length '%d'", data.length)); | ||
} | ||
} | ||
|
||
private static int intFromBytes(int lowByte, int highByte) { | ||
return (highByte & 0xFF) << 8 | (lowByte & 0xFF); | ||
} | ||
} |
Oops, something went wrong.