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.velbus.internal.handler;
15 import static org.openhab.binding.velbus.internal.VelbusBindingConstants.*;
17 import java.util.Arrays;
18 import java.util.HashSet;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.velbus.internal.VelbusChannelIdentifier;
23 import org.openhab.binding.velbus.internal.packets.VelbusButtonPacket;
24 import org.openhab.binding.velbus.internal.packets.VelbusPacket;
25 import org.openhab.core.library.types.StringType;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.thing.CommonTriggerEvents;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingStatus;
30 import org.openhab.core.thing.ThingStatusDetail;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.types.Command;
35 * The {@link VelbusRelayWithInputHandler} is responsible for handling commands, which are
36 * sent to one of the channels.
38 * @author Daniel Rosengarten - Initial contribution
41 public class VelbusRelayWithInputHandler extends VelbusRelayHandler {
42 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = new HashSet<>(Arrays.asList(THING_TYPE_VMB1RYS));
44 private static final StringType PRESSED = new StringType("PRESSED");
45 private static final StringType LONG_PRESSED = new StringType("LONG_PRESSED");
47 public VelbusRelayWithInputHandler(Thing thing) {
52 public void handleCommand(ChannelUID channelUID, Command command) {
53 super.handleCommand(channelUID, command);
55 VelbusBridgeHandler velbusBridgeHandler = getVelbusBridgeHandler();
56 if (velbusBridgeHandler == null) {
57 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
61 if (isButtonChannel(channelUID) && command instanceof StringType) {
62 StringType stringTypeCommand = (StringType) command;
64 if (stringTypeCommand.equals(PRESSED) || stringTypeCommand.equals(LONG_PRESSED)) {
65 VelbusButtonPacket packet = new VelbusButtonPacket(getModuleAddress().getChannelIdentifier(channelUID));
68 velbusBridgeHandler.sendPacket(packet.getBytes());
69 triggerChannel("CH6t", CommonTriggerEvents.PRESSED);
71 if (stringTypeCommand.equals(LONG_PRESSED)) {
73 velbusBridgeHandler.sendPacket(packet.getBytes());
74 triggerChannel("CH6t", CommonTriggerEvents.LONG_PRESSED);
78 velbusBridgeHandler.sendPacket(packet.getBytes());
79 triggerChannel("CH6t", CommonTriggerEvents.RELEASED);
81 throw new UnsupportedOperationException(
82 "The command '" + command + "' is not supported on channel '" + channelUID + "'.");
87 private boolean isButtonChannel(ChannelUID channelUID) {
88 return "CH6".equals(channelUID.toString().substring(channelUID.toString().length() - 3));
91 private boolean isTriggerChannel(byte address, byte channel) {
92 VelbusChannelIdentifier velbusChannelIdentifier = new VelbusChannelIdentifier(address, channel);
94 return getModuleAddress().getChannelNumber(velbusChannelIdentifier) == 6;
98 public void onPacketReceived(byte[] packet) {
99 super.onPacketReceived(packet);
101 if (packet[0] == VelbusPacket.STX && packet.length >= 5) {
102 byte command = packet[4];
104 if (command == COMMAND_PUSH_BUTTON_STATUS && packet.length >= 6) {
105 byte address = packet[2];
107 byte channelJustPressed = packet[5];
108 if (isTriggerChannel(address, channelJustPressed)) {
109 triggerChannel("CH6t", CommonTriggerEvents.PRESSED);
112 byte channelJustReleased = packet[6];
113 if (isTriggerChannel(address, channelJustReleased)) {
114 triggerChannel("CH6t", CommonTriggerEvents.RELEASED);
117 byte channelLongPressed = packet[7];
118 if (isTriggerChannel(address, channelLongPressed)) {
119 triggerChannel("CH6t", CommonTriggerEvents.LONG_PRESSED);