2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.gpio.internal.handler;
15 import static org.openhab.binding.gpio.internal.GPIOBindingConstants.*;
17 import java.util.HashMap;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.gpio.internal.InvalidPullUpDownException;
22 import org.openhab.binding.gpio.internal.NoGpioIdException;
23 import org.openhab.binding.gpio.internal.configuration.GPIOInputConfiguration;
24 import org.openhab.binding.gpio.internal.configuration.GPIOOutputConfiguration;
25 import org.openhab.binding.gpio.internal.configuration.PigpioConfiguration;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingStatus;
29 import org.openhab.core.thing.ThingStatusDetail;
30 import org.openhab.core.thing.binding.BaseThingHandler;
31 import org.openhab.core.thing.type.ChannelTypeUID;
32 import org.openhab.core.types.Command;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import eu.xeli.jpigpio.JPigpio;
37 import eu.xeli.jpigpio.PigpioException;
38 import eu.xeli.jpigpio.PigpioSocket;
41 * Remote pigpio Handler
43 * This bridge is used to control remote pigpio instances.
45 * @author Nils Bauer - Initial contribution
46 * @author Jan N. Klug - Channel redesign
49 public class PigpioRemoteHandler extends BaseThingHandler {
50 private final Logger logger = LoggerFactory.getLogger(PigpioRemoteHandler.class);
51 private final Map<ChannelUID, ChannelHandler> channelHandlers = new HashMap<>();
54 * Instantiates a new pigpio remote bridge handler.
56 * @param thing the thing
58 public PigpioRemoteHandler(Thing thing) {
63 public void handleCommand(ChannelUID channelUID, Command command) {
64 ChannelHandler channelHandler = channelHandlers.get(channelUID);
65 if (channelHandler != null) {
66 channelHandler.handleCommand(command);
71 public void initialize() {
72 PigpioConfiguration config = getConfigAs(PigpioConfiguration.class);
73 String host = config.host;
74 int port = config.port;
77 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR,
78 "Cannot connect to PiGPIO Service on remote raspberry. IP address not set.");
82 jPigpio = new PigpioSocket(host, port);
83 updateStatus(ThingStatus.ONLINE);
84 } catch (PigpioException e) {
85 if (e.getErrorCode() == PigpioException.PI_BAD_SOCKET_PORT) {
86 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR, "Port out of range");
88 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
89 e.getLocalizedMessage());
93 thing.getChannels().forEach(channel -> {
94 ChannelUID channelUID = channel.getUID();
95 ChannelTypeUID type = channel.getChannelTypeUID();
97 if (CHANNEL_TYPE_DIGITAL_INPUT.equals(type)) {
98 GPIOInputConfiguration configuration = channel.getConfiguration().as(GPIOInputConfiguration.class);
99 channelHandlers.put(channelUID, new PigpioDigitalInputHandler(configuration, jPigpio, scheduler,
100 state -> updateState(channelUID.getId(), state)));
101 } else if (CHANNEL_TYPE_DIGITAL_OUTPUT.equals(type)) {
102 GPIOOutputConfiguration configuration = channel.getConfiguration()
103 .as(GPIOOutputConfiguration.class);
104 channelHandlers.put(channelUID, new PigpioDigitalOutputHandler(configuration, jPigpio,
105 state -> updateState(channelUID.getId(), state)));
107 } catch (PigpioException e) {
108 logger.warn("Failed to initialize {}: {}", channelUID, e.getMessage());
109 } catch (InvalidPullUpDownException e) {
110 logger.warn("Failed to initialize {}: Invalid Pull Up/Down resistor configuration", channelUID);
111 } catch (NoGpioIdException e) {
112 logger.warn("Failed to initialize {}: GpioId is not set", channelUID);