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.internal.data;
15 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.core.util.HexUtils;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * Blukii data decoding.
26 * @author Markus Rathgeb - Initial contribution (migrate from handler)
27 * @author Markus Rathgeb - Fixed temperature decoding
30 public class BlukiiDataDecoder {
32 private final Logger logger = LoggerFactory.getLogger(BlukiiDataDecoder.class);
34 public @Nullable BlukiiData decode(final byte[] data) {
35 if (data.length < 22) {
36 logger.debug("Blukii data length to short (skip decoding): {}", HexUtils.bytesToHex(data, " "));
40 if (data[0] != 0x4F) {
44 if (logger.isDebugEnabled()) {
45 logger.debug("Decode Blukii data: {}", HexUtils.bytesToHex(data, " "));
48 final int battery = data[12] & 0x7F;
49 final Optional<Magnetometer> magnetometer;
50 final Optional<Environment> environment;
51 final Optional<Accelerometer> accelerometer;
53 if ((data[14] & 0x30) == 0x30) {
54 magnetometer = Optional.of(processMagnetometerData(data));
55 environment = Optional.empty();
56 accelerometer = Optional.empty();
57 } else if ((data[14] & 0x10) == 0x10) {
58 magnetometer = Optional.empty();
59 environment = Optional.of(processEnvironmentData(data));
60 accelerometer = Optional.empty();
61 } else if ((data[14] & 0x20) == 0x20) {
62 magnetometer = Optional.empty();
63 environment = Optional.empty();
64 accelerometer = Optional.of(processAccelerometerData(data));
66 magnetometer = Optional.empty();
67 environment = Optional.empty();
68 accelerometer = Optional.empty();
71 return new BlukiiData(battery, magnetometer, environment, accelerometer);
74 private static Environment processEnvironmentData(byte[] data) {
75 double pressure = doubleByteToInt(data[15], data[16]) / 10;
76 int luminance = doubleByteToInt(data[17], data[18]);
77 int humidity = data[19] & 0xFF;
78 double temperature = (data[20] << 8 | data[21] & 0xFF) / 256d;
80 return new Environment(pressure, luminance, humidity, temperature);
83 private static Accelerometer processAccelerometerData(byte[] data) {
84 int range = data[15] & 0xFF;
85 int x = (short) doubleByteToInt(data[16], data[17]) * (range / 2);
86 int y = (short) doubleByteToInt(data[18], data[19]) * (range / 2);
87 int z = (short) doubleByteToInt(data[20], data[21]) * (range / 2);
89 double tiltX = 180 * Math.acos(x / Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2))) / Math.PI;
90 double tiltY = 180 * Math.acos(y / Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2))) / Math.PI;
91 double tiltZ = 180 * Math.acos(z / Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2))) / Math.PI;
93 return new Accelerometer(tiltX, tiltY, tiltZ);
96 private static Magnetometer processMagnetometerData(byte[] data) {
97 int x = (short) doubleByteToInt(data[16], data[17]);
98 int y = (short) doubleByteToInt(data[18], data[19]);
99 int z = (short) doubleByteToInt(data[20], data[21]);
101 return new Magnetometer(x, y, z);
104 private static int doubleByteToInt(byte b1, byte b2) {
107 return (i1 * 0x100) + i2;