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 Rain message.
31 * @author Damien Servant - Initial contribution
32 * @author Martin van Wingerden - ported to openHAB 2.0
34 public class RFXComTemperatureRainMessage extends RFXComBatteryDeviceMessage<RFXComTemperatureRainMessage.SubType> {
36 public enum SubType implements ByteEnumWrapper {
39 private final int subType;
41 SubType(int subType) {
42 this.subType = subType;
46 public byte toByte() {
47 return (byte) subType;
51 public SubType subType;
53 public double temperature;
54 public double rainTotal;
56 public RFXComTemperatureRainMessage() {
57 super(PacketType.TEMPERATURE_RAIN);
60 public RFXComTemperatureRainMessage(byte[] data) throws RFXComException {
65 public String toString() {
66 String str = super.toString();
67 str += ", Sub type = " + subType;
68 str += ", Device Id = " + sensorId;
69 str += ", Temperature = " + temperature;
70 str += ", Rain total = " + rainTotal;
71 str += ", Signal level = " + signalLevel;
72 str += ", Battery level = " + batteryLevel;
78 public String getDeviceId() {
79 return String.valueOf(sensorId);
83 public void encodeMessage(byte[] data) throws RFXComException {
84 super.encodeMessage(data);
86 subType = fromByte(SubType.class, super.subType);
87 sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
89 temperature = ((data[6] & 0x7F) << 8 | (data[7] & 0xFF)) * 0.1;
90 if ((data[6] & 0x80) != 0) {
91 temperature = -temperature;
93 rainTotal = ((data[8] & 0xFF) << 8 | (data[9] & 0xFF)) * 0.1;
94 signalLevel = (byte) ((data[10] & 0xF0) >> 4);
95 batteryLevel = (byte) (data[10] & 0x0F);
99 public byte[] decodeMessage() {
100 byte[] data = new byte[11];
102 data[0] = (byte) (data.length - 1);
103 data[1] = RFXComBaseMessage.PacketType.TEMPERATURE_RAIN.toByte();
104 data[2] = subType.toByte();
106 data[4] = (byte) ((sensorId & 0xFF00) >> 8);
107 data[5] = (byte) (sensorId & 0x00FF);
109 short temp = (short) Math.abs(temperature * 10);
110 data[6] = (byte) ((temp >> 8) & 0xFF);
111 data[7] = (byte) (temp & 0xFF);
112 if (temperature < 0) {
116 short rainT = (short) Math.abs(rainTotal * 10);
117 data[8] = (byte) ((rainT >> 8) & 0xFF);
118 data[9] = (byte) (rainT & 0xFF);
119 data[10] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
125 public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
126 throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
128 case CHANNEL_TEMPERATURE:
129 return new DecimalType(temperature);
131 case CHANNEL_RAIN_TOTAL:
132 return new DecimalType(rainTotal);
135 return super.convertToState(channelId, config, deviceState);
140 public void convertFromState(String channelId, Type type) {
141 throw new UnsupportedOperationException();
145 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
146 return ByteEnumUtil.convertSubType(SubType.class, subType);
150 public void setSubType(SubType subType) {
151 throw new UnsupportedOperationException();
155 public void setDeviceId(String deviceId) {
156 throw new UnsupportedOperationException();