]> git.basschouten.com Git - openhab-addons.git/blob
77f21b8d7ce8e10227f0d31d1c55a1212d7ff3a0
[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.config.VelbusDimmerConfig;
24 import org.openhab.binding.velbus.internal.packets.VelbusDimmerPacket;
25 import org.openhab.binding.velbus.internal.packets.VelbusPacket;
26 import org.openhab.binding.velbus.internal.packets.VelbusStatusRequestPacket;
27 import org.openhab.core.library.types.OnOffType;
28 import org.openhab.core.library.types.PercentType;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.thing.ThingStatusDetail;
33 import org.openhab.core.thing.ThingTypeUID;
34 import org.openhab.core.types.Command;
35 import org.openhab.core.types.RefreshType;
36
37 /**
38  * The {@link VelbusDimmerHandler} is responsible for handling commands, which are
39  * sent to one of the channels.
40  *
41  * @author Cedric Boon - Initial contribution
42  */
43 @NonNullByDefault
44 public class VelbusDimmerHandler extends VelbusThingHandler {
45     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = new HashSet<>(Arrays.asList(THING_TYPE_VMB1DM,
46             THING_TYPE_VMB1LED, THING_TYPE_VMB4DC, THING_TYPE_VMBDME, THING_TYPE_VMBDMI, THING_TYPE_VMBDMIR));
47
48     private @NonNullByDefault({}) VelbusDimmerConfig dimmerConfig;
49
50     public VelbusDimmerHandler(Thing thing) {
51         super(thing, 0);
52     }
53
54     @Override
55     public void initialize() {
56         this.dimmerConfig = getConfigAs(VelbusDimmerConfig.class);
57
58         super.initialize();
59     }
60
61     @Override
62     public void handleCommand(ChannelUID channelUID, Command command) {
63         VelbusBridgeHandler velbusBridgeHandler = getVelbusBridgeHandler();
64         if (velbusBridgeHandler == null) {
65             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
66             return;
67         }
68
69         if (command instanceof RefreshType) {
70             VelbusStatusRequestPacket packet = new VelbusStatusRequestPacket(
71                     getModuleAddress().getChannelIdentifier(channelUID));
72
73             byte[] packetBytes = packet.getBytes();
74             velbusBridgeHandler.sendPacket(packetBytes);
75         } else if (command instanceof PercentType) {
76             byte commandByte = COMMAND_SET_VALUE;
77
78             VelbusDimmerPacket packet = new VelbusDimmerPacket(getModuleAddress().getChannelIdentifier(channelUID),
79                     commandByte, ((PercentType) command).byteValue(), this.dimmerConfig.dimspeed,
80                     isFirstGenerationDevice());
81
82             byte[] packetBytes = packet.getBytes();
83             velbusBridgeHandler.sendPacket(packetBytes);
84         } else if (command instanceof OnOffType) {
85             byte commandByte = determineCommandByte((OnOffType) command);
86
87             VelbusDimmerPacket packet = new VelbusDimmerPacket(getModuleAddress().getChannelIdentifier(channelUID),
88                     commandByte, (byte) 0x00, this.dimmerConfig.dimspeed, isFirstGenerationDevice());
89
90             byte[] packetBytes = packet.getBytes();
91             velbusBridgeHandler.sendPacket(packetBytes);
92         } else {
93             logger.debug("The command '{}' is not supported by this handler.", command.getClass());
94         }
95     }
96
97     private byte determineCommandByte(OnOffType command) {
98         return (command == OnOffType.ON) ? COMMAND_RESTORE_LAST_DIMVALUE : COMMAND_SET_VALUE;
99     }
100
101     private Boolean isFirstGenerationDevice() {
102         ThingTypeUID thingTypeUID = this.getThing().getThingTypeUID();
103         return thingTypeUID.equals(THING_TYPE_VMB1DM) || thingTypeUID.equals(THING_TYPE_VMB1LED)
104                 || thingTypeUID.equals(THING_TYPE_VMBDME);
105     }
106
107     @Override
108     public void onPacketReceived(byte[] packet) {
109         logger.trace("onPacketReceived() was called");
110
111         if (packet[0] == VelbusPacket.STX && packet.length >= 5) {
112             byte address = packet[2];
113             byte command = packet[4];
114
115             if ((command == COMMAND_DIMMERCONTROLLER_STATUS) && packet.length >= 8) {
116                 byte channel = packet[5];
117                 byte dimValue = packet[7];
118
119                 VelbusChannelIdentifier velbusChannelIdentifier = new VelbusChannelIdentifier(address, channel);
120                 PercentType state = new PercentType(dimValue);
121                 updateState(getModuleAddress().getChannelId(velbusChannelIdentifier), state);
122             } else if ((command == COMMAND_DIMMER_STATUS) && packet.length >= 7) {
123                 byte dimValue = packet[6];
124
125                 VelbusChannelIdentifier velbusChannelIdentifier = new VelbusChannelIdentifier(address, (byte) 0x01);
126                 PercentType state = new PercentType(dimValue);
127                 updateState(getModuleAddress().getChannelId(velbusChannelIdentifier), state);
128             }
129         }
130     }
131 }