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;
22 import javax.measure.quantity.Length;
23 import javax.measure.quantity.Speed;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.openhab.binding.velbus.internal.packets.VelbusPacket;
27 import org.openhab.binding.velbus.internal.packets.VelbusSensorReadoutRequestPacket;
28 import org.openhab.core.library.types.QuantityType;
29 import org.openhab.core.library.unit.MetricPrefix;
30 import org.openhab.core.library.unit.SIUnits;
31 import org.openhab.core.library.unit.Units;
32 import org.openhab.core.thing.ChannelUID;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingTypeUID;
35 import org.openhab.core.types.Command;
36 import org.openhab.core.types.RefreshType;
39 * The {@link VelbusVMBMeteoHandler} is responsible for handling commands, which are
40 * sent to one of the channels.
42 * @author Cedric Boon - Initial contribution
45 public class VelbusVMBMeteoHandler extends VelbusTemperatureSensorHandler {
46 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = new HashSet<>(Arrays.asList(THING_TYPE_VMBMETEO));
48 private static final byte RAIN_SENSOR_CHANNEL = 0x02;
49 private static final byte LIGHT_SENSOR_CHANNEL = 0x04;
50 private static final byte WIND_SENSOR_CHANNEL = 0x08;
51 private static final byte ALL_SENSOR_CHANNELS = RAIN_SENSOR_CHANNEL | LIGHT_SENSOR_CHANNEL | WIND_SENSOR_CHANNEL;
53 private ChannelUID rainfallChannel;
54 private ChannelUID illuminanceChannel;
55 private ChannelUID windspeedChannel;
57 public VelbusVMBMeteoHandler(Thing thing) {
58 super(thing, 0, new ChannelUID(thing.getUID(), "weatherStation", "CH10"));
60 this.rainfallChannel = new ChannelUID(thing.getUID(), "weatherStation", "CH11");
61 this.illuminanceChannel = new ChannelUID(thing.getUID(), "weatherStation", "CH12");
62 this.windspeedChannel = new ChannelUID(thing.getUID(), "weatherStation", "CH13");
66 public void handleCommand(ChannelUID channelUID, Command command) {
67 super.handleCommand(channelUID, command);
69 VelbusBridgeHandler velbusBridgeHandler = getVelbusBridgeHandler();
70 if (velbusBridgeHandler == null) {
71 logger.warn("Velbus bridge handler not found. Cannot handle command without bridge.");
75 if (command instanceof RefreshType) {
76 if (channelUID.equals(rainfallChannel)) {
77 sendSensorReadoutRequest(velbusBridgeHandler, RAIN_SENSOR_CHANNEL);
78 } else if (channelUID.equals(illuminanceChannel)) {
79 sendSensorReadoutRequest(velbusBridgeHandler, LIGHT_SENSOR_CHANNEL);
80 } else if (channelUID.equals(windspeedChannel)) {
81 sendSensorReadoutRequest(velbusBridgeHandler, WIND_SENSOR_CHANNEL);
87 protected void sendSensorReadoutRequest(VelbusBridgeHandler velbusBridgeHandler) {
88 super.sendSensorReadoutRequest(velbusBridgeHandler);
90 sendSensorReadoutRequest(velbusBridgeHandler, ALL_SENSOR_CHANNELS);
93 protected void sendSensorReadoutRequest(VelbusBridgeHandler velbusBridgeHandler, byte channel) {
94 VelbusSensorReadoutRequestPacket packet = new VelbusSensorReadoutRequestPacket(getModuleAddress().getAddress(),
97 byte[] packetBytes = packet.getBytes();
98 velbusBridgeHandler.sendPacket(packetBytes);
102 protected int getClockAlarmAndProgramSelectionIndexInModuleStatus() {
107 public void onPacketReceived(byte[] packet) {
108 super.onPacketReceived(packet);
110 logger.trace("onPacketReceived() was called");
112 if (packet[0] == VelbusPacket.STX && packet.length >= 5) {
113 byte command = packet[4];
115 if (command == COMMAND_SENSOR_RAW_DATA && packet.length >= 10) {
116 byte highByteCurrentRainValue = packet[5];
117 byte lowByteCurrentRainValue = packet[6];
118 byte highByteCurrentLightValue = packet[7];
119 byte lowByteCurrentLightValue = packet[8];
120 byte highByteCurrentWindValue = packet[9];
121 byte lowByteCurrentWindValue = packet[10];
123 double rainValue = (((highByteCurrentRainValue & 0xff) << 8) + (lowByteCurrentRainValue & 0xff)) / 10;
124 double lightValue = (((highByteCurrentLightValue & 0xff) << 8) + (lowByteCurrentLightValue & 0xff));
125 double windValue = (((highByteCurrentWindValue & 0xff) << 8) + (lowByteCurrentWindValue & 0xff)) / 10;
127 QuantityType<Length> rainValueState = new QuantityType<>(rainValue, MetricPrefix.MILLI(SIUnits.METRE));
128 QuantityType<Illuminance> lightValueState = new QuantityType<>(lightValue, Units.LUX);
129 QuantityType<Speed> windValueState = new QuantityType<>(windValue, SIUnits.KILOMETRE_PER_HOUR);
131 updateState(rainfallChannel, rainValueState);
132 updateState(illuminanceChannel, lightValueState);
133 updateState(windspeedChannel, windValueState);