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