]> git.basschouten.com Git - openhab-addons.git/blob
dfd8dfa7200f3993c170828b765db36ccbe5b3dc
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.gpio.internal.handler;
14
15 import java.util.Date;
16 import java.util.concurrent.ScheduledExecutorService;
17 import java.util.concurrent.TimeUnit;
18 import java.util.function.Consumer;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.gpio.internal.NoGpioIdException;
22 import org.openhab.binding.gpio.internal.configuration.GPIOInputConfiguration;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.types.Command;
25 import org.openhab.core.types.RefreshType;
26 import org.openhab.core.types.State;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import eu.xeli.jpigpio.GPIO;
31 import eu.xeli.jpigpio.JPigpio;
32 import eu.xeli.jpigpio.PigpioException;
33
34 /**
35  * Thing Handler for digital GPIO inputs.
36  *
37  * @author Nils Bauer - Initial contribution
38  * @author Jan N. Klug - Channel redesign
39  */
40 @NonNullByDefault
41 public class PigpioDigitalInputHandler implements ChannelHandler {
42
43     private final Logger logger = LoggerFactory.getLogger(PigpioDigitalInputHandler.class);
44     private Date lastChanged = new Date();
45
46     private final GPIOInputConfiguration configuration;
47     private final GPIO gpio;
48     private final Consumer<State> updateStatus;
49
50     public PigpioDigitalInputHandler(GPIOInputConfiguration configuration, JPigpio jPigpio,
51             ScheduledExecutorService scheduler, Consumer<State> updateStatus)
52             throws PigpioException, NoGpioIdException {
53         this.configuration = configuration;
54         this.updateStatus = updateStatus;
55         Integer gpioId = configuration.gpioId;
56         if (gpioId == null) {
57             throw new NoGpioIdException();
58         }
59         gpio = new GPIO(jPigpio, gpioId, JPigpio.PI_INPUT);
60         jPigpio.gpioSetAlertFunc(gpio.getPin(), (gpio, level, tick) -> {
61             lastChanged = new Date();
62             Date thisChange = new Date();
63             scheduler.schedule(() -> afterDebounce(thisChange), configuration.debouncingTime, TimeUnit.MILLISECONDS);
64         });
65         Integer pullupdown = configuration.pullupdown;
66         jPigpio.gpioSetPullUpDown(gpio.getPin(), pullupdown);
67     }
68
69     private void afterDebounce(Date thisChange) {
70         try {
71             // Check if value changed over time
72             if (!thisChange.before(lastChanged)) {
73                 updateStatus.accept(OnOffType.from(configuration.invert != gpio.getValue()));
74             }
75         } catch (PigpioException e) {
76             logger.warn("Unknown pigpio exception", e);
77         }
78     }
79
80     @Override
81     public void handleCommand(Command command) {
82         if (command instanceof RefreshType) {
83             try {
84                 updateStatus.accept(OnOffType.from(configuration.invert != gpio.getValue()));
85             } catch (PigpioException e) {
86                 logger.warn("Unknown pigpio exception while handling Refresh", e);
87             }
88         }
89     }
90 }