]> git.basschouten.com Git - openhab-addons.git/blob
5b8e1ae5edca28669d54902d071c1dfaeb0abe85
[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.library.types.StringType;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.Type;
28
29 /**
30  * RFXCOM data class for temperature and humidity message.
31  *
32  * @author Pauli Anttila - Initial contribution
33  */
34 public class RFXComTemperatureHumidityMessage
35         extends RFXComBatteryDeviceMessage<RFXComTemperatureHumidityMessage.SubType> {
36
37     public enum SubType implements ByteEnumWrapper {
38         TH1(1),
39         TH2(2),
40         TH3(3),
41         TH4(4),
42         TH5(5),
43         TH6(6),
44         TH7(7),
45         TH8(8),
46         TH9(9),
47         TH10(10),
48         TH11(11),
49         TH12(12),
50         TH13(13),
51         TH14(14);
52
53         private final int subType;
54
55         SubType(int subType) {
56             this.subType = subType;
57         }
58
59         @Override
60         public byte toByte() {
61             return (byte) subType;
62         }
63     }
64
65     public enum HumidityStatus implements ByteEnumWrapper {
66         NORMAL(0),
67         COMFORT(1),
68         DRY(2),
69         WET(3);
70
71         private final int humidityStatus;
72
73         HumidityStatus(int humidityStatus) {
74             this.humidityStatus = humidityStatus;
75         }
76
77         @Override
78         public byte toByte() {
79             return (byte) humidityStatus;
80         }
81     }
82
83     public SubType subType;
84     public int sensorId;
85     public double temperature;
86     public byte humidity;
87     public HumidityStatus humidityStatus;
88
89     public RFXComTemperatureHumidityMessage() {
90         super(PacketType.TEMPERATURE_HUMIDITY);
91     }
92
93     public RFXComTemperatureHumidityMessage(byte[] data) throws RFXComException {
94         encodeMessage(data);
95     }
96
97     @Override
98     public String toString() {
99         String str = "";
100
101         str += super.toString();
102         str += ", Sub type = " + subType;
103         str += ", Device Id = " + getDeviceId();
104         str += ", Temperature = " + temperature;
105         str += ", Humidity = " + humidity;
106         str += ", Humidity status = " + humidityStatus;
107         str += ", Signal level = " + signalLevel;
108         str += ", Battery level = " + batteryLevel;
109
110         return str;
111     }
112
113     @Override
114     public void encodeMessage(byte[] data) throws RFXComException {
115         super.encodeMessage(data);
116
117         subType = fromByte(SubType.class, super.subType);
118         sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
119
120         temperature = (short) ((data[6] & 0x7F) << 8 | (data[7] & 0xFF)) * 0.1;
121         if ((data[6] & 0x80) != 0) {
122             temperature = -temperature;
123         }
124
125         humidity = data[8];
126         humidityStatus = fromByte(HumidityStatus.class, data[9]);
127
128         signalLevel = (byte) ((data[10] & 0xF0) >> 4);
129         batteryLevel = (byte) (data[10] & 0x0F);
130     }
131
132     @Override
133     public byte[] decodeMessage() {
134         byte[] data = new byte[11];
135
136         data[0] = 0x0A;
137         data[1] = RFXComBaseMessage.PacketType.TEMPERATURE_HUMIDITY.toByte();
138         data[2] = subType.toByte();
139         data[3] = seqNbr;
140         data[4] = (byte) ((sensorId & 0xFF00) >> 8);
141         data[5] = (byte) (sensorId & 0x00FF);
142
143         short temp = (short) Math.abs(temperature * 10);
144         data[6] = (byte) ((temp >> 8) & 0xFF);
145         data[7] = (byte) (temp & 0xFF);
146         if (temperature < 0) {
147             data[6] |= 0x80;
148         }
149
150         data[8] = humidity;
151         data[9] = humidityStatus.toByte();
152         data[10] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
153
154         return data;
155     }
156
157     @Override
158     public String getDeviceId() {
159         return String.valueOf(sensorId);
160     }
161
162     @Override
163     public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
164             throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
165         switch (channelId) {
166             case CHANNEL_TEMPERATURE:
167                 return new DecimalType(temperature);
168
169             case CHANNEL_HUMIDITY:
170                 return new DecimalType(humidity);
171
172             case CHANNEL_HUMIDITY_STATUS:
173                 return new StringType(humidityStatus.toString());
174
175             default:
176                 return super.convertToState(channelId, config, deviceState);
177         }
178     }
179
180     @Override
181     public void setSubType(SubType subType) {
182         throw new UnsupportedOperationException();
183     }
184
185     @Override
186     public void setDeviceId(String deviceId) {
187         throw new UnsupportedOperationException();
188     }
189
190     @Override
191     public void convertFromState(String channelId, Type type) {
192         throw new UnsupportedOperationException();
193     }
194
195     @Override
196     public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
197         return ByteEnumUtil.convertSubType(SubType.class, subType);
198     }
199 }