diff --git a/bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/dpt/ValueEncoder.java b/bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/dpt/ValueEncoder.java index f2768a7b83a..582a57d561f 100644 --- a/bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/dpt/ValueEncoder.java +++ b/bundles/org.openhab.binding.knx/src/main/java/org/openhab/binding/knx/internal/dpt/ValueEncoder.java @@ -45,6 +45,7 @@ import tuwien.auto.calimero.dptxlator.DPT; import tuwien.auto.calimero.dptxlator.DPTXlator; import tuwien.auto.calimero.dptxlator.DPTXlator1BitControlled; import tuwien.auto.calimero.dptxlator.DPTXlator2ByteFloat; +import tuwien.auto.calimero.dptxlator.DPTXlator2ByteUnsigned; import tuwien.auto.calimero.dptxlator.DPTXlator3BitControlled; import tuwien.auto.calimero.dptxlator.DPTXlator4ByteFloat; import tuwien.auto.calimero.dptxlator.DPTXlatorBoolean; @@ -258,6 +259,14 @@ public class ValueEncoder { default: return "1 " + valueDPT.getUpperValue(); } + case "7": + if (DPTXlator2ByteUnsigned.DPT_TIMEPERIOD_10.getID().equals(dptId) + || DPTXlator2ByteUnsigned.DPT_TIMEPERIOD_100.getID().equals(dptId)) { + return bigDecimal.divide(BigDecimal.valueOf(1000)).stripTrailingZeros().toPlainString().replace('.', + ((DecimalFormat) DecimalFormat.getInstance()).getDecimalFormatSymbols() + .getDecimalSeparator()); + } + return bigDecimal.stripTrailingZeros().toPlainString(); case "18": int intVal = bigDecimal.intValue(); if (intVal > 63) { diff --git a/bundles/org.openhab.binding.knx/src/test/java/org/openhab/binding/knx/internal/dpt/DPTTest.java b/bundles/org.openhab.binding.knx/src/test/java/org/openhab/binding/knx/internal/dpt/DPTTest.java index 1775261087a..45ce5f8521b 100644 --- a/bundles/org.openhab.binding.knx/src/test/java/org/openhab/binding/knx/internal/dpt/DPTTest.java +++ b/bundles/org.openhab.binding.knx/src/test/java/org/openhab/binding/knx/internal/dpt/DPTTest.java @@ -73,8 +73,9 @@ class DPTTest { @Test void testToDPT7ValueFromQuantityType() { assertEquals("1000", ValueEncoder.encode(new QuantityType<>("1000 ms"), "7.002")); - assertEquals("1000", ValueEncoder.encode(new QuantityType<>("1000 ms"), "7.003")); - assertEquals("1000", ValueEncoder.encode(new QuantityType<>("1000 ms"), "7.004")); + // according to spec this should be 1000 for 7.003 and 7.004 - 1 is a workaround for Calimero 2.5.1 + assertEquals("1", ValueEncoder.encode(new QuantityType<>("1000 ms"), "7.003")); + assertEquals("1", ValueEncoder.encode(new QuantityType<>("1000 ms"), "7.004")); assertEquals("1", ValueEncoder.encode(new QuantityType<>("1000 ms"), "7.005")); assertEquals("1", ValueEncoder.encode(new QuantityType<>("60 s"), "7.006")); assertEquals("1", ValueEncoder.encode(new QuantityType<>("60 min"), "7.007")); diff --git a/bundles/org.openhab.binding.knx/src/test/java/org/openhab/binding/knx/internal/itests/Back2BackTest.java b/bundles/org.openhab.binding.knx/src/test/java/org/openhab/binding/knx/internal/itests/Back2BackTest.java index 20c88d05d61..ae34945b8ff 100644 --- a/bundles/org.openhab.binding.knx/src/test/java/org/openhab/binding/knx/internal/itests/Back2BackTest.java +++ b/bundles/org.openhab.binding.knx/src/test/java/org/openhab/binding/knx/internal/itests/Back2BackTest.java @@ -53,6 +53,7 @@ import tuwien.auto.calimero.GroupAddress; import tuwien.auto.calimero.KNXException; import tuwien.auto.calimero.datapoint.CommandDP; import tuwien.auto.calimero.datapoint.Datapoint; +import tuwien.auto.calimero.dptxlator.DPTXlator2ByteUnsigned; import tuwien.auto.calimero.dptxlator.TranslatorTypes; import tuwien.auto.calimero.process.ProcessCommunicator; import tuwien.auto.calimero.process.ProcessCommunicatorImpl; @@ -368,9 +369,45 @@ public class Back2BackTest { @Test void testDpt7() { - // TODO add tests for more subtypes helper("7.001", new byte[] { 0, 42 }, new DecimalType(42)); helper("7.001", new byte[] { (byte) 0xff, (byte) 0xff }, new DecimalType(65535)); + // workaround in place, as Calimero uses "s" instead of "ms" + // refs: ValueEncoder::handleNumericTypes() (case 7) and DptUnits (static initialization) + assertTrue("s".equals(DPTXlator2ByteUnsigned.DPT_TIMEPERIOD_10.getUnit())); + helper("7.002", new byte[] { (byte) 0x00, (byte) 0xff }, new QuantityType<>("255 ms")); + helper("7.002", new byte[] { (byte) 0x00, (byte) 0x00 }, new QuantityType<>("0 ms")); + helper("7.002", new byte[] { (byte) 0xff, (byte) 0xff }, new QuantityType<>("65535 ms")); + assertTrue("s".equals(DPTXlator2ByteUnsigned.DPT_TIMEPERIOD_100.getUnit())); + helper("7.003", new byte[] { (byte) 0x00, (byte) 0x00 }, new QuantityType<>("0 ms")); + helper("7.003", new byte[] { (byte) 0x00, (byte) 0x64 }, new QuantityType<>("1000 ms")); + helper("7.003", new byte[] { (byte) 0x00, (byte) 0xff }, new QuantityType<>("2550 ms")); + helper("7.003", new byte[] { (byte) 0xff, (byte) 0xff }, new QuantityType<>("655350 ms")); + helper("7.004", new byte[] { (byte) 0x00, (byte) 0x00 }, new QuantityType<>("0 ms")); + helper("7.004", new byte[] { (byte) 0x00, (byte) 0xff }, new QuantityType<>("25500 ms")); + helper("7.004", new byte[] { (byte) 0xff, (byte) 0xff }, new QuantityType<>("6553500 ms")); + helper("7.005", new byte[] { (byte) 0x00, (byte) 0x00 }, new QuantityType<>("0 s")); + helper("7.005", new byte[] { (byte) 0x00, (byte) 0xff }, new QuantityType<>("255 s")); + helper("7.005", new byte[] { (byte) 0xff, (byte) 0xff }, new QuantityType<>("65535 s")); + helper("7.006", new byte[] { (byte) 0x00, (byte) 0x00 }, new QuantityType<>("0 min")); + helper("7.006", new byte[] { (byte) 0x00, (byte) 0xff }, new QuantityType<>("255 min")); + helper("7.006", new byte[] { (byte) 0xff, (byte) 0xff }, new QuantityType<>("65535 min")); + helper("7.006", new byte[] { (byte) 0xff, (byte) 0xff }, new QuantityType<>("3932100 s")); + helper("7.007", new byte[] { (byte) 0x00, (byte) 0x00 }, new QuantityType<>("0 h")); + helper("7.007", new byte[] { (byte) 0x00, (byte) 0xff }, new QuantityType<>("255 h")); + helper("7.007", new byte[] { (byte) 0x00, (byte) 0xff }, new QuantityType<>("918000 s")); + helper("7.007", new byte[] { (byte) 0xff, (byte) 0xff }, new QuantityType<>("65535 h")); + + helper("7.010", new byte[] { 0, 42 }, new DecimalType(42)); + helper("7.010", new byte[] { (byte) 0xff, (byte) 0xff }, new DecimalType(65535)); + helper("7.011", new byte[] { (byte) 0x00, (byte) 0x00 }, new QuantityType<>("0 mm")); + helper("7.011", new byte[] { (byte) 0x00, (byte) 0xff }, new QuantityType<>("255 mm")); + helper("7.011", new byte[] { (byte) 0xff, (byte) 0xff }, new QuantityType<>("65535 mm")); + helper("7.012", new byte[] { (byte) 0x00, (byte) 0x00 }, new QuantityType<>("0 mA")); + helper("7.012", new byte[] { (byte) 0x00, (byte) 0xff }, new QuantityType<>("255 mA")); + helper("7.012", new byte[] { (byte) 0xff, (byte) 0xff }, new QuantityType<>("65535 mA")); + helper("7.013", new byte[] { (byte) 0x00, (byte) 0x00 }, new QuantityType<>("0 lx")); + helper("7.013", new byte[] { (byte) 0x00, (byte) 0xff }, new QuantityType<>("255 lx")); + helper("7.013", new byte[] { (byte) 0xff, (byte) 0xff }, new QuantityType<>("65535 lx")); } @Test