]> git.basschouten.com Git - openhab-addons.git/blob
1be5e4d08c58c99fe4fc8e4ad7e008c94c43458c
[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.irtrans.internal.handler;
14
15 import static org.openhab.binding.irtrans.internal.IRtransBindingConstants.CHANNEL_IO;
16
17 import org.apache.commons.lang3.StringUtils;
18 import org.openhab.binding.irtrans.internal.IRtransBindingConstants.Led;
19 import org.openhab.binding.irtrans.internal.IrCommand;
20 import org.openhab.core.library.types.StringType;
21 import org.openhab.core.thing.ChannelUID;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.ThingStatus;
24 import org.openhab.core.thing.binding.BaseThingHandler;
25 import org.openhab.core.types.Command;
26 import org.openhab.core.types.RefreshType;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * The {@link BlasterHandler} is responsible for handling commands, which are
32  * sent to one of the channels.
33  *
34  * @author Karel Goderis - Initial contribution
35  *
36  */
37 public class BlasterHandler extends BaseThingHandler implements TransceiverStatusListener {
38
39     // List of Configuration constants
40     public static final String COMMAND = "command";
41     public static final String LED = "led";
42     public static final String REMOTE = "remote";
43
44     private Logger logger = LoggerFactory.getLogger(BlasterHandler.class);
45
46     public BlasterHandler(Thing thing) {
47         super(thing);
48     }
49
50     @Override
51     public void initialize() {
52         ((EthernetBridgeHandler) getBridge().getHandler()).registerTransceiverStatusListener(this);
53     }
54
55     @Override
56     public void handleRemoval() {
57         ((EthernetBridgeHandler) getBridge().getHandler()).unregisterTransceiverStatusListener(this);
58         updateStatus(ThingStatus.REMOVED);
59     }
60
61     @Override
62     public void handleCommand(ChannelUID channelUID, Command command) {
63         EthernetBridgeHandler ethernetBridge = (EthernetBridgeHandler) getBridge().getHandler();
64
65         if (ethernetBridge == null) {
66             logger.warn("IRtrans Ethernet bridge handler not found. Cannot handle command without bridge.");
67             return;
68         }
69
70         if (!(command instanceof RefreshType)) {
71             if (channelUID.getId().equals(CHANNEL_IO)) {
72                 if (command instanceof StringType) {
73                     String remoteName = StringUtils.substringBefore(command.toString(), ",");
74                     String irCommandName = StringUtils.substringAfter(command.toString(), ",");
75
76                     IrCommand ircommand = new IrCommand();
77                     ircommand.setRemote(remoteName);
78                     ircommand.setCommand(irCommandName);
79
80                     IrCommand thingCompatibleCommand = new IrCommand();
81                     thingCompatibleCommand.setRemote((String) getConfig().get(REMOTE));
82                     thingCompatibleCommand.setCommand((String) getConfig().get(COMMAND));
83
84                     if (ircommand.matches(thingCompatibleCommand)) {
85                         if (!ethernetBridge.sendIRcommand(ircommand, Led.get((String) getConfig().get(LED)))) {
86                             logger.warn("An error occured whilst sending the infrared command '{}' for Channel '{}'",
87                                     ircommand, channelUID);
88                         }
89                     }
90                 }
91             }
92         }
93     }
94
95     @Override
96     public void onCommandReceived(EthernetBridgeHandler bridge, IrCommand command) {
97         logger.debug("Received command {},{} for thing {}", command.getRemote(), command.getCommand(),
98                 this.getThing().getUID());
99
100         IrCommand thingCompatibleCommand = new IrCommand();
101         thingCompatibleCommand.setRemote((String) getConfig().get(REMOTE));
102         thingCompatibleCommand.setCommand((String) getConfig().get(COMMAND));
103
104         if (command.matches(thingCompatibleCommand)) {
105             StringType stringType = new StringType(command.getRemote() + "," + command.getCommand());
106             updateState(CHANNEL_IO, stringType);
107         }
108     }
109 }