]> git.basschouten.com Git - openhab-addons.git/blob
d51c8c398dbd4da47f8ab5f38900944871be6a89
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.plugwise.internal;
14
15 import java.time.Duration;
16 import java.util.concurrent.ScheduledExecutorService;
17 import java.util.concurrent.ScheduledFuture;
18 import java.util.concurrent.TimeUnit;
19 import java.util.concurrent.locks.ReentrantLock;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.plugwise.internal.protocol.field.DeviceType;
24 import org.openhab.binding.plugwise.internal.protocol.field.MACAddress;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * A recurring Plugwise device task that can for instance be extended for updating a channel or setting the clock.
30  *
31  * @author Wouter Born - Initial contribution
32  */
33 @NonNullByDefault
34 public abstract class PlugwiseDeviceTask {
35
36     private final Logger logger = LoggerFactory.getLogger(PlugwiseDeviceTask.class);
37
38     private final ReentrantLock lock = new ReentrantLock();
39     private final String name;
40     private final ScheduledExecutorService scheduler;
41
42     private @Nullable DeviceType deviceType;
43     private @Nullable Duration interval;
44     private @Nullable MACAddress macAddress;
45
46     private @Nullable ScheduledFuture<?> future;
47
48     private Runnable scheduledRunnable = new Runnable() {
49         @Override
50         public void run() {
51             try {
52                 lock.lock();
53                 logger.debug("Running '{}' Plugwise task for {} ({})", name, deviceType, macAddress);
54                 runTask();
55             } catch (Exception e) {
56                 logger.warn("Error while running '{}' Plugwise task for {} ({})", name, deviceType, macAddress, e);
57             } finally {
58                 lock.unlock();
59             }
60         }
61     };
62
63     public PlugwiseDeviceTask(String name, ScheduledExecutorService scheduler) {
64         this.name = name;
65         this.scheduler = scheduler;
66     }
67
68     public abstract Duration getConfiguredInterval();
69
70     public @Nullable Duration getInterval() {
71         return interval;
72     }
73
74     public String getName() {
75         return name;
76     }
77
78     public boolean isScheduled() {
79         return future != null && !future.isCancelled();
80     }
81
82     public abstract void runTask();
83
84     public abstract boolean shouldBeScheduled();
85
86     public void start() {
87         try {
88             lock.lock();
89             if (!isScheduled()) {
90                 Duration configuredInterval = getConfiguredInterval();
91                 future = scheduler.scheduleWithFixedDelay(scheduledRunnable, 0, configuredInterval.getSeconds(),
92                         TimeUnit.SECONDS);
93                 interval = configuredInterval;
94                 logger.debug("Scheduled '{}' Plugwise task for {} ({}) with {} seconds interval", name, deviceType,
95                         macAddress, configuredInterval.getSeconds());
96             }
97         } finally {
98             lock.unlock();
99         }
100     }
101
102     public void stop() {
103         try {
104             lock.lock();
105             if (isScheduled()) {
106                 ScheduledFuture<?> localFuture = future;
107                 if (localFuture != null) {
108                     localFuture.cancel(true);
109                 }
110                 future = null;
111                 logger.debug("Stopped '{}' Plugwise task for {} ({})", name, deviceType, macAddress);
112             }
113         } finally {
114             lock.unlock();
115         }
116     }
117
118     public void update(DeviceType deviceType, @Nullable MACAddress macAddress) {
119         this.deviceType = deviceType;
120         this.macAddress = macAddress;
121     }
122 }