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.types.State;
24 import org.openhab.core.types.Type;
27 * RFXCOM data class for BBQ temperature message.
29 * @author Mike Jagdis - Initial contribution
31 public class RFXComBBQTemperatureMessage extends RFXComBatteryDeviceMessage<RFXComBBQTemperatureMessage.SubType> {
33 public enum SubType implements ByteEnumWrapper {
34 BBQ1(1); // Maverick ET-732
36 private final int subType;
38 SubType(int subType) {
39 this.subType = subType;
43 public byte toByte() {
44 return (byte) subType;
48 public SubType subType;
50 public double foodTemperature;
51 public double bbqTemperature;
52 public byte signalLevel;
53 public byte batteryLevel;
55 public RFXComBBQTemperatureMessage() {
56 super(PacketType.BBQ);
59 public RFXComBBQTemperatureMessage(byte[] data) throws RFXComException {
64 public String toString() {
65 return super.toString() + ", Sub type = " + subType + ", Device Id = " + getDeviceId() + ", Food temperature = "
66 + foodTemperature + ", BBQ temperature = " + bbqTemperature + ", Signal level = " + signalLevel
67 + ", Battery level = " + batteryLevel;
71 public void encodeMessage(byte[] data) throws RFXComException {
72 super.encodeMessage(data);
74 subType = fromByte(SubType.class, super.subType);
75 sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
77 foodTemperature = (data[6] & 0x7F) << 8 | (data[7] & 0xFF);
78 if ((data[6] & 0x80) != 0) {
79 foodTemperature = -foodTemperature;
82 bbqTemperature = (data[8] & 0x7F) << 8 | (data[9] & 0xFF);
83 if ((data[8] & 0x80) != 0) {
84 bbqTemperature = -bbqTemperature;
87 signalLevel = (byte) ((data[10] & 0xF0) >> 4);
88 batteryLevel = (byte) (data[10] & 0x0F);
92 public byte[] decodeMessage() {
93 byte[] data = new byte[11];
96 data[1] = RFXComBaseMessage.PacketType.BBQ.toByte();
97 data[2] = subType.toByte();
99 data[4] = (byte) ((sensorId & 0xFF00) >> 8);
100 data[5] = (byte) (sensorId & 0x00FF);
102 short temp = (short) Math.abs(foodTemperature);
103 data[6] = (byte) ((temp >> 8) & 0x7F);
104 data[7] = (byte) (temp & 0xFF);
105 if (foodTemperature < 0) {
109 temp = (short) Math.abs(bbqTemperature);
110 data[8] = (byte) ((temp >> 8) & 0x7F);
111 data[9] = (byte) (temp & 0xFF);
112 if (bbqTemperature < 0) {
116 data[10] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
122 public String getDeviceId() {
123 return String.valueOf(sensorId);
127 public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
128 if (CHANNEL_FOOD_TEMPERATURE.equals(channelId)) {
129 return new DecimalType(foodTemperature);
130 } else if (CHANNEL_BBQ_TEMPERATURE.equals(channelId)) {
131 return new DecimalType(bbqTemperature);
133 return super.convertToState(channelId, deviceState);
138 public void setSubType(SubType subType) {
139 throw new UnsupportedOperationException("Not supported");
143 public void setDeviceId(String deviceId) {
144 throw new UnsupportedOperationException("Not supported");
148 public void convertFromState(String channelId, Type type) {
149 throw new UnsupportedOperationException("Not supported");
153 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
154 return ByteEnumUtil.convertSubType(SubType.class, subType);