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.VelbusPacket;
24 import org.openhab.binding.velbus.internal.packets.VelbusRelayPacket;
25 import org.openhab.binding.velbus.internal.packets.VelbusStatusRequestPacket;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.thing.ChannelUID;
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;
33 import org.openhab.core.types.RefreshType;
36 * The {@link VelbusRelayHandler} is responsible for handling commands, which are
37 * sent to one of the channels.
39 * @author Cedric Boon - Initial contribution
42 public class VelbusRelayHandler extends VelbusThingHandler {
43 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = new HashSet<>(Arrays.asList(THING_TYPE_VMB1RY,
44 THING_TYPE_VMB1RYNO, THING_TYPE_VMB1RYNOS, THING_TYPE_VMB4RY, THING_TYPE_VMB4RYLD, THING_TYPE_VMB4RYNO));
46 public VelbusRelayHandler(Thing thing) {
51 public void handleCommand(ChannelUID channelUID, Command command) {
52 VelbusBridgeHandler velbusBridgeHandler = getVelbusBridgeHandler();
53 if (velbusBridgeHandler == null) {
54 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
58 if (command instanceof RefreshType) {
59 VelbusStatusRequestPacket packet = new VelbusStatusRequestPacket(
60 getModuleAddress().getChannelIdentifier(channelUID));
62 byte[] packetBytes = packet.getBytes();
63 velbusBridgeHandler.sendPacket(packetBytes);
64 } else if (command instanceof OnOffType) {
65 byte commandByte = determineCommandByte((OnOffType) command);
67 VelbusRelayPacket packet = new VelbusRelayPacket(getModuleAddress().getChannelIdentifier(channelUID),
70 byte[] packetBytes = packet.getBytes();
71 velbusBridgeHandler.sendPacket(packetBytes);
73 logger.debug("The command '{}' is not supported by this handler.", command.getClass());
77 private byte determineCommandByte(OnOffType command) {
78 return (command == OnOffType.ON) ? COMMAND_SWITCH_RELAY_ON : COMMAND_SWITCH_RELAY_OFF;
82 public void onPacketReceived(byte[] packet) {
83 logger.trace("onPacketReceived() was called");
85 if (packet[0] == VelbusPacket.STX && packet.length >= 5) {
86 byte command = packet[4];
88 if (command == COMMAND_RELAY_STATUS && packet.length >= 7) {
89 byte address = packet[2];
90 byte channel = packet[5];
91 boolean on = packet[7] != 0x00;
93 VelbusChannelIdentifier velbusChannelIdentifier = new VelbusChannelIdentifier(address, channel);
94 OnOffType state = on ? OnOffType.ON : OnOffType.OFF;
95 updateState(getModuleAddress().getChannelId(velbusChannelIdentifier), state);