]> git.basschouten.com Git - openhab-addons.git/blob
9b9f7adcb3e435ffc4e56633446d848226f0300b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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 javax.measure.quantity.Illuminance;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.openhab.binding.velbus.internal.packets.VelbusLightValueRequestPacket;
25 import org.openhab.binding.velbus.internal.packets.VelbusPacket;
26 import org.openhab.core.library.types.QuantityType;
27 import org.openhab.core.library.unit.Units;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingStatus;
31 import org.openhab.core.thing.ThingStatusDetail;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.types.Command;
34 import org.openhab.core.types.RefreshType;
35
36 /**
37  * The {@link VelbusVMBPIROHandler} is responsible for handling commands, which are
38  * sent to one of the channels.
39  *
40  * @author Cedric Boon - Initial contribution
41  */
42 @NonNullByDefault
43 public class VelbusVMBPIROHandler extends VelbusTemperatureSensorHandler {
44     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = new HashSet<>(Arrays.asList(THING_TYPE_VMBPIRO));
45
46     private ChannelUID illuminanceChannel;
47
48     public VelbusVMBPIROHandler(Thing thing) {
49         super(thing, 0, new ChannelUID(thing.getUID(), "input", "CH9"));
50
51         this.illuminanceChannel = new ChannelUID(thing.getUID(), "input", "LIGHT");
52     }
53
54     @Override
55     public void handleCommand(ChannelUID channelUID, Command command) {
56         super.handleCommand(channelUID, command);
57
58         VelbusBridgeHandler velbusBridgeHandler = getVelbusBridgeHandler();
59         if (velbusBridgeHandler == null) {
60             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
61             return;
62         }
63
64         if (command instanceof RefreshType) {
65             if (channelUID.equals(illuminanceChannel)) {
66                 VelbusLightValueRequestPacket packet = new VelbusLightValueRequestPacket(
67                         getModuleAddress().getAddress());
68
69                 byte[] packetBytes = packet.getBytes();
70                 velbusBridgeHandler.sendPacket(packetBytes);
71             }
72         }
73     }
74
75     @Override
76     public void onPacketReceived(byte[] packet) {
77         super.onPacketReceived(packet);
78
79         logger.trace("onPacketReceived() was called");
80
81         if (packet[0] == VelbusPacket.STX && packet.length >= 5) {
82             byte command = packet[4];
83
84             if (command == COMMAND_MODULE_STATUS && packet.length >= 6) {
85                 byte highByteCurrentLightValue = packet[6];
86                 byte lowByteCurrentLightValue = packet[7];
87
88                 double lightValue = (((highByteCurrentLightValue & 0xff) << 8) + (lowByteCurrentLightValue & 0xff));
89                 QuantityType<Illuminance> lightValueState = new QuantityType<>(lightValue, Units.LUX);
90                 updateState(illuminanceChannel, lightValueState);
91             }
92         }
93     }
94 }