Skip to content

Commit

Permalink
[bluetooth.enoceanble] Prevent that channels are triggered multiple t…
Browse files Browse the repository at this point in the history
…imes per click (openhab#11381)

Signed-off-by: Patrick Fink <[email protected]>
Signed-off-by: Dave J Schoepel <[email protected]>
  • Loading branch information
pfink authored and dschoepel committed Nov 9, 2021
1 parent 894ae20 commit 9cd88c2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package org.openhab.binding.bluetooth.enoceanble.internal;

import java.nio.ByteBuffer;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
Expand All @@ -30,9 +32,15 @@ public class EnoceanBlePtm215Event {
private static final byte BUTTON2_DIR2 = 0x2;

private final byte byteState;
private final int sequence;

public EnoceanBlePtm215Event(byte[] manufacturerData) {
byteState = manufacturerData[6];

byte[] sequenceBytes = new byte[] { manufacturerData[5], manufacturerData[4], manufacturerData[3],
manufacturerData[2] };
ByteBuffer sequenceBytesBuffered = ByteBuffer.wrap(sequenceBytes); // big-endian by default
sequence = sequenceBytesBuffered.getInt();
}

public boolean isPressed() {
Expand All @@ -59,9 +67,13 @@ private boolean checkFlag(int flag) {
return (byteState & flag) == flag;
}

public int getSequence() {
return sequence;
}

@Override
public String toString() {
return "Button " + (isButton1() ? 1 : 2) + " Dir " + (isDir1() ? 1 : 2) + " "
+ (isPressed() ? "PRESSED" : "RELEASED");
+ (isPressed() ? "PRESSED" : "RELEASED") + " (seq. " + this.sequence + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
public class EnoceanBleRockerHandler extends BeaconBluetoothHandler {

private final Logger logger = LoggerFactory.getLogger(EnoceanBleRockerHandler.class);
private int lastSequence = Integer.MIN_VALUE;

public EnoceanBleRockerHandler(Thing thing) {
super(thing);
Expand All @@ -44,7 +45,12 @@ public void onScanRecordReceived(BluetoothScanNotification scanNotification) {
if (manufacturerData != null && manufacturerData.length > 0) {
EnoceanBlePtm215Event event = new EnoceanBlePtm215Event(manufacturerData);
logger.debug("Parsed manufacturer data to PTM215B event: {}", event);
triggerChannel(resolveChannel(event), resolveTriggerEvent(event));
synchronized (this) {
if (event.getSequence() > lastSequence) {
lastSequence = event.getSequence();
triggerChannel(resolveChannel(event), resolveTriggerEvent(event));
}
}
}
} catch (IllegalStateException e) {
logger.warn("PTM215B event could not be parsed correctly, exception occured:", e);
Expand Down

0 comments on commit 9cd88c2

Please sign in to comment.