]> git.basschouten.com Git - openhab-addons.git/blob
a543bae88b1928c9c0e81ef3d49ae0739a2ce6bf
[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.VelbusButtonPacket;
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;
33
34 /**
35  * The {@link VelbusRelayWithInputHandler} is responsible for handling commands, which are
36  * sent to one of the channels.
37  *
38  * @author Daniel Rosengarten - Initial contribution
39  */
40 @NonNullByDefault
41 public class VelbusRelayWithInputHandler extends VelbusRelayHandler {
42     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = new HashSet<>(Arrays.asList(THING_TYPE_VMB1RYS));
43
44     private static final StringType PRESSED = new StringType("PRESSED");
45     private static final StringType LONG_PRESSED = new StringType("LONG_PRESSED");
46
47     public VelbusRelayWithInputHandler(Thing thing) {
48         super(thing);
49     }
50
51     @Override
52     public void handleCommand(ChannelUID channelUID, Command command) {
53         super.handleCommand(channelUID, command);
54
55         VelbusBridgeHandler velbusBridgeHandler = getVelbusBridgeHandler();
56         if (velbusBridgeHandler == null) {
57             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
58             return;
59         }
60
61         if (isButtonChannel(channelUID) && command instanceof StringType stringCommand) {
62             if (stringCommand.equals(PRESSED) || stringCommand.equals(LONG_PRESSED)) {
63                 VelbusButtonPacket packet = new VelbusButtonPacket(getModuleAddress().getChannelIdentifier(channelUID));
64
65                 packet.Pressed();
66                 velbusBridgeHandler.sendPacket(packet.getBytes());
67                 triggerChannel("CH6t", CommonTriggerEvents.PRESSED);
68
69                 if (stringCommand.equals(LONG_PRESSED)) {
70                     packet.LongPressed();
71                     velbusBridgeHandler.sendPacket(packet.getBytes());
72                     triggerChannel("CH6t", CommonTriggerEvents.LONG_PRESSED);
73                 }
74
75                 packet.Released();
76                 velbusBridgeHandler.sendPacket(packet.getBytes());
77                 triggerChannel("CH6t", CommonTriggerEvents.RELEASED);
78             } else {
79                 throw new UnsupportedOperationException(
80                         "The command '" + command + "' is not supported on channel '" + channelUID + "'.");
81             }
82         }
83     }
84
85     private boolean isButtonChannel(ChannelUID channelUID) {
86         return "CH6".equals(channelUID.toString().substring(channelUID.toString().length() - 3));
87     }
88
89     private boolean isTriggerChannel(byte address, byte channel) {
90         VelbusChannelIdentifier velbusChannelIdentifier = new VelbusChannelIdentifier(address, channel);
91
92         return getModuleAddress().getChannelNumber(velbusChannelIdentifier) == 6;
93     }
94
95     @Override
96     public void onPacketReceived(byte[] packet) {
97         super.onPacketReceived(packet);
98
99         if (packet[0] == VelbusPacket.STX && packet.length >= 5) {
100             byte command = packet[4];
101
102             if (command == COMMAND_PUSH_BUTTON_STATUS && packet.length >= 6) {
103                 byte address = packet[2];
104
105                 byte channelJustPressed = packet[5];
106                 if (isTriggerChannel(address, channelJustPressed)) {
107                     triggerChannel("CH6t", CommonTriggerEvents.PRESSED);
108                 }
109
110                 byte channelJustReleased = packet[6];
111                 if (isTriggerChannel(address, channelJustReleased)) {
112                     triggerChannel("CH6t", CommonTriggerEvents.RELEASED);
113                 }
114
115                 byte channelLongPressed = packet[7];
116                 if (isTriggerChannel(address, channelLongPressed)) {
117                     triggerChannel("CH6t", CommonTriggerEvents.LONG_PRESSED);
118                 }
119             }
120         }
121     }
122 }