]> git.basschouten.com Git - openhab-addons.git/blob
97a4cc6d11fba6c2a6cb8d07a736ef547b8c81cf
[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.jeelink.internal;
14
15 import java.util.concurrent.ScheduledExecutorService;
16 import java.util.concurrent.ScheduledFuture;
17 import java.util.concurrent.TimeUnit;
18
19 import org.openhab.binding.jeelink.internal.config.JeeLinkSensorConfig;
20 import org.openhab.core.thing.ChannelUID;
21 import org.openhab.core.thing.Thing;
22 import org.openhab.core.thing.ThingStatus;
23 import org.openhab.core.thing.binding.BaseThingHandler;
24 import org.openhab.core.types.Command;
25
26 /**
27  * Abstract thing handler for sensors connected to a JeeLink.
28  *
29  * @author Volker Bier - Initial contribution
30  */
31 public abstract class JeeLinkSensorHandler<R extends Reading> extends BaseThingHandler implements ReadingHandler<R> {
32     protected String id;
33     protected final String sensorType;
34
35     private ReadingPublisher<R> publisher;
36     private long secsSinceLastReading;
37     private ScheduledFuture<?> statusUpdateJob;
38
39     public JeeLinkSensorHandler(Thing thing, String sensorType) {
40         super(thing);
41         this.sensorType = sensorType;
42     }
43
44     public abstract ReadingPublisher<R> createPublisher();
45
46     @Override
47     public String getSensorType() {
48         return sensorType;
49     }
50
51     @Override
52     public void handleReading(R r) {
53         if (r != null && id.equals(r.getSensorId())) {
54             secsSinceLastReading = 0;
55             updateStatus(ThingStatus.ONLINE);
56
57             if (publisher != null) {
58                 publisher.publish(r);
59             }
60         }
61     }
62
63     @Override
64     public synchronized void handleCommand(ChannelUID channelUid, Command command) {
65     }
66
67     @Override
68     public synchronized void initialize() {
69         JeeLinkHandler jlh = (JeeLinkHandler) getBridge().getHandler();
70         jlh.addReadingHandler(this);
71
72         JeeLinkSensorConfig cfg = getConfigAs(JeeLinkSensorConfig.class);
73         id = cfg.sensorId;
74
75         statusUpdateJob = createStatusUpdateJob(scheduler, cfg.sensorTimeout);
76
77         publisher = createPublisher();
78
79         updateStatus(ThingStatus.UNKNOWN);
80     }
81
82     @Override
83     public synchronized void dispose() {
84         id = null;
85
86         JeeLinkHandler jlh = (JeeLinkHandler) getBridge().getHandler();
87         jlh.removeReadingHandler(this);
88
89         if (statusUpdateJob != null) {
90             statusUpdateJob.cancel(true);
91             statusUpdateJob = null;
92         }
93
94         if (publisher != null) {
95             publisher.dispose();
96             publisher = null;
97         }
98
99         super.dispose();
100     }
101
102     private ScheduledFuture<?> createStatusUpdateJob(ScheduledExecutorService execService, final int sensorTimeout) {
103         return execService.scheduleWithFixedDelay(() -> {
104             if (secsSinceLastReading++ > sensorTimeout) {
105                 updateStatus(ThingStatus.OFFLINE);
106             }
107         }, sensorTimeout, 1, TimeUnit.SECONDS);
108     }
109 }