]> git.basschouten.com Git - openhab-addons.git/blob
993b66dc335898849a6c90cb10b589db2534337c
[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 static org.openhab.binding.gpio.internal.GPIOBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Map;
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.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;
34
35 import eu.xeli.jpigpio.JPigpio;
36 import eu.xeli.jpigpio.PigpioException;
37 import eu.xeli.jpigpio.PigpioSocket;
38
39 /**
40  * Remote pigpio Handler
41  *
42  * This bridge is used to control remote pigpio instances.
43  *
44  * @author Nils Bauer - Initial contribution
45  * @author Jan N. Klug - Channel redesign
46  */
47 @NonNullByDefault
48 public class PigpioRemoteHandler extends BaseThingHandler {
49     private final Logger logger = LoggerFactory.getLogger(PigpioRemoteHandler.class);
50     private final Map<ChannelUID, ChannelHandler> channelHandlers = new HashMap<>();
51
52     /**
53      * Instantiates a new pigpio remote bridge handler.
54      *
55      * @param thing the thing
56      */
57     public PigpioRemoteHandler(Thing thing) {
58         super(thing);
59     }
60
61     @Override
62     public void handleCommand(ChannelUID channelUID, Command command) {
63         ChannelHandler channelHandler = channelHandlers.get(channelUID);
64         if (channelHandler != null) {
65             channelHandler.handleCommand(command);
66         }
67     }
68
69     @Override
70     public void initialize() {
71         PigpioConfiguration config = getConfigAs(PigpioConfiguration.class);
72         String host = config.host;
73         int port = config.port;
74         JPigpio jPigpio;
75         if (host == null) {
76             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR,
77                     "Cannot connect to PiGPIO Service on remote raspberry. IP address not set.");
78             return;
79         }
80         try {
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");
86             } else {
87                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
88                         e.getLocalizedMessage());
89             }
90             return;
91         }
92         thing.getChannels().forEach(channel -> {
93             ChannelUID channelUID = channel.getUID();
94             ChannelTypeUID type = channel.getChannelTypeUID();
95             try {
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)));
105                 }
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);
110             }
111         });
112     }
113 }