2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.rfxcom.internal.messages;
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.openhab.binding.rfxcom.internal.RFXComTestHelper.commandChannelUID;
18 import java.nio.ByteBuffer;
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;
31 * Test for RFXCom-binding
33 * @author James Hewitt-Thomas - New addition to the PRO RFXCom firmware
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");
51 public void testSomeRxMessages() throws RFXComException {
52 testMessageRx("087F0027051356ECC0", RFXComRawMessage.SubType.RAW_PACKET1, 0x27, 5, "1356ECC0");
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();
61 assertEquals(hexMsg, HexUtils.bytesToHex(decoded), "Transmitted message");
65 public void testTxBasicPulses() throws RFXComException {
66 RFXComRawDeviceConfiguration config = new RFXComRawDeviceConfiguration();
67 config.deviceId = "RAW";
68 config.subType = "RAW_PACKET1";
70 config.onPulsesArray = new short[] { 0x10, 0x20, 0x30, 0x40 };
72 testMessageTx(config, OnOffType.ON, "0C7F0000050010002000300040");
76 public void testTxMissingPulses() throws RFXComException {
77 RFXComRawDeviceConfiguration config = new RFXComRawDeviceConfiguration();
78 config.deviceId = "RAW";
79 config.subType = "RAW_PACKET1";
81 config.onPulsesArray = new short[] { 0x10, 0x20, 0x30, 0x40 };
83 assertThrows(RFXComInvalidStateException.class,
84 () -> testMessageTx(config, OnOffType.OFF, "0C7F0000050010002000300040"));