]> git.basschouten.com Git - openhab-addons.git/blob
7209ce76f7a7682f065aaeb2bc833da2adf21d54
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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<>(Arrays.asList(THING_TYPE_VMB1RY,
44             THING_TYPE_VMB1RYNO, THING_TYPE_VMB1RYNOS, THING_TYPE_VMB4RY, THING_TYPE_VMB4RYLD, THING_TYPE_VMB4RYNO));
45
46     public VelbusRelayHandler(Thing thing) {
47         super(thing, 0);
48     }
49
50     @Override
51     public void handleCommand(ChannelUID channelUID, Command command) {
52         VelbusBridgeHandler velbusBridgeHandler = getVelbusBridgeHandler();
53         if (velbusBridgeHandler == null) {
54             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
55             return;
56         }
57
58         if (command instanceof RefreshType) {
59             VelbusStatusRequestPacket packet = new VelbusStatusRequestPacket(
60                     getModuleAddress().getChannelIdentifier(channelUID));
61
62             byte[] packetBytes = packet.getBytes();
63             velbusBridgeHandler.sendPacket(packetBytes);
64         } else if (command instanceof OnOffType) {
65             byte commandByte = determineCommandByte((OnOffType) command);
66
67             VelbusRelayPacket packet = new VelbusRelayPacket(getModuleAddress().getChannelIdentifier(channelUID),
68                     commandByte);
69
70             byte[] packetBytes = packet.getBytes();
71             velbusBridgeHandler.sendPacket(packetBytes);
72         } else {
73             logger.debug("The command '{}' is not supported by this handler.", command.getClass());
74         }
75     }
76
77     private byte determineCommandByte(OnOffType command) {
78         return (command == OnOffType.ON) ? COMMAND_SWITCH_RELAY_ON : COMMAND_SWITCH_RELAY_OFF;
79     }
80
81     @Override
82     public void onPacketReceived(byte[] packet) {
83         logger.trace("onPacketReceived() was called");
84
85         if (packet[0] == VelbusPacket.STX && packet.length >= 5) {
86             byte command = packet[4];
87
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;
92
93                 VelbusChannelIdentifier velbusChannelIdentifier = new VelbusChannelIdentifier(address, channel);
94                 OnOffType state = on ? OnOffType.ON : OnOffType.OFF;
95                 updateState(getModuleAddress().getChannelId(velbusChannelIdentifier), state);
96             }
97         }
98     }
99 }