]> git.basschouten.com Git - openhab-addons.git/blob
1c33e0896dafe3cd8ccf81373c8f7672f39b448a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.flicbutton.internal.handler;
14
15 import java.io.IOException;
16 import java.util.concurrent.Semaphore;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.flicbutton.internal.FlicButtonBindingConstants;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import io.flic.fliclib.javaclient.ButtonConnectionChannel;
25 import io.flic.fliclib.javaclient.enums.ClickType;
26 import io.flic.fliclib.javaclient.enums.ConnectionStatus;
27 import io.flic.fliclib.javaclient.enums.CreateConnectionChannelError;
28 import io.flic.fliclib.javaclient.enums.DisconnectReason;
29 import io.flic.fliclib.javaclient.enums.RemovedReason;
30
31 /**
32  * Each {@link FlicButtonEventListener} object listens to events of a specific Flic button and calls the
33  * associated {@link FlicButtonHandler} back accordingly.
34  *
35  * @author Patrick Fink - Initial contribution
36  *
37  */
38 @NonNullByDefault
39 public class FlicButtonEventListener extends ButtonConnectionChannel.Callbacks {
40     private final Logger logger = LoggerFactory.getLogger(FlicButtonEventListener.class);
41
42     private final FlicButtonHandler thingHandler;
43     private final Semaphore channelResponseSemaphore = new Semaphore(0);
44
45     FlicButtonEventListener(FlicButtonHandler thingHandler) {
46         this.thingHandler = thingHandler;
47     }
48
49     public Semaphore getChannelResponseSemaphore() {
50         return channelResponseSemaphore;
51     }
52
53     @Override
54     public synchronized void onCreateConnectionChannelResponse(@Nullable ButtonConnectionChannel channel,
55             @Nullable CreateConnectionChannelError createConnectionChannelError,
56             @Nullable ConnectionStatus connectionStatus) {
57         logger.debug("Create response {}: {}, {}", channel.getBdaddr(), createConnectionChannelError, connectionStatus);
58         // Handling does not differ from Status change, so redirect
59         if (connectionStatus != null) {
60             thingHandler.initializeStatus(connectionStatus);
61             channelResponseSemaphore.release();
62         }
63     }
64
65     @Override
66     public void onRemoved(@Nullable ButtonConnectionChannel channel, @Nullable RemovedReason removedReason) {
67         thingHandler.flicButtonRemoved();
68         logger.debug("Button {} removed. ThingStatus updated to OFFLINE. Reason: {}", channel.getBdaddr(),
69                 removedReason);
70     }
71
72     @Override
73     public void onConnectionStatusChanged(@Nullable ButtonConnectionChannel channel,
74             @Nullable ConnectionStatus connectionStatus, @Nullable DisconnectReason disconnectReason) {
75         logger.trace("New status for {}: {}", channel.getBdaddr(),
76                 connectionStatus + (connectionStatus == ConnectionStatus.Disconnected ? ", " + disconnectReason : ""));
77         if (connectionStatus != null) {
78             thingHandler.connectionStatusChanged(connectionStatus, disconnectReason);
79         }
80     }
81
82     @Override
83     public void onButtonUpOrDown(@Nullable ButtonConnectionChannel channel, @Nullable ClickType clickType,
84             boolean wasQueued, int timeDiff) throws IOException {
85         if (channel != null && clickType != null) {
86             logger.trace("{} {}", channel.getBdaddr(), clickType.name());
87             String commonTriggerEvent = FlicButtonBindingConstants.FLIC_OPENHAB_TRIGGER_EVENT_MAP.get(clickType.name());
88             if (commonTriggerEvent != null) {
89                 thingHandler.fireTriggerEvent(commonTriggerEvent);
90             }
91         }
92     }
93
94     @Override
95     public void onButtonSingleOrDoubleClickOrHold(@Nullable ButtonConnectionChannel channel,
96             @Nullable ClickType clickType, boolean wasQueued, int timeDiff) throws IOException {
97         // Handling does not differ from up/down events, so redirect
98         if (channel != null && clickType != null) {
99             onButtonUpOrDown(channel, clickType, wasQueued, timeDiff);
100         }
101     }
102 }