2 * Copyright (c) 2010-2023 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.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.flicbutton.internal.discovery.FlicButtonDiscoveryService;
25 import org.openhab.core.thing.Bridge;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.thing.ThingStatusDetail;
29 import org.openhab.core.thing.binding.BaseBridgeHandler;
30 import org.openhab.core.types.Command;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import io.flic.fliclib.javaclient.FlicClient;
37 * The {@link FlicDaemonBridgeHandler} handles a running instance of the fliclib-linux-hci server (flicd).
39 * @author Patrick Fink - Initial contribution
42 public class FlicDaemonBridgeHandler extends BaseBridgeHandler {
43 private final Logger logger = LoggerFactory.getLogger(FlicDaemonBridgeHandler.class);
44 private static final long REINITIALIZE_DELAY_SECONDS = 10;
46 private @Nullable FlicDaemonBridgeConfiguration cfg;
48 private FlicButtonDiscoveryService buttonDiscoveryService;
49 private @Nullable Future<?> flicClientFuture;
51 private Collection<@Nullable Future<?>> startedTasks = new ArrayList<>(3);
52 private @Nullable FlicClient flicClient;
54 public FlicDaemonBridgeHandler(Bridge bridge, FlicButtonDiscoveryService buttonDiscoveryService) {
56 this.buttonDiscoveryService = buttonDiscoveryService;
59 public @Nullable FlicClient getFlicClient() {
64 public void initialize() {
65 startedTasks.add(scheduler.submit(this::initializeThing));
68 public void initializeThing() {
70 initConfigParameters();
71 startFlicdClientAsync();
72 activateButtonDiscoveryService();
74 } catch (UnknownHostException ignored) {
75 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Hostname wrong or unknown!");
76 } catch (IOException e) {
77 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
78 "Error connecting to flicd: " + e.getMessage());
80 scheduleReinitialize();
84 private void initConfigParameters() {
85 cfg = getConfigAs(FlicDaemonBridgeConfiguration.class);
88 private void activateButtonDiscoveryService() {
89 FlicClient flicClient = this.flicClient;
90 if (flicClient != null) {
91 buttonDiscoveryService.activate(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