]> git.basschouten.com Git - openhab-addons.git/blob
8305cd977d06c1944b51ba8cfd2003ac3877e46f
[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 javax.measure.quantity.Illuminance;
22 import javax.measure.quantity.Length;
23 import javax.measure.quantity.Speed;
24
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;
37
38 /**
39  * The {@link VelbusVMBMeteoHandler} is responsible for handling commands, which are
40  * sent to one of the channels.
41  *
42  * @author Cedric Boon - Initial contribution
43  */
44 @NonNullByDefault
45 public class VelbusVMBMeteoHandler extends VelbusTemperatureSensorHandler {
46     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = new HashSet<>(Arrays.asList(THING_TYPE_VMBMETEO));
47
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;
52
53     private ChannelUID rainfallChannel;
54     private ChannelUID illuminanceChannel;
55     private ChannelUID windspeedChannel;
56
57     public VelbusVMBMeteoHandler(Thing thing) {
58         super(thing, 0, new ChannelUID(thing.getUID(), "weatherStation", "CH10"));
59
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");
63     }
64
65     @Override
66     public void handleCommand(ChannelUID channelUID, Command command) {
67         super.handleCommand(channelUID, command);
68
69         VelbusBridgeHandler velbusBridgeHandler = getVelbusBridgeHandler();
70         if (velbusBridgeHandler == null) {
71             logger.warn("Velbus bridge handler not found. Cannot handle command without bridge.");
72             return;
73         }
74
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);
82             }
83         }
84     }
85
86     @Override
87     protected void sendSensorReadoutRequest(VelbusBridgeHandler velbusBridgeHandler) {
88         super.sendSensorReadoutRequest(velbusBridgeHandler);
89
90         sendSensorReadoutRequest(velbusBridgeHandler, ALL_SENSOR_CHANNELS);
91     }
92
93     protected void sendSensorReadoutRequest(VelbusBridgeHandler velbusBridgeHandler, byte channel) {
94         VelbusSensorReadoutRequestPacket packet = new VelbusSensorReadoutRequestPacket(getModuleAddress().getAddress(),
95                 channel);
96
97         byte[] packetBytes = packet.getBytes();
98         velbusBridgeHandler.sendPacket(packetBytes);
99     }
100
101     @Override
102     protected int getClockAlarmAndProgramSelectionIndexInModuleStatus() {
103         return 8;
104     }
105
106     @Override
107     public void onPacketReceived(byte[] packet) {
108         super.onPacketReceived(packet);
109
110         logger.trace("onPacketReceived() was called");
111
112         if (packet[0] == VelbusPacket.STX && packet.length >= 5) {
113             byte command = packet[4];
114
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];
122
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;
126
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);
130
131                 updateState(rainfallChannel, rainValueState);
132                 updateState(illuminanceChannel, lightValueState);
133                 updateState(windspeedChannel, windValueState);
134             }
135         }
136     }
137 }