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.library.types.StringType;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.Type;
30 * RFXCOM data class for temperature and humidity message.
32 * @author Pauli Anttila - Initial contribution
34 public class RFXComTemperatureHumidityMessage
35 extends RFXComBatteryDeviceMessage<RFXComTemperatureHumidityMessage.SubType> {
37 public enum SubType implements ByteEnumWrapper {
53 private final int subType;
55 SubType(int subType) {
56 this.subType = subType;
60 public byte toByte() {
61 return (byte) subType;
65 public enum HumidityStatus implements ByteEnumWrapper {
71 private final int humidityStatus;
73 HumidityStatus(int humidityStatus) {
74 this.humidityStatus = humidityStatus;
78 public byte toByte() {
79 return (byte) humidityStatus;
83 public SubType subType;
85 public double temperature;
87 public HumidityStatus humidityStatus;
89 public RFXComTemperatureHumidityMessage() {
90 super(PacketType.TEMPERATURE_HUMIDITY);
93 public RFXComTemperatureHumidityMessage(byte[] data) throws RFXComException {
98 public String toString() {
101 str += super.toString();
102 str += ", Sub type = " + subType;
103 str += ", Device Id = " + getDeviceId();
104 str += ", Temperature = " + temperature;
105 str += ", Humidity = " + humidity;
106 str += ", Humidity status = " + humidityStatus;
107 str += ", Signal level = " + signalLevel;
108 str += ", Battery level = " + batteryLevel;
114 public void encodeMessage(byte[] data) throws RFXComException {
115 super.encodeMessage(data);
117 subType = fromByte(SubType.class, super.subType);
118 sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
120 temperature = (short) ((data[6] & 0x7F) << 8 | (data[7] & 0xFF)) * 0.1;
121 if ((data[6] & 0x80) != 0) {
122 temperature = -temperature;
126 humidityStatus = fromByte(HumidityStatus.class, data[9]);
128 signalLevel = (byte) ((data[10] & 0xF0) >> 4);
129 batteryLevel = (byte) (data[10] & 0x0F);
133 public byte[] decodeMessage() {
134 byte[] data = new byte[11];
137 data[1] = RFXComBaseMessage.PacketType.TEMPERATURE_HUMIDITY.toByte();
138 data[2] = subType.toByte();
140 data[4] = (byte) ((sensorId & 0xFF00) >> 8);
141 data[5] = (byte) (sensorId & 0x00FF);
143 short temp = (short) Math.abs(temperature * 10);
144 data[6] = (byte) ((temp >> 8) & 0xFF);
145 data[7] = (byte) (temp & 0xFF);
146 if (temperature < 0) {
151 data[9] = humidityStatus.toByte();
152 data[10] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
158 public String getDeviceId() {
159 return String.valueOf(sensorId);
163 public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
164 throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
166 case CHANNEL_TEMPERATURE:
167 return new DecimalType(temperature);
169 case CHANNEL_HUMIDITY:
170 return new DecimalType(humidity);
172 case CHANNEL_HUMIDITY_STATUS:
173 return new StringType(humidityStatus.toString());
176 return super.convertToState(channelId, config, deviceState);
181 public void setSubType(SubType subType) {
182 throw new UnsupportedOperationException();
186 public void setDeviceId(String deviceId) {
187 throw new UnsupportedOperationException();
191 public void convertFromState(String channelId, Type type) {
192 throw new UnsupportedOperationException();
196 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
197 return ByteEnumUtil.convertSubType(SubType.class, subType);