]> git.basschouten.com Git - openhab-addons.git/blob
f422c1273358dac007a5ab1ec5662160faa53818
[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.junit.jupiter.api.Assertions.assertEquals;
16 import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
17 import static org.openhab.binding.rfxcom.internal.messages.RFXComRFXSensorMessage.SubType.*;
18
19 import javax.xml.bind.DatatypeConverter;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.junit.jupiter.api.Test;
24 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
25 import org.openhab.binding.rfxcom.internal.handler.DeviceState;
26 import org.openhab.core.library.types.DecimalType;
27 import org.openhab.core.types.State;
28
29 /**
30  * Test for RFXCom-binding
31  *
32  * @author Martin van Wingerden - Initial contribution
33  */
34 @NonNullByDefault
35 public class RFXComRFXSensorMessageTest {
36     private final MockDeviceState mockedDeviceState = new MockDeviceState();
37
38     private void testMessage(String hexMsg, RFXComRFXSensorMessage.SubType subType, int seqNbr, String deviceId,
39             @Nullable Double temperature, @Nullable Double voltage, @Nullable Double referenceVoltage,
40             @Nullable Double expectedPressure, @Nullable Double expectedHumidity, int signalLevel,
41             DeviceState deviceState) throws RFXComException {
42         final RFXComRFXSensorMessage msg = (RFXComRFXSensorMessage) RFXComMessageFactoryImpl.INSTANCE
43                 .createMessage(DatatypeConverter.parseHexBinary(hexMsg));
44         assertEquals(subType, msg.subType, "SubType");
45         assertEquals(seqNbr, (short) (msg.seqNbr & 0xFF), "Seq Number");
46         assertEquals(deviceId, msg.getDeviceId(), "Sensor Id");
47         assertEquals(signalLevel, msg.signalLevel, "Signal Level");
48         assertEquals(temperature, getMessageTemperature(msg, deviceState), "Temperature");
49         assertEquals(voltage, getChannelAsDouble(CHANNEL_VOLTAGE, msg, deviceState), "Voltage");
50         assertEquals(referenceVoltage, getChannelAsDouble(CHANNEL_REFERENCE_VOLTAGE, msg, deviceState),
51                 "Reference Voltage");
52         assertEquals(expectedHumidity, getChannelAsDouble(CHANNEL_HUMIDITY, msg, deviceState), "Humidity");
53         assertEquals(expectedPressure, getChannelAsDouble(CHANNEL_PRESSURE, msg, deviceState), "Pressure");
54
55         byte[] decoded = msg.decodeMessage();
56
57         assertEquals(hexMsg, DatatypeConverter.printHexBinary(decoded), "Message converted back");
58     }
59
60     @Test
61     public void testSomeMessages() throws RFXComException {
62         testMessage("0770000008080270", TEMPERATURE, 0, "8", 20.5d, null, null, null, null, 7, mockedDeviceState);
63         testMessage("0770000208809650", TEMPERATURE, 2, "8", -1.5d, null, null, null, null, 5, mockedDeviceState);
64         testMessage("077002010801F270", VOLTAGE, 1, "8", null, null, 4.98, null, null, 7, mockedDeviceState);
65         testMessage("077001020800F470", A_D, 2, "8", null, 2.44, null, null, null, 7, mockedDeviceState);
66     }
67
68     @Test
69     public void testPressure() throws RFXComException {
70         MockDeviceState deviceState = new MockDeviceState();
71         deviceState.set(CHANNEL_REFERENCE_VOLTAGE, new DecimalType(4.98));
72
73         testMessage("077001020800F470", A_D, 2, "8", null, 2.44, null, 650.0, null, 7, deviceState);
74     }
75
76     @Test
77     public void testHumidity() throws RFXComException {
78         MockDeviceState deviceState = new MockDeviceState();
79         deviceState.set(CHANNEL_TEMPERATURE, new DecimalType(20.5));
80         deviceState.set(CHANNEL_REFERENCE_VOLTAGE, new DecimalType(4.98));
81
82         testMessage("077001020800F470", A_D, 2, "8", null, 2.44, null, 650.0, 52.6821, 7, deviceState);
83     }
84
85     private @Nullable Double getMessageTemperature(RFXComRFXSensorMessage msg, DeviceState deviceState)
86             throws RFXComException {
87         return getChannelAsDouble(CHANNEL_TEMPERATURE, msg, deviceState);
88     }
89
90     private @Nullable Double getChannelAsDouble(String channelId, RFXComRFXSensorMessage msg, DeviceState deviceState)
91             throws RFXComException {
92         return getStateAsDouble(msg.convertToState(channelId, null, deviceState));
93     }
94
95     private @Nullable Double getStateAsDouble(State state) {
96         if (state instanceof DecimalType) {
97             return ((DecimalType) state).doubleValue();
98         } else {
99             return null;
100         }
101     }
102 }