2 * Copyright (c) 2010-2021 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.*;
17 import java.nio.ByteBuffer;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.junit.jupiter.api.Test;
21 import org.openhab.binding.rfxcom.internal.RFXComBindingConstants;
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.Type;
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 {
38 private void testMessageRx(String hexMsg, RFXComRawMessage.SubType subType, int seqNbr, int repeat, String pulses)
39 throws RFXComException {
40 final RFXComRawMessage msg = (RFXComRawMessage) RFXComMessageFactoryImpl.INSTANCE
41 .createMessage(HexUtils.hexToBytes(hexMsg));
42 assertEquals(subType, msg.subType, "SubType");
43 assertEquals(seqNbr, (short) (msg.seqNbr & 0xFF), "Seq Number");
44 assertEquals("RAW", msg.getDeviceId(), "Device Id");
45 assertEquals(repeat, msg.repeat, "Repeat");
46 byte[] payload = new byte[msg.pulses.length * 2];
47 ByteBuffer.wrap(payload).asShortBuffer().put(msg.pulses);
48 assertEquals(pulses, HexUtils.bytesToHex(payload), "Pulses");
52 public void testSomeRxMessages() throws RFXComException {
53 testMessageRx("087F0027051356ECC0", RFXComRawMessage.SubType.RAW_PACKET1, 0x27, 5, "1356ECC0");
56 private void testMessageTx(RFXComRawDeviceConfiguration config, Type command, String hexMsg)
57 throws RFXComException {
58 RFXComRawMessage msg = (RFXComRawMessage) RFXComMessageFactoryImpl.INSTANCE.createMessage(PacketType.RAW);
59 msg.setConfig(config);
60 msg.convertFromState(RFXComBindingConstants.CHANNEL_COMMAND, command);
61 byte[] decoded = msg.decodeMessage();
63 assertEquals(hexMsg, HexUtils.bytesToHex(decoded), "Transmitted message");
67 public void testTxBasicPulses() throws RFXComException {
68 RFXComRawDeviceConfiguration config = new RFXComRawDeviceConfiguration();
69 config.deviceId = "RAW";
70 config.subType = "RAW_PACKET1";
72 config.onPulsesArray = new short[] { 0x10, 0x20, 0x30, 0x40 };
74 testMessageTx(config, OnOffType.ON, "0C7F0000050010002000300040");
78 public void testTxMissingPulses() throws RFXComException {
79 RFXComRawDeviceConfiguration config = new RFXComRawDeviceConfiguration();
80 config.deviceId = "RAW";
81 config.subType = "RAW_PACKET1";
83 config.onPulsesArray = new short[] { 0x10, 0x20, 0x30, 0x40 };
85 assertThrows(RFXComInvalidStateException.class,
86 () -> testMessageTx(config, OnOffType.OFF, "0C7F0000050010002000300040"));