]> git.basschouten.com Git - openhab-addons.git/blob
7a36cfc83f3e949704a77c4896c8eef4b6f58e22
[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.internal.data;
14
15 import java.util.Optional;
16
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;
22
23 /**
24  * Blukii data decoding.
25  *
26  * @author Markus Rathgeb - Initial contribution (migrate from handler)
27  * @author Markus Rathgeb - Fixed temperature decoding
28  */
29 @NonNullByDefault
30 public class BlukiiDataDecoder {
31
32     private final Logger logger = LoggerFactory.getLogger(BlukiiDataDecoder.class);
33
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, " "));
37             return null;
38         }
39
40         if (data[0] != 0x4F) {
41             return null;
42         }
43
44         if (logger.isDebugEnabled()) {
45             logger.debug("Decode Blukii data: {}", HexUtils.bytesToHex(data, " "));
46         }
47
48         final int battery = data[12] & 0x7F;
49         final Optional<Magnetometer> magnetometer;
50         final Optional<Environment> environment;
51         final Optional<Accelerometer> accelerometer;
52
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));
65         } else {
66             magnetometer = Optional.empty();
67             environment = Optional.empty();
68             accelerometer = Optional.empty();
69         }
70
71         return new BlukiiData(battery, magnetometer, environment, accelerometer);
72     }
73
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;
79
80         return new Environment(pressure, luminance, humidity, temperature);
81     }
82
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);
88
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;
92
93         return new Accelerometer(tiltX, tiltY, tiltZ);
94     }
95
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]);
100
101         return new Magnetometer(x, y, z);
102     }
103
104     private static int doubleByteToInt(byte b1, byte b2) {
105         int i1 = b1 & 0xFF;
106         int i2 = b2 & 0xFF;
107         return (i1 * 0x100) + i2;
108     }
109 }