]> git.basschouten.com Git - openhab-addons.git/blob
a48874c18ff951c12869bedef86907b21fc658a4
[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.gpio.internal.handler;
14
15 import java.util.function.Consumer;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.gpio.internal.NoGpioIdException;
19 import org.openhab.binding.gpio.internal.configuration.GPIOOutputConfiguration;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.types.Command;
22 import org.openhab.core.types.RefreshType;
23 import org.openhab.core.types.State;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import eu.xeli.jpigpio.GPIO;
28 import eu.xeli.jpigpio.JPigpio;
29 import eu.xeli.jpigpio.PigpioException;
30
31 /**
32  * Thing Handler for digital GPIO outputs.
33  *
34  * @author Nils Bauer - Initial contribution
35  * @author Jan N. Klug - Channel redesign
36  */
37 @NonNullByDefault
38 public class PigpioDigitalOutputHandler implements ChannelHandler {
39
40     /** The logger. */
41     private final Logger logger = LoggerFactory.getLogger(PigpioDigitalOutputHandler.class);
42
43     private final GPIOOutputConfiguration configuration;
44     private final GPIO gpio;
45     private final Consumer<State> updateStatus;
46
47     /**
48      * Constructor for PigpioDigitalOutputHandler
49      * 
50      * @param configuration The channel configuration
51      * @param jPigpio The jPigpio instance
52      * @param updateStatus Is called when the state should be changed
53      * 
54      * @throws PigpioException Can be thrown by Pigpio
55      * @throws NoGpioIdException Is thrown when no gpioId is defined
56      */
57     public PigpioDigitalOutputHandler(GPIOOutputConfiguration configuration, JPigpio jPigpio,
58             Consumer<State> updateStatus) throws PigpioException, NoGpioIdException {
59         this.configuration = configuration;
60         this.updateStatus = updateStatus;
61         Integer gpioId = configuration.gpioId;
62         if (gpioId == null) {
63             throw new NoGpioIdException();
64         }
65         this.gpio = new GPIO(jPigpio, gpioId, JPigpio.PI_OUTPUT);
66     }
67
68     @Override
69     public void handleCommand(Command command) {
70         if (command instanceof RefreshType) {
71             try {
72                 updateStatus.accept(OnOffType.from(configuration.invert != gpio.getValue()));
73             } catch (PigpioException e) {
74                 logger.warn("Unknown pigpio exception while handling Refresh", e);
75             }
76         }
77         if (command instanceof OnOffType) {
78             try {
79                 gpio.setValue(configuration.invert != (OnOffType.ON.equals(command)));
80             } catch (PigpioException e) {
81                 logger.warn("An error occured while changing the gpio value: {}", e.getMessage());
82             }
83         }
84     }
85 }