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