]> git.basschouten.com Git - openhab-addons.git/blob
72a6c0ee7af7e5875c6df8cd40bf2c023e182937
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.rfxcom.internal.messages;
14
15 import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
16 import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
17
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;
27
28 /**
29  * RFXCOM data class for BBQ temperature message.
30  *
31  * @author Mike Jagdis - Initial contribution
32  */
33 public class RFXComBBQTemperatureMessage extends RFXComBatteryDeviceMessage<RFXComBBQTemperatureMessage.SubType> {
34
35     public enum SubType implements ByteEnumWrapper {
36         BBQ1(1); // Maverick ET-732
37
38         private final int subType;
39
40         SubType(int subType) {
41             this.subType = subType;
42         }
43
44         @Override
45         public byte toByte() {
46             return (byte) subType;
47         }
48     }
49
50     public SubType subType;
51     public int sensorId;
52     public double foodTemperature;
53     public double bbqTemperature;
54     public byte signalLevel;
55     public byte batteryLevel;
56
57     public RFXComBBQTemperatureMessage() {
58         super(PacketType.BBQ);
59     }
60
61     public RFXComBBQTemperatureMessage(byte[] data) throws RFXComException {
62         encodeMessage(data);
63     }
64
65     @Override
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;
70     }
71
72     @Override
73     public void encodeMessage(byte[] data) throws RFXComException {
74         super.encodeMessage(data);
75
76         subType = fromByte(SubType.class, super.subType);
77         sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
78
79         foodTemperature = (data[6] & 0x7F) << 8 | (data[7] & 0xFF);
80         if ((data[6] & 0x80) != 0) {
81             foodTemperature = -foodTemperature;
82         }
83
84         bbqTemperature = (data[8] & 0x7F) << 8 | (data[9] & 0xFF);
85         if ((data[8] & 0x80) != 0) {
86             bbqTemperature = -bbqTemperature;
87         }
88
89         signalLevel = (byte) ((data[10] & 0xF0) >> 4);
90         batteryLevel = (byte) (data[10] & 0x0F);
91     }
92
93     @Override
94     public byte[] decodeMessage() {
95         byte[] data = new byte[11];
96
97         data[0] = 0x0A;
98         data[1] = RFXComBaseMessage.PacketType.BBQ.toByte();
99         data[2] = subType.toByte();
100         data[3] = seqNbr;
101         data[4] = (byte) ((sensorId & 0xFF00) >> 8);
102         data[5] = (byte) (sensorId & 0x00FF);
103
104         short temp = (short) Math.abs(foodTemperature);
105         data[6] = (byte) ((temp >> 8) & 0x7F);
106         data[7] = (byte) (temp & 0xFF);
107         if (foodTemperature < 0) {
108             data[6] |= 0x80;
109         }
110
111         temp = (short) Math.abs(bbqTemperature);
112         data[8] = (byte) ((temp >> 8) & 0x7F);
113         data[9] = (byte) (temp & 0xFF);
114         if (bbqTemperature < 0) {
115             data[8] |= 0x80;
116         }
117
118         data[10] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
119
120         return data;
121     }
122
123     @Override
124     public String getDeviceId() {
125         return String.valueOf(sensorId);
126     }
127
128     @Override
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);
135         } else {
136             return super.convertToState(channelId, config, deviceState);
137         }
138     }
139
140     @Override
141     public void setSubType(SubType subType) {
142         throw new UnsupportedOperationException("Not supported");
143     }
144
145     @Override
146     public void setDeviceId(String deviceId) {
147         throw new UnsupportedOperationException("Not supported");
148     }
149
150     @Override
151     public void convertFromState(String channelId, Type type) {
152         throw new UnsupportedOperationException("Not supported");
153     }
154
155     @Override
156     public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
157         return ByteEnumUtil.convertSubType(SubType.class, subType);
158     }
159 }