]> git.basschouten.com Git - openhab-addons.git/blob
188c7fc8b6209c990f9e53a0fcc4176241165edd
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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
17 import java.nio.ByteBuffer;
18
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;
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
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");
49     }
50
51     @Test
52     public void testSomeRxMessages() throws RFXComException {
53         testMessageRx("087F0027051356ECC0", RFXComRawMessage.SubType.RAW_PACKET1, 0x27, 5, "1356ECC0");
54     }
55
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();
62
63         assertEquals(hexMsg, HexUtils.bytesToHex(decoded), "Transmitted message");
64     }
65
66     @Test
67     public void testTxBasicPulses() throws RFXComException {
68         RFXComRawDeviceConfiguration config = new RFXComRawDeviceConfiguration();
69         config.deviceId = "RAW";
70         config.subType = "RAW_PACKET1";
71         config.repeat = 5;
72         config.onPulsesArray = new short[] { 0x10, 0x20, 0x30, 0x40 };
73
74         testMessageTx(config, OnOffType.ON, "0C7F0000050010002000300040");
75     }
76
77     @Test
78     public void testTxMissingPulses() throws RFXComException {
79         RFXComRawDeviceConfiguration config = new RFXComRawDeviceConfiguration();
80         config.deviceId = "RAW";
81         config.subType = "RAW_PACKET1";
82         config.repeat = 5;
83         config.onPulsesArray = new short[] { 0x10, 0x20, 0x30, 0x40 };
84
85         assertThrows(RFXComInvalidStateException.class,
86                 () -> testMessageTx(config, OnOffType.OFF, "0C7F0000050010002000300040"));
87     }
88 }