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