]> git.basschouten.com Git - openhab-addons.git/blob
df56e4f9f78d80b36d4cff77f2f4086834101132
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.RFXComTestHelper.commandChannelUID;
17
18 import java.nio.ByteBuffer;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.junit.jupiter.api.Test;
22 import org.openhab.binding.rfxcom.internal.config.RFXComRawDeviceConfiguration;
23 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
24 import org.openhab.binding.rfxcom.internal.exceptions.RFXComInvalidStateException;
25 import org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.PacketType;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.types.Command;
28 import org.openhab.core.util.HexUtils;
29
30 /**
31  * Test for RFXCom-binding
32  *
33  * @author James Hewitt-Thomas - New addition to the PRO RFXCom firmware
34  */
35 @NonNullByDefault
36 public class RFXComRawMessageTest {
37     private void testMessageRx(String hexMsg, RFXComRawMessage.SubType subType, int seqNbr, int repeat, String pulses)
38             throws RFXComException {
39         final RFXComRawMessage msg = (RFXComRawMessage) RFXComMessageFactoryImpl.INSTANCE
40                 .createMessage(HexUtils.hexToBytes(hexMsg));
41         assertEquals(subType, msg.subType, "SubType");
42         assertEquals(seqNbr, (short) (msg.seqNbr & 0xFF), "Seq Number");
43         assertEquals("RAW", msg.getDeviceId(), "Device Id");
44         assertEquals(repeat, msg.repeat, "Repeat");
45         byte[] payload = new byte[msg.pulses.length * 2];
46         ByteBuffer.wrap(payload).asShortBuffer().put(msg.pulses);
47         assertEquals(pulses, HexUtils.bytesToHex(payload), "Pulses");
48     }
49
50     @Test
51     public void testSomeRxMessages() throws RFXComException {
52         testMessageRx("087F0027051356ECC0", RFXComRawMessage.SubType.RAW_PACKET1, 0x27, 5, "1356ECC0");
53     }
54
55     private void testMessageTx(RFXComRawDeviceConfiguration config, Command command, String hexMsg)
56             throws RFXComException {
57         RFXComRawMessage msg = (RFXComRawMessage) RFXComMessageFactoryImpl.INSTANCE.createMessage(PacketType.RAW,
58                 config, commandChannelUID, command);
59         byte[] decoded = msg.decodeMessage();
60
61         assertEquals(hexMsg, HexUtils.bytesToHex(decoded), "Transmitted message");
62     }
63
64     @Test
65     public void testTxBasicPulses() throws RFXComException {
66         RFXComRawDeviceConfiguration config = new RFXComRawDeviceConfiguration();
67         config.deviceId = "RAW";
68         config.subType = "RAW_PACKET1";
69         config.repeat = 5;
70         config.onPulsesArray = new short[] { 0x10, 0x20, 0x30, 0x40 };
71
72         testMessageTx(config, OnOffType.ON, "0C7F0000050010002000300040");
73     }
74
75     @Test
76     public void testTxMissingPulses() throws RFXComException {
77         RFXComRawDeviceConfiguration config = new RFXComRawDeviceConfiguration();
78         config.deviceId = "RAW";
79         config.subType = "RAW_PACKET1";
80         config.repeat = 5;
81         config.onPulsesArray = new short[] { 0x10, 0x20, 0x30, 0x40 };
82
83         assertThrows(RFXComInvalidStateException.class,
84                 () -> testMessageTx(config, OnOffType.OFF, "0C7F0000050010002000300040"));
85     }
86 }