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.rfxcom.internal.messages;
15 import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
16 import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
18 import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
19 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
20 import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
21 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
22 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
23 import org.openhab.binding.rfxcom.internal.handler.DeviceState;
24 import org.openhab.core.library.types.DecimalType;
25 import org.openhab.core.types.State;
26 import org.openhab.core.types.Type;
29 * RFXCOM data class for temperature and humidity message.
31 * @author Marc SAUVEUR - Initial contribution
32 * @author Pauli Anttila - Migrated for OH2
34 public class RFXComRainMessage extends RFXComBatteryDeviceMessage<RFXComRainMessage.SubType> {
36 public enum SubType implements ByteEnumWrapper {
47 private final int subType;
49 SubType(int subType) {
50 this.subType = subType;
54 public byte toByte() {
55 return (byte) subType;
59 public SubType subType;
61 public double rainRate;
62 public double rainTotal;
64 public RFXComRainMessage() {
65 super(PacketType.RAIN);
68 public RFXComRainMessage(byte[] data) throws RFXComException {
73 public String toString() {
76 str += super.toString();
77 str += ", Sub type = " + subType;
78 str += ", Device Id = " + getDeviceId();
79 str += ", Rain rate = " + rainRate;
80 str += ", Rain total = " + rainTotal;
81 str += ", Signal level = " + signalLevel;
82 str += ", Battery level = " + batteryLevel;
88 public void encodeMessage(byte[] data) throws RFXComException {
89 super.encodeMessage(data);
91 subType = fromByte(SubType.class, super.subType);
92 sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
94 rainRate = (short) ((data[6] & 0xFF) << 8 | (data[7] & 0xFF));
95 if (subType == SubType.RAIN2) {
99 if (subType == SubType.RAIN6) {
100 rainTotal = (short) ((data[10] & 0xFF)) * 0.266;
102 rainTotal = (short) ((data[8] & 0xFF) << 8 | (data[9] & 0xFF) << 8 | (data[10] & 0xFF)) * 0.1;
105 signalLevel = (byte) ((data[11] & 0xF0) >> 4);
106 batteryLevel = (byte) (data[11] & 0x0F);
110 public byte[] decodeMessage() {
111 byte[] data = new byte[12];
114 data[1] = RFXComBaseMessage.PacketType.RAIN.toByte();
115 data[2] = subType.toByte();
117 data[4] = (byte) ((sensorId & 0xFF00) >> 8);
118 data[5] = (byte) (sensorId & 0x00FF);
120 short rainR = (short) Math.abs(rainRate * 100);
121 data[6] = (byte) ((rainR >> 8) & 0xFF);
122 data[7] = (byte) (rainR & 0xFF);
124 short rainT = (short) Math.abs(rainTotal * 10);
125 data[8] = (byte) ((rainT >> 16) & 0xFF);
126 data[9] = (byte) ((rainT >> 8) & 0xFF);
127 data[10] = (byte) (rainT & 0xFF);
129 data[11] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
135 public String getDeviceId() {
136 return String.valueOf(sensorId);
140 public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
141 throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
143 case CHANNEL_RAIN_RATE:
144 return new DecimalType(rainRate);
146 case CHANNEL_RAIN_TOTAL:
147 return new DecimalType(rainTotal);
150 return super.convertToState(channelId, config, deviceState);
155 public void setSubType(SubType subType) {
156 throw new UnsupportedOperationException();
160 public void setDeviceId(String deviceId) {
161 throw new UnsupportedOperationException();
165 public void convertFromState(String channelId, Type type) {
166 throw new UnsupportedOperationException();
170 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
171 return ByteEnumUtil.convertSubType(SubType.class, subType);