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.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;
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;
32 * Each {@link FlicButtonEventListener} object listens to events of a specific Flic button and calls the
33 * associated {@link FlicButtonHandler} back accordingly.
35 * @author Patrick Fink - Initial contribution
39 public class FlicButtonEventListener extends ButtonConnectionChannel.Callbacks {
40 private final Logger logger = LoggerFactory.getLogger(FlicButtonEventListener.class);
42 private final FlicButtonHandler thingHandler;
43 private final Semaphore channelResponseSemaphore = new Semaphore(0);
45 FlicButtonEventListener(FlicButtonHandler thingHandler) {
46 this.thingHandler = thingHandler;
49 public Semaphore getChannelResponseSemaphore() {
50 return channelResponseSemaphore;
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();
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(),
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);
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);
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);