Skip to content

Commit

Permalink
Add some requested new units : J/m², gr/ft³,gr (#4467)
Browse files Browse the repository at this point in the history
Signed-off-by: Gaël L'hopital <[email protected]>
  • Loading branch information
clinique authored Dec 10, 2024
1 parent 31e2838 commit 241e55f
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
import org.openhab.core.library.dimension.EmissionIntensity;
import org.openhab.core.library.dimension.EnergyPrice;
import org.openhab.core.library.dimension.Intensity;
import org.openhab.core.library.dimension.RadiantExposure;
import org.openhab.core.library.dimension.RadiationSpecificActivity;
import org.openhab.core.library.dimension.VolumetricFlowRate;
import org.openhab.core.library.types.PointType;
Expand Down Expand Up @@ -429,6 +430,7 @@ public static Map<Class<? extends Quantity<?>>, Map<SystemOfUnits, Unit<? extend
addDefaultUnit(dimensionMap, RadiationDoseAbsorbed.class, Units.GRAY);
addDefaultUnit(dimensionMap, RadiationDoseEffective.class, Units.SIEVERT);
addDefaultUnit(dimensionMap, RadiationSpecificActivity.class, Units.BECQUEREL_PER_CUBIC_METRE);
addDefaultUnit(dimensionMap, RadiantExposure.class, Units.JOULE_PER_SQUARE_METRE);
addDefaultUnit(dimensionMap, Radioactivity.class, Units.BECQUEREL);
addDefaultUnit(dimensionMap, SolidAngle.class, Units.STERADIAN);
addDefaultUnit(dimensionMap, Speed.class, SIUnits.KILOMETRE_PER_HOUR, ImperialUnits.MILES_PER_HOUR);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (c) 2010-2024 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.library.dimension;

import javax.measure.Quantity;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* The {@link RadiantExposure} defines the dimension for Radiant Exposure
* https://en.wikipedia.org/wiki/Radiant_exposure
*
* @author Gaël L'hopital - Initial contribution
*/
@NonNullByDefault
public interface RadiantExposure extends Quantity<RadiantExposure> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import javax.measure.spi.SystemOfUnits;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.library.dimension.Density;
import org.openhab.core.library.dimension.VolumetricFlowRate;

import tech.units.indriya.format.SimpleUnitFormat;
Expand All @@ -48,6 +49,8 @@ public final class ImperialUnits extends CustomUnits {

public static final Unit<Mass> POUND = addUnit(new TransformedUnit<>("lb", Units.GRAM,
MultiplyConverter.ofRational(BigInteger.valueOf(45359237), BigInteger.valueOf(100000))));
public static final Unit<Mass> GRAIN = addUnit(
new TransformedUnit<>("gr", Units.GRAM, MultiplyConverter.of(0.06479891)));
/** Additionally defined units to be used in openHAB **/
public static final Unit<Pressure> INCH_OF_MERCURY = addUnit(new TransformedUnit<>("inHg", Units.PASCAL,
MultiplyConverter.ofRational(BigInteger.valueOf(3386388), BigInteger.valueOf(1000))));
Expand Down Expand Up @@ -94,6 +97,8 @@ public final class ImperialUnits extends CustomUnits {
public static final Unit<VolumetricFlowRate> GALLON_PER_MINUTE = addUnit(
new ProductUnit<>(GALLON_LIQUID_US.divide(tech.units.indriya.unit.Units.MINUTE)));

public static final Unit<Density> GRAIN_PER_CUBICFOOT = addUnit(new ProductUnit<>(GRAIN.divide(CUBIC_FOOT)));

/*
* Add unit symbols for imperial units.
*/
Expand All @@ -111,6 +116,7 @@ public final class ImperialUnits extends CustomUnits {
SimpleUnitFormat.getInstance().label(GALLON_LIQUID_US, GALLON_LIQUID_US.getSymbol());
SimpleUnitFormat.getInstance().label(GALLON_PER_MINUTE, "gal/min");
SimpleUnitFormat.getInstance().label(POUND_FORCE_SQUARE_INCH, POUND_FORCE_SQUARE_INCH.getSymbol());
SimpleUnitFormat.getInstance().label(GRAIN, GRAIN.getSymbol());
}

private ImperialUnits() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.openhab.core.library.dimension.ElectricConductivity;
import org.openhab.core.library.dimension.EmissionIntensity;
import org.openhab.core.library.dimension.Intensity;
import org.openhab.core.library.dimension.RadiantExposure;
import org.openhab.core.library.dimension.RadiationSpecificActivity;
import org.openhab.core.library.dimension.VolumetricFlowRate;

Expand Down Expand Up @@ -91,13 +92,15 @@ public final class Units extends CustomUnits {
public static final Unit<AmountOfSubstance> MOLE = addUnit(tech.units.indriya.unit.Units.MOLE);
public static final Unit<Volume> LITRE = addUnit(tech.units.indriya.unit.Units.LITRE);
@SuppressWarnings("unchecked")
public static final Unit<AmountOfSubstance> DEUTSCHE_HAERTE = addUnit(new TransformedUnit<>("°dH",
(Unit<AmountOfSubstance>) MetricPrefix.MILLI(Units.MOLE).divide(Units.LITRE), MultiplyConverter.of(5.6)));
public static final Unit<Dimensionless> DEUTSCHE_HAERTE = addUnit((Unit<Dimensionless>) new TransformedUnit<>("°dH",
MetricPrefix.MILLI(Units.MOLE).divide(Units.LITRE), MultiplyConverter.of(0.17833)));
public static final Unit<Angle> DEGREE_ANGLE = addUnit(NonSI.DEGREE_ANGLE);
public static final Unit<Angle> RADIAN = addUnit(tech.units.indriya.unit.Units.RADIAN);
public static final Unit<ArealDensity> DOBSON_UNIT = addUnit(
new ProductUnit<>(MetricPrefix.MILLI(tech.units.indriya.unit.Units.MOLE).multiply(0.4462)
.divide(tech.units.indriya.unit.Units.SQUARE_METRE)));
public static final Unit<ArealDensity> KILOGRAM_PER_SQUARE_METRE = addUnit(new ProductUnit<>(
tech.units.indriya.unit.Units.KILOGRAM.divide(tech.units.indriya.unit.Units.SQUARE_METRE)));
public static final Unit<CatalyticActivity> KATAL = addUnit(tech.units.indriya.unit.Units.KATAL);
public static final Unit<Density> KILOGRAM_PER_CUBICMETRE = addUnit(new ProductUnit<>(
tech.units.indriya.unit.Units.KILOGRAM.divide(tech.units.indriya.unit.Units.CUBIC_METRE)));
Expand Down Expand Up @@ -187,6 +190,10 @@ public final class Units extends CustomUnits {
new TransformedUnit<>("kn", tech.units.indriya.unit.Units.KILOMETRE_PER_HOUR,
MultiplyConverter.ofRational(BigInteger.valueOf(1852), BigInteger.valueOf(1000))));
public static final Unit<SolidAngle> STERADIAN = addUnit(tech.units.indriya.unit.Units.STERADIAN);
public static final Unit<RadiantExposure> WATT_HOUR_PER_SQUARE_METRE = addUnit(
new ProductUnit<>(WATT_HOUR.divide(tech.units.indriya.unit.Units.SQUARE_METRE)));
public static final Unit<RadiantExposure> JOULE_PER_SQUARE_METRE = addUnit(
new ProductUnit<>(JOULE.divide(tech.units.indriya.unit.Units.SQUARE_METRE)));
public static final Unit<Temperature> KELVIN = addUnit(tech.units.indriya.unit.Units.KELVIN);
public static final Unit<?> MIRED = addUnit(MetricPrefix.MEGA(tech.units.indriya.unit.Units.KELVIN).inverse());
public static final Unit<Time> SECOND = addUnit(tech.units.indriya.unit.Units.SECOND);
Expand Down Expand Up @@ -262,7 +269,7 @@ public final class Units extends CustomUnits {
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(DEUTSCHE_HAERTE, DEUTSCHE_HAERTE.getSymbol());
SimpleUnitFormat.getInstance().label(DOBSON_UNIT, "DU");
SimpleUnitFormat.getInstance().label(GRAM_PER_KILOWATT_HOUR, "g/kWh");
SimpleUnitFormat.getInstance().label(GIGABYTE, "GB");
Expand All @@ -276,6 +283,7 @@ public final class Units extends CustomUnits {
SimpleUnitFormat.getInstance().alias(KIBIBYTE, "kio");
SimpleUnitFormat.getInstance().label(KILOBIT, "kbit");
SimpleUnitFormat.getInstance().label(KILOBIT_PER_SECOND, "kbit/s");
SimpleUnitFormat.getInstance().label(KILOGRAM_PER_SQUARE_METRE, "kg/m²");
SimpleUnitFormat.getInstance().label(KILOVAR, "kvar");
SimpleUnitFormat.getInstance().label(KILOVAR_HOUR, "kvarh");
SimpleUnitFormat.getInstance().label(KILOVOLT_AMPERE, "kVA");
Expand Down Expand Up @@ -315,6 +323,7 @@ public final class Units extends CustomUnits {
SimpleUnitFormat.getInstance().label(VOLT_AMPERE, "VA");
SimpleUnitFormat.getInstance().label(VOLT_AMPERE_HOUR, "VAh");
SimpleUnitFormat.getInstance().label(WATT_HOUR, "Wh");
SimpleUnitFormat.getInstance().label(WATT_HOUR_PER_SQUARE_METRE, "Wh/m²");
SimpleUnitFormat.getInstance().label(WATT_SECOND, "Ws");

// workarounds for https://github.com/unitsofmeasurement/indriya/issues/409
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.number.IsCloseTo.closeTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;

import java.math.BigDecimal;

Expand All @@ -38,6 +38,7 @@
import org.openhab.core.library.dimension.Density;
import org.openhab.core.library.dimension.Intensity;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.types.util.UnitUtils;

import tech.units.indriya.quantity.Quantities;

Expand Down Expand Up @@ -442,6 +443,44 @@ public void testColorTemperatureAliases() {
assertEquals(Units.MIRED, value.getUnit());
}

public void testGrains() {
assertThat(ImperialUnits.GRAIN.getSymbol(), is("gr"));
QuantityType<?> oneHundredGrains = QuantityType.valueOf("100 gr");
QuantityType<?> converted = oneHundredGrains.toUnit("g");
assertThat(converted.doubleValue(), is(closeTo(6.479891, DEFAULT_ERROR)));
assertThat(ImperialUnits.GRAIN_PER_CUBICFOOT.toString(), is("gr/ft³"));
QuantityType<?> grainDensity = QuantityType.valueOf("20 gr/ft³");
QuantityType<?> convertedDensity = grainDensity.toUnit(Units.MICROGRAM_PER_CUBICMETRE);
assertThat(convertedDensity.doubleValue(), is(closeTo(45767038.211314686, DEFAULT_ERROR)));
}

@Test
public void testArealDensity() {
QuantityType<?> newspaper = QuantityType.valueOf("72 g/m²");
QuantityType<?> converted = newspaper.toUnit(Units.KILOGRAM_PER_SQUARE_METRE);
assertEquals(converted.doubleValue(), 0.072);
}

@Test
public void testAmountOfSubstance() {
QuantityType<?> mmolpl = QuantityType.valueOf("0.17833 mmol/l");
String mmolplDimension = UnitUtils.getDimensionName(mmolpl.getUnit());
QuantityType<?> dh = QuantityType.valueOf("1 °dH");
String hDimension = UnitUtils.getDimensionName(dh.getUnit());
assertTrue(hDimension.equals(mmolplDimension));
assertTrue("Dimensionless".equalsIgnoreCase(hDimension));
QuantityType<?> converted = dh.toUnit("mmol/l");
assertThat(converted.doubleValue(), is(closeTo(mmolpl.doubleValue(), DEFAULT_ERROR)));
}

@Test
public void testSolarIrradiation() {
QuantityType<?> whpsm2 = QuantityType.valueOf(10, Units.WATT_HOUR_PER_SQUARE_METRE);
assertThat(whpsm2.getUnit().toString(), is("Wh/m²"));
QuantityType<?> jpsm2 = QuantityType.valueOf(10, Units.JOULE_PER_SQUARE_METRE);
assertThat(jpsm2.getUnit().toString(), is("J/m²"));
}

private static class QuantityEquals extends IsEqual<Quantity<?>> {
private Quantity<?> quantity;

Expand Down

0 comments on commit 241e55f

Please sign in to comment.