]> git.basschouten.com Git - openhab-addons.git/blob
e37a2321677be3ae87929fbe13c38ded14131c53
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.blukii.handler;
14
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;
28
29 /**
30  * The {@link BlukiiHandler} is responsible for handling commands, which are
31  * sent to one of the channels.
32  *
33  * @author Kai Kreuzer - Initial contribution and API
34  * @author Markus Rathgeb - Split data (decoding and types) and handler
35  */
36 @NonNullByDefault
37 public class BlukiiHandler extends BeaconBluetoothHandler implements BluetoothDeviceListener {
38
39     private final BlukiiDataDecoder decoder = new BlukiiDataDecoder();
40
41     public BlukiiHandler(Thing thing) {
42         super(thing);
43     }
44
45     @Override
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));
61                 });
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));
69                 });
70                 blukiiData.magnetometer.ifPresent(magnetometer -> {
71                     // It isn't easy to get a heading from these values without any calibration, so we ignore those
72                     // right
73                     // now.
74                 });
75             }
76         }
77         super.onScanRecordReceived(scanNotification);
78     }
79 }