]> git.basschouten.com Git - openhab-addons.git/blob
78a8cbfe891abf4b5528f43e4cd3e8bcc09ba211
[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.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;
28
29 /**
30  * The {@link BlasterHandler} is responsible for handling commands, which are
31  * sent to one of the channels.
32  *
33  * @author Karel Goderis - Initial contribution
34  *
35  */
36 public class BlasterHandler extends BaseThingHandler implements TransceiverStatusListener {
37
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";
42
43     private Logger logger = LoggerFactory.getLogger(BlasterHandler.class);
44
45     public BlasterHandler(Thing thing) {
46         super(thing);
47     }
48
49     @Override
50     public void initialize() {
51         ((EthernetBridgeHandler) getBridge().getHandler()).registerTransceiverStatusListener(this);
52     }
53
54     @Override
55     public void handleRemoval() {
56         ((EthernetBridgeHandler) getBridge().getHandler()).unregisterTransceiverStatusListener(this);
57         updateStatus(ThingStatus.REMOVED);
58     }
59
60     @Override
61     public void handleCommand(ChannelUID channelUID, Command command) {
62         EthernetBridgeHandler ethernetBridge = (EthernetBridgeHandler) getBridge().getHandler();
63
64         if (ethernetBridge == null) {
65             logger.warn("IRtrans Ethernet bridge handler not found. Cannot handle command without bridge.");
66             return;
67         }
68
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);
75                         return;
76                     }
77
78                     IrCommand ircommand = new IrCommand();
79                     ircommand.setRemote(remoteCommand[0]);
80                     ircommand.setCommand(remoteCommand[1]);
81
82                     IrCommand thingCompatibleCommand = new IrCommand();
83                     thingCompatibleCommand.setRemote((String) getConfig().get(REMOTE));
84                     thingCompatibleCommand.setCommand((String) getConfig().get(COMMAND));
85
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);
90                         }
91                     }
92                 }
93             }
94         }
95     }
96
97     @Override
98     public void onCommandReceived(EthernetBridgeHandler bridge, IrCommand command) {
99         logger.debug("Received command {},{} for thing {}", command.getRemote(), command.getCommand(),
100                 this.getThing().getUID());
101
102         IrCommand thingCompatibleCommand = new IrCommand();
103         thingCompatibleCommand.setRemote((String) getConfig().get(REMOTE));
104         thingCompatibleCommand.setCommand((String) getConfig().get(COMMAND));
105
106         if (command.matches(thingCompatibleCommand)) {
107             StringType stringType = new StringType(command.getRemote() + "," + command.getCommand());
108             updateState(CHANNEL_IO, stringType);
109         }
110     }
111 }