]> git.basschouten.com Git - openhab-addons.git/blob
aa7c13f9fbe08a9ec3f776a4f2a8c7f8d7a02b2b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.VelbusFeedbackLEDPacket;
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 VelbusSensorHandler} is responsible for handling commands, which are
36  * sent to one of the channels.
37  *
38  * @author Cedric Boon - Initial contribution
39  */
40 @NonNullByDefault
41 public class VelbusSensorHandler extends VelbusThingHandler {
42     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = new HashSet<>(
43             Arrays.asList(THING_TYPE_VMB6IN, THING_TYPE_VMB8IR, THING_TYPE_VMB8PB));
44
45     private static final StringType SET_LED = new StringType("SET_LED");
46     private static final StringType SLOW_BLINK_LED = new StringType("SLOW_BLINK_LED");
47     private static final StringType FAST_BLINK_LED = new StringType("FAST_BLINK_LED");
48     private static final StringType VERY_FAST_BLINK_LED = new StringType("VERY_FAST_BLINK_LED");
49     private static final StringType CLEAR_LED = new StringType("CLEAR_LED");
50
51     public VelbusSensorHandler(Thing thing) {
52         this(thing, 0);
53     }
54
55     public VelbusSensorHandler(Thing thing, int numberOfSubAddresses) {
56         super(thing, numberOfSubAddresses);
57     }
58
59     @Override
60     public void handleCommand(ChannelUID channelUID, Command command) {
61         VelbusBridgeHandler velbusBridgeHandler = getVelbusBridgeHandler();
62         if (velbusBridgeHandler == null) {
63             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
64             return;
65         }
66
67         if (isFeedbackChannel(channelUID) && command instanceof StringType) {
68             byte commandByte;
69
70             StringType stringTypeCommand = (StringType) command;
71             if (stringTypeCommand.equals(SET_LED)) {
72                 commandByte = COMMAND_SET_LED;
73             } else if (stringTypeCommand.equals(SLOW_BLINK_LED)) {
74                 commandByte = COMMAND_SLOW_BLINK_LED;
75             } else if (stringTypeCommand.equals(FAST_BLINK_LED)) {
76                 commandByte = COMMAND_FAST_BLINK_LED;
77             } else if (stringTypeCommand.equals(VERY_FAST_BLINK_LED)) {
78                 commandByte = COMMAND_VERY_FAST_BLINK_LED;
79             } else if (stringTypeCommand.equals(CLEAR_LED)) {
80                 commandByte = COMMAND_CLEAR_LED;
81             } else {
82                 throw new UnsupportedOperationException(
83                         "The command '" + command + "' is not supported on channel '" + channelUID + "'.");
84             }
85
86             VelbusFeedbackLEDPacket packet = new VelbusFeedbackLEDPacket(
87                     getModuleAddress().getChannelIdentifier(channelUID), commandByte);
88
89             byte[] packetBytes = packet.getBytes();
90             velbusBridgeHandler.sendPacket(packetBytes);
91         }
92     }
93
94     private boolean isFeedbackChannel(ChannelUID channelUID) {
95         return "feedback".equals(channelUID.getGroupId());
96     }
97
98     @Override
99     public void onPacketReceived(byte[] packet) {
100         logger.trace("onPacketReceived() was called");
101
102         if (packet[0] == VelbusPacket.STX && packet.length >= 5) {
103             byte address = packet[2];
104             byte command = packet[4];
105
106             if (command == COMMAND_PUSH_BUTTON_STATUS && packet.length >= 6) {
107                 byte channelJustPressed = packet[5];
108                 if (channelJustPressed != 0) {
109                     VelbusChannelIdentifier velbusChannelIdentifier = new VelbusChannelIdentifier(address,
110                             channelJustPressed);
111                     triggerChannel("input#" + getModuleAddress().getChannelId(velbusChannelIdentifier),
112                             CommonTriggerEvents.PRESSED);
113                 }
114
115                 byte channelJustReleased = packet[6];
116                 if (channelJustReleased != 0) {
117                     VelbusChannelIdentifier velbusChannelIdentifier = new VelbusChannelIdentifier(address,
118                             channelJustReleased);
119                     triggerChannel("input#" + getModuleAddress().getChannelId(velbusChannelIdentifier),
120                             CommonTriggerEvents.RELEASED);
121                 }
122
123                 byte channelLongPressed = packet[7];
124                 if (channelLongPressed != 0) {
125                     VelbusChannelIdentifier velbusChannelIdentifier = new VelbusChannelIdentifier(address,
126                             channelLongPressed);
127                     triggerChannel("input#" + getModuleAddress().getChannelId(velbusChannelIdentifier),
128                             CommonTriggerEvents.LONG_PRESSED);
129                 }
130             }
131         }
132     }
133 }