2 * Copyright (c) 2010-2022 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.blukii.handler;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.bluetooth.BeaconBluetoothHandler;
17 import org.openhab.binding.bluetooth.BluetoothDeviceListener;
18 import org.openhab.binding.bluetooth.blukii.BlukiiBindingConstants;
19 import org.openhab.binding.bluetooth.blukii.internal.data.BlukiiData;
20 import org.openhab.binding.bluetooth.blukii.internal.data.BlukiiDataDecoder;
21 import org.openhab.binding.bluetooth.notification.BluetoothScanNotification;
22 import org.openhab.core.library.types.DecimalType;
23 import org.openhab.core.library.types.QuantityType;
24 import org.openhab.core.library.unit.MetricPrefix;
25 import org.openhab.core.library.unit.SIUnits;
26 import org.openhab.core.library.unit.Units;
27 import org.openhab.core.thing.Thing;
30 * The {@link BlukiiHandler} is responsible for handling commands, which are
31 * sent to one of the channels.
33 * @author Kai Kreuzer - Initial contribution and API
34 * @author Markus Rathgeb - Split data (decoding and types) and handler
37 public class BlukiiHandler extends BeaconBluetoothHandler implements BluetoothDeviceListener {
39 private final BlukiiDataDecoder decoder = new BlukiiDataDecoder();
41 public BlukiiHandler(Thing thing) {
46 public void onScanRecordReceived(BluetoothScanNotification scanNotification) {
47 final byte[] manufacturerData = scanNotification.getManufacturerData();
48 if (manufacturerData != null) {
49 final BlukiiData blukiiData = decoder.decode(manufacturerData);
50 if (blukiiData != null) {
51 updateState(BlukiiBindingConstants.CHANNEL_ID_BATTERY, new DecimalType(blukiiData.battery));
52 blukiiData.environment.ifPresent(environment -> {
53 updateState(BlukiiBindingConstants.CHANNEL_ID_TEMPERATURE,
54 new QuantityType<>(environment.temperature, SIUnits.CELSIUS));
55 updateState(BlukiiBindingConstants.CHANNEL_ID_HUMIDITY,
56 new QuantityType<>(environment.humidity, Units.PERCENT));
57 updateState(BlukiiBindingConstants.CHANNEL_ID_PRESSURE,
58 new QuantityType<>(environment.pressure, MetricPrefix.HECTO(SIUnits.PASCAL)));
59 updateState(BlukiiBindingConstants.CHANNEL_ID_LUMINANCE,
60 new QuantityType<>(environment.luminance, Units.LUX));
62 blukiiData.accelerometer.ifPresent(accelerometer -> {
63 updateState(BlukiiBindingConstants.CHANNEL_ID_TILTX,
64 new QuantityType<>(accelerometer.tiltX, Units.DEGREE_ANGLE));
65 updateState(BlukiiBindingConstants.CHANNEL_ID_TILTY,
66 new QuantityType<>(accelerometer.tiltY, Units.DEGREE_ANGLE));
67 updateState(BlukiiBindingConstants.CHANNEL_ID_TILTZ,
68 new QuantityType<>(accelerometer.tiltZ, Units.DEGREE_ANGLE));
70 blukiiData.magnetometer.ifPresent(magnetometer -> {
71 // It isn't easy to get a heading from these values without any calibration, so we ignore those
77 super.onScanRecordReceived(scanNotification);