]> git.basschouten.com Git - openhab-addons.git/blob
80499040032e626829eca36438501131d2d72ea9
[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.knx.internal.itests;
14
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.junit.jupiter.api.Assertions.assertNotNull;
17 import static org.junit.jupiter.api.Assertions.assertNull;
18 import static org.junit.jupiter.api.Assertions.assertTrue;
19
20 import java.util.Arrays;
21 import java.util.HashSet;
22 import java.util.Objects;
23 import java.util.Set;
24
25 import javax.measure.quantity.Temperature;
26
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.junit.jupiter.api.AfterAll;
29 import org.junit.jupiter.api.Assertions;
30 import org.junit.jupiter.api.Test;
31 import org.openhab.binding.knx.internal.client.DummyKNXNetworkLink;
32 import org.openhab.binding.knx.internal.client.DummyProcessListener;
33 import org.openhab.binding.knx.internal.dpt.DPTUtil;
34 import org.openhab.binding.knx.internal.dpt.ValueDecoder;
35 import org.openhab.binding.knx.internal.dpt.ValueEncoder;
36 import org.openhab.core.library.types.DateTimeType;
37 import org.openhab.core.library.types.DecimalType;
38 import org.openhab.core.library.types.HSBType;
39 import org.openhab.core.library.types.IncreaseDecreaseType;
40 import org.openhab.core.library.types.OnOffType;
41 import org.openhab.core.library.types.OpenClosedType;
42 import org.openhab.core.library.types.PercentType;
43 import org.openhab.core.library.types.QuantityType;
44 import org.openhab.core.library.types.StopMoveType;
45 import org.openhab.core.library.types.StringType;
46 import org.openhab.core.library.types.UpDownType;
47 import org.openhab.core.types.Type;
48 import org.openhab.core.util.ColorUtil;
49 import org.openhab.core.util.HexUtils;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 import tuwien.auto.calimero.DataUnitBuilder;
54 import tuwien.auto.calimero.GroupAddress;
55 import tuwien.auto.calimero.KNXException;
56 import tuwien.auto.calimero.datapoint.CommandDP;
57 import tuwien.auto.calimero.datapoint.Datapoint;
58 import tuwien.auto.calimero.dptxlator.TranslatorTypes;
59 import tuwien.auto.calimero.process.ProcessCommunicator;
60 import tuwien.auto.calimero.process.ProcessCommunicatorImpl;
61
62 /**
63  * Integration test to check conversion from raw KNX frame data to OH data types and back.
64  *
65  * This test checks
66  * <ul>
67  * <li>if OH can properly decode raw data payload from KNX frames using {@link ValueDecoder#decode()},
68  * <li>if OH can properly encode the data for handover to Calimero using {@link ValueEncoder#encode()},
69  * <li>if Calimero supports and correctly handles the data conversion to raw bytes for sending.
70  * </ul>
71  *
72  * In addition, it checks if newly integrated releases of Calimero introduce new DPT types not yet
73  * handled by this test. However, new subtypes are not detected.
74  *
75  * @see DummyKNXNetworkLink
76  * @see DummyClient
77  * @author Holger Friedrich - Initial contribution
78  *
79  */
80 @NonNullByDefault
81 public class Back2BackTest {
82     public static final Logger LOGGER = LoggerFactory.getLogger(Back2BackTest.class);
83     static Set<Integer> dptTested = new HashSet<>();
84     boolean testsMissing = false;
85
86     /**
87      * helper method for integration tests
88      *
89      * @param dpt DPT type, e.g. "251.600", see 03_07_02-Datapoint-Types-v02.02.01-AS.pdf
90      * @param rawData byte array containing raw data, known content
91      * @param ohReferenceData OpenHAB data type, initialized to known good value
92      * @param maxDistance byte array containing maximal deviations when comparing byte arrays (rawData against created
93      *            KNX frame), may be empty if no deviation is considered
94      * @param bitmask to mask certain bits in the raw to raw comparison, required for multi-valued KNX frames
95      */
96     void helper(String dpt, byte[] rawData, Type ohReferenceData, byte[] maxDistance, byte[] bitmask) {
97         try {
98             DummyKNXNetworkLink link = new DummyKNXNetworkLink();
99             ProcessCommunicator pc = new ProcessCommunicatorImpl(link);
100             DummyProcessListener processListener = new DummyProcessListener();
101             pc.addProcessListener(processListener);
102
103             GroupAddress groupAddress = new GroupAddress(2, 4, 6);
104             Datapoint datapoint = new CommandDP(groupAddress, "dummy GA", 0,
105                     DPTUtil.NORMALIZED_DPT.getOrDefault(dpt, dpt));
106
107             // 0) check usage of helper()
108             assertEquals(true, rawData.length > 0);
109             if (maxDistance.length == 0) {
110                 maxDistance = new byte[rawData.length];
111             }
112             assertEquals(rawData.length, maxDistance.length, "incorrect length of maxDistance array");
113             if (bitmask.length == 0) {
114                 bitmask = new byte[rawData.length];
115                 Arrays.fill(bitmask, (byte) 0xff);
116             }
117             assertEquals(rawData.length, bitmask.length, "incorrect length of bitmask array");
118             int mainType = Integer.parseUnsignedInt(dpt.substring(0, dpt.indexOf('.')));
119             dptTested.add(Integer.valueOf(mainType));
120             // check if OH would be able to send out a frame, given the type
121             Set<Integer> knownWorking = Set.of(1, 3, 5);
122             if (!knownWorking.contains(mainType)) {
123                 Set<Class<? extends Type>> allowedTypes = DPTUtil.getAllowedTypes("" + mainType);
124                 if (!allowedTypes.contains(ohReferenceData.getClass())) {
125                     LOGGER.warn(
126                             "test for DPT {} uses type {} which is not contained in DPT_TYPE_MAP, sending may not be allowed",
127                             dpt, ohReferenceData.getClass());
128                 }
129             }
130
131             // 1) check if the decoder works (rawData to known good type ohReferenceData)
132             //
133             // This test is based on known raw data. The mapping to openHAB type is known and confirmed.
134             // In this test, only ValueDecoder.decode() is involved.
135
136             // raw data of the DPT on application layer, without all headers from the layers below
137             // see 03_07_02-Datapoint-Types-v02.02.01-AS.pdf
138             Type ohData = (Type) ValueDecoder.decode(dpt, rawData, ohReferenceData.getClass());
139             assertNotNull(ohData, "could not decode frame data for DPT " + dpt);
140             if ((ohReferenceData instanceof HSBType hsbReferenceData) && (ohData instanceof HSBType hsbData)) {
141                 assertTrue(hsbReferenceData.closeTo(hsbData, 0.001),
142                         "comparing reference to decoded value for DPT " + dpt);
143             } else {
144                 assertEquals(ohReferenceData, ohData, "comparing reference to decoded value: failed for DPT " + dpt
145                         + ", check ValueEncoder.decode()");
146             }
147
148             // 2) check the encoding (ohData to raw data)
149             //
150             // Test approach is to a) encode the value into String format using ValueEncoder.encode(),
151             // b) pass it to Calimero for conversion into a raw representation, and
152             // c) finally grab raw data bytes from a custom KNXNetworkLink implementation
153             String enc = ValueEncoder.encode(ohData, dpt);
154             pc.write(datapoint, enc);
155
156             byte[] frame = link.getLastFrame();
157             assertNotNull(frame);
158             // remove header; for compact frames extract data byte from header
159             frame = DataUnitBuilder.extractASDU(frame);
160             assertEquals(rawData.length, frame.length,
161                     "unexpected length of KNX frame: " + HexUtils.bytesToHex(frame, " "));
162             for (int i = 0; i < rawData.length; i++) {
163                 assertEquals(rawData[i] & bitmask[i] & 0xff, frame[i] & bitmask[i] & 0xff, maxDistance[i],
164                         "unexpected content in encoded data, " + i);
165             }
166
167             // 3) Check date provided by Calimero library as input via loopback, it should match the initial data
168             //
169             // Deviations in some bytes of the frame may be possible due to data conversion, e.g. for HSBType.
170             // This is why maxDistance is used.
171             byte[] input = processListener.getLastFrame();
172             LOGGER.info("loopback {}", HexUtils.bytesToHex(input, " "));
173             assertNotNull(input);
174             assertEquals(rawData.length, input.length, "unexpected length of loopback frame");
175             for (int i = 0; i < rawData.length; i++) {
176                 assertEquals(rawData[i] & bitmask[i] & 0xff, input[i] & bitmask[i] & 0xff, maxDistance[i],
177                         "unexpected content in loopback data, " + i);
178             }
179
180             pc.close();
181         } catch (KNXException e) {
182             LOGGER.warn("exception occurred", e.toString());
183             assertEquals("", e.toString());
184         }
185     }
186
187     void helper(String dpt, byte[] rawData, Type ohReferenceData) {
188         helper(dpt, rawData, ohReferenceData, new byte[0], new byte[0]);
189     }
190
191     @Test
192     void testDpt1() {
193         helper("1.001", new byte[] { 0 }, OnOffType.OFF);
194         helper("1.001", new byte[] { 1 }, OnOffType.ON);
195         helper("1.001", new byte[] { 0 }, OpenClosedType.CLOSED);
196         helper("1.001", new byte[] { 1 }, OpenClosedType.OPEN);
197         helper("1.002", new byte[] { 0 }, OnOffType.OFF);
198         helper("1.002", new byte[] { 1 }, OnOffType.ON);
199         helper("1.002", new byte[] { 0 }, OpenClosedType.CLOSED);
200         helper("1.002", new byte[] { 1 }, OpenClosedType.OPEN);
201         helper("1.003", new byte[] { 0 }, OnOffType.OFF);
202         helper("1.003", new byte[] { 1 }, OnOffType.ON);
203         helper("1.003", new byte[] { 0 }, OpenClosedType.CLOSED);
204         helper("1.003", new byte[] { 1 }, OpenClosedType.OPEN);
205         helper("1.004", new byte[] { 0 }, OnOffType.OFF);
206         helper("1.004", new byte[] { 1 }, OnOffType.ON);
207         helper("1.004", new byte[] { 0 }, OpenClosedType.CLOSED);
208         helper("1.004", new byte[] { 1 }, OpenClosedType.OPEN);
209         helper("1.005", new byte[] { 0 }, OnOffType.OFF);
210         helper("1.005", new byte[] { 1 }, OnOffType.ON);
211         helper("1.005", new byte[] { 0 }, OpenClosedType.CLOSED);
212         helper("1.005", new byte[] { 1 }, OpenClosedType.OPEN);
213         helper("1.006", new byte[] { 0 }, OnOffType.OFF);
214         helper("1.006", new byte[] { 1 }, OnOffType.ON);
215         helper("1.006", new byte[] { 0 }, OpenClosedType.CLOSED);
216         helper("1.006", new byte[] { 1 }, OpenClosedType.OPEN);
217         helper("1.007", new byte[] { 0 }, OnOffType.OFF);
218         helper("1.007", new byte[] { 1 }, OnOffType.ON);
219         helper("1.007", new byte[] { 0 }, OpenClosedType.CLOSED);
220         helper("1.007", new byte[] { 1 }, OpenClosedType.OPEN);
221         helper("1.008", new byte[] { 0 }, UpDownType.UP);
222         helper("1.008", new byte[] { 1 }, UpDownType.DOWN);
223         // NOTE: This is how DPT 1.009 is defined: 0: open, 1: closed
224         // For historical reasons it is defined the other way on OH
225         helper("1.009", new byte[] { 0 }, OnOffType.OFF);
226         helper("1.009", new byte[] { 1 }, OnOffType.ON);
227         helper("1.009", new byte[] { 0 }, OpenClosedType.CLOSED);
228         helper("1.009", new byte[] { 1 }, OpenClosedType.OPEN);
229         helper("1.010", new byte[] { 0 }, StopMoveType.STOP);
230         helper("1.010", new byte[] { 1 }, StopMoveType.MOVE);
231         helper("1.011", new byte[] { 0 }, OnOffType.OFF);
232         helper("1.011", new byte[] { 1 }, OnOffType.ON);
233         helper("1.011", new byte[] { 0 }, OpenClosedType.CLOSED);
234         helper("1.011", new byte[] { 1 }, OpenClosedType.OPEN);
235         helper("1.012", new byte[] { 0 }, OnOffType.OFF);
236         helper("1.012", new byte[] { 1 }, OnOffType.ON);
237         helper("1.012", new byte[] { 0 }, OpenClosedType.CLOSED);
238         helper("1.012", new byte[] { 1 }, OpenClosedType.OPEN);
239         helper("1.013", new byte[] { 0 }, OnOffType.OFF);
240         helper("1.013", new byte[] { 1 }, OnOffType.ON);
241         helper("1.013", new byte[] { 0 }, OpenClosedType.CLOSED);
242         helper("1.013", new byte[] { 1 }, OpenClosedType.OPEN);
243         helper("1.014", new byte[] { 0 }, OnOffType.OFF);
244         helper("1.014", new byte[] { 1 }, OnOffType.ON);
245         helper("1.014", new byte[] { 0 }, OpenClosedType.CLOSED);
246         helper("1.014", new byte[] { 1 }, OpenClosedType.OPEN);
247         helper("1.015", new byte[] { 0 }, OnOffType.OFF);
248         helper("1.015", new byte[] { 1 }, OnOffType.ON);
249         helper("1.015", new byte[] { 0 }, OpenClosedType.CLOSED);
250         helper("1.015", new byte[] { 1 }, OpenClosedType.OPEN);
251         helper("1.016", new byte[] { 0 }, OnOffType.OFF);
252         helper("1.016", new byte[] { 1 }, OnOffType.ON);
253         helper("1.016", new byte[] { 0 }, OpenClosedType.CLOSED);
254         helper("1.016", new byte[] { 1 }, OpenClosedType.OPEN);
255         // DPT 1.017 is a special case, "trigger" has no "value", both 0 and 1 shall trigger
256         helper("1.017", new byte[] { 0 }, OnOffType.OFF);
257         helper("1.017", new byte[] { 0 }, OpenClosedType.CLOSED);
258         // Calimero maps it always to 0
259         // helper("1.017", new byte[] { 1 }, OnOffType.ON);
260         helper("1.018", new byte[] { 0 }, OnOffType.OFF);
261         helper("1.018", new byte[] { 1 }, OnOffType.ON);
262         helper("1.018", new byte[] { 0 }, OpenClosedType.CLOSED);
263         helper("1.018", new byte[] { 1 }, OpenClosedType.OPEN);
264         helper("1.019", new byte[] { 0 }, OnOffType.OFF);
265         helper("1.019", new byte[] { 1 }, OnOffType.ON);
266         helper("1.019", new byte[] { 0 }, OpenClosedType.CLOSED);
267         helper("1.019", new byte[] { 1 }, OpenClosedType.OPEN);
268
269         helper("1.021", new byte[] { 0 }, OnOffType.OFF);
270         helper("1.021", new byte[] { 1 }, OnOffType.ON);
271         helper("1.021", new byte[] { 0 }, OpenClosedType.CLOSED);
272         helper("1.021", new byte[] { 1 }, OpenClosedType.OPEN);
273         // DPT 1.022 is mapped to decimal, Calimero does not follow the recommendation
274         // from KNX spec to add offset 1
275         helper("1.022", new byte[] { 0 }, DecimalType.valueOf("0"));
276         helper("1.022", new byte[] { 1 }, DecimalType.valueOf("1"));
277         helper("1.023", new byte[] { 0 }, OnOffType.OFF);
278         helper("1.023", new byte[] { 1 }, OnOffType.ON);
279         helper("1.023", new byte[] { 0 }, OpenClosedType.CLOSED);
280         helper("1.023", new byte[] { 1 }, OpenClosedType.OPEN);
281         helper("1.024", new byte[] { 0 }, OnOffType.OFF);
282         helper("1.024", new byte[] { 1 }, OnOffType.ON);
283         helper("1.024", new byte[] { 0 }, OpenClosedType.CLOSED);
284         helper("1.024", new byte[] { 1 }, OpenClosedType.OPEN);
285
286         helper("1.100", new byte[] { 0 }, OnOffType.OFF);
287         helper("1.100", new byte[] { 1 }, OnOffType.ON);
288         helper("1.100", new byte[] { 0 }, OpenClosedType.CLOSED);
289         helper("1.100", new byte[] { 1 }, OpenClosedType.OPEN);
290
291         helper("1.1200", new byte[] { 0 }, OnOffType.OFF);
292         helper("1.1200", new byte[] { 1 }, OnOffType.ON);
293         helper("1.1200", new byte[] { 0 }, OpenClosedType.CLOSED);
294         helper("1.1200", new byte[] { 1 }, OpenClosedType.OPEN);
295         helper("1.1201", new byte[] { 0 }, OnOffType.OFF);
296         helper("1.1201", new byte[] { 1 }, OnOffType.ON);
297         helper("1.1201", new byte[] { 0 }, OpenClosedType.CLOSED);
298         helper("1.1201", new byte[] { 1 }, OpenClosedType.OPEN);
299     }
300
301     @Test
302     void testDpt2() {
303         for (int subType = 1; subType <= 12; subType++) {
304             helper("2." + String.format("%03d", subType), new byte[] { 3 }, new DecimalType(3));
305         }
306     }
307
308     @Test
309     void testDpt3() {
310         // DPT 3.007 and DPT 3.008 consist of a control bit (1 bit) and stepsize (3 bit)
311         // if stepsize is 0, OH will ignore the command
312         byte controlBit = 1 << 3;
313         // loop all other step sizes and check only the control bit
314         for (byte i = 1; i < 8; i++) {
315             helper("3.007", new byte[] { i }, IncreaseDecreaseType.DECREASE, new byte[0], new byte[] { controlBit });
316             helper("3.007", new byte[] { (byte) (i + controlBit) }, IncreaseDecreaseType.INCREASE, new byte[0],
317                     new byte[] { controlBit });
318             helper("3.008", new byte[] { i }, UpDownType.UP, new byte[0], new byte[] { controlBit });
319             helper("3.008", new byte[] { (byte) (i + controlBit) }, UpDownType.DOWN, new byte[0],
320                     new byte[] { controlBit });
321         }
322
323         // check if OH ignores incoming frames with mask 0 (mapped to UndefType)
324         Assertions.assertFalse(ValueDecoder.decode("3.007", new byte[] { 0 },
325                 IncreaseDecreaseType.class) instanceof IncreaseDecreaseType);
326         Assertions.assertFalse(ValueDecoder.decode("3.007", new byte[] { controlBit },
327                 IncreaseDecreaseType.class) instanceof IncreaseDecreaseType);
328         Assertions.assertFalse(ValueDecoder.decode("3.008", new byte[] { 0 }, UpDownType.class) instanceof UpDownType);
329         Assertions.assertFalse(
330                 ValueDecoder.decode("3.008", new byte[] { controlBit }, UpDownType.class) instanceof UpDownType);
331     }
332
333     @Test
334     void testDpt5() {
335         // TODO add tests for more subtypes
336         helper("5.001", new byte[] { 0 }, new PercentType(0));
337         helper("5.001", new byte[] { (byte) 0x80 }, new PercentType(50));
338         helper("5.001", new byte[] { (byte) 0xff }, new PercentType(100));
339
340         helper("5.010", new byte[] { 42 }, new DecimalType(42));
341         helper("5.010", new byte[] { (byte) 0xff }, new DecimalType(255));
342     }
343
344     @Test
345     void testDpt6() {
346         helper("6.010", new byte[] { 0 }, new DecimalType(0));
347         helper("6.010", new byte[] { (byte) 0x7f }, new DecimalType(127));
348         helper("6.010", new byte[] { (byte) 0xff }, new DecimalType(-1));
349         // TODO 6.001 is mapped to PercentType, which can only cover 0-100%, not -128..127%
350         // helper("6.001", new byte[] { 0 }, new DecimalType(0));
351     }
352
353     @Test
354     void testDpt7() {
355         // TODO add tests for more subtypes
356         helper("7.001", new byte[] { 0, 42 }, new DecimalType(42));
357         helper("7.001", new byte[] { (byte) 0xff, (byte) 0xff }, new DecimalType(65535));
358     }
359
360     @Test
361     void testDpt8() {
362         // TODO add tests for more subtypes
363         helper("8.001", new byte[] { (byte) 0x7f, (byte) 0xff }, new DecimalType(32767));
364         helper("8.001", new byte[] { (byte) 0x80, (byte) 0x00 }, new DecimalType(-32768));
365     }
366
367     @Test
368     void testDpt9() {
369         // TODO add tests for more subtypes
370         helper("9.001", new byte[] { (byte) 0x00, (byte) 0x64 }, new QuantityType<Temperature>("1 °C"));
371     }
372
373     @Test
374     void testDpt10() {
375         // TODO check handling of DPT10: date is not set to current date, but 1970-01-01 + offset if day is given
376         // maybe we should change the semantics and use current date + offset if day is given
377
378         // note: local timezone is set when creating DateTimeType, for example "1970-01-01Thh:mm:ss.000+0100"
379
380         // no-day
381         assertTrue(Objects
382                 .toString(ValueDecoder.decode("10.001", new byte[] { (byte) 0x11, (byte) 0x1e, 0 }, DecimalType.class))
383                 .startsWith("1970-01-01T17:30:00.000+"));
384         // Thursday, this is correct for 1970-01-01
385         assertTrue(Objects
386                 .toString(ValueDecoder.decode("10.001", new byte[] { (byte) 0x91, (byte) 0x1e, 0 }, DecimalType.class))
387                 .startsWith("1970-01-01T17:30:00.000+"));
388         // Monday -> 1970-01-05
389         assertTrue(Objects
390                 .toString(ValueDecoder.decode("10.001", new byte[] { (byte) 0x31, (byte) 0x1e, 0 }, DecimalType.class))
391                 .startsWith("1970-01-05T17:30:00.000+"));
392
393         // Thursday, otherwise first byte of encoded data will not match
394         helper("10.001", new byte[] { (byte) 0x91, (byte) 0x1e, (byte) 0x0 }, new DateTimeType("17:30:00"));
395         helper("10.001", new byte[] { (byte) 0x11, (byte) 0x1e, (byte) 0x0 }, new DateTimeType("17:30:00"), new byte[0],
396                 new byte[] { (byte) 0x1f, (byte) 0xff, (byte) 0xff });
397     }
398
399     @Test
400     void testDpt11() {
401         // note: local timezone and dst is set when creating DateTimeType, for example "2019-06-12T00:00:00.000+0200"
402         helper("11.001", new byte[] { (byte) 12, 6, 19 }, new DateTimeType("2019-06-12"));
403     }
404
405     @Test
406     void testDpt12() {
407         helper("12.001", new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xfe },
408                 new DecimalType("4294967294"));
409         helper("12.100", new byte[] { 0, 0, 0, 60 }, new QuantityType<>("60 s"));
410         helper("12.100", new byte[] { 0, 0, 0, 60 }, new QuantityType<>("1 min"));
411         helper("12.101", new byte[] { 0, 0, 0, 60 }, new QuantityType<>("60 min"));
412         helper("12.101", new byte[] { 0, 0, 0, 60 }, new QuantityType<>("1 h"));
413         helper("12.102", new byte[] { 0, 0, 0, 1 }, new QuantityType<>("1 h"));
414         helper("12.102", new byte[] { 0, 0, 0, 1 }, new QuantityType<>("60 min"));
415
416         helper("12.1200", new byte[] { 0, 0, 0, 1 }, new QuantityType<>("1 l"));
417         helper("12.1200", new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xfe },
418                 new QuantityType<>("4294967294 l"));
419         helper("12.1201", new byte[] { 0, 0, 0, 1 }, new QuantityType<>("1 m³"));
420         helper("12.1201", new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xfe },
421                 new QuantityType<>("4294967294 m³"));
422     }
423
424     @Test
425     void testDpt13() {
426         // TODO add tests for more subtypes
427         helper("13.001", new byte[] { 0, 0, 0, 0 }, new DecimalType(0));
428         helper("13.001", new byte[] { 0, 0, 0, 42 }, new DecimalType(42));
429         helper("13.001", new byte[] { (byte) 0x7f, (byte) 0xff, (byte) 0xff, (byte) 0xff },
430                 new DecimalType(2147483647));
431         // KNX representation typically uses two's complement
432         helper("13.001", new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff }, new DecimalType(-1));
433         helper("13.001", new byte[] { (byte) 0x80, (byte) 0x0, (byte) 0x0, (byte) 0x0 }, new DecimalType(-2147483648));
434     }
435
436     @Test
437     void testDpt14() {
438         // TODO add tests for more subtypes
439         helper("14.068", new byte[] { (byte) 0x3f, (byte) 0x80, 0, 0 }, new QuantityType<Temperature>("1 °C"));
440     }
441
442     @Test
443     void testDpt16() {
444         helper("16.000", new byte[] { (byte) 0x4B, (byte) 0x4E, 0x58, 0x20, 0x69, 0x73, 0x20, (byte) 0x4F, (byte) 0x4B,
445                 0x0, 0x0, 0x0, 0x0, 0x0 }, new StringType("KNX is OK"));
446         helper("16.001", new byte[] { (byte) 0x4B, (byte) 0x4E, 0x58, 0x20, 0x69, 0x73, 0x20, (byte) 0x4F, (byte) 0x4B,
447                 0x0, 0x0, 0x0, 0x0, 0x0 }, new StringType("KNX is OK"));
448     }
449
450     @Test
451     void testDpt17() {
452         helper("17.001", new byte[] { 0 }, new DecimalType(0));
453         helper("17.001", new byte[] { 42 }, new DecimalType(42));
454         helper("17.001", new byte[] { 63 }, new DecimalType(63));
455     }
456
457     @Test
458     void testDpt18() {
459         // scene, activate 0..63
460         helper("18.001", new byte[] { 0 }, new DecimalType(0));
461         helper("18.001", new byte[] { 42 }, new DecimalType(42));
462         helper("18.001", new byte[] { 63 }, new DecimalType(63));
463         // scene, learn += 0x80
464         helper("18.001", new byte[] { (byte) (0x80 + 0) }, new DecimalType(0x80));
465         helper("18.001", new byte[] { (byte) (0x80 + 42) }, new DecimalType(0x80 + 42));
466         helper("18.001", new byte[] { (byte) (0x80 + 63) }, new DecimalType(0x80 + 63));
467     }
468
469     @Test
470     void testDpt19() {
471         // 2019-01-15 17:30:00
472         helper("19.001", new byte[] { (byte) (2019 - 1900), 1, 15, 17, 30, 0, (byte) 0x25, (byte) 0x00 },
473                 new DateTimeType("2019-01-15T17:30:00"));
474         helper("19.001", new byte[] { (byte) (2019 - 1900), 1, 15, 17, 30, 0, (byte) 0x24, (byte) 0x00 },
475                 new DateTimeType("2019-01-15T17:30:00"));
476         // 2019-07-15 17:30:00
477         helper("19.001", new byte[] { (byte) (2019 - 1900), 7, 15, 17, 30, 0, (byte) 0x25, (byte) 0x00 },
478                 new DateTimeType("2019-07-15T17:30:00"), new byte[0], new byte[] { 0, 0, 0, 0, 0, 0, 0, 1 });
479         helper("19.001", new byte[] { (byte) (2019 - 1900), 7, 15, 17, 30, 0, (byte) 0x24, (byte) 0x00 },
480                 new DateTimeType("2019-07-15T17:30:00"), new byte[0], new byte[] { 0, 0, 0, 0, 0, 0, 0, 1 });
481     }
482
483     @Test
484     void testDpt20() {
485         // test default String representation of enum (incomplete)
486         helper("20.001", new byte[] { 0 }, new StringType("autonomous"));
487         helper("20.001", new byte[] { 1 }, new StringType("slave"));
488         helper("20.001", new byte[] { 2 }, new StringType("master"));
489
490         helper("20.002", new byte[] { 0 }, new StringType("building in use"));
491         helper("20.002", new byte[] { 1 }, new StringType("building not used"));
492         helper("20.002", new byte[] { 2 }, new StringType("building protection"));
493
494         // test DecimalType representation of enum
495         int[] subTypes = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 11, 12, 13, 14, 17, 20, 21, 100, 101, 102, 103, 104, 105,
496                 106, 107, 108, 109, 110, 111, 112, 113, 114, 120, 121, 122, 600, 601, 602, 603, 604, 605, 606, 607, 608,
497                 609, 610, 801, 802, 803, 804, 1000, 1001, 1002, 1003, 1004, 1005, 1200, 1202 };
498         for (int subType : subTypes) {
499             helper("20." + String.format("%03d", subType), new byte[] { 1 }, new DecimalType(1));
500         }
501         // once these DPTs are available in Calimero, add to check above
502         int[] unsupportedSubTypes = new int[] { 22, 115, 611, 612, 613, 1203, 1204, 1205, 1206, 1207, 1208, 1209 };
503         for (int subType : unsupportedSubTypes) {
504             assertNull(ValueDecoder.decode("20." + String.format("%03d", subType), new byte[] { 0 }, StringType.class));
505         }
506     }
507
508     @Test
509     void testDpt21() {
510         // test default String representation of bitfield (incomplete)
511         helper("21.001", new byte[] { 5 }, new StringType("overridden, out of service"));
512
513         // test DecimalType representation of bitfield
514         int[] subTypes = new int[] { 1, 2, 100, 101, 102, 103, 104, 105, 106, 601, 1000, 1001, 1002, 1010 };
515         for (int subType : subTypes) {
516             helper("21." + String.format("%03d", subType), new byte[] { 1 }, new DecimalType(1));
517         }
518         // once these DPTs are available in Calimero, add to check above
519         assertNull(ValueDecoder.decode("21.1200", new byte[] { 0 }, StringType.class));
520         assertNull(ValueDecoder.decode("21.1201", new byte[] { 0 }, StringType.class));
521     }
522
523     @Test
524     void testDpt22() {
525         // test default String representation of bitfield (incomplete)
526         helper("22.101", new byte[] { 1, 0 }, new StringType("heating mode"));
527         helper("22.101", new byte[] { 1, 2 }, new StringType("heating mode, heating eco mode"));
528
529         // test DecimalType representation of bitfield
530         helper("22.101", new byte[] { 0, 2 }, new DecimalType(2));
531         helper("22.1000", new byte[] { 0, 2 }, new DecimalType(2));
532         // once these DPTs are available in Calimero, add to check above
533         assertNull(ValueDecoder.decode("22.100", new byte[] { 0, 2 }, StringType.class));
534         assertNull(ValueDecoder.decode("22.1010", new byte[] { 0, 2 }, StringType.class));
535     }
536
537     @Test
538     void testDpt28() {
539         // null terminated strings, UTF8
540         helper("28.001", new byte[] { 0x31, 0x32, 0x33, 0x34, 0x0 }, new StringType("1234"));
541         helper("28.001", new byte[] { (byte) 0xce, (byte) 0xb5, 0x34, 0x0 }, new StringType("\u03b54"));
542     }
543
544     @Test
545     void testDpt29() {
546         helper("29.010", new byte[] { 0, 0, 0, 0, 0, 0, 0, 42 }, new QuantityType<>("42 Wh"));
547         helper("29.010", new byte[] { (byte) 0x80, 0, 0, 0, 0, 0, 0, 0 },
548                 new QuantityType<>("-9223372036854775808 Wh"));
549         helper("29.010", new byte[] { (byte) 0xff, 0, 0, 0, 0, 0, 0, 0 }, new QuantityType<>("-72057594037927936 Wh"));
550         helper("29.010", new byte[] { 0, 0, 0, 0, 0, 0, 0, 42 }, new QuantityType<>("42 Wh"));
551         helper("29.011", new byte[] { 0, 0, 0, 0, 0, 0, 0, 42 }, new QuantityType<>("42 VAh"));
552         helper("29.012", new byte[] { 0, 0, 0, 0, 0, 0, 0, 42 }, new QuantityType<>("42 varh"));
553     }
554
555     @Test
556     void testDpt229() {
557         // special DPT for metering, allows several units and different scaling
558         // -> Calimero uses scaling, but always encodes as dimensionless value
559         final int dimensionlessCounter = 0b10111010;
560         helper("229.001", new byte[] { 0, 0, 0, 0, (byte) dimensionlessCounter, 0 }, new DecimalType(0));
561     }
562
563     @Test
564     void testColorDpts() {
565         // HSB
566         helper("232.600", new byte[] { 123, 45, 67 }, ColorUtil.rgbToHsb(new int[] { 123, 45, 67 }));
567         // RGB, MDT specific
568         helper("232.60000", new byte[] { 123, 45, 67 }, new HSBType("173.6, 17.6, 26.3"));
569
570         // xyY
571         int x = (int) (14.65 * 65535.0 / 100.0);
572         int y = (int) (11.56 * 65535.0 / 100.0);
573         // encoding is always xy and brightness (C+B, 0x03), do not test other combinations
574         helper("242.600", new byte[] { (byte) ((x >> 8) & 0xff), (byte) (x & 0xff), (byte) ((y >> 8) & 0xff),
575                 (byte) (y & 0xff), (byte) 0x28, 0x3 }, new HSBType("220,90,50"), new byte[] { 0, 8, 0, 8, 0, 0 },
576                 new byte[0]);
577         // TODO check brightness
578
579         // RGBW, only RGB part
580         helper("251.600", new byte[] { 0x26, 0x2b, 0x31, 0x00, 0x00, 0x0e }, new HSBType("207, 23, 19"),
581                 new byte[] { 1, 1, 1, 0, 0, 0 }, new byte[0]);
582         // RGBW, only RGB part
583         helper("251.600", new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x00, 0x00, 0x0e },
584                 new HSBType("0, 0, 100"), new byte[] { 1, 1, 1, 0, 0, 0 }, new byte[0]);
585     }
586
587     @Test
588     void testColorTransitionDpts() {
589         // DPT 243.600 DPT_Colour_Transition_xyY
590         // time(2) y(2) x(2), %brightness(1), flags(1)
591         helper("243.600", new byte[] { 0, 5, 0x7F, 0, (byte) 0xfe, 0, 42, 3 },
592                 new StringType("(0.9922, 0.4961) 16.5 % 0.5 s"));
593         // DPT 249.600 DPT_Brightness_Colour_Temperature_Transition
594         // time(2) colortemp(2), brightness(1), flags(1)
595         helper("249.600", new byte[] { 0, 5, 0, 40, 127, 7 }, new StringType("49.8 % 40 K 0.5 s"));
596         // DPT 250.600 DPT_Brightness_Colour_Temperature_Control
597         // cct(1) cb(1) flags(1)
598         helper("250.600", new byte[] { 0x0f, 0x0e, 3 }, new StringType("CT increase 7 steps BRT increase 6 steps"));
599         // DPT 252.600 DPT_Relative_Control_RGBW
600         // r(1) g(1) b(1) w(1) flags(1)
601         helper("252.600", new byte[] { 0x0f, 0x0e, 0x0d, 0x0c, 0x0f },
602                 new StringType("R increase 7 steps G increase 6 steps B increase 5 steps W increase 4 steps"));
603         // DPT 253.600 DPT_Relative_Control_xyY
604         // cs(1) ct(1) cb(1) flags(1)
605         helper("253.600", new byte[] { 0x0f, 0x0e, 0x0d, 0x7 },
606                 new StringType("x increase 7 steps y increase 6 steps Y increase 5 steps"));
607         // DPT 254.600 DPT_Relative_Control_RGB
608         // cr(1) cg(1) cb(1)
609         helper("254.600", new byte[] { 0x0f, 0x0e, 0x0d },
610                 new StringType("R increase 7 steps G increase 6 steps B increase 5 steps"));
611     }
612
613     @Test
614     @AfterAll
615     static void checkForMissingMainTypes() {
616         // checks if we have itests for all main DPT types supported by Calimero library,
617         // data is collected within method helper()
618         var wrapper = new Object() {
619             boolean testsMissing = false;
620         };
621         TranslatorTypes.getAllMainTypes().forEach((i, t) -> {
622             if (!dptTested.contains(i)) {
623                 LOGGER.warn("missing tests for main DPT type " + i);
624                 wrapper.testsMissing = true;
625             }
626         });
627         assertEquals(false, wrapper.testsMissing, "add tests for new DPT main types");
628     }
629 }