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