]> git.basschouten.com Git - openhab-addons.git/blob
cb244573225a910e5663bc9b04d33cc90f2361da
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.velbus.internal.handler;
14
15 import static org.openhab.binding.velbus.internal.VelbusBindingConstants.*;
16
17 import java.util.Arrays;
18 import java.util.HashSet;
19 import java.util.Set;
20
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;
34
35 /**
36  * The {@link VelbusRelayHandler} is responsible for handling commands, which are
37  * sent to one of the channels.
38  *
39  * @author Cedric Boon - Initial contribution
40  */
41 @NonNullByDefault
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));
46
47     public VelbusRelayHandler(Thing thing) {
48         super(thing, 0);
49     }
50
51     @Override
52     public void handleCommand(ChannelUID channelUID, Command command) {
53         VelbusBridgeHandler velbusBridgeHandler = getVelbusBridgeHandler();
54         if (velbusBridgeHandler == null) {
55             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
56             return;
57         }
58
59         if (command instanceof RefreshType) {
60             VelbusStatusRequestPacket packet = new VelbusStatusRequestPacket(
61                     getModuleAddress().getChannelIdentifier(channelUID));
62
63             byte[] packetBytes = packet.getBytes();
64             velbusBridgeHandler.sendPacket(packetBytes);
65         } else if (command instanceof OnOffType) {
66             byte commandByte = determineCommandByte((OnOffType) command);
67
68             VelbusRelayPacket packet = new VelbusRelayPacket(getModuleAddress().getChannelIdentifier(channelUID),
69                     commandByte);
70
71             byte[] packetBytes = packet.getBytes();
72             velbusBridgeHandler.sendPacket(packetBytes);
73         } else {
74             logger.debug("The command '{}' is not supported by this handler.", command.getClass());
75         }
76     }
77
78     private byte determineCommandByte(OnOffType command) {
79         return (command == OnOffType.ON) ? COMMAND_SWITCH_RELAY_ON : COMMAND_SWITCH_RELAY_OFF;
80     }
81
82     @Override
83     public void onPacketReceived(byte[] packet) {
84         logger.trace("onPacketReceived() was called");
85
86         if (packet[0] == VelbusPacket.STX && packet.length >= 5) {
87             byte command = packet[4];
88
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;
93
94                 VelbusChannelIdentifier velbusChannelIdentifier = new VelbusChannelIdentifier(address, channel);
95                 OnOffType state = on ? OnOffType.ON : OnOffType.OFF;
96                 updateState(getModuleAddress().getChannelId(velbusChannelIdentifier), state);
97             }
98         }
99     }
100 }