]> git.basschouten.com Git - openhab-addons.git/blob
813e49cb7e0f84bc50ee7e32d89e4c333c2982be
[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.neohub.internal;
14
15 import static org.openhab.binding.neohub.internal.NeoHubBindingConstants.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 NeoHubDebouncer} determines if change events should be forwarded
25  * to a channel
26  *
27  * @author Andrew Fiddian-Green - Initial contribution
28  */
29 @NonNullByDefault
30 public class NeoHubDebouncer {
31
32     private final Map<String, DebounceDelay> channels = new HashMap<>();
33
34     @SuppressWarnings("null")
35     @NonNullByDefault
36     static class DebounceDelay {
37
38         private long expireTime;
39
40         public DebounceDelay(Boolean enabled) {
41             if (enabled) {
42                 expireTime = new Date().getTime() + (DEBOUNCE_DELAY * 1000);
43             }
44         }
45
46         public boolean timeExpired() {
47             return (expireTime < new Date().getTime());
48         }
49     }
50
51     public NeoHubDebouncer() {
52     }
53
54     public void initialize(String channelId) {
55         channels.put(channelId, new DebounceDelay(true));
56     }
57
58     @SuppressWarnings("null")
59     public boolean timeExpired(String channelId) {
60         return (channels.containsKey(channelId) ? channels.get(channelId).timeExpired() : true);
61     }
62 }