]> git.basschouten.com Git - openhab-addons.git/blob
31a0c01dae438fd17f8490e55765843f8edc2097
[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.siemensrds.internal;
14
15 import static org.openhab.binding.siemensrds.internal.RdsBindingConstants.DEBOUNCE_DELAY;
16
17 import java.util.Date;
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22
23 /**
24  * The {@link RdsDebouncer} determines if change events should be forwarded to a
25  * channel
26  *
27  * @author Andrew Fiddian-Green - Initial contribution
28  */
29 @NonNullByDefault
30 public class RdsDebouncer {
31
32     private final Map<String, DebounceDelay> channels = new HashMap<>();
33
34     static class DebounceDelay {
35
36         private long expireTime;
37
38         public DebounceDelay(boolean enabled) {
39             if (enabled) {
40                 expireTime = new Date().getTime() + (DEBOUNCE_DELAY * 1000);
41             }
42         }
43
44         public boolean timeExpired() {
45             return (expireTime < new Date().getTime());
46         }
47     }
48
49     public RdsDebouncer() {
50     }
51
52     public void initialize(String channelId) {
53         channels.put(channelId, new DebounceDelay(true));
54     }
55
56     public Boolean timeExpired(String channelId) {
57         if (channels.containsKey(channelId)) {
58             DebounceDelay debounceDelay = channels.get(channelId);
59             if (debounceDelay != null) {
60                 return debounceDelay.timeExpired();
61             }
62         }
63         return true;
64     }
65 }