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.*;
17 import java.util.HashMap;
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.binding.gpio.internal.configuration.GPIOOutputConfiguration;
24 import org.openhab.binding.gpio.internal.configuration.PigpioConfiguration;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.thing.ThingStatusDetail;
29 import org.openhab.core.thing.binding.BaseThingHandler;
30 import org.openhab.core.thing.type.ChannelTypeUID;
31 import org.openhab.core.types.Command;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import eu.xeli.jpigpio.JPigpio;
36 import eu.xeli.jpigpio.PigpioException;
37 import eu.xeli.jpigpio.PigpioSocket;
40 * Remote pigpio Handler
42 * This bridge is used to control remote pigpio instances.
44 * @author Nils Bauer - Initial contribution
45 * @author Jan N. Klug - Channel redesign
48 public class PigpioRemoteHandler extends BaseThingHandler {
49 private final Logger logger = LoggerFactory.getLogger(PigpioRemoteHandler.class);
50 private final Map<ChannelUID, ChannelHandler> channelHandlers = new HashMap<>();
53 * Instantiates a new pigpio remote bridge handler.
55 * @param thing the thing
57 public PigpioRemoteHandler(Thing thing) {
62 public void handleCommand(ChannelUID channelUID, Command command) {
63 ChannelHandler channelHandler = channelHandlers.get(channelUID);
64 if (channelHandler != null) {
65 channelHandler.handleCommand(command);
70 public void initialize() {
71 PigpioConfiguration config = getConfigAs(PigpioConfiguration.class);
72 String host = config.host;
73 int port = config.port;
76 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR,
77 "Cannot connect to PiGPIO Service on remote raspberry. IP address not set.");
81 jPigpio = new PigpioSocket(host, port);
82 updateStatus(ThingStatus.ONLINE);
83 } catch (PigpioException e) {
84 if (e.getErrorCode() == PigpioException.PI_BAD_SOCKET_PORT) {
85 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR, "Port out of range");
87 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
88 e.getLocalizedMessage());
92 thing.getChannels().forEach(channel -> {
93 ChannelUID channelUID = channel.getUID();
94 ChannelTypeUID type = channel.getChannelTypeUID();
96 if (CHANNEL_TYPE_DIGITAL_INPUT.equals(type)) {
97 GPIOInputConfiguration configuration = channel.getConfiguration().as(GPIOInputConfiguration.class);
98 channelHandlers.put(channelUID, new PigpioDigitalInputHandler(configuration, jPigpio, scheduler,
99 state -> updateState(channelUID.getId(), state)));
100 } else if (CHANNEL_TYPE_DIGITAL_OUTPUT.equals(type)) {
101 GPIOOutputConfiguration configuration = channel.getConfiguration()
102 .as(GPIOOutputConfiguration.class);
103 channelHandlers.put(channelUID, new PigpioDigitalOutputHandler(configuration, jPigpio,
104 state -> updateState(channelUID.getId(), state)));
106 } catch (PigpioException e) {
107 logger.warn("Failed to initialize {}: {}", channelUID, e.getMessage());
108 } catch (NoGpioIdException e) {
109 logger.warn("Failed to initialize {}: GpioId is not set", channelUID);