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