2 * Copyright (c) 2010-2021 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.CHANNEL_TYPE_DIGITAL_INPUT;
16 import static org.openhab.binding.gpio.internal.GPIOBindingConstants.CHANNEL_TYPE_DIGITAL_OUTPUT;
18 import java.util.HashMap;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
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.*;
27 import org.openhab.core.thing.binding.BaseThingHandler;
28 import org.openhab.core.thing.type.ChannelTypeUID;
29 import org.openhab.core.types.Command;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import eu.xeli.jpigpio.JPigpio;
34 import eu.xeli.jpigpio.PigpioException;
35 import eu.xeli.jpigpio.PigpioSocket;
38 * Remote pigpio Handler
40 * This bridge is used to control remote pigpio instances.
42 * @author Nils Bauer - Initial contribution
43 * @author Jan N. Klug - Channel redesign
46 public class PigpioRemoteHandler extends BaseThingHandler {
47 private final Logger logger = LoggerFactory.getLogger(PigpioRemoteHandler.class);
48 private final Map<ChannelUID, ChannelHandler> channelHandlers = new HashMap<>();
51 * Instantiates a new pigpio remote bridge handler.
53 * @param thing the thing
55 public PigpioRemoteHandler(Thing thing) {
60 public void handleCommand(ChannelUID channelUID, Command command) {
61 ChannelHandler channelHandler = channelHandlers.get(channelUID);
62 if (channelHandler != null) {
63 channelHandler.handleCommand(command);
68 public void initialize() {
69 PigpioConfiguration config = getConfigAs(PigpioConfiguration.class);
70 String host = config.host;
71 int port = config.port;
74 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR,
75 "Cannot connect to PiGPIO Service on remote raspberry. IP address not set.");
79 jPigpio = new PigpioSocket(host, port);
80 updateStatus(ThingStatus.ONLINE);
81 } catch (PigpioException e) {
82 if (e.getErrorCode() == PigpioException.PI_BAD_SOCKET_PORT) {
83 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR, "Port out of range");
85 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
86 e.getLocalizedMessage());
90 thing.getChannels().forEach(channel -> {
91 ChannelUID channelUID = channel.getUID();
92 ChannelTypeUID type = channel.getChannelTypeUID();
94 if (CHANNEL_TYPE_DIGITAL_INPUT.equals(type)) {
95 GPIOInputConfiguration configuration = channel.getConfiguration().as(GPIOInputConfiguration.class);
96 channelHandlers.put(channelUID, new PigpioDigitalInputHandler(configuration, jPigpio, scheduler,
97 state -> updateState(channelUID.getId(), state)));
98 } else if (CHANNEL_TYPE_DIGITAL_OUTPUT.equals(type)) {
99 GPIOOutputConfiguration configuration = channel.getConfiguration()
100 .as(GPIOOutputConfiguration.class);
101 channelHandlers.put(channelUID, new PigpioDigitalOutputHandler(configuration, jPigpio,
102 state -> updateState(channelUID.getId(), state)));
104 } catch (PigpioException e) {
105 logger.warn("Failed to initialize {}: {}", channelUID, e.getMessage());
106 } catch (NoGpioIdException e) {
107 logger.warn("Failed to initialize {}: GpioId is not set", channelUID);