]> git.basschouten.com Git - openhab-addons.git/blob
0cccc23a96bb971b2fdccb5b4be0b87cda1eb122
[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.enoceanble.internal;
14
15 import java.nio.ByteBuffer;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18
19 /**
20  * The {@link EnoceanBlePtm215Event} class is parsing the BLE manufacturer data into an event object.
21  *
22  * @author Patrick Fink - Initial contribution
23  */
24 @NonNullByDefault
25 public class EnoceanBlePtm215Event {
26
27     private static final byte PRESSED = 0x1;
28
29     private static final byte BUTTON1_DIR1 = 0x10;
30     private static final byte BUTTON1_DIR2 = 0x8;
31     private static final byte BUTTON2_DIR1 = 0x4;
32     private static final byte BUTTON2_DIR2 = 0x2;
33
34     private final byte byteState;
35     private final int sequence;
36
37     public EnoceanBlePtm215Event(byte[] manufacturerData) {
38         byteState = manufacturerData[6];
39
40         byte[] sequenceBytes = new byte[] { manufacturerData[5], manufacturerData[4], manufacturerData[3],
41                 manufacturerData[2] };
42         ByteBuffer sequenceBytesBuffered = ByteBuffer.wrap(sequenceBytes); // big-endian by default
43         sequence = sequenceBytesBuffered.getInt();
44     }
45
46     public boolean isPressed() {
47         return checkFlag(PRESSED);
48     }
49
50     public boolean isButton1() {
51         return checkFlag(BUTTON1_DIR1) || checkFlag(BUTTON1_DIR2);
52     }
53
54     public boolean isButton2() {
55         return checkFlag(BUTTON2_DIR1) || checkFlag(BUTTON2_DIR2);
56     }
57
58     public boolean isDir1() {
59         return checkFlag(BUTTON1_DIR1) || checkFlag(BUTTON2_DIR1);
60     }
61
62     public boolean isDir2() {
63         return checkFlag(BUTTON1_DIR2) || checkFlag(BUTTON2_DIR2);
64     }
65
66     private boolean checkFlag(int flag) {
67         return (byteState & flag) == flag;
68     }
69
70     public int getSequence() {
71         return sequence;
72     }
73
74     @Override
75     public String toString() {
76         return "Button " + (isButton1() ? 1 : 2) + " Dir " + (isDir1() ? 1 : 2) + " "
77                 + (isPressed() ? "PRESSED" : "RELEASED") + " (seq. " + this.sequence + ")";
78     }
79 }