]> git.basschouten.com Git - openhab-addons.git/blob
f57298e3fe11ca45a57909ce3858426acba429d8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.CHANNEL_TEMPERATURE;
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 temperature and humidity message.
28  *
29  * @author Pauli Anttila - Initial contribution
30  */
31 public class RFXComTemperatureMessage extends RFXComBatteryDeviceMessage<RFXComTemperatureMessage.SubType> {
32
33     public enum SubType implements ByteEnumWrapper {
34         TEMP1(1),
35         TEMP2(2),
36         TEMP3(3),
37         TEMP4(4),
38         TEMP5(5),
39         TEMP6(6),
40         TEMP7(7),
41         TEMP8(8),
42         TEMP9(9),
43         TEMP10(10),
44         TEMP11(11);
45
46         private final int subType;
47
48         SubType(int subType) {
49             this.subType = subType;
50         }
51
52         @Override
53         public byte toByte() {
54             return (byte) subType;
55         }
56     }
57
58     public SubType subType;
59     public int sensorId;
60     public double temperature;
61
62     public RFXComTemperatureMessage() {
63         super(PacketType.TEMPERATURE);
64     }
65
66     public RFXComTemperatureMessage(byte[] data) throws RFXComException {
67         encodeMessage(data);
68     }
69
70     @Override
71     public String toString() {
72         String str = "";
73
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;
80
81         return str;
82     }
83
84     @Override
85     public void encodeMessage(byte[] data) throws RFXComException {
86         super.encodeMessage(data);
87
88         subType = fromByte(SubType.class, super.subType);
89         sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
90
91         temperature = (short) ((data[6] & 0x7F) << 8 | (data[7] & 0xFF)) * 0.1;
92         if ((data[6] & 0x80) != 0) {
93             temperature = -temperature;
94         }
95
96         signalLevel = (byte) ((data[8] & 0xF0) >> 4);
97         batteryLevel = (byte) (data[8] & 0x0F);
98     }
99
100     @Override
101     public byte[] decodeMessage() {
102         byte[] data = new byte[9];
103
104         data[0] = 0x08;
105         data[1] = RFXComBaseMessage.PacketType.TEMPERATURE.toByte();
106         data[2] = subType.toByte();
107         data[3] = seqNbr;
108         data[4] = (byte) ((sensorId & 0xFF00) >> 8);
109         data[5] = (byte) (sensorId & 0x00FF);
110
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) {
115             data[6] |= 0x80;
116         }
117
118         data[8] = (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, DeviceState deviceState) throws RFXComUnsupportedChannelException {
130         switch (channelId) {
131             case CHANNEL_TEMPERATURE:
132                 return new DecimalType(temperature);
133
134             default:
135                 return super.convertToState(channelId, deviceState);
136         }
137     }
138
139     @Override
140     public void setSubType(SubType subType) {
141         throw new UnsupportedOperationException();
142     }
143
144     @Override
145     public void setDeviceId(String deviceId) {
146         throw new UnsupportedOperationException();
147     }
148
149     @Override
150     public void convertFromState(String channelId, Type type) {
151         throw new UnsupportedOperationException();
152     }
153
154     @Override
155     public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
156         return ByteEnumUtil.convertSubType(SubType.class, subType);
157     }
158 }