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