2 * Copyright (c) 2010-2022 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.flicbutton.internal.handler;
15 import java.io.IOException;
16 import java.util.concurrent.Semaphore;
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;
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;
33 * Each {@link FlicButtonEventListener} object listens to events of a specific Flic button and calls the
34 * associated {@link FlicButtonHandler} back accordingly.
36 * @author Patrick Fink - Initial contribution
40 public class FlicButtonEventListener extends ButtonConnectionChannel.Callbacks {
41 private final Logger logger = LoggerFactory.getLogger(FlicButtonEventListener.class);
43 private final FlicButtonHandler thingHandler;
44 private final Semaphore channelResponseSemaphore = new Semaphore(0);
46 FlicButtonEventListener(FlicButtonHandler thingHandler) {
47 this.thingHandler = thingHandler;
50 public Semaphore getChannelResponseSemaphore() {
51 return channelResponseSemaphore;
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();
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(),
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);
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);
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,