]> git.basschouten.com Git - openhab-addons.git/blob
81bcbff9923dbae30f446ab950fcf7f048a2d1c9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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
34     public EnoceanBleRockerHandler(Thing thing) {
35         super(thing);
36     }
37
38     @Override
39     public void onScanRecordReceived(BluetoothScanNotification scanNotification) {
40         super.onScanRecordReceived(scanNotification);
41         final byte[] manufacturerData = scanNotification.getManufacturerData();
42         logger.debug("Received new scan notification for {}: {}", device.getAddress(), manufacturerData);
43         try {
44             if (manufacturerData != null && manufacturerData.length > 0) {
45                 EnoceanBlePtm215Event event = new EnoceanBlePtm215Event(manufacturerData);
46                 logger.debug("Parsed manufacturer data to PTM215B event: {}", event);
47                 triggerChannel(resolveChannel(event), resolveTriggerEvent(event));
48             }
49         } catch (IllegalStateException e) {
50             logger.warn("PTM215B event could not be parsed correctly, exception occured:", e);
51         }
52     }
53
54     protected String resolveChannel(EnoceanBlePtm215Event event) {
55         if (event.isButton1()) {
56             return EnoceanBleBindingConstants.CHANNEL_ID_ROCKER1;
57         }
58         if (event.isButton2()) {
59             return EnoceanBleBindingConstants.CHANNEL_ID_ROCKER2;
60         }
61         throw new IllegalStateException(
62                 "PTM215B event cannot be resolved correctly to a channel, probably received message is invalid");
63     }
64
65     protected String resolveTriggerEvent(EnoceanBlePtm215Event event) {
66         if (event.isDir1()) {
67             if (event.isPressed()) {
68                 return CommonTriggerEvents.DIR1_PRESSED;
69             } else {
70                 return CommonTriggerEvents.DIR1_RELEASED;
71             }
72         }
73         if (event.isDir2()) {
74             if (event.isPressed()) {
75                 return CommonTriggerEvents.DIR2_PRESSED;
76             } else {
77                 return CommonTriggerEvents.DIR2_RELEASED;
78             }
79         }
80         throw new IllegalStateException(
81                 "PTM215B event cannot be resolved correctly to an openHAB event, probably received message is invalid");
82     }
83 }