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.CHANNEL_TEMPERATURE;
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.types.State;
24 import org.openhab.core.types.Type;
27 * RFXCOM data class for temperature and humidity message.
29 * @author Pauli Anttila - Initial contribution
31 public class RFXComTemperatureMessage extends RFXComBatteryDeviceMessage<RFXComTemperatureMessage.SubType> {
33 public enum SubType implements ByteEnumWrapper {
46 private final int subType;
48 SubType(int subType) {
49 this.subType = subType;
53 public byte toByte() {
54 return (byte) subType;
58 public SubType subType;
60 public double temperature;
62 public RFXComTemperatureMessage() {
63 super(PacketType.TEMPERATURE);
66 public RFXComTemperatureMessage(byte[] data) throws RFXComException {
71 public String toString() {
74 str += super.toString();
75 str += ", Sub type = " + subType;
76 str += ", Device Id = " + getDeviceId();
77 str += ", Temperature = " + temperature;
78 str += ", Signal level = " + signalLevel;
79 str += ", Battery level = " + batteryLevel;
85 public void encodeMessage(byte[] data) throws RFXComException {
86 super.encodeMessage(data);
88 subType = fromByte(SubType.class, super.subType);
89 sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
91 temperature = (short) ((data[6] & 0x7F) << 8 | (data[7] & 0xFF)) * 0.1;
92 if ((data[6] & 0x80) != 0) {
93 temperature = -temperature;
96 signalLevel = (byte) ((data[8] & 0xF0) >> 4);
97 batteryLevel = (byte) (data[8] & 0x0F);
101 public byte[] decodeMessage() {
102 byte[] data = new byte[9];
105 data[1] = RFXComBaseMessage.PacketType.TEMPERATURE.toByte();
106 data[2] = subType.toByte();
108 data[4] = (byte) ((sensorId & 0xFF00) >> 8);
109 data[5] = (byte) (sensorId & 0x00FF);
111 short temp = (short) Math.abs(temperature * 10);
112 data[6] = (byte) ((temp >> 8) & 0xFF);
113 data[7] = (byte) (temp & 0xFF);
114 if (temperature < 0) {
118 data[8] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
124 public String getDeviceId() {
125 return String.valueOf(sensorId);
129 public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
131 case CHANNEL_TEMPERATURE:
132 return new DecimalType(temperature);
135 return super.convertToState(channelId, deviceState);
140 public void setSubType(SubType subType) {
141 throw new UnsupportedOperationException();
145 public void setDeviceId(String deviceId) {
146 throw new UnsupportedOperationException();
150 public void convertFromState(String channelId, Type type) {
151 throw new UnsupportedOperationException();
155 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
156 return ByteEnumUtil.convertSubType(SubType.class, subType);