2 * Copyright (c) 2010-2020 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.VelbusFeedbackLEDPacket;
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 VelbusSensorHandler} is responsible for handling commands, which are
36 * sent to one of the channels.
38 * @author Cedric Boon - Initial contribution
41 public class VelbusSensorHandler extends VelbusThingHandler {
42 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = new HashSet<>(
43 Arrays.asList(THING_TYPE_VMB6IN, THING_TYPE_VMB8IR, THING_TYPE_VMB8PB));
45 private static final StringType SET_LED = new StringType("SET_LED");
46 private static final StringType SLOW_BLINK_LED = new StringType("SLOW_BLINK_LED");
47 private static final StringType FAST_BLINK_LED = new StringType("FAST_BLINK_LED");
48 private static final StringType VERY_FAST_BLINK_LED = new StringType("VERY_FAST_BLINK_LED");
49 private static final StringType CLEAR_LED = new StringType("CLEAR_LED");
51 public VelbusSensorHandler(Thing thing) {
55 public VelbusSensorHandler(Thing thing, int numberOfSubAddresses) {
56 super(thing, numberOfSubAddresses);
60 public void handleCommand(ChannelUID channelUID, Command command) {
61 VelbusBridgeHandler velbusBridgeHandler = getVelbusBridgeHandler();
62 if (velbusBridgeHandler == null) {
63 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
67 if (isFeedbackChannel(channelUID) && command instanceof StringType) {
70 StringType stringTypeCommand = (StringType) command;
71 if (stringTypeCommand.equals(SET_LED)) {
72 commandByte = COMMAND_SET_LED;
73 } else if (stringTypeCommand.equals(SLOW_BLINK_LED)) {
74 commandByte = COMMAND_SLOW_BLINK_LED;
75 } else if (stringTypeCommand.equals(FAST_BLINK_LED)) {
76 commandByte = COMMAND_FAST_BLINK_LED;
77 } else if (stringTypeCommand.equals(VERY_FAST_BLINK_LED)) {
78 commandByte = COMMAND_VERY_FAST_BLINK_LED;
79 } else if (stringTypeCommand.equals(CLEAR_LED)) {
80 commandByte = COMMAND_CLEAR_LED;
82 throw new UnsupportedOperationException(
83 "The command '" + command + "' is not supported on channel '" + channelUID + "'.");
86 VelbusFeedbackLEDPacket packet = new VelbusFeedbackLEDPacket(
87 getModuleAddress().getChannelIdentifier(channelUID), commandByte);
89 byte[] packetBytes = packet.getBytes();
90 velbusBridgeHandler.sendPacket(packetBytes);
94 private boolean isFeedbackChannel(ChannelUID channelUID) {
95 return "feedback".equals(channelUID.getGroupId());
99 public void onPacketReceived(byte[] packet) {
100 logger.trace("onPacketReceived() was called");
102 if (packet[0] == VelbusPacket.STX && packet.length >= 5) {
103 byte address = packet[2];
104 byte command = packet[4];
106 if (command == COMMAND_PUSH_BUTTON_STATUS && packet.length >= 6) {
107 byte channelJustPressed = packet[5];
108 if (channelJustPressed != 0) {
109 VelbusChannelIdentifier velbusChannelIdentifier = new VelbusChannelIdentifier(address,
111 triggerChannel("input#" + getModuleAddress().getChannelId(velbusChannelIdentifier),
112 CommonTriggerEvents.PRESSED);
115 byte channelJustReleased = packet[6];
116 if (channelJustReleased != 0) {
117 VelbusChannelIdentifier velbusChannelIdentifier = new VelbusChannelIdentifier(address,
118 channelJustReleased);
119 triggerChannel("input#" + getModuleAddress().getChannelId(velbusChannelIdentifier),
120 CommonTriggerEvents.RELEASED);
123 byte channelLongPressed = packet[7];
124 if (channelLongPressed != 0) {
125 VelbusChannelIdentifier velbusChannelIdentifier = new VelbusChannelIdentifier(address,
127 triggerChannel("input#" + getModuleAddress().getChannelId(velbusChannelIdentifier),
128 CommonTriggerEvents.LONG_PRESSED);