]> git.basschouten.com Git - openhab-addons.git/blob
7b5b6433c2aca3a4a5e11f88f4717d5fcab9fbad
[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.*;
16 import static org.openhab.binding.rfxcom.internal.messages.RFXComSecurity1Message.SubType.*;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.junit.jupiter.api.Test;
21 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
22 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
23 import org.openhab.binding.rfxcom.internal.messages.RFXComSecurity1Message.Contact;
24 import org.openhab.binding.rfxcom.internal.messages.RFXComSecurity1Message.Motion;
25 import org.openhab.binding.rfxcom.internal.messages.RFXComSecurity1Message.Status;
26 import org.openhab.binding.rfxcom.internal.messages.RFXComSecurity1Message.SubType;
27 import org.openhab.core.util.HexUtils;
28
29 /**
30  * Test for RFXCom-binding
31  *
32  * @author Martin van Wingerden - Initial contribution
33  */
34 @NonNullByDefault
35 public class RFXComSecurity1MessageTest {
36     private void testSomeMessages(String hexMessage, @Nullable SubType subType, int sequenceNumber,
37             @Nullable String deviceId, int batteryLevel, @Nullable Contact contact, @Nullable Motion motion,
38             @Nullable Status status, int signalLevel) throws RFXComException {
39         byte[] message = HexUtils.hexToBytes(hexMessage);
40         RFXComSecurity1Message msg = (RFXComSecurity1Message) RFXComMessageFactoryImpl.INSTANCE.createMessage(message);
41         assertEquals(subType, msg.subType, "SubType");
42         assertEquals(sequenceNumber, (short) (msg.seqNbr & 0xFF), "Seq Number");
43         assertEquals(deviceId, msg.getDeviceId(), "Sensor Id");
44         assertEquals(batteryLevel, msg.batteryLevel, "Battery level");
45         assertEquals(contact, msg.contact, "Contact");
46         assertEquals(motion, msg.motion, "Motion");
47         assertEquals(status, msg.status, "Status");
48         assertEquals(signalLevel, msg.signalLevel, "Signal Level");
49
50         byte[] decoded = msg.decodeMessage();
51
52         assertEquals(hexMessage, HexUtils.bytesToHex(decoded), "Message converted back");
53     }
54
55     @Test
56     public void testX10SecurityMessage() throws RFXComException {
57         testSomeMessages("0820004DD3DC540089", X10_SECURITY, 77, "13884500", 8, Contact.NORMAL, Motion.UNKNOWN,
58                 Status.NORMAL, 9);
59     }
60
61     @Test
62     public void testRM174RFSecurityMessage() throws RFXComException {
63         testSomeMessages("08200A0E8000200650", RM174RF, 14, "8388640", 5, Contact.UNKNOWN, Motion.UNKNOWN, Status.PANIC,
64                 0);
65         testSomeMessages("08200A081450450650", RM174RF, 8, "1331269", 5, Contact.UNKNOWN, Motion.UNKNOWN, Status.PANIC,
66                 0);
67     }
68
69     @Test
70     public void testSomeInvalidSecurityMessage() {
71         assertThrows(RFXComUnsupportedValueException.class,
72                 () -> testSomeMessages("08FF0A1F0000000650", null, 0, null, 0, null, null, null, 0));
73     }
74 }