]> git.basschouten.com Git - openhab-addons.git/blob
d0caa5a5d64f5ebb76d2b9400dff0c37682737f4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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         if (getModuleAddress().getChannelNumber(velbusChannelIdentifier) == 6) {
95             return true;
96         } else {
97             return false;
98         }
99     }
100
101     @Override
102     public void onPacketReceived(byte[] packet) {
103         super.onPacketReceived(packet);
104
105         if (packet[0] == VelbusPacket.STX && packet.length >= 5) {
106             byte command = packet[4];
107
108             if (command == COMMAND_PUSH_BUTTON_STATUS && packet.length >= 6) {
109                 byte address = packet[2];
110
111                 byte channelJustPressed = packet[5];
112                 if (isTriggerChannel(address, channelJustPressed)) {
113                     triggerChannel("CH6t", CommonTriggerEvents.PRESSED);
114                 }
115
116                 byte channelJustReleased = packet[6];
117                 if (isTriggerChannel(address, channelJustReleased)) {
118                     triggerChannel("CH6t", CommonTriggerEvents.RELEASED);
119                 }
120
121                 byte channelLongPressed = packet[7];
122                 if (isTriggerChannel(address, channelLongPressed)) {
123                     triggerChannel("CH6t", CommonTriggerEvents.LONG_PRESSED);
124                 }
125             }
126         }
127     }
128 }