2 * Copyright (c) 2010-2021 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.exceptions.RFXComException;
19 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
20 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
21 import org.openhab.binding.rfxcom.internal.handler.DeviceState;
22 import org.openhab.core.library.types.DecimalType;
23 import org.openhab.core.types.State;
24 import org.openhab.core.types.Type;
27 * RFXCOM data class for Temperature and Rain message.
29 * @author Damien Servant - Initial contribution
30 * @author Martin van Wingerden - ported to openHAB 2.0
32 public class RFXComTemperatureRainMessage extends RFXComBatteryDeviceMessage<RFXComTemperatureRainMessage.SubType> {
34 public enum SubType implements ByteEnumWrapper {
37 private final int subType;
39 SubType(int subType) {
40 this.subType = subType;
44 public byte toByte() {
45 return (byte) subType;
49 public SubType subType;
51 public double temperature;
52 public double rainTotal;
54 public RFXComTemperatureRainMessage() {
55 super(PacketType.TEMPERATURE_RAIN);
58 public RFXComTemperatureRainMessage(byte[] data) throws RFXComException {
63 public String toString() {
64 String str = super.toString();
65 str += ", Sub type = " + subType;
66 str += ", Device Id = " + sensorId;
67 str += ", Temperature = " + temperature;
68 str += ", Rain total = " + rainTotal;
69 str += ", Signal level = " + signalLevel;
70 str += ", Battery level = " + batteryLevel;
76 public String getDeviceId() {
77 return String.valueOf(sensorId);
81 public void encodeMessage(byte[] data) throws RFXComException {
82 super.encodeMessage(data);
84 subType = fromByte(SubType.class, super.subType);
85 sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
87 temperature = ((data[6] & 0x7F) << 8 | (data[7] & 0xFF)) * 0.1;
88 if ((data[6] & 0x80) != 0) {
89 temperature = -temperature;
91 rainTotal = ((data[8] & 0xFF) << 8 | (data[9] & 0xFF)) * 0.1;
92 signalLevel = (byte) ((data[10] & 0xF0) >> 4);
93 batteryLevel = (byte) (data[10] & 0x0F);
97 public byte[] decodeMessage() {
98 byte[] data = new byte[11];
100 data[0] = (byte) (data.length - 1);
101 data[1] = RFXComBaseMessage.PacketType.TEMPERATURE_RAIN.toByte();
102 data[2] = subType.toByte();
104 data[4] = (byte) ((sensorId & 0xFF00) >> 8);
105 data[5] = (byte) (sensorId & 0x00FF);
107 short temp = (short) Math.abs(temperature * 10);
108 data[6] = (byte) ((temp >> 8) & 0xFF);
109 data[7] = (byte) (temp & 0xFF);
110 if (temperature < 0) {
114 short rainT = (short) Math.abs(rainTotal * 10);
115 data[8] = (byte) ((rainT >> 8) & 0xFF);
116 data[9] = (byte) (rainT & 0xFF);
117 data[10] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
123 public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
125 case CHANNEL_TEMPERATURE:
126 return new DecimalType(temperature);
128 case CHANNEL_RAIN_TOTAL:
129 return new DecimalType(rainTotal);
132 return super.convertToState(channelId, deviceState);
137 public void convertFromState(String channelId, Type type) {
138 throw new UnsupportedOperationException();
142 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
143 return ByteEnumUtil.convertSubType(SubType.class, subType);
147 public void setSubType(SubType subType) {
148 throw new UnsupportedOperationException();
152 public void setDeviceId(String deviceId) {
153 throw new UnsupportedOperationException();