]> git.basschouten.com Git - openhab-addons.git/blob
7c141ac864e5c8aede21606260819acfe26110dc
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.openwebnet.internal.handler;
14
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.mockito.Mockito.mock;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.junit.jupiter.api.Test;
21 import org.openhab.core.thing.Bridge;
22 import org.openwebnet4j.message.BaseOpenMessage;
23 import org.openwebnet4j.message.FrameException;
24 import org.openwebnet4j.message.Where;
25 import org.openwebnet4j.message.WhereAlarm;
26 import org.openwebnet4j.message.WhereAuxiliary;
27 import org.openwebnet4j.message.WhereCEN;
28 import org.openwebnet4j.message.WhereEnergyManagement;
29 import org.openwebnet4j.message.WhereLightAutom;
30 import org.openwebnet4j.message.WhereThermo;
31 import org.openwebnet4j.message.WhereZigBee;
32 import org.openwebnet4j.message.Who;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Test class for {@link OpenWebNetBridgeHandler#ownID} and ThingID calculation
38  * using {@link OpenWebNetBridgeHandler} methods: normalizeWhere(),
39  * ownIdFromWhoWhere(), ownIdFromMessage(), thingIdFromWhere()
40  *
41  * @author Massimo Valla - Initial contribution, various updates
42  * @author Andrea Conte - Energy management
43  * @author Giovanni Fabiani - Auxiliary message support
44  */
45 @NonNullByDefault
46 public class OwnIdTest {
47
48     private final Logger logger = LoggerFactory.getLogger(OwnIdTest.class);
49
50  // @formatter:off
51     /**
52      *                                      deviceWhere
53      *                                      (DevAddrParam)
54      * TYPE                 WHERE           = normalizeWhere()  ownId           ThingID
55      * ---------------------------------------------------------------------------------
56      * Zigbee Switch        789309801#9     789309800h9         1.789309800h9   789309800h9
57      * Zigbee Switch_2u u1  789301201#9     789301200h9         1.789309800h9   789309800h9
58      * Zigbee Switch_2u u2  789301202#9     789301200h9         1.789309800h9   789309800h9
59      * BUS Switch           51              51                  1.51            51
60      * BUS Local Bus        25#4#01         25h4h01             1.25h4h01       25h4h01
61      * BUS Autom            93              93                  2.93            93
62      * BUS Thermo zone      1               1                   4.1             1
63      * BUS Thermo zone CU   #1              1                   4.1             1
64      * BUS Thermo CU 99-z   #0              0                   4.0             0
65      * BUS Thermo CU 4-z    #0#1            0h1                 4.0h1           0h1
66      * BUS Thermo actuator  1#2             1                   4.1             1
67      * BUS TempSensor       500             500                 4.500           500
68      * BUS Energy           51              51                  18.51           51
69      * BUS CEN              51              51                  15.51           51
70      * BUS CEN+             212             212                 25.212          212
71      * BUS DryContact       399             399                 25.399          399
72      * BUS AUX              4               4                   9.4             4
73      * BUS Scenario         05              05                  0.05            05
74      * BUS Alarm Zone       #2 or 2         2                   5.2             2
75      * BUS Alarm silent     0               0                   5.0             0
76      * BUS Alarm system     null            0                   5.0             0
77      */
78 // @formatter:on
79
80     public enum TEST {
81         // @formatter:off
82         // msg, who, where, normalizeWhere, ownId, thingId
83         zb_switch("*1*1*789309801#9##", Who.fromValue(1), new WhereZigBee("789309801#9"), "789309800h9", "1.789309800h9", "789309800h9"),
84         zb_switch_2u_1("*1*1*789301201#9##", Who.fromValue(1), new WhereZigBee("789301201#9"), "789301200h9", "1.789301200h9", "789301200h9"),
85         zb_switch_2u_2("*1*1*789301202#9##", Who.fromValue(1),    new WhereZigBee("789301202#9"), "789301200h9", "1.789301200h9", "789301200h9"),
86         bus_switch("*1*1*51##", Who.fromValue(1), new WhereLightAutom("51"),"51", "1.51", "51"),
87         bus_localbus("*1*1*25#4#01##",  Who.fromValue(1), new WhereLightAutom("25#4#01"), "25h4h01", "1.25h4h01", "25h4h01"),
88         bus_autom("*2*0*93##",Who.fromValue(2),  new WhereLightAutom("93"), "93", "2.93", "93"),
89         bus_thermo_zone("*#4*1*0*0020##", Who.fromValue(4),  new WhereThermo("1"),  "1", "4.1", "1"),
90         bus_thermo_zone_via_cu("*#4*#1*0*0020##",  Who.fromValue(4), new WhereThermo("#1"), "1", "4.1", "1"),
91         bus_thermo_cu_99("*#4*#0##",   Who.fromValue(4), new WhereThermo("#0") ,"0", "4.0", "0"),
92         bus_thermo_cu_4("*#4*#0#1##",   Who.fromValue(4), new WhereThermo("#0#1") ,"0h1", "4.0h1", "0h1"),
93         bus_thermo_act("*#4*1#2*20*0##",   Who.fromValue(4), new WhereThermo("1#2") ,"1", "4.1", "1"),
94         bus_tempSensor("*#4*500*15*1*0020*0001##",Who.fromValue(4),  new WhereThermo("500"), "500", "4.500", "500"),
95         bus_energy("*#18*51*113##",   Who.fromValue(18), new WhereEnergyManagement("51"),  "51", "18.51", "51"),
96         bus_cen("*15*31*51##", Who.fromValue(15),  new WhereCEN("51"),  "51", "15.51", "51"),
97         bus_cen_plus("*25*21#31*212##", Who.fromValue(25),  new WhereCEN("212"), "212", "25.212", "212"),
98         bus_drycontact("*25*32#1*399##", Who.fromValue(25),   new WhereCEN("399"), "399", "25.399", "399"),
99         bus_aux( "*9*1*4##", Who.fromValue(9),  new WhereAuxiliary("4"),"4","9.4","4"),
100         bus_scenario( "*0*2*05##", Who.fromValue(0), new WhereLightAutom("05"), "05","0.05","05"),
101         bus_alarm_zh("*#5*#2##",  Who.fromValue(5), new WhereAlarm("#2"),  "2", "5.2", "2"),
102         bus_alarm_silent("*5*2*0##",  Who.fromValue(5), new WhereAlarm("0"),  "0", "5.0", "0"),
103         bus_alarm_system( "*5*7*##", Who.fromValue(5),new WhereAlarm("0"), "0", "5.0", "0");
104
105         // @formatter:on
106
107         private final Logger logger = LoggerFactory.getLogger(TEST.class);
108
109         public final Where where;
110         public final Who who;
111         public final @Nullable BaseOpenMessage msg;
112         public final String norm, ownId, thingId;
113
114         private TEST(String msg, Who who, Where where, String norm, String ownId, String thingId) {
115             this.where = where;
116             this.who = who;
117             BaseOpenMessage bmsg = null;
118             try {
119                 bmsg = (BaseOpenMessage) BaseOpenMessage.parse(msg);
120             } catch (FrameException e) {
121                 logger.warn("something is wrong in the test table ({}). ownIdFromMessage test will be skipped",
122                         e.getMessage());
123             }
124             this.msg = bmsg;
125             this.norm = norm;
126             this.ownId = ownId;
127             this.thingId = thingId;
128         }
129     }
130
131     @Test
132     public void testOwnId() {
133         Bridge mockBridge = mock(Bridge.class);
134         OpenWebNetBridgeHandler brH = new OpenWebNetBridgeHandler(mockBridge);
135         BaseOpenMessage bmsg;
136         for (int i = 0; i < TEST.values().length; i++) {
137             TEST test = TEST.values()[i];
138             logger.info("testing {} (who={} where={})", test.msg, test.who, test.where);
139             assertEquals(test.norm, brH.normalizeWhere(test.where));
140             assertEquals(test.ownId, brH.ownIdFromWhoWhere(test.who, test.where));
141             bmsg = test.msg;
142             if (bmsg != null) {
143                 assertEquals(test.ownId, brH.ownIdFromMessage(bmsg));
144             }
145             assertEquals(test.thingId, brH.thingIdFromWhere(test.where));
146         }
147     }
148 }