]> git.basschouten.com Git - openhab-addons.git/blob
7f625ed7f1688f935795b7c462d296e42c25052a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.linuxinput.internal;
14
15 import java.io.IOException;
16 import java.util.concurrent.CancellationException;
17
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;
26
27 /**
28  * Abstract handler, that encapsulates the lifecycle of an underlying device.
29  *
30  * @author Thomas Weißschuh - Initial contribution
31  */
32 @NonNullByDefault
33 public abstract class DeviceReadingHandler extends BaseThingHandler {
34     private final Logger logger = LoggerFactory.getLogger(DeviceReadingHandler.class);
35
36     private @Nullable Thread worker = null;
37
38     public DeviceReadingHandler(Thing thing) {
39         super(thing);
40     }
41
42     abstract boolean immediateSetup() throws IOException;
43
44     abstract boolean delayedSetup() throws IOException;
45
46     abstract void handleEventsInThread() throws IOException;
47
48     abstract void closeDevice() throws IOException;
49
50     abstract String getInstanceName();
51
52     @Override
53     public final void initialize() {
54         boolean performDelayedSetup = performImmediateSetup();
55         if (performDelayedSetup) {
56             scheduler.execute(() -> {
57                 boolean handleEvents = performDelayedSetup();
58                 if (handleEvents) {
59                     Thread thread = Utils.backgroundThread(() -> {
60                         try {
61                             handleEventsInThread();
62                         } catch (IOException e) {
63                             logger.warn("Could not read event", e);
64                             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
65                         }
66                     }, "events", thing);
67                     thread.start();
68                     worker = thread;
69                 }
70             });
71         }
72     }
73
74     private boolean performImmediateSetup() {
75         try {
76             return immediateSetup();
77         } catch (IOException e) {
78             handleSetupError(e);
79             return false;
80         }
81     }
82
83     private boolean performDelayedSetup() {
84         try {
85             return delayedSetup();
86         } catch (IOException e) {
87             handleSetupError(e);
88             return false;
89         }
90     }
91
92     private void handleSetupError(Exception e) {
93         updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
94     }
95
96     @Override
97     public final void dispose() {
98         try {
99             stopWorker();
100         } catch (InterruptedException e) {
101             Thread.currentThread().interrupt();
102         } finally {
103             try {
104                 closeDevice();
105             } catch (IOException e) {
106                 logger.debug("Could not close device", e);
107             }
108             logger.trace("disposed");
109         }
110     }
111
112     private void stopWorker() throws InterruptedException {
113         @Nullable
114         Thread activeWorker = this.worker;
115         logger.debug("interrupting worker {}", activeWorker);
116         worker = null;
117
118         if (activeWorker == null) {
119             return;
120         }
121         activeWorker.interrupt();
122         try {
123             activeWorker.join(100);
124         } catch (CancellationException e) {
125             /* expected */
126         }
127         logger.debug("worker interrupted");
128         if (activeWorker.isAlive()) {
129             logger.warn("Worker not stopped");
130         }
131     }
132 }