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