From 4517f3959be3fc305f22e9822621ab82ddb07c33 Mon Sep 17 00:00:00 2001 From: Hilbrand Bouwkamp Date: Fri, 1 Mar 2019 14:46:33 +0100 Subject: [PATCH] Added standard gravity and dBm units. Signed-off-by: Hilbrand Bouwkamp --- .../smarthome/core/library/unit/SmartHomeUnits.java | 8 ++++++++ .../eclipse/smarthome/core/types/util/UnitUtilsTest.java | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/bundles/org.openhab.core/src/main/java/org/eclipse/smarthome/core/library/unit/SmartHomeUnits.java b/bundles/org.openhab.core/src/main/java/org/eclipse/smarthome/core/library/unit/SmartHomeUnits.java index f193b33e8..159c8dea2 100644 --- a/bundles/org.openhab.core/src/main/java/org/eclipse/smarthome/core/library/unit/SmartHomeUnits.java +++ b/bundles/org.openhab.core/src/main/java/org/eclipse/smarthome/core/library/unit/SmartHomeUnits.java @@ -56,7 +56,9 @@ import org.eclipse.smarthome.core.library.dimension.VolumetricFlowRate; import tec.uom.se.AbstractSystemOfUnits; import tec.uom.se.AbstractUnit; import tec.uom.se.format.SimpleUnitFormat; +import tec.uom.se.function.ExpConverter; import tec.uom.se.function.LogConverter; +import tec.uom.se.function.MultiplyConverter; import tec.uom.se.function.PiMultiplierConverter; import tec.uom.se.function.RationalConverter; import tec.uom.se.unit.AlternateUnit; @@ -79,6 +81,8 @@ public final class SmartHomeUnits extends AbstractSystemOfUnits { // Alphabetical ordered by Unit. public static final Unit METRE_PER_SQUARE_SECOND = addUnit(Units.METRE_PER_SQUARE_SECOND); + public static final Unit STANDARD_GRAVITY = new TransformedUnit<>("gₙ", + SmartHomeUnits.METRE_PER_SQUARE_SECOND, new MultiplyConverter(9.80665)); public static final Unit MOLE = addUnit(Units.MOLE); public static final Unit DEUTSCHE_HAERTE = addUnit(new TransformedUnit("°dH", (Unit) MetricPrefix.MILLI(Units.MOLE).divide(Units.LITRE), @@ -123,6 +127,8 @@ public final class SmartHomeUnits extends AbstractSystemOfUnits { public static final Unit WEBER = addUnit(Units.WEBER); public static final Unit TESLA = addUnit(Units.TESLA); public static final Unit WATT = addUnit(Units.WATT); + public static final Unit DECIBEL_MILLIWATTS = new TransformedUnit<>("dBm", + MetricPrefix.MILLI(SmartHomeUnits.WATT), new ExpConverter(10.0).concatenate(new MultiplyConverter(0.1))); public static final Unit MILLIMETRE_OF_MERCURY = addUnit(new TransformedUnit<>("mmHg", Units.PASCAL, new RationalConverter(BigInteger.valueOf(133322368), BigInteger.valueOf(1000000)))); public static final Unit BAR = addUnit(new TransformedUnit<>("bar", Units.PASCAL, @@ -169,6 +175,7 @@ public final class SmartHomeUnits extends AbstractSystemOfUnits { SimpleUnitFormat.getInstance().label(CUBICMETRE_PER_MINUTE, "m³/min"); SimpleUnitFormat.getInstance().label(CUBICMETRE_PER_SECOND, "m³/s"); SimpleUnitFormat.getInstance().label(DECIBEL, "dB"); + SimpleUnitFormat.getInstance().label(DECIBEL_MILLIWATTS, "dBm"); SimpleUnitFormat.getInstance().label(DEGREE_ANGLE, "°"); SimpleUnitFormat.getInstance().label(DEUTSCHE_HAERTE, "°dH"); SimpleUnitFormat.getInstance().label(DOBSON_UNIT, "DU"); @@ -182,6 +189,7 @@ public final class SmartHomeUnits extends AbstractSystemOfUnits { SimpleUnitFormat.getInstance().label(MILLIBAR, "mbar"); SimpleUnitFormat.getInstance().label(MILLIMETRE_OF_MERCURY, MILLIMETRE_OF_MERCURY.getSymbol()); SimpleUnitFormat.getInstance().label(PARTS_PER_MILLION, "ppm"); + SimpleUnitFormat.getInstance().label(STANDARD_GRAVITY, "gₙ"); SimpleUnitFormat.getInstance().label(WATT_HOUR, "Wh"); SimpleUnitFormat.getInstance().label(WATT_SECOND, "Ws"); } diff --git a/itests/org.openhab.core.tests/src/main/java/org/eclipse/smarthome/core/types/util/UnitUtilsTest.java b/itests/org.openhab.core.tests/src/main/java/org/eclipse/smarthome/core/types/util/UnitUtilsTest.java index 997fc101a..edba1264e 100644 --- a/itests/org.openhab.core.tests/src/main/java/org/eclipse/smarthome/core/types/util/UnitUtilsTest.java +++ b/itests/org.openhab.core.tests/src/main/java/org/eclipse/smarthome/core/types/util/UnitUtilsTest.java @@ -14,6 +14,7 @@ package org.eclipse.smarthome.core.types.util; import static org.eclipse.smarthome.core.library.unit.MetricPrefix.*; import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.number.IsCloseTo.closeTo; import static org.junit.Assert.*; import javax.measure.Quantity; @@ -62,6 +63,12 @@ public class UnitUtilsTest { assertTrue(Intensity.class.isAssignableFrom(intensity)); } + @Test + public void testConversionOfUnit() { + assertThat(SmartHomeUnits.DECIBEL_MILLIWATTS.getConverterTo(SmartHomeUnits.WATT).convert(50), closeTo(100, 3)); + assertThat(SmartHomeUnits.WATT.getConverterTo(SmartHomeUnits.DECIBEL_MILLIWATTS).convert(0.1), closeTo(20, 3)); + } + @Test public void shouldParseUnitFromPattern() { assertThat(UnitUtils.parseUnit("%.2f °F"), is(ImperialUnits.FAHRENHEIT));