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.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<>(
44 Arrays.asList(THING_TYPE_VMB1RY, THING_TYPE_VMB1RYNO, THING_TYPE_VMB1RYNOS, THING_TYPE_VMB1RYS,
45 THING_TYPE_VMB4RY, THING_TYPE_VMB4RYLD, THING_TYPE_VMB4RYNO));
47 public VelbusRelayHandler(Thing thing) {
52 public void handleCommand(ChannelUID channelUID, Command command) {
53 VelbusBridgeHandler velbusBridgeHandler = getVelbusBridgeHandler();
54 if (velbusBridgeHandler == null) {
55 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
59 if (command instanceof RefreshType) {
60 VelbusStatusRequestPacket packet = new VelbusStatusRequestPacket(
61 getModuleAddress().getChannelIdentifier(channelUID));
63 byte[] packetBytes = packet.getBytes();
64 velbusBridgeHandler.sendPacket(packetBytes);
65 } else if (command instanceof OnOffType) {
66 byte commandByte = determineCommandByte((OnOffType) command);
68 VelbusRelayPacket packet = new VelbusRelayPacket(getModuleAddress().getChannelIdentifier(channelUID),
71 byte[] packetBytes = packet.getBytes();
72 velbusBridgeHandler.sendPacket(packetBytes);
74 logger.debug("The command '{}' is not supported by this handler.", command.getClass());
78 private byte determineCommandByte(OnOffType command) {
79 return (command == OnOffType.ON) ? COMMAND_SWITCH_RELAY_ON : COMMAND_SWITCH_RELAY_OFF;
83 public void onPacketReceived(byte[] packet) {
84 logger.trace("onPacketReceived() was called");
86 if (packet[0] == VelbusPacket.STX && packet.length >= 5) {
87 byte command = packet[4];
89 if (command == COMMAND_RELAY_STATUS && packet.length >= 7) {
90 byte address = packet[2];
91 byte channel = packet[5];
92 boolean on = packet[7] != 0x00;
94 VelbusChannelIdentifier velbusChannelIdentifier = new VelbusChannelIdentifier(address, channel);
95 OnOffType state = on ? OnOffType.ON : OnOffType.OFF;
96 updateState(getModuleAddress().getChannelId(velbusChannelIdentifier), state);