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.net.UnknownHostException;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.concurrent.Future;
20 import java.util.concurrent.TimeUnit;
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.flicbutton.internal.discovery.FlicButtonDiscoveryService;
26 import org.openhab.core.thing.Bridge;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.ThingStatus;
29 import org.openhab.core.thing.ThingStatusDetail;
30 import org.openhab.core.thing.binding.BaseBridgeHandler;
31 import org.openhab.core.types.Command;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import io.flic.fliclib.javaclient.FlicClient;
38 * The {@link FlicDaemonBridgeHandler} handles a running instance of the fliclib-linux-hci server (flicd).
40 * @author Patrick Fink - Initial contribution
43 public class FlicDaemonBridgeHandler extends BaseBridgeHandler {
44 private final Logger logger = LoggerFactory.getLogger(FlicDaemonBridgeHandler.class);
45 private static final long REINITIALIZE_DELAY_SECONDS = 10;
47 private @Nullable FlicDaemonBridgeConfiguration cfg;
49 private FlicButtonDiscoveryService buttonDiscoveryService;
50 private @Nullable Future<?> flicClientFuture;
52 private Collection<@Nullable Future<?>> startedTasks = new ArrayList<>(3);
53 private @Nullable FlicClient flicClient;
55 public FlicDaemonBridgeHandler(Bridge bridge, FlicButtonDiscoveryService buttonDiscoveryService) {
57 this.buttonDiscoveryService = buttonDiscoveryService;
60 public @Nullable FlicClient getFlicClient() {
65 public void initialize() {
66 startedTasks.add(scheduler.submit(this::initializeThing));
69 public void initializeThing() {
71 initConfigParameters();
72 startFlicdClientAsync();
73 activateButtonDiscoveryService();
75 } catch (UnknownHostException ignored) {
76 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Hostname wrong or unknown!");
77 } catch (IOException e) {
78 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
79 "Error connecting to flicd: " + e.getMessage());
81 scheduleReinitialize();
85 private void initConfigParameters() {
86 cfg = getConfigAs(FlicDaemonBridgeConfiguration.class);
89 private void activateButtonDiscoveryService() {
90 if (flicClient != null) {
91 buttonDiscoveryService.activate((@NonNull FlicClient) flicClient);
93 throw new IllegalStateException("flicClient not properly initialized");
97 private void startFlicdClientAsync() throws IOException {
98 flicClient = new FlicClient(cfg.getHost().getHostAddress(), cfg.getPort());
99 Runnable flicClientService = () -> {
101 flicClient.handleEvents();
103 logger.debug("Listening to flicd ended");
104 } catch (IOException e) {
105 logger.debug("Error occured while listening to flicd", e);
107 if (Thread.currentThread().isInterrupted()) {
113 if (!Thread.currentThread().isInterrupted()) {
114 flicClientFuture = scheduler.submit(flicClientService);
115 startedTasks.add(flicClientFuture);
119 private void onClientFailure() {
120 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
121 "flicd client terminated, probably flicd is not reachable anymore.");
123 scheduleReinitialize();
126 private void initThingStatus() {
127 if (!flicClientFuture.isDone()) {
128 updateStatus(ThingStatus.ONLINE);
130 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
131 "flicd client could not be started, probably flicd is not reachable.");
136 public void dispose() {
138 startedTasks.forEach(task -> task.cancel(true));
139 startedTasks = new ArrayList<>(2);
140 buttonDiscoveryService.deactivate();
143 private void scheduleReinitialize() {
144 startedTasks.add(scheduler.schedule(this::initialize, REINITIALIZE_DELAY_SECONDS, TimeUnit.SECONDS));
148 public void handleCommand(ChannelUID channelUID, Command command) {
149 // No commands to the fliclib-linux-hci are supported.
150 // So there is nothing to handle in the bridge handler