]> git.basschouten.com Git - openhab-addons.git/blob
c103800a168ab8f5b9d22c560a4373997b507cd7
[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.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 final Runnable scheduledRunnable = () -> {
49         try {
50             lock.lock();
51             logger.debug("Running '{}' Plugwise task for {} ({})", getName(), deviceType, macAddress);
52             runTask();
53         } catch (Exception e) {
54             logger.warn("Error while running '{}' Plugwise task for {} ({})", getName(), deviceType, macAddress, e);
55         } finally {
56             lock.unlock();
57         }
58     };
59
60     protected PlugwiseDeviceTask(String name, ScheduledExecutorService scheduler) {
61         this.name = name;
62         this.scheduler = scheduler;
63     }
64
65     public abstract Duration getConfiguredInterval();
66
67     public @Nullable Duration getInterval() {
68         return interval;
69     }
70
71     public String getName() {
72         return name;
73     }
74
75     public boolean isScheduled() {
76         return future != null && !future.isCancelled();
77     }
78
79     public abstract void runTask();
80
81     public abstract boolean shouldBeScheduled();
82
83     public void start() {
84         try {
85             lock.lock();
86             if (!isScheduled()) {
87                 Duration configuredInterval = getConfiguredInterval();
88                 future = scheduler.scheduleWithFixedDelay(scheduledRunnable, 0, configuredInterval.getSeconds(),
89                         TimeUnit.SECONDS);
90                 interval = configuredInterval;
91                 logger.debug("Scheduled '{}' Plugwise task for {} ({}) with {} seconds interval", name, deviceType,
92                         macAddress, configuredInterval.getSeconds());
93             }
94         } finally {
95             lock.unlock();
96         }
97     }
98
99     public void stop() {
100         try {
101             lock.lock();
102             if (isScheduled()) {
103                 ScheduledFuture<?> localFuture = future;
104                 if (localFuture != null) {
105                     localFuture.cancel(true);
106                 }
107                 future = null;
108                 logger.debug("Stopped '{}' Plugwise task for {} ({})", name, deviceType, macAddress);
109             }
110         } finally {
111             lock.unlock();
112         }
113     }
114
115     public void update(DeviceType deviceType, @Nullable MACAddress macAddress) {
116         this.deviceType = deviceType;
117         this.macAddress = macAddress;
118     }
119 }