2 * Copyright (c) 2010-2020 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.library.types.StringType;
24 import org.openhab.core.types.State;
25 import org.openhab.core.types.Type;
28 * RFXCOM data class for temperature and humidity message.
30 * @author Pauli Anttila - Initial contribution
32 public class RFXComTemperatureHumidityMessage
33 extends RFXComBatteryDeviceMessage<RFXComTemperatureHumidityMessage.SubType> {
35 public enum SubType implements ByteEnumWrapper {
51 private final int subType;
53 SubType(int subType) {
54 this.subType = subType;
58 public byte toByte() {
59 return (byte) subType;
63 public enum HumidityStatus implements ByteEnumWrapper {
69 private final int humidityStatus;
71 HumidityStatus(int humidityStatus) {
72 this.humidityStatus = humidityStatus;
76 public byte toByte() {
77 return (byte) humidityStatus;
81 public SubType subType;
83 public double temperature;
85 public HumidityStatus humidityStatus;
87 public RFXComTemperatureHumidityMessage() {
88 super(PacketType.TEMPERATURE_HUMIDITY);
91 public RFXComTemperatureHumidityMessage(byte[] data) throws RFXComException {
96 public String toString() {
99 str += super.toString();
100 str += ", Sub type = " + subType;
101 str += ", Device Id = " + getDeviceId();
102 str += ", Temperature = " + temperature;
103 str += ", Humidity = " + humidity;
104 str += ", Humidity status = " + humidityStatus;
105 str += ", Signal level = " + signalLevel;
106 str += ", Battery level = " + batteryLevel;
112 public void encodeMessage(byte[] data) throws RFXComException {
113 super.encodeMessage(data);
115 subType = fromByte(SubType.class, super.subType);
116 sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
118 temperature = (short) ((data[6] & 0x7F) << 8 | (data[7] & 0xFF)) * 0.1;
119 if ((data[6] & 0x80) != 0) {
120 temperature = -temperature;
124 humidityStatus = fromByte(HumidityStatus.class, data[9]);
126 signalLevel = (byte) ((data[10] & 0xF0) >> 4);
127 batteryLevel = (byte) (data[10] & 0x0F);
131 public byte[] decodeMessage() {
132 byte[] data = new byte[11];
135 data[1] = RFXComBaseMessage.PacketType.TEMPERATURE_HUMIDITY.toByte();
136 data[2] = subType.toByte();
138 data[4] = (byte) ((sensorId & 0xFF00) >> 8);
139 data[5] = (byte) (sensorId & 0x00FF);
141 short temp = (short) Math.abs(temperature * 10);
142 data[6] = (byte) ((temp >> 8) & 0xFF);
143 data[7] = (byte) (temp & 0xFF);
144 if (temperature < 0) {
149 data[9] = humidityStatus.toByte();
150 data[10] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
156 public String getDeviceId() {
157 return String.valueOf(sensorId);
161 public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
163 case CHANNEL_TEMPERATURE:
164 return new DecimalType(temperature);
166 case CHANNEL_HUMIDITY:
167 return new DecimalType(humidity);
169 case CHANNEL_HUMIDITY_STATUS:
170 return new StringType(humidityStatus.toString());
173 return super.convertToState(channelId, deviceState);
178 public void setSubType(SubType subType) {
179 throw new UnsupportedOperationException();
183 public void setDeviceId(String deviceId) {
184 throw new UnsupportedOperationException();
188 public void convertFromState(String channelId, Type type) {
189 throw new UnsupportedOperationException();
193 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
194 return ByteEnumUtil.convertSubType(SubType.class, subType);