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.CHANNEL_TEMPERATURE;
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.types.State;
26 import org.openhab.core.types.Type;
29 * RFXCOM data class for temperature and humidity message.
31 * @author Pauli Anttila - Initial contribution
33 public class RFXComTemperatureMessage extends RFXComBatteryDeviceMessage<RFXComTemperatureMessage.SubType> {
35 public enum SubType implements ByteEnumWrapper {
48 private final int subType;
50 SubType(int subType) {
51 this.subType = subType;
55 public byte toByte() {
56 return (byte) subType;
60 public SubType subType;
62 public double temperature;
64 public RFXComTemperatureMessage() {
65 super(PacketType.TEMPERATURE);
68 public RFXComTemperatureMessage(byte[] data) throws RFXComException {
73 public String toString() {
76 str += super.toString();
77 str += ", Sub type = " + subType;
78 str += ", Device Id = " + getDeviceId();
79 str += ", Temperature = " + temperature;
80 str += ", Signal level = " + signalLevel;
81 str += ", Battery level = " + batteryLevel;
87 public void encodeMessage(byte[] data) throws RFXComException {
88 super.encodeMessage(data);
90 subType = fromByte(SubType.class, super.subType);
91 sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
93 temperature = (short) ((data[6] & 0x7F) << 8 | (data[7] & 0xFF)) * 0.1;
94 if ((data[6] & 0x80) != 0) {
95 temperature = -temperature;
98 signalLevel = (byte) ((data[8] & 0xF0) >> 4);
99 batteryLevel = (byte) (data[8] & 0x0F);
103 public byte[] decodeMessage() {
104 byte[] data = new byte[9];
107 data[1] = RFXComBaseMessage.PacketType.TEMPERATURE.toByte();
108 data[2] = subType.toByte();
110 data[4] = (byte) ((sensorId & 0xFF00) >> 8);
111 data[5] = (byte) (sensorId & 0x00FF);
113 short temp = (short) Math.abs(temperature * 10);
114 data[6] = (byte) ((temp >> 8) & 0xFF);
115 data[7] = (byte) (temp & 0xFF);
116 if (temperature < 0) {
120 data[8] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
126 public String getDeviceId() {
127 return String.valueOf(sensorId);
131 public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
132 throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
134 case CHANNEL_TEMPERATURE:
135 return new DecimalType(temperature);
138 return super.convertToState(channelId, config, deviceState);
143 public void setSubType(SubType subType) {
144 throw new UnsupportedOperationException();
148 public void setDeviceId(String deviceId) {
149 throw new UnsupportedOperationException();
153 public void convertFromState(String channelId, Type type) {
154 throw new UnsupportedOperationException();
158 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
159 return ByteEnumUtil.convertSubType(SubType.class, subType);