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.types.State;
26 import org.openhab.core.types.Type;
29 * RFXCOM data class for BBQ temperature message.
31 * @author Mike Jagdis - Initial contribution
33 public class RFXComBBQTemperatureMessage extends RFXComBatteryDeviceMessage<RFXComBBQTemperatureMessage.SubType> {
35 public enum SubType implements ByteEnumWrapper {
36 BBQ1(1); // Maverick ET-732
38 private final int subType;
40 SubType(int subType) {
41 this.subType = subType;
45 public byte toByte() {
46 return (byte) subType;
50 public SubType subType;
52 public double foodTemperature;
53 public double bbqTemperature;
54 public byte signalLevel;
55 public byte batteryLevel;
57 public RFXComBBQTemperatureMessage() {
58 super(PacketType.BBQ);
61 public RFXComBBQTemperatureMessage(byte[] data) throws RFXComException {
66 public String toString() {
67 return super.toString() + ", Sub type = " + subType + ", Device Id = " + getDeviceId() + ", Food temperature = "
68 + foodTemperature + ", BBQ temperature = " + bbqTemperature + ", Signal level = " + signalLevel
69 + ", Battery level = " + batteryLevel;
73 public void encodeMessage(byte[] data) throws RFXComException {
74 super.encodeMessage(data);
76 subType = fromByte(SubType.class, super.subType);
77 sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
79 foodTemperature = (data[6] & 0x7F) << 8 | (data[7] & 0xFF);
80 if ((data[6] & 0x80) != 0) {
81 foodTemperature = -foodTemperature;
84 bbqTemperature = (data[8] & 0x7F) << 8 | (data[9] & 0xFF);
85 if ((data[8] & 0x80) != 0) {
86 bbqTemperature = -bbqTemperature;
89 signalLevel = (byte) ((data[10] & 0xF0) >> 4);
90 batteryLevel = (byte) (data[10] & 0x0F);
94 public byte[] decodeMessage() {
95 byte[] data = new byte[11];
98 data[1] = RFXComBaseMessage.PacketType.BBQ.toByte();
99 data[2] = subType.toByte();
101 data[4] = (byte) ((sensorId & 0xFF00) >> 8);
102 data[5] = (byte) (sensorId & 0x00FF);
104 short temp = (short) Math.abs(foodTemperature);
105 data[6] = (byte) ((temp >> 8) & 0x7F);
106 data[7] = (byte) (temp & 0xFF);
107 if (foodTemperature < 0) {
111 temp = (short) Math.abs(bbqTemperature);
112 data[8] = (byte) ((temp >> 8) & 0x7F);
113 data[9] = (byte) (temp & 0xFF);
114 if (bbqTemperature < 0) {
118 data[10] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
124 public String getDeviceId() {
125 return String.valueOf(sensorId);
129 public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
130 throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
131 if (CHANNEL_FOOD_TEMPERATURE.equals(channelId)) {
132 return new DecimalType(foodTemperature);
133 } else if (CHANNEL_BBQ_TEMPERATURE.equals(channelId)) {
134 return new DecimalType(bbqTemperature);
136 return super.convertToState(channelId, config, deviceState);
141 public void setSubType(SubType subType) {
142 throw new UnsupportedOperationException("Not supported");
146 public void setDeviceId(String deviceId) {
147 throw new UnsupportedOperationException("Not supported");
151 public void convertFromState(String channelId, Type type) {
152 throw new UnsupportedOperationException("Not supported");
156 public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
157 return ByteEnumUtil.convertSubType(SubType.class, subType);