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