2 * Copyright (c) 2010-2022 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, humidity and barometric message.
32 * @author Damien Servant - Initial contribution
33 * @author Martin van Wingerden - ported to openHAB 2.0
35 public class RFXComTemperatureHumidityBarometricMessage
36 extends RFXComBatteryDeviceMessage<RFXComTemperatureHumidityBarometricMessage.SubType> {
38 public enum SubType implements ByteEnumWrapper {
39 THB1(1), // BTHR918, BTHGN129
40 THB2(2); // BTHR918N, BTHR968
42 private final int subType;
44 SubType(int subType) {
45 this.subType = subType;
49 public byte toByte() {
50 return (byte) subType;
54 public enum HumidityStatus implements ByteEnumWrapper {
60 private final int humidityStatus;
62 HumidityStatus(int humidityStatus) {
63 this.humidityStatus = humidityStatus;
67 public byte toByte() {
68 return (byte) humidityStatus;
72 public enum ForecastStatus implements ByteEnumWrapper {
79 private final int forecastStatus;
81 ForecastStatus(int forecastStatus) {
82 this.forecastStatus = forecastStatus;
86 public byte toByte() {
87 return (byte) forecastStatus;
91 public SubType subType;
93 public double temperature;
95 public HumidityStatus humidityStatus;
96 public double pressure;
97 public ForecastStatus forecastStatus;
99 public RFXComTemperatureHumidityBarometricMessage() {
100 super(PacketType.TEMPERATURE_HUMIDITY_BAROMETRIC);
103 public RFXComTemperatureHumidityBarometricMessage(byte[] data) throws RFXComException {
108 public String toString() {
109 String str = super.toString();
110 str += ", Sub type = " + subType;
111 str += ", Device Id = " + sensorId;
112 str += ", Temperature = " + temperature;
113 str += ", Humidity = " + humidity;
114 str += ", Humidity status = " + humidityStatus;
115 str += ", Pressure = " + pressure;
116 str += ", Forecast = " + forecastStatus;
117 str += ", Signal level = " + signalLevel;
118 str += ", Battery level = " + batteryLevel;
124 public void encodeMessage(byte[] data) throws RFXComException {
125 super.encodeMessage(data);
127 subType = fromByte(SubType.class, super.subType);
128 sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
130 temperature = (short) ((data[6] & 0x7F) << 8 | (data[7] & 0xFF)) * 0.1;
131 if ((data[6] & 0x80) != 0) {
132 temperature = -temperature;
136 humidityStatus = fromByte(HumidityStatus.class, data[9]);
138 pressure = (data[10] & 0xFF) << 8 | (data[11] & 0xFF);
139 forecastStatus = fromByte(ForecastStatus.class, data[12]);
141 signalLevel = (byte) ((data[13] & 0xF0) >> 4);
142 batteryLevel = (byte) (data[13] & 0x0F);
146 public byte[] decodeMessage() {
147 byte[] data = new byte[14];
150 data[1] = RFXComBaseMessage.PacketType.TEMPERATURE_HUMIDITY_BAROMETRIC.toByte();
151 data[2] = subType.toByte();
153 data[4] = (byte) ((sensorId & 0xFF00) >> 8);
154 data[5] = (byte) (sensorId & 0x00FF);
156 short temp = (short) Math.abs(temperature * 10);
157 data[6] = (byte) ((temp >> 8) & 0xFF);
158 data[7] = (byte) (temp & 0xFF);
159 if (temperature < 0) {
164 data[9] = humidityStatus.toByte();
166 temp = (short) (pressure);
167 data[10] = (byte) ((temp >> 8) & 0xFF);
168 data[11] = (byte) (temp & 0xFF);
170 data[12] = forecastStatus.toByte();
172 data[13] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
178 public String getDeviceId() {
179 return String.valueOf(sensorId);
183 public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
184 throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
186 case CHANNEL_TEMPERATURE:
187 return new DecimalType(temperature);
189 case CHANNEL_HUMIDITY:
190 return new DecimalType(humidity);
192 case CHANNEL_PRESSURE:
193 return new DecimalType(pressure);
195 case CHANNEL_HUMIDITY_STATUS:
196 return new StringType(humidityStatus.toString());
198 case CHANNEL_FORECAST:
199 return new StringType(forecastStatus.toString());
202 return super.convertToState(channelId, config, deviceState);
207 public void convertFromState(String channelId, Type type) {
208 throw new UnsupportedOperationException();
212 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
213 return ByteEnumUtil.convertSubType(SubType.class, subType);
217 public void setSubType(SubType subType) {
218 throw new UnsupportedOperationException();
222 public void setDeviceId(String deviceId) {
223 throw new UnsupportedOperationException();