]> git.basschouten.com Git - openhab-addons.git/blob
fe8034b5c631f5644ad24895dbc77de468a70b18
[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 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;
22
23 /**
24  * The {@link EnoceanBleRockerHandler} is responsible for handling commands, which are
25  * sent to one of the channels.
26  *
27  * @author Patrick Fink - Initial contribution
28  */
29 @NonNullByDefault
30 public class EnoceanBleRockerHandler extends BeaconBluetoothHandler {
31
32     private final Logger logger = LoggerFactory.getLogger(EnoceanBleRockerHandler.class);
33     private int lastSequence = Integer.MIN_VALUE;
34
35     public EnoceanBleRockerHandler(Thing thing) {
36         super(thing);
37     }
38
39     @Override
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);
44         try {
45             if (manufacturerData != null && manufacturerData.length > 0) {
46                 EnoceanBlePtm215Event event = new EnoceanBlePtm215Event(manufacturerData);
47                 logger.debug("Parsed manufacturer data to PTM215B event: {}", event);
48                 synchronized (this) {
49                     if (event.getSequence() > lastSequence) {
50                         lastSequence = event.getSequence();
51                         triggerChannel(resolveChannel(event), resolveTriggerEvent(event));
52                     }
53                 }
54             }
55         } catch (IllegalStateException e) {
56             logger.warn("PTM215B event could not be parsed correctly, exception occured:", e);
57         }
58     }
59
60     protected String resolveChannel(EnoceanBlePtm215Event event) {
61         if (event.isButton1()) {
62             return EnoceanBleBindingConstants.CHANNEL_ID_ROCKER1;
63         }
64         if (event.isButton2()) {
65             return EnoceanBleBindingConstants.CHANNEL_ID_ROCKER2;
66         }
67         throw new IllegalStateException(
68                 "PTM215B event cannot be resolved correctly to a channel, probably received message is invalid");
69     }
70
71     protected String resolveTriggerEvent(EnoceanBlePtm215Event event) {
72         if (event.isDir1()) {
73             if (event.isPressed()) {
74                 return CommonTriggerEvents.DIR1_PRESSED;
75             } else {
76                 return CommonTriggerEvents.DIR1_RELEASED;
77             }
78         }
79         if (event.isDir2()) {
80             if (event.isPressed()) {
81                 return CommonTriggerEvents.DIR2_PRESSED;
82             } else {
83                 return CommonTriggerEvents.DIR2_RELEASED;
84             }
85         }
86         throw new IllegalStateException(
87                 "PTM215B event cannot be resolved correctly to an openHAB event, probably received message is invalid");
88     }
89 }