]> git.basschouten.com Git - openhab-addons.git/blob
e2c96c9d37b1f99a12eaaf76b1ed20dda36c4eaa
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.Assert.assertEquals;
16 import static org.openhab.binding.rfxcom.internal.RFXComBindingConstants.*;
17 import static org.openhab.binding.rfxcom.internal.messages.RFXComBaseMessage.PacketType.LIGHTING4;
18 import static org.openhab.binding.rfxcom.internal.messages.RFXComLighting4Message.Commands.*;
19 import static org.openhab.binding.rfxcom.internal.messages.RFXComLighting4Message.SubType.PT2262;
20
21 import java.util.Arrays;
22 import java.util.List;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.junit.Test;
27 import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfiguration;
28 import org.openhab.binding.rfxcom.internal.config.RFXComDeviceConfigurationBuilder;
29 import org.openhab.binding.rfxcom.internal.exceptions.RFXComException;
30 import org.openhab.core.library.types.OnOffType;
31 import org.openhab.core.util.HexUtils;
32
33 /**
34  * Test for RFXCom-binding
35  *
36  * @author Martin van Wingerden - Initial contribution
37  */
38 @NonNullByDefault
39 public class RFXComLighting4MessageTest {
40     @Test
41     public void basicBoundaryCheck() throws RFXComException {
42         RFXComLighting4Message message = (RFXComLighting4Message) RFXComMessageFactory.createMessage(LIGHTING4);
43
44         RFXComDeviceConfiguration build = new RFXComDeviceConfigurationBuilder().withDeviceId("90000").withPulse(300)
45                 .withSubType("PT2262").build();
46         message.setConfig(build);
47         message.convertFromState(CHANNEL_COMMAND, OnOffType.ON);
48
49         byte[] binaryMessage = message.decodeMessage();
50         RFXComLighting4Message msg = (RFXComLighting4Message) RFXComMessageFactory.createMessage(binaryMessage);
51
52         assertEquals("Sensor Id", "90000", msg.getDeviceId());
53     }
54
55     private void testMessage(String hexMsg, RFXComLighting4Message.SubType subType, String deviceId,
56             @Nullable Integer pulse, RFXComLighting4Message.Commands command, @Nullable Integer seqNbr, int signalLevel,
57             int offCommand, int onCommand) throws RFXComException {
58         testMessage(hexMsg, subType, deviceId, pulse, command.toByte(), seqNbr, signalLevel, offCommand, onCommand);
59     }
60
61     private void testMessage(String hexMsg, RFXComLighting4Message.SubType subType, String deviceId,
62             @Nullable Integer pulse, byte commandByte, @Nullable Integer seqNbr, int signalLevel, int offCommand,
63             int onCommand) throws RFXComException {
64         RFXComLighting4Message msg = (RFXComLighting4Message) RFXComMessageFactory
65                 .createMessage(HexUtils.hexToBytes(hexMsg));
66         assertEquals("Sensor Id", deviceId, msg.getDeviceId());
67         assertEquals("Command", commandByte, RFXComTestHelper.getActualIntValue(msg, CHANNEL_COMMAND_ID));
68         if (seqNbr != null) {
69             assertEquals("Seq Number", seqNbr.shortValue(), (short) (msg.seqNbr & 0xFF));
70         }
71         assertEquals("Signal Level", signalLevel, RFXComTestHelper.getActualIntValue(msg, CHANNEL_SIGNAL_LEVEL));
72
73         byte[] decoded = msg.decodeMessage();
74
75         assertEquals("Message converted back", hexMsg, HexUtils.bytesToHex(decoded));
76
77         RFXComTestHelper.checkDiscoveryResult(msg, deviceId, pulse, subType.toString(), offCommand, onCommand);
78     }
79
80     @Test
81     public void testSomeMessages() throws RFXComException {
82         testMessage("091300E1D8AD59018F70", PT2262, "887509", 399, ON_9, 225, 2, 4, 9);
83         testMessage("0913005FA9A9C901A170", PT2262, "694940", 417, ON_9, 95, 2, 4, 9);
84         testMessage("091300021D155C01E960", PT2262, "119125", 489, ON_12, 2, 2, 4, 12);
85         testMessage("091300D345DD99018C50", PT2262, "286169", 396, ON_9, 211, 2, 4, 9);
86         testMessage("09130035D149A2017750", PT2262, "857242", 375, OFF_2, 53, 2, 2, 1);
87         testMessage("0913000B4E462A012280", PT2262, "320610", 290, ON_10, 11, 3, 4, 10);
88         testMessage("09130009232D2E013970", PT2262, "144082", 313, OFF_14, 9, 2, 14, 1);
89         testMessage("091300CA0F8D2801AA70", PT2262, "63698", 426, ON_8, 202, 2, 4, 8);
90     }
91
92     @Test
93     public void testSomeAlarmRemote() throws RFXComException {
94         testMessage("0913004A0D8998016E60", PT2262, "55449", 366, ON_8, 74, 2, 4, 8);
95     }
96
97     @Test
98     public void testCheapPirSensor() throws RFXComException {
99         testMessage("091300EF505FC6019670", PT2262, "329212", 406, ON_6, 239, 2, 4, 6);
100     }
101
102     @Test
103     public void testSomeConradMessages() throws RFXComException {
104         testMessage("0913003554545401A150", PT2262, "345413", 417, OFF_4, 53, 2, 4, 1);
105     }
106
107     @Test
108     public void testPhenixMessages() throws RFXComException {
109         List<String> onMessages = Arrays.asList("09130046044551013780", "09130048044551013780", "0913004A044551013980",
110                 "0913004C044551013780", "0913004E044551013780");
111
112         for (String message : onMessages) {
113             testMessage(message, PT2262, "17493", null, ON_1, null, 3, 4, 1);
114         }
115
116         List<String> offMessages = Arrays.asList("09130051044554013980", "09130053044554013680", "09130055044554013680",
117                 "09130057044554013680", "09130059044554013680", "0913005B044554013680", "0913005D044554013480",
118                 "09130060044554013980", "09130062044554013680", "09130064044554013280");
119
120         for (String message : offMessages) {
121             testMessage(message, PT2262, "17493", null, OFF_4, null, 3, 4, 1);
122         }
123     }
124 }