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.linuxinput.internal;
15 import java.io.IOException;
16 import java.util.concurrent.CancellationException;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.core.thing.Thing;
21 import org.openhab.core.thing.ThingStatus;
22 import org.openhab.core.thing.ThingStatusDetail;
23 import org.openhab.core.thing.binding.BaseThingHandler;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Abstract handler, that encapsulates the lifecycle of an underlying device.
30 * @author Thomas Weißschuh - Initial contribution
33 public abstract class DeviceReadingHandler extends BaseThingHandler {
34 private final Logger logger = LoggerFactory.getLogger(DeviceReadingHandler.class);
36 private @Nullable Thread worker = null;
38 public DeviceReadingHandler(Thing thing) {
42 abstract boolean immediateSetup() throws IOException;
44 abstract boolean delayedSetup() throws IOException;
46 abstract void handleEventsInThread() throws IOException;
48 abstract void closeDevice() throws IOException;
50 abstract String getInstanceName();
53 public final void initialize() {
54 boolean performDelayedSetup = performImmediateSetup();
55 if (performDelayedSetup) {
56 scheduler.execute(() -> {
57 boolean handleEvents = performDelayedSetup();
59 Thread thread = Utils.backgroundThread(() -> {
61 handleEventsInThread();
62 } catch (IOException e) {
63 logger.warn("Could not read event", e);
64 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
74 private boolean performImmediateSetup() {
76 return immediateSetup();
77 } catch (IOException e) {
83 private boolean performDelayedSetup() {
85 return delayedSetup();
86 } catch (IOException e) {
92 private void handleSetupError(Exception e) {
93 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
97 public final void dispose() {
100 } catch (InterruptedException e) {
101 Thread.currentThread().interrupt();
105 } catch (IOException e) {
106 logger.debug("Could not close device", e);
108 logger.trace("disposed");
112 private void stopWorker() throws InterruptedException {
114 Thread activeWorker = this.worker;
115 logger.debug("interrupting worker {}", activeWorker);
118 if (activeWorker == null) {
121 activeWorker.interrupt();
123 activeWorker.join(100);
124 } catch (CancellationException e) {
127 logger.debug("worker interrupted");
128 if (activeWorker.isAlive()) {
129 logger.warn("Worker not stopped");