2 * Copyright (c) 2010-2023 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.irtrans.internal.handler;
15 import static org.openhab.binding.irtrans.internal.IRtransBindingConstants.CHANNEL_IO;
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;
31 * The {@link BlasterHandler} is responsible for handling commands, which are
32 * sent to one of the channels.
34 * @author Karel Goderis - Initial contribution
37 public class BlasterHandler extends BaseThingHandler implements TransceiverStatusListener {
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";
44 private Logger logger = LoggerFactory.getLogger(BlasterHandler.class);
46 public BlasterHandler(Thing thing) {
51 public void initialize() {
52 ((EthernetBridgeHandler) getBridge().getHandler()).registerTransceiverStatusListener(this);
56 public void handleRemoval() {
57 ((EthernetBridgeHandler) getBridge().getHandler()).unregisterTransceiverStatusListener(this);
58 updateStatus(ThingStatus.REMOVED);
62 public void handleCommand(ChannelUID channelUID, Command command) {
63 EthernetBridgeHandler ethernetBridge = (EthernetBridgeHandler) getBridge().getHandler();
65 if (ethernetBridge == null) {
66 logger.warn("IRtrans Ethernet bridge handler not found. Cannot handle command without bridge.");
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(), ",");
76 IrCommand ircommand = new IrCommand();
77 ircommand.setRemote(remoteName);
78 ircommand.setCommand(irCommandName);
80 IrCommand thingCompatibleCommand = new IrCommand();
81 thingCompatibleCommand.setRemote((String) getConfig().get(REMOTE));
82 thingCompatibleCommand.setCommand((String) getConfig().get(COMMAND));
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);
96 public void onCommandReceived(EthernetBridgeHandler bridge, IrCommand command) {
97 logger.debug("Received command {},{} for thing {}", command.getRemote(), command.getCommand(),
98 this.getThing().getUID());
100 IrCommand thingCompatibleCommand = new IrCommand();
101 thingCompatibleCommand.setRemote((String) getConfig().get(REMOTE));
102 thingCompatibleCommand.setCommand((String) getConfig().get(COMMAND));
104 if (command.matches(thingCompatibleCommand)) {
105 StringType stringType = new StringType(command.getRemote() + "," + command.getCommand());
106 updateState(CHANNEL_IO, stringType);