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.library.types.StringType;
24 import org.openhab.core.types.State;
25 import org.openhab.core.types.Type;
28 * RFXCOM data class for temperature, humidity and barometric message.
30 * @author Damien Servant - Initial contribution
31 * @author Martin van Wingerden - ported to openHAB 2.0
33 public class RFXComTemperatureHumidityBarometricMessage
34 extends RFXComBatteryDeviceMessage<RFXComTemperatureHumidityBarometricMessage.SubType> {
36 public enum SubType implements ByteEnumWrapper {
37 THB1(1), // BTHR918, BTHGN129
38 THB2(2); // BTHR918N, BTHR968
40 private final int subType;
42 SubType(int subType) {
43 this.subType = subType;
47 public byte toByte() {
48 return (byte) subType;
52 public enum HumidityStatus implements ByteEnumWrapper {
58 private final int humidityStatus;
60 HumidityStatus(int humidityStatus) {
61 this.humidityStatus = humidityStatus;
65 public byte toByte() {
66 return (byte) humidityStatus;
70 public enum ForecastStatus implements ByteEnumWrapper {
77 private final int forecastStatus;
79 ForecastStatus(int forecastStatus) {
80 this.forecastStatus = forecastStatus;
84 public byte toByte() {
85 return (byte) forecastStatus;
89 public SubType subType;
91 public double temperature;
93 public HumidityStatus humidityStatus;
94 public double pressure;
95 public ForecastStatus forecastStatus;
97 public RFXComTemperatureHumidityBarometricMessage() {
98 super(PacketType.TEMPERATURE_HUMIDITY_BAROMETRIC);
101 public RFXComTemperatureHumidityBarometricMessage(byte[] data) throws RFXComException {
106 public String toString() {
107 String str = super.toString();
108 str += ", Sub type = " + subType;
109 str += ", Device Id = " + sensorId;
110 str += ", Temperature = " + temperature;
111 str += ", Humidity = " + humidity;
112 str += ", Humidity status = " + humidityStatus;
113 str += ", Pressure = " + pressure;
114 str += ", Forecast = " + forecastStatus;
115 str += ", Signal level = " + signalLevel;
116 str += ", Battery level = " + batteryLevel;
122 public void encodeMessage(byte[] data) throws RFXComException {
123 super.encodeMessage(data);
125 subType = fromByte(SubType.class, super.subType);
126 sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
128 temperature = (short) ((data[6] & 0x7F) << 8 | (data[7] & 0xFF)) * 0.1;
129 if ((data[6] & 0x80) != 0) {
130 temperature = -temperature;
134 humidityStatus = fromByte(HumidityStatus.class, data[9]);
136 pressure = (data[10] & 0xFF) << 8 | (data[11] & 0xFF);
137 forecastStatus = fromByte(ForecastStatus.class, data[12]);
139 signalLevel = (byte) ((data[13] & 0xF0) >> 4);
140 batteryLevel = (byte) (data[13] & 0x0F);
144 public byte[] decodeMessage() {
145 byte[] data = new byte[14];
148 data[1] = RFXComBaseMessage.PacketType.TEMPERATURE_HUMIDITY_BAROMETRIC.toByte();
149 data[2] = subType.toByte();
151 data[4] = (byte) ((sensorId & 0xFF00) >> 8);
152 data[5] = (byte) (sensorId & 0x00FF);
154 short temp = (short) Math.abs(temperature * 10);
155 data[6] = (byte) ((temp >> 8) & 0xFF);
156 data[7] = (byte) (temp & 0xFF);
157 if (temperature < 0) {
162 data[9] = humidityStatus.toByte();
164 temp = (short) (pressure);
165 data[10] = (byte) ((temp >> 8) & 0xFF);
166 data[11] = (byte) (temp & 0xFF);
168 data[12] = forecastStatus.toByte();
170 data[13] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
176 public String getDeviceId() {
177 return String.valueOf(sensorId);
181 public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
183 case CHANNEL_TEMPERATURE:
184 return new DecimalType(temperature);
186 case CHANNEL_HUMIDITY:
187 return new DecimalType(humidity);
189 case CHANNEL_PRESSURE:
190 return new DecimalType(pressure);
192 case CHANNEL_HUMIDITY_STATUS:
193 return new StringType(humidityStatus.toString());
195 case CHANNEL_FORECAST:
196 return new StringType(forecastStatus.toString());
199 return super.convertToState(channelId, deviceState);
204 public void convertFromState(String channelId, Type type) {
205 throw new UnsupportedOperationException();
209 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
210 return ByteEnumUtil.convertSubType(SubType.class, subType);
214 public void setSubType(SubType subType) {
215 throw new UnsupportedOperationException();
219 public void setDeviceId(String deviceId) {
220 throw new UnsupportedOperationException();