]> git.basschouten.com Git - openhab-addons.git/blob
daf91de7d32274ed72bb3c7ae9ed1867da0501c4
[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) {
62             StringType stringTypeCommand = (StringType) command;
63
64             if (stringTypeCommand.equals(PRESSED) || stringTypeCommand.equals(LONG_PRESSED)) {
65                 VelbusButtonPacket packet = new VelbusButtonPacket(getModuleAddress().getChannelIdentifier(channelUID));
66
67                 packet.Pressed();
68                 velbusBridgeHandler.sendPacket(packet.getBytes());
69                 triggerChannel("CH6t", CommonTriggerEvents.PRESSED);
70
71                 if (stringTypeCommand.equals(LONG_PRESSED)) {
72                     packet.LongPressed();
73                     velbusBridgeHandler.sendPacket(packet.getBytes());
74                     triggerChannel("CH6t", CommonTriggerEvents.LONG_PRESSED);
75                 }
76
77                 packet.Released();
78                 velbusBridgeHandler.sendPacket(packet.getBytes());
79                 triggerChannel("CH6t", CommonTriggerEvents.RELEASED);
80             } else {
81                 throw new UnsupportedOperationException(
82                         "The command '" + command + "' is not supported on channel '" + channelUID + "'.");
83             }
84         }
85     }
86
87     private boolean isButtonChannel(ChannelUID channelUID) {
88         return "CH6".equals(channelUID.toString().substring(channelUID.toString().length() - 3));
89     }
90
91     private boolean isTriggerChannel(byte address, byte channel) {
92         VelbusChannelIdentifier velbusChannelIdentifier = new VelbusChannelIdentifier(address, channel);
93
94         return getModuleAddress().getChannelNumber(velbusChannelIdentifier) == 6;
95     }
96
97     @Override
98     public void onPacketReceived(byte[] packet) {
99         super.onPacketReceived(packet);
100
101         if (packet[0] == VelbusPacket.STX && packet.length >= 5) {
102             byte command = packet[4];
103
104             if (command == COMMAND_PUSH_BUTTON_STATUS && packet.length >= 6) {
105                 byte address = packet[2];
106
107                 byte channelJustPressed = packet[5];
108                 if (isTriggerChannel(address, channelJustPressed)) {
109                     triggerChannel("CH6t", CommonTriggerEvents.PRESSED);
110                 }
111
112                 byte channelJustReleased = packet[6];
113                 if (isTriggerChannel(address, channelJustReleased)) {
114                     triggerChannel("CH6t", CommonTriggerEvents.RELEASED);
115                 }
116
117                 byte channelLongPressed = packet[7];
118                 if (isTriggerChannel(address, channelLongPressed)) {
119                     triggerChannel("CH6t", CommonTriggerEvents.LONG_PRESSED);
120                 }
121             }
122         }
123     }
124 }