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.openhab.binding.irtrans.internal.IRtransBindingConstants.Led;
18 import org.openhab.binding.irtrans.internal.IrCommand;
19 import org.openhab.core.library.types.StringType;
20 import org.openhab.core.thing.ChannelUID;
21 import org.openhab.core.thing.Thing;
22 import org.openhab.core.thing.ThingStatus;
23 import org.openhab.core.thing.binding.BaseThingHandler;
24 import org.openhab.core.types.Command;
25 import org.openhab.core.types.RefreshType;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * The {@link BlasterHandler} is responsible for handling commands, which are
31 * sent to one of the channels.
33 * @author Karel Goderis - Initial contribution
36 public class BlasterHandler extends BaseThingHandler implements TransceiverStatusListener {
38 // List of Configuration constants
39 public static final String COMMAND = "command";
40 public static final String LED = "led";
41 public static final String REMOTE = "remote";
43 private Logger logger = LoggerFactory.getLogger(BlasterHandler.class);
45 public BlasterHandler(Thing thing) {
50 public void initialize() {
51 ((EthernetBridgeHandler) getBridge().getHandler()).registerTransceiverStatusListener(this);
55 public void handleRemoval() {
56 ((EthernetBridgeHandler) getBridge().getHandler()).unregisterTransceiverStatusListener(this);
57 updateStatus(ThingStatus.REMOVED);
61 public void handleCommand(ChannelUID channelUID, Command command) {
62 EthernetBridgeHandler ethernetBridge = (EthernetBridgeHandler) getBridge().getHandler();
64 if (ethernetBridge == null) {
65 logger.warn("IRtrans Ethernet bridge handler not found. Cannot handle command without bridge.");
69 if (!(command instanceof RefreshType)) {
70 if (channelUID.getId().equals(CHANNEL_IO)) {
71 if (command instanceof StringType) {
72 String[] remoteCommand = command.toString().split(",", 2);
73 if (remoteCommand.length < 2) {
74 logger.warn("Ignoring invalid command '{}'", command);
78 IrCommand ircommand = new IrCommand();
79 ircommand.setRemote(remoteCommand[0]);
80 ircommand.setCommand(remoteCommand[1]);
82 IrCommand thingCompatibleCommand = new IrCommand();
83 thingCompatibleCommand.setRemote((String) getConfig().get(REMOTE));
84 thingCompatibleCommand.setCommand((String) getConfig().get(COMMAND));
86 if (ircommand.matches(thingCompatibleCommand)) {
87 if (!ethernetBridge.sendIRcommand(ircommand, Led.get((String) getConfig().get(LED)))) {
88 logger.warn("An error occured whilst sending the infrared command '{}' for Channel '{}'",
89 ircommand, channelUID);
98 public void onCommandReceived(EthernetBridgeHandler bridge, IrCommand command) {
99 logger.debug("Received command {},{} for thing {}", command.getRemote(), command.getCommand(),
100 this.getThing().getUID());
102 IrCommand thingCompatibleCommand = new IrCommand();
103 thingCompatibleCommand.setRemote((String) getConfig().get(REMOTE));
104 thingCompatibleCommand.setCommand((String) getConfig().get(COMMAND));
106 if (command.matches(thingCompatibleCommand)) {
107 StringType stringType = new StringType(command.getRemote() + "," + command.getCommand());
108 updateState(CHANNEL_IO, stringType);