]> git.basschouten.com Git - openhab-addons.git/blob
b27401ace3ba08f3db2b3b390fbd23171346d678
[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.homematic.internal.misc;
14
15 import java.io.IOException;
16 import java.util.HashMap;
17 import java.util.Map;
18 import java.util.Timer;
19 import java.util.TimerTask;
20
21 import org.openhab.binding.homematic.internal.model.HmDatapointInfo;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Executes a callback method either immediately or after a given delay for a datapoint.
27  *
28  * @author Gerhard Riegler - Initial contribution
29  */
30 public class DelayedExecuter {
31     private final Logger logger = LoggerFactory.getLogger(DelayedExecuter.class);
32
33     private Map<HmDatapointInfo, Timer> delayedEvents = new HashMap<>();
34
35     /**
36      * Executes a callback method either immediately or after a given delay.
37      */
38     public void start(final HmDatapointInfo dpInfo, final double delay, final DelayedExecuterCallback callback)
39             throws IOException, HomematicClientException {
40         if (delay > 0.0) {
41             synchronized (DelayedExecuter.class) {
42                 logger.debug("Delaying event for {} seconds: '{}'", delay, dpInfo);
43
44                 Timer timer = delayedEvents.get(dpInfo);
45                 if (timer != null) {
46                     timer.cancel();
47                 }
48
49                 timer = new Timer();
50                 delayedEvents.put(dpInfo, timer);
51                 timer.schedule(new TimerTask() {
52                     @Override
53                     public void run() {
54                         logger.debug("Executing delayed event for '{}'", dpInfo);
55                         delayedEvents.remove(dpInfo);
56                         try {
57                             callback.execute();
58                         } catch (Exception ex) {
59                             logger.error("{}", ex.getMessage(), ex);
60                         }
61                     }
62                 }, (long) (delay * 1000));
63             }
64         } else {
65             callback.execute();
66         }
67     }
68
69     /**
70      * Stops all delayed events.
71      */
72     public void stop() {
73         for (Timer timer : delayedEvents.values()) {
74             timer.cancel();
75         }
76         delayedEvents.clear();
77     }
78
79     /**
80      * Callback interface for the {@link DelayedExecuter}.
81      *
82      * @author Gerhard Riegler - Initial contribution
83      */
84     public interface DelayedExecuterCallback {
85
86         public void execute() throws IOException, HomematicClientException;
87     }
88 }