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