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.enoceanble.internal;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.bluetooth.BeaconBluetoothHandler;
17 import org.openhab.binding.bluetooth.notification.BluetoothScanNotification;
18 import org.openhab.core.thing.CommonTriggerEvents;
19 import org.openhab.core.thing.Thing;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * The {@link EnoceanBleRockerHandler} is responsible for handling commands, which are
25 * sent to one of the channels.
27 * @author Patrick Fink - Initial contribution
30 public class EnoceanBleRockerHandler extends BeaconBluetoothHandler {
32 private final Logger logger = LoggerFactory.getLogger(EnoceanBleRockerHandler.class);
33 private int lastSequence = Integer.MIN_VALUE;
35 public EnoceanBleRockerHandler(Thing thing) {
40 public void onScanRecordReceived(BluetoothScanNotification scanNotification) {
41 super.onScanRecordReceived(scanNotification);
42 final byte[] manufacturerData = scanNotification.getManufacturerData();
43 logger.debug("Received new scan notification for {}: {}", device.getAddress(), manufacturerData);
45 if (manufacturerData != null && manufacturerData.length > 0) {
46 EnoceanBlePtm215Event event = new EnoceanBlePtm215Event(manufacturerData);
47 logger.debug("Parsed manufacturer data to PTM215B event: {}", event);
49 if (event.getSequence() > lastSequence) {
50 lastSequence = event.getSequence();
51 triggerChannel(resolveChannel(event), resolveTriggerEvent(event));
55 } catch (IllegalStateException e) {
56 logger.warn("PTM215B event could not be parsed correctly, exception occured:", e);
60 protected String resolveChannel(EnoceanBlePtm215Event event) {
61 if (event.isButton1()) {
62 return EnoceanBleBindingConstants.CHANNEL_ID_ROCKER1;
64 if (event.isButton2()) {
65 return EnoceanBleBindingConstants.CHANNEL_ID_ROCKER2;
67 throw new IllegalStateException(
68 "PTM215B event cannot be resolved correctly to a channel, probably received message is invalid");
71 protected String resolveTriggerEvent(EnoceanBlePtm215Event event) {
73 if (event.isPressed()) {
74 return CommonTriggerEvents.DIR1_PRESSED;
76 return CommonTriggerEvents.DIR1_RELEASED;
80 if (event.isPressed()) {
81 return CommonTriggerEvents.DIR2_PRESSED;
83 return CommonTriggerEvents.DIR2_RELEASED;
86 throw new IllegalStateException(
87 "PTM215B event cannot be resolved correctly to an openHAB event, probably received message is invalid");