]> git.basschouten.com Git - openhab-addons.git/blob
e5af3962a5eb4041185370fda38b2682083ed310
[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 java.math.RoundingMode.*;
16 import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
17 import static org.openhab.binding.rfxcom.internal.messages.ByteEnumUtil.fromByte;
18
19 import java.math.BigDecimal;
20
21 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
22 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedChannelException;
23 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
24 import org.openhab.binding.rfxcom.internal.handler.DeviceState;
25 import org.openhab.core.library.types.DecimalType;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.Type;
28 import org.openhab.core.types.UnDefType;
29 import org.openhab.core.util.HexUtils;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Add support for the rfx-sensor
35  *
36  * @author Martin van Wingerden - Initial contribution
37  */
38 public class RFXComRFXSensorMessage extends RFXComDeviceMessageImpl<RFXComRFXSensorMessage.SubType> {
39     private final Logger logger = LoggerFactory.getLogger(RFXComRFXSensorMessage.class);
40
41     private static final BigDecimal PRESSURE_ADDITION = new BigDecimal("0.095");
42     private static final BigDecimal PRESSURE_DIVIDER = new BigDecimal("0.0009");
43
44     private static final BigDecimal HUMIDITY_VOLTAGE_SUBTRACTION = new BigDecimal("0.16");
45     private static final BigDecimal HUMIDITY_VOLTAGE_DIVIDER = new BigDecimal("0.0062");
46     private static final BigDecimal HUMIDITY_TEMPERATURE_CORRECTION = new BigDecimal("1.0546");
47     private static final BigDecimal HUMIDITY_TEMPERATURE_MULTIPLIER = new BigDecimal("0.00216");
48
49     private static final BigDecimal ONE_HUNDRED = BigDecimal.valueOf(100);
50
51     public enum SubType implements ByteEnumWrapper {
52         TEMPERATURE(0),
53         A_D(1),
54         VOLTAGE(2),
55         MESSAGE(3);
56
57         private final int subType;
58
59         SubType(int subType) {
60             this.subType = subType;
61         }
62
63         @Override
64         public byte toByte() {
65             return (byte) subType;
66         }
67     }
68
69     public SubType subType;
70
71     public int sensorId;
72     private Double temperature;
73     private BigDecimal miliVoltageTimesTen;
74     public byte signalLevel;
75
76     public RFXComRFXSensorMessage() {
77         super(PacketType.RFXSENSOR);
78     }
79
80     public RFXComRFXSensorMessage(byte[] data) throws RFXComException {
81         encodeMessage(data);
82     }
83
84     @Override
85     public String toString() {
86         String str = super.toString();
87
88         str += ", Sub type = " + subType;
89         str += ", Device Id = " + getDeviceId();
90         str += ", Temperature = " + temperature;
91         str += ", Voltage = " + getVoltage();
92         str += ", Signal level = " + signalLevel;
93
94         return str;
95     }
96
97     @Override
98     public void encodeMessage(byte[] data) throws RFXComException {
99         super.encodeMessage(data);
100
101         subType = fromByte(SubType.class, super.subType);
102
103         sensorId = (data[4] & 0xFF);
104
105         byte msg1 = data[5];
106         byte msg2 = data[6];
107
108         switch (subType) {
109             case TEMPERATURE:
110                 encodeTemperatureMessage(msg1, msg2);
111                 break;
112             case A_D:
113             case VOLTAGE:
114                 encodeVoltageMessage(msg1, msg2);
115                 break;
116             case MESSAGE:
117                 encodeStatusMessage(msg2);
118                 break;
119         }
120
121         signalLevel = (byte) ((data[7] & 0xF0) >> 4);
122     }
123
124     private void encodeTemperatureMessage(byte msg1, byte msg2) {
125         temperature = (short) ((msg1 & 0x7F) << 8 | (msg2 & 0xFF)) * 0.01;
126         if ((msg1 & 0x80) != 0) {
127             temperature = -temperature;
128         }
129     }
130
131     private void encodeVoltageMessage(byte msg1, byte msg2) {
132         miliVoltageTimesTen = BigDecimal.valueOf((short) ((msg1 & 0xFF) << 8 | (msg2 & 0xFF)));
133     }
134
135     private void encodeStatusMessage(byte msg2) {
136         // noop
137     }
138
139     @Override
140     public byte[] decodeMessage() {
141         byte[] data = new byte[8];
142
143         data[0] = 0x07;
144         data[1] = PacketType.RFXSENSOR.toByte();
145         data[2] = subType.toByte();
146         data[3] = seqNbr;
147         data[4] = (byte) (sensorId & 0x00FF);
148
149         if (subType == SubType.TEMPERATURE) {
150             decodeTemperatureMessage(data);
151         } else if (subType == SubType.A_D) {
152             decodeVoltageMessage(data);
153         } else if (subType == SubType.VOLTAGE) {
154             decodeVoltageMessage(data);
155         } else if (subType == SubType.MESSAGE) {
156             decodeStatusMessage(data);
157         }
158
159         data[7] = (byte) ((signalLevel & 0x0F) << 4);
160
161         return data;
162     }
163
164     @Override
165     public void convertFromState(String channelId, Type type) {
166         throw new UnsupportedOperationException();
167     }
168
169     private void decodeTemperatureMessage(byte[] data) {
170         short temp = (short) Math.abs(temperature * 100);
171         data[5] = (byte) ((temp >> 8) & 0xFF);
172         data[6] = (byte) (temp & 0xFF);
173         if (temperature < 0) {
174             data[5] |= 0x80;
175         }
176     }
177
178     private void decodeVoltageMessage(byte[] data) {
179         short miliVoltageTimesTenShort = this.miliVoltageTimesTen.shortValueExact();
180         data[5] = (byte) ((miliVoltageTimesTenShort >> 8) & 0xFF);
181         data[6] = (byte) (miliVoltageTimesTenShort & 0xFF);
182     }
183
184     private void decodeStatusMessage(byte[] data) {
185         logger.info("A status message was received {}", HexUtils.bytesToHex(data));
186     }
187
188     @Override
189     public String getDeviceId() {
190         return String.valueOf(sensorId);
191     }
192
193     @Override
194     public SubType convertSubType(String subType) throws RFXComUnsupportedValueException {
195         return ByteEnumUtil.convertSubType(SubType.class, subType);
196     }
197
198     @Override
199     public void setSubType(SubType subType) {
200         throw new UnsupportedOperationException();
201     }
202
203     @Override
204     public State convertToState(String channelId, DeviceState deviceState) throws RFXComUnsupportedChannelException {
205         switch (channelId) {
206             case CHANNEL_TEMPERATURE:
207                 return subType == SubType.TEMPERATURE ? getTemperature() : null;
208
209             case CHANNEL_VOLTAGE:
210                 return subType == SubType.A_D ? handleVoltage() : null;
211
212             case CHANNEL_REFERENCE_VOLTAGE:
213                 return subType == SubType.VOLTAGE ? handleVoltage() : null;
214
215             case CHANNEL_HUMIDITY:
216                 return subType == SubType.A_D ? handleHumidity(deviceState) : null;
217
218             case CHANNEL_PRESSURE:
219                 return subType == SubType.A_D ? handlePressure(deviceState) : null;
220
221             default:
222                 return super.convertToState(channelId, deviceState);
223         }
224     }
225
226     private State getTemperature() {
227         if (temperature != null) {
228             return new DecimalType(temperature);
229         } else {
230             return UnDefType.UNDEF;
231         }
232     }
233
234     private State handleVoltage() {
235         if (miliVoltageTimesTen != null) {
236             return new DecimalType(getVoltage());
237         } else {
238             return UnDefType.UNDEF;
239         }
240     }
241
242     private State handleHumidity(DeviceState deviceState) {
243         DecimalType temperatureState = (DecimalType) deviceState.getLastState(CHANNEL_TEMPERATURE);
244         Type referenceVoltageState = deviceState.getLastState(CHANNEL_REFERENCE_VOLTAGE);
245         BigDecimal adVoltage = getVoltage();
246
247         if (temperatureState == null || referenceVoltageState == null || adVoltage == null) {
248             return null;
249         }
250
251         if (!(referenceVoltageState instanceof DecimalType)) {
252             return UnDefType.UNDEF;
253         }
254
255         BigDecimal temperature = temperatureState.toBigDecimal();
256         BigDecimal supplyVoltage = ((DecimalType) referenceVoltageState).toBigDecimal();
257
258         // RH = (((A/D voltage / supply voltage) - 0.16) / 0.0062) / (1.0546 - 0.00216 * temperature)
259         BigDecimal belowTheDivider = adVoltage.divide(supplyVoltage, 4, HALF_DOWN)
260                 .subtract(HUMIDITY_VOLTAGE_SUBTRACTION).divide(HUMIDITY_VOLTAGE_DIVIDER, 4, HALF_DOWN);
261         BigDecimal underTheDivider = HUMIDITY_TEMPERATURE_CORRECTION
262                 .subtract(HUMIDITY_TEMPERATURE_MULTIPLIER.multiply(temperature));
263
264         return new DecimalType(belowTheDivider.divide(underTheDivider, 4, HALF_DOWN));
265     }
266
267     private State handlePressure(DeviceState deviceState) {
268         DecimalType referenceVoltageState = (DecimalType) deviceState.getLastState(CHANNEL_REFERENCE_VOLTAGE);
269         BigDecimal adVoltage = getVoltage();
270
271         if (referenceVoltageState == null || adVoltage == null) {
272             return null;
273         }
274
275         BigDecimal supplyVoltage = referenceVoltageState.toBigDecimal();
276
277         // hPa = ((A/D voltage / supply voltage) + 0.095) / 0.0009
278         return new DecimalType((adVoltage.divide(supplyVoltage, 4, HALF_DOWN).add(PRESSURE_ADDITION))
279                 .divide(PRESSURE_DIVIDER, 4, HALF_DOWN));
280     }
281
282     private BigDecimal getVoltage() {
283         if (miliVoltageTimesTen == null) {
284             return null;
285         }
286         return miliVoltageTimesTen.divide(ONE_HUNDRED, 100, CEILING);
287     }
288
289     @Override
290     public void setDeviceId(String deviceId) throws RFXComException {
291         throw new RFXComException("Not supported");
292     }
293 }