]> git.basschouten.com Git - openhab-addons.git/blob
73a454707cce2e99e94ff69b61adc015f326d706
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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, humidity and barometric message.
31  *
32  * @author Damien Servant - Initial contribution
33  * @author Martin van Wingerden - ported to openHAB 2.0
34  */
35 public class RFXComTemperatureHumidityBarometricMessage
36         extends RFXComBatteryDeviceMessage<RFXComTemperatureHumidityBarometricMessage.SubType> {
37
38     public enum SubType implements ByteEnumWrapper {
39         THB1(1), // BTHR918, BTHGN129
40         THB2(2); // BTHR918N, BTHR968
41
42         private final int subType;
43
44         SubType(int subType) {
45             this.subType = subType;
46         }
47
48         @Override
49         public byte toByte() {
50             return (byte) subType;
51         }
52     }
53
54     public enum HumidityStatus implements ByteEnumWrapper {
55         NORMAL(0),
56         COMFORT(1),
57         DRY(2),
58         WET(3);
59
60         private final int humidityStatus;
61
62         HumidityStatus(int humidityStatus) {
63             this.humidityStatus = humidityStatus;
64         }
65
66         @Override
67         public byte toByte() {
68             return (byte) humidityStatus;
69         }
70     }
71
72     public enum ForecastStatus implements ByteEnumWrapper {
73         NO_INFO_AVAILABLE(0),
74         SUNNY(1),
75         PARTLY_CLOUDY(2),
76         CLOUDY(3),
77         RAIN(4);
78
79         private final int forecastStatus;
80
81         ForecastStatus(int forecastStatus) {
82             this.forecastStatus = forecastStatus;
83         }
84
85         @Override
86         public byte toByte() {
87             return (byte) forecastStatus;
88         }
89     }
90
91     public SubType subType;
92     public int sensorId;
93     public double temperature;
94     public byte humidity;
95     public HumidityStatus humidityStatus;
96     public double pressure;
97     public ForecastStatus forecastStatus;
98
99     public RFXComTemperatureHumidityBarometricMessage() {
100         super(PacketType.TEMPERATURE_HUMIDITY_BAROMETRIC);
101     }
102
103     public RFXComTemperatureHumidityBarometricMessage(byte[] data) throws RFXComException {
104         encodeMessage(data);
105     }
106
107     @Override
108     public String toString() {
109         String str = super.toString();
110         str += ", Sub type = " + subType;
111         str += ", Device Id = " + sensorId;
112         str += ", Temperature = " + temperature;
113         str += ", Humidity = " + humidity;
114         str += ", Humidity status = " + humidityStatus;
115         str += ", Pressure = " + pressure;
116         str += ", Forecast = " + forecastStatus;
117         str += ", Signal level = " + signalLevel;
118         str += ", Battery level = " + batteryLevel;
119
120         return str;
121     }
122
123     @Override
124     public void encodeMessage(byte[] data) throws RFXComException {
125         super.encodeMessage(data);
126
127         subType = fromByte(SubType.class, super.subType);
128         sensorId = (data[4] & 0xFF) << 8 | (data[5] & 0xFF);
129
130         temperature = (short) ((data[6] & 0x7F) << 8 | (data[7] & 0xFF)) * 0.1;
131         if ((data[6] & 0x80) != 0) {
132             temperature = -temperature;
133         }
134
135         humidity = data[8];
136         humidityStatus = fromByte(HumidityStatus.class, data[9]);
137
138         pressure = (data[10] & 0xFF) << 8 | (data[11] & 0xFF);
139         forecastStatus = fromByte(ForecastStatus.class, data[12]);
140
141         signalLevel = (byte) ((data[13] & 0xF0) >> 4);
142         batteryLevel = (byte) (data[13] & 0x0F);
143     }
144
145     @Override
146     public byte[] decodeMessage() {
147         byte[] data = new byte[14];
148
149         data[0] = 0x0D;
150         data[1] = RFXComBaseMessage.PacketType.TEMPERATURE_HUMIDITY_BAROMETRIC.toByte();
151         data[2] = subType.toByte();
152         data[3] = seqNbr;
153         data[4] = (byte) ((sensorId & 0xFF00) >> 8);
154         data[5] = (byte) (sensorId & 0x00FF);
155
156         short temp = (short) Math.abs(temperature * 10);
157         data[6] = (byte) ((temp >> 8) & 0xFF);
158         data[7] = (byte) (temp & 0xFF);
159         if (temperature < 0) {
160             data[6] |= 0x80;
161         }
162
163         data[8] = humidity;
164         data[9] = humidityStatus.toByte();
165
166         temp = (short) (pressure);
167         data[10] = (byte) ((temp >> 8) & 0xFF);
168         data[11] = (byte) (temp & 0xFF);
169
170         data[12] = forecastStatus.toByte();
171
172         data[13] = (byte) (((signalLevel & 0x0F) << 4) | (batteryLevel & 0x0F));
173
174         return data;
175     }
176
177     @Override
178     public String getDeviceId() {
179         return String.valueOf(sensorId);
180     }
181
182     @Override
183     public State convertToState(String channelId, RFXComDeviceConfiguration config, DeviceState deviceState)
184             throws RFXComUnsupportedChannelException, RFXComInvalidStateException {
185         switch (channelId) {
186             case CHANNEL_TEMPERATURE:
187                 return new DecimalType(temperature);
188
189             case CHANNEL_HUMIDITY:
190                 return new DecimalType(humidity);
191
192             case CHANNEL_PRESSURE:
193                 return new DecimalType(pressure);
194
195             case CHANNEL_HUMIDITY_STATUS:
196                 return new StringType(humidityStatus.toString());
197
198             case CHANNEL_FORECAST:
199                 return new StringType(forecastStatus.toString());
200
201             default:
202                 return super.convertToState(channelId, config, deviceState);
203         }
204     }
205
206     @Override
207     public void convertFromState(String channelId, Type type) {
208         throw new UnsupportedOperationException();
209     }
210
211     @Override
212     public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
213         return ByteEnumUtil.convertSubType(SubType.class, subType);
214     }
215
216     @Override
217     public void setSubType(SubType subType) {
218         throw new UnsupportedOperationException();
219     }
220
221     @Override
222     public void setDeviceId(String deviceId) {
223         throw new UnsupportedOperationException();
224     }
225 }