2 * Copyright (c) 2010-2024 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 stringCommand) {
62 if (stringCommand.equals(PRESSED) || stringCommand.equals(LONG_PRESSED)) {
63 VelbusButtonPacket packet = new VelbusButtonPacket(getModuleAddress().getChannelIdentifier(channelUID));
66 velbusBridgeHandler.sendPacket(packet.getBytes());
67 triggerChannel("CH6t", CommonTriggerEvents.PRESSED);
69 if (stringCommand.equals(LONG_PRESSED)) {
71 velbusBridgeHandler.sendPacket(packet.getBytes());
72 triggerChannel("CH6t", CommonTriggerEvents.LONG_PRESSED);
76 velbusBridgeHandler.sendPacket(packet.getBytes());
77 triggerChannel("CH6t", CommonTriggerEvents.RELEASED);
79 throw new UnsupportedOperationException(
80 "The command '" + command + "' is not supported on channel '" + channelUID + "'.");
85 private boolean isButtonChannel(ChannelUID channelUID) {
86 return "CH6".equals(channelUID.toString().substring(channelUID.toString().length() - 3));
89 private boolean isTriggerChannel(byte address, byte channel) {
90 VelbusChannelIdentifier velbusChannelIdentifier = new VelbusChannelIdentifier(address, channel);
92 return getModuleAddress().getChannelNumber(velbusChannelIdentifier) == 6;
96 public void onPacketReceived(byte[] packet) {
97 super.onPacketReceived(packet);
99 if (packet[0] == VelbusPacket.STX && packet.length >= 5) {
100 byte command = packet[4];
102 if (command == COMMAND_PUSH_BUTTON_STATUS && packet.length >= 6) {
103 byte address = packet[2];
105 byte channelJustPressed = packet[5];
106 if (isTriggerChannel(address, channelJustPressed)) {
107 triggerChannel("CH6t", CommonTriggerEvents.PRESSED);
110 byte channelJustReleased = packet[6];
111 if (isTriggerChannel(address, channelJustReleased)) {
112 triggerChannel("CH6t", CommonTriggerEvents.RELEASED);
115 byte channelLongPressed = packet[7];
116 if (isTriggerChannel(address, channelLongPressed)) {
117 triggerChannel("CH6t", CommonTriggerEvents.LONG_PRESSED);