2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.velbus.internal.handler;
15 import static org.openhab.binding.velbus.internal.VelbusBindingConstants.*;
17 import java.util.Arrays;
18 import java.util.HashSet;
21 import javax.measure.quantity.Illuminance;
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;
37 * The {@link VelbusVMBPIROHandler} is responsible for handling commands, which are
38 * sent to one of the channels.
40 * @author Cedric Boon - Initial contribution
43 public class VelbusVMBPIROHandler extends VelbusTemperatureSensorHandler {
44 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = new HashSet<>(Arrays.asList(THING_TYPE_VMBPIRO));
46 private ChannelUID illuminanceChannel;
48 public VelbusVMBPIROHandler(Thing thing) {
49 super(thing, 0, new ChannelUID(thing.getUID(), "input", "CH9"));
51 this.illuminanceChannel = new ChannelUID(thing.getUID(), "input", "LIGHT");
55 public void handleCommand(ChannelUID channelUID, Command command) {
56 super.handleCommand(channelUID, command);
58 VelbusBridgeHandler velbusBridgeHandler = getVelbusBridgeHandler();
59 if (velbusBridgeHandler == null) {
60 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
64 if (command instanceof RefreshType) {
65 if (channelUID.equals(illuminanceChannel)) {
66 VelbusLightValueRequestPacket packet = new VelbusLightValueRequestPacket(
67 getModuleAddress().getAddress());
69 byte[] packetBytes = packet.getBytes();
70 velbusBridgeHandler.sendPacket(packetBytes);
76 public void onPacketReceived(byte[] packet) {
77 super.onPacketReceived(packet);
79 logger.trace("onPacketReceived() was called");
81 if (packet[0] == VelbusPacket.STX && packet.length >= 5) {
82 byte command = packet[4];
84 if (command == COMMAND_MODULE_STATUS && packet.length >= 6) {
85 byte highByteCurrentLightValue = packet[6];
86 byte lowByteCurrentLightValue = packet[7];
88 double lightValue = (((highByteCurrentLightValue & 0xff) << 8) + (lowByteCurrentLightValue & 0xff));
89 QuantityType<Illuminance> lightValueState = new QuantityType<>(lightValue, Units.LUX);
90 updateState(illuminanceChannel, lightValueState);