javax.measure.quantity.Dimensionless Java Examples

The following examples show how to use javax.measure.quantity.Dimensionless. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GroupItemOSGiTest.java    From openhab-core with Eclipse Public License 2.0 7 votes vote down vote up
@Test
public void assertThatNumberGroupItemWithDifferentDimensionsCalculatesCorrectState() {
    NumberItem baseItem = createNumberItem("baseItem", Temperature.class, UnDefType.NULL);
    GroupFunctionDTO gfDTO = new GroupFunctionDTO();
    gfDTO.name = "sum";
    GroupFunction function = groupFunctionHelper.createGroupFunction(gfDTO, Collections.emptyList(),
            Temperature.class);
    GroupItem groupItem = new GroupItem("number", baseItem, function);
    groupItem.setUnitProvider(unitProvider);
    groupItem.setItemStateConverter(itemStateConverter);

    NumberItem celsius = createNumberItem("C", Temperature.class, new QuantityType<>("23 °C"));
    groupItem.addMember(celsius);
    NumberItem hectoPascal = createNumberItem("F", Pressure.class, new QuantityType<>("1010 hPa"));
    groupItem.addMember(hectoPascal);
    NumberItem percent = createNumberItem("K", Dimensionless.class, new QuantityType<>("110 %"));
    groupItem.addMember(percent);

    QuantityType<?> state = groupItem.getStateAs(QuantityType.class);

    assertThat(state, is(new QuantityType<>("23 °C")));

    groupItem.stateUpdated(celsius, UnDefType.NULL);
    assertThat(groupItem.getState(), is(new QuantityType<>("23 °C")));
}
 
Example #2
Source File: UnitsTest.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Tests getting a unit for a given quantity type.
 */
@Test
public void testGetForQuantity() {
    assertSame("Length",            Units.METRE,             Units.get(Length.class));
    assertSame("Mass",              Units.KILOGRAM,          Units.get(Mass.class));
    assertSame("Time",              Units.SECOND,            Units.get(Time.class));
    assertSame("Temperature",       Units.KELVIN,            Units.get(Temperature.class));
    assertSame("Area",              Units.SQUARE_METRE,      Units.get(Area.class));
    assertSame("Volume",            Units.CUBIC_METRE,       Units.get(Volume.class));
    assertSame("Speed",             Units.METRES_PER_SECOND, Units.get(Speed.class));
    assertSame("LuminousIntensity", Units.CANDELA,           Units.get(LuminousIntensity.class));
    assertSame("LuminousFlux",      Units.LUMEN,             Units.get(LuminousFlux.class));
    assertSame("SolidAngle",        Units.STERADIAN,         Units.get(SolidAngle.class));
    assertSame("Angle",             Units.RADIAN,            Units.get(Angle.class));
    assertSame("Dimensionless",     Units.UNITY,             Units.get(Dimensionless.class));
}
 
Example #3
Source File: DatumShiftGridFileTest.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new grid using an affine transform as a reference.
 * An arbitrary non-uniform scale is applied on axes.
 *
 * @param  rotation  rotation angle in degrees to apply on affine transform.
 */
void init(final double rotation) throws NoninvertibleTransformException {
    reference = AffineTransform.getRotateInstance(StrictMath.toRadians(rotation), WIDTH/2, HEIGHT/2);
    reference.scale(2, 5);
    final DatumShiftGridFile.Float<Dimensionless,Dimensionless> grid = new DatumShiftGridFile.Float<>(
            2, Units.UNITY, Units.UNITY, true, 0, 0, 1, 1, WIDTH, HEIGHT, null);
    assertEquals(2, grid.offsets.length);
    final Point2D.Float point = new Point2D.Float();
    int i = 0;
    for (int y=0; y<HEIGHT; y++) {
        for (int x=0; x<WIDTH; x++) {
            point.x = x;
            point.y = y;
            assertSame(point, reference.transform(point, point));
            grid.offsets[0][i] = point.x - x;
            grid.offsets[1][i] = point.y - y;
            i++;
        }
    }
    assertEquals(grid.offsets[0].length, i);
    assertEquals(grid.offsets[1].length, i);
    this.grid = grid;
}
 
Example #4
Source File: GroupItemTest.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void assertThatNumberGroupItemWithDifferentDimensionsCalculatesCorrectState() {
    NumberItem baseItem = createNumberItem("baseItem", Temperature.class, UnDefType.NULL);
    GroupFunctionDTO gfDTO = new GroupFunctionDTO();
    gfDTO.name = "sum";
    GroupFunction function = groupFunctionHelper.createGroupFunction(gfDTO, Collections.emptyList(),
            Temperature.class);
    GroupItem groupItem = new GroupItem("number", baseItem, function);
    groupItem.setUnitProvider(unitProvider);
    groupItem.setItemStateConverter(itemStateConverter);

    NumberItem celsius = createNumberItem("C", Temperature.class, new QuantityType<Temperature>("23 °C"));
    groupItem.addMember(celsius);
    NumberItem hectoPascal = createNumberItem("F", Pressure.class, new QuantityType<Pressure>("1010 hPa"));
    groupItem.addMember(hectoPascal);
    NumberItem percent = createNumberItem("K", Dimensionless.class, new QuantityType<Dimensionless>("110 %"));
    groupItem.addMember(percent);

    QuantityType<?> state = (QuantityType<?>) groupItem.getStateAs(QuantityType.class);

    assertThat(state, is(new QuantityType<Temperature>("23 °C")));

    groupItem.stateUpdated(celsius, UnDefType.NULL);
    assertThat(groupItem.getState(), is(new QuantityType<Temperature>("23 °C")));
}
 
Example #5
Source File: QuantityTypeTest.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testDimensionless() {
    // Dimensionless value that works
    new QuantityType<Dimensionless>("57%");

    QuantityType<Dimensionless> dt0 = new QuantityType<>("12");
    assertTrue(dt0.getUnit().getDimension() == QuantityDimension.NONE);
    dt0 = new QuantityType<>("2rad");
    assertTrue(dt0.getUnit().getDimension() == QuantityDimension.NONE);

}
 
Example #6
Source File: DatumShiftGridCompressedTest.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new grid using an affine transform as a reference.
 *
 * @param  rotation  ignored.
 */
@Override
void init(final double rotation) throws NoninvertibleTransformException {
    super.init(0);      // No rotation in order to have integer values.
    grid = DatumShiftGridCompressed.compress((DatumShiftGridFile.Float<Dimensionless,Dimensionless>) grid, null, 0.5);
    assertInstanceOf("grid", DatumShiftGridCompressed.class, grid);
}
 
Example #7
Source File: Util.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * calculates the dewpoint in °C from measured values
 *
 * @param temperature the measured temperature
 * @param relativeHumidity the measured relative humidity
 * @return the corresponding dewpoint
 */
public static State calculateDewpoint(QuantityType<Temperature> temperature,
        QuantityType<Dimensionless> relativeHumidity) {
    Double theta = temperature.toUnit(SIUnits.CELSIUS).doubleValue();
    Double rH = relativeHumidity.doubleValue() / 100;
    // dewpoint in °C
    Double dP = 243.12 * (((17.62 * theta) / (243.12 + theta) + Math.log(rH))
            / (((17.62 * 243.12) / (243.12 + theta) - Math.log(rH))));
    State dewPoint = new QuantityType<Temperature>(dP, SIUnits.CELSIUS);
    return dewPoint;
}
 
Example #8
Source File: Util.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * calculate absolute humidity in g/m³ from measured values
 *
 * @param temperature the measured temperature
 * @param relativeHumidity the measured relative humidity
 * @return the corresponding absolute humidity
 */
public static State calculateAbsoluteHumidity(QuantityType<Temperature> temperature,
        QuantityType<Dimensionless> relativeHumidity) {
    Double theta = temperature.toUnit(SIUnits.CELSIUS).doubleValue();
    // saturation vapor pressure in kg/(m s^2)
    Double saturationVaporPressure = 611.2 * Math.exp(17.62 * theta / (243.12 + theta));
    // absolute humidity in kg/m^3
    Double aH = relativeHumidity.doubleValue() / 100 * saturationVaporPressure / (461.52 * (273.15 + theta));
    State absoluteHumidity = new QuantityType<Density>(aH, SmartHomeUnits.KILOGRAM_PER_CUBICMETRE).toUnit("g/m³");
    if (absoluteHumidity != null) {
        return absoluteHumidity;
    } else {
        throw new IllegalArgumentException("could not change unit");
    }
}
 
Example #9
Source File: DS1923.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void refresh(OwBaseBridgeHandler bridgeHandler, Boolean forcedRefresh) throws OwException {
    if (isConfigured) {
        if (enabledChannels.contains(CHANNEL_TEMPERATURE) || enabledChannels.contains(CHANNEL_HUMIDITY)
                || enabledChannels.contains(CHANNEL_ABSOLUTE_HUMIDITY)
                || enabledChannels.contains(CHANNEL_DEWPOINT)) {
            QuantityType<Temperature> temperature = new QuantityType<Temperature>(
                    (DecimalType) bridgeHandler.readDecimalType(sensorId, temperatureParameter), SIUnits.CELSIUS);
            logger.trace("read temperature {} from {}", temperature, sensorId);
            callback.postUpdate(CHANNEL_TEMPERATURE, temperature);

            if (enabledChannels.contains(CHANNEL_HUMIDITY) || enabledChannels.contains(CHANNEL_ABSOLUTE_HUMIDITY)
                    || enabledChannels.contains(CHANNEL_DEWPOINT)) {
                QuantityType<Dimensionless> humidity = new QuantityType<Dimensionless>(
                        (DecimalType) bridgeHandler.readDecimalType(sensorId, humidityParameterR),
                        SmartHomeUnits.PERCENT);
                logger.trace("read humidity {} from {}", humidity, sensorId);

                if (enabledChannels.contains(CHANNEL_HUMIDITY)) {
                    callback.postUpdate(CHANNEL_HUMIDITY, humidity);
                }

                if (enabledChannels.contains(CHANNEL_ABSOLUTE_HUMIDITY)) {
                    callback.postUpdate(CHANNEL_ABSOLUTE_HUMIDITY,
                            Util.calculateAbsoluteHumidity(temperature, humidity));
                }

                if (enabledChannels.contains(CHANNEL_DEWPOINT)) {
                    callback.postUpdate(CHANNEL_DEWPOINT, Util.calculateDewpoint(temperature, humidity));
                }
            }
        }
    }
}
 
Example #10
Source File: UtilTest.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void dewPointTest() {
    QuantityType<Temperature> temperature = new QuantityType<>("20 °C");
    QuantityType<Dimensionless> relativeHumidity = new QuantityType<>("75%");

    QuantityType<Temperature> dewPoint = (QuantityType<Temperature>) Util.calculateDewpoint(temperature,
            relativeHumidity);
    assertEquals(15.43, dewPoint.doubleValue(), 0.01);
}
 
Example #11
Source File: UtilTest.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void convertAbsoluteHumidityTest() {
    QuantityType<Temperature> temperature = new QuantityType<>("20 °C");
    QuantityType<Dimensionless> relativeHumidity = new QuantityType<>("75%");

    QuantityType<Density> absoluteHumidity = (QuantityType<Density>) Util.calculateAbsoluteHumidity(temperature,
            relativeHumidity);
    assertEquals(12.93, absoluteHumidity.doubleValue(), 0.01);

}
 
Example #12
Source File: BlukiiHandler.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void onScanRecordReceived(BluetoothScanNotification scanNotification) {
    final byte[] manufacturerData = scanNotification.getManufacturerData();
    if (manufacturerData != null) {
        final BlukiiData blukiiData = decoder.decode(manufacturerData);
        if (blukiiData != null) {
            updateState(BlukiiBindingConstants.CHANNEL_ID_BATTERY, new DecimalType(blukiiData.battery));
            blukiiData.environment.ifPresent(environment -> {
                updateState(BlukiiBindingConstants.CHANNEL_ID_TEMPERATURE,
                        new QuantityType<Temperature>(environment.temperature, SIUnits.CELSIUS));
                updateState(BlukiiBindingConstants.CHANNEL_ID_HUMIDITY,
                        new QuantityType<Dimensionless>(environment.humidity, SmartHomeUnits.PERCENT));
                updateState(BlukiiBindingConstants.CHANNEL_ID_PRESSURE,
                        new QuantityType<Pressure>(environment.pressure, MetricPrefix.HECTO(SIUnits.PASCAL)));
                updateState(BlukiiBindingConstants.CHANNEL_ID_LUMINANCE,
                        new QuantityType<Illuminance>(environment.luminance, SmartHomeUnits.LUX));
            });
            blukiiData.accelerometer.ifPresent(accelerometer -> {
                updateState(BlukiiBindingConstants.CHANNEL_ID_TILTX,
                        new QuantityType<Angle>(accelerometer.tiltX, SmartHomeUnits.DEGREE_ANGLE));
                updateState(BlukiiBindingConstants.CHANNEL_ID_TILTY,
                        new QuantityType<Angle>(accelerometer.tiltY, SmartHomeUnits.DEGREE_ANGLE));
                updateState(BlukiiBindingConstants.CHANNEL_ID_TILTZ,
                        new QuantityType<Angle>(accelerometer.tiltZ, SmartHomeUnits.DEGREE_ANGLE));
            });
            blukiiData.magnetometer.ifPresent(magnetometer -> {
                // It isn't easy to get a heading from these values without any calibration, so we ignore those
                // right
                // now.
            });
        }
    }
    super.onScanRecordReceived(scanNotification);
}
 
Example #13
Source File: ScaleTransformServiceTest.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testTransformQuantityType() throws TransformationException {
    QuantityType<Dimensionless> airQuality = new QuantityType<>("992 ppm");
    String aqScaleFile = "scale/netatmo_aq.scale";
    String expected = "Correcte";

    String transformedResponse = processor.transform(aqScaleFile, airQuality.toString());
    Assert.assertEquals(expected, transformedResponse);
}
 
Example #14
Source File: I18nProviderImpl.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
private void initDimensionMap() {
    Map<SystemOfUnits, Unit<? extends Quantity<?>>> temperatureMap = new HashMap<>();
    temperatureMap.put(SIUnits.getInstance(), SIUnits.CELSIUS);
    temperatureMap.put(ImperialUnits.getInstance(), ImperialUnits.FAHRENHEIT);
    dimensionMap.put(Temperature.class, temperatureMap);

    Map<SystemOfUnits, Unit<? extends Quantity<?>>> pressureMap = new HashMap<>();
    pressureMap.put(SIUnits.getInstance(), HECTO(SIUnits.PASCAL));
    pressureMap.put(ImperialUnits.getInstance(), ImperialUnits.INCH_OF_MERCURY);
    dimensionMap.put(Pressure.class, pressureMap);

    Map<SystemOfUnits, Unit<? extends Quantity<?>>> speedMap = new HashMap<>();
    speedMap.put(SIUnits.getInstance(), SIUnits.KILOMETRE_PER_HOUR);
    speedMap.put(ImperialUnits.getInstance(), ImperialUnits.MILES_PER_HOUR);
    dimensionMap.put(Speed.class, speedMap);

    Map<SystemOfUnits, Unit<? extends Quantity<?>>> lengthMap = new HashMap<>();
    lengthMap.put(SIUnits.getInstance(), SIUnits.METRE);
    lengthMap.put(ImperialUnits.getInstance(), ImperialUnits.INCH);
    dimensionMap.put(Length.class, lengthMap);

    Map<SystemOfUnits, Unit<? extends Quantity<?>>> intensityMap = new HashMap<>();
    intensityMap.put(SIUnits.getInstance(), SmartHomeUnits.IRRADIANCE);
    intensityMap.put(ImperialUnits.getInstance(), SmartHomeUnits.IRRADIANCE);
    dimensionMap.put(Intensity.class, intensityMap);

    Map<SystemOfUnits, Unit<? extends Quantity<?>>> percentMap = new HashMap<>();
    percentMap.put(SIUnits.getInstance(), SmartHomeUnits.ONE);
    percentMap.put(ImperialUnits.getInstance(), SmartHomeUnits.ONE);
    dimensionMap.put(Dimensionless.class, percentMap);

    Map<SystemOfUnits, Unit<? extends Quantity<?>>> angleMap = new HashMap<>();
    angleMap.put(SIUnits.getInstance(), SmartHomeUnits.DEGREE_ANGLE);
    angleMap.put(ImperialUnits.getInstance(), SmartHomeUnits.DEGREE_ANGLE);
    dimensionMap.put(Angle.class, angleMap);
}
 
Example #15
Source File: Dimension.java    From microMathematics with GNU General Public License v3.0 5 votes vote down vote up
public Dimension getDimension(BaseUnit<?> unit) {
    if (unit.equals(SI.METRE)) return Dimension.LENGTH;
    if (unit.equals(SI.KILOGRAM)) return Dimension.MASS;
    if (unit.equals(SI.KELVIN)) return Dimension.TEMPERATURE;
    if (unit.equals(SI.SECOND)) return Dimension.TIME;
    if (unit.equals(SI.AMPERE)) return Dimension.ELECTRIC_CURRENT;
    if (unit.equals(SI.MOLE)) return Dimension.AMOUNT_OF_SUBSTANCE;
    if (unit.equals(SI.CANDELA)) return SI.WATT.getDimension();
    return new Dimension(new BaseUnit<Dimensionless>("[" + unit.getSymbol() + "]"));
}
 
Example #16
Source File: I18nProviderImpl.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
private void initDimensionMap() {
    Map<SystemOfUnits, Unit<? extends Quantity<?>>> temperatureMap = new HashMap<>();
    temperatureMap.put(SIUnits.getInstance(), SIUnits.CELSIUS);
    temperatureMap.put(ImperialUnits.getInstance(), ImperialUnits.FAHRENHEIT);
    dimensionMap.put(Temperature.class, temperatureMap);

    Map<SystemOfUnits, Unit<? extends Quantity<?>>> pressureMap = new HashMap<>();
    pressureMap.put(SIUnits.getInstance(), HECTO(SIUnits.PASCAL));
    pressureMap.put(ImperialUnits.getInstance(), ImperialUnits.INCH_OF_MERCURY);
    dimensionMap.put(Pressure.class, pressureMap);

    Map<SystemOfUnits, Unit<? extends Quantity<?>>> speedMap = new HashMap<>();
    speedMap.put(SIUnits.getInstance(), SIUnits.KILOMETRE_PER_HOUR);
    speedMap.put(ImperialUnits.getInstance(), ImperialUnits.MILES_PER_HOUR);
    dimensionMap.put(Speed.class, speedMap);

    Map<SystemOfUnits, Unit<? extends Quantity<?>>> lengthMap = new HashMap<>();
    lengthMap.put(SIUnits.getInstance(), SIUnits.METRE);
    lengthMap.put(ImperialUnits.getInstance(), ImperialUnits.INCH);
    dimensionMap.put(Length.class, lengthMap);

    Map<SystemOfUnits, Unit<? extends Quantity<?>>> intensityMap = new HashMap<>();
    intensityMap.put(SIUnits.getInstance(), SmartHomeUnits.IRRADIANCE);
    intensityMap.put(ImperialUnits.getInstance(), SmartHomeUnits.IRRADIANCE);
    dimensionMap.put(Intensity.class, intensityMap);

    Map<SystemOfUnits, Unit<? extends Quantity<?>>> percentMap = new HashMap<>();
    percentMap.put(SIUnits.getInstance(), SmartHomeUnits.ONE);
    percentMap.put(ImperialUnits.getInstance(), SmartHomeUnits.ONE);
    dimensionMap.put(Dimensionless.class, percentMap);

    Map<SystemOfUnits, Unit<? extends Quantity<?>>> angleMap = new HashMap<>();
    angleMap.put(SIUnits.getInstance(), SmartHomeUnits.DEGREE_ANGLE);
    angleMap.put(ImperialUnits.getInstance(), SmartHomeUnits.DEGREE_ANGLE);
    dimensionMap.put(Angle.class, angleMap);
}
 
Example #17
Source File: SmartHomeUnitsTest.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void test_one_UnitSymbol() {
    assertThat(SmartHomeUnits.ONE.getSymbol(), is(""));

    Quantity<Dimensionless> one1 = Quantities.getQuantity(BigDecimal.ONE, SmartHomeUnits.ONE);
    Quantity<Dimensionless> one2 = Quantities.getQuantity(BigDecimal.ONE, SmartHomeUnits.ONE);

    assertThat(one1.add(one2).toString(), is("2 one"));
}
 
Example #18
Source File: QuantityTypeTest.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testDimensionless() {
    // Dimensionless value that works
    new QuantityType<>("57%");

    QuantityType<Dimensionless> dt0 = new QuantityType<>("12");
    assertTrue(dt0.getUnit().getDimension() == QuantityDimension.NONE);
    dt0 = new QuantityType<>("2rad");
    assertTrue(dt0.getUnit().getDimension() == QuantityDimension.NONE);
}
 
Example #19
Source File: UnitsTest.java    From uom-systems with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test method for {@link javax.measure.Unit#asType(java.lang.Class)}.
 */
@Test
public void testAsTypeFails() {
	assertThrows(ClassCastException.class, () -> {
		METER.asType(Dimensionless.class);
	});
}
 
Example #20
Source File: QuantityTypeArithmeticGroupFunctionTest.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testMaxFunctionQuantityTypeOnDimensionless() {
    items.add(createNumberItem("TestItem1", Dimensionless.class, new QuantityType<>("48 %")));
    items.add(createNumberItem("TestItem2", Dimensionless.class, new QuantityType<>("36 %")));
    items.add(createNumberItem("TestItem3", Dimensionless.class, new QuantityType<>("0 %")));
    items.add(createNumberItem("TestItem4", Dimensionless.class, new QuantityType<>("48 %")));
    items.add(createNumberItem("TestItem5", Dimensionless.class, new QuantityType<>("0 %")));
    items.add(createNumberItem("TestItem6", Dimensionless.class, new QuantityType<>("0 %")));

    function = new QuantityTypeArithmeticGroupFunction.Max(Dimensionless.class);
    State state = function.calculate(items);

    assertEquals(new QuantityType<>("48 %"), state);
}
 
Example #21
Source File: SmartHomeUnitsTest.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testOneUnitSymbol() {
    assertThat(SmartHomeUnits.ONE.getSymbol(), is(""));

    Quantity<Dimensionless> one1 = Quantities.getQuantity(BigDecimal.ONE, SmartHomeUnits.ONE);
    Quantity<Dimensionless> one2 = Quantities.getQuantity(BigDecimal.ONE, SmartHomeUnits.ONE);

    assertThat(one1.add(one2).toString(), is("2 one"));
}
 
Example #22
Source File: EDS006x.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void refresh(OwBaseBridgeHandler bridgeHandler, Boolean forcedRefresh) throws OwException {
    if (isConfigured) {
        if (enabledChannels.contains(CHANNEL_TEMPERATURE) || enabledChannels.contains(CHANNEL_HUMIDITY)
                || enabledChannels.contains(CHANNEL_ABSOLUTE_HUMIDITY)
                || enabledChannels.contains(CHANNEL_DEWPOINT)) {
            QuantityType<Temperature> temperature = new QuantityType<>(
                    (DecimalType) bridgeHandler.readDecimalType(sensorId, temperatureParameter), SIUnits.CELSIUS);
            logger.trace("read temperature {} from {}", temperature, sensorId);

            if (enabledChannels.contains(CHANNEL_TEMPERATURE)) {
                callback.postUpdate(CHANNEL_TEMPERATURE, temperature);
            }

            if (enabledChannels.contains(CHANNEL_HUMIDITY) || enabledChannels.contains(CHANNEL_ABSOLUTE_HUMIDITY)
                    || enabledChannels.contains(CHANNEL_DEWPOINT)) {
                QuantityType<Dimensionless> humidity = new QuantityType<>(
                        (DecimalType) bridgeHandler.readDecimalType(sensorId, humidityParameter),
                        SmartHomeUnits.PERCENT);
                logger.trace("read humidity {} from {}", humidity, sensorId);

                if (enabledChannels.contains(CHANNEL_HUMIDITY)) {
                    callback.postUpdate(CHANNEL_HUMIDITY, humidity);
                }

                if (enabledChannels.contains(CHANNEL_ABSOLUTE_HUMIDITY)) {
                    callback.postUpdate(CHANNEL_ABSOLUTE_HUMIDITY,
                            Util.calculateAbsoluteHumidity(temperature, humidity));
                }

                if (enabledChannels.contains(CHANNEL_DEWPOINT)) {
                    callback.postUpdate(CHANNEL_DEWPOINT, Util.calculateDewpoint(temperature, humidity));
                }
            }
        }

        if (enabledChannels.contains(CHANNEL_LIGHT)) {
            QuantityType<Illuminance> light = new QuantityType<>(
                    (DecimalType) bridgeHandler.readDecimalType(sensorId, lightParameter), SmartHomeUnits.LUX);
            logger.trace("read light {} from {}", light, sensorId);
            callback.postUpdate(CHANNEL_LIGHT, light);
        }

        if (enabledChannels.contains(CHANNEL_PRESSURE)) {
            QuantityType<Pressure> pressure = new QuantityType<>(
                    (DecimalType) bridgeHandler.readDecimalType(sensorId, pressureParameter),
                    MetricPrefix.HECTO(SIUnits.PASCAL));
            logger.trace("read pressure {} from {}", pressure, sensorId);
            callback.postUpdate(CHANNEL_PRESSURE, pressure);
        }
    }
}
 
Example #23
Source File: MoonPhase.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Returns the age in percent.
 */
public QuantityType<Dimensionless> getAgePercent() {
    return new QuantityType<Dimensionless>(agePercent, SmartHomeUnits.PERCENT);
}
 
Example #24
Source File: MoonPhase.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Returns the illumination.
 */
public QuantityType<Dimensionless> getIllumination() {
    return new QuantityType<Dimensionless>(illumination, SmartHomeUnits.PERCENT);
}
 
Example #25
Source File: SmartHomeUnitsTest.java    From openhab-core with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testPpm() {
    QuantityType<Dimensionless> ppm = new QuantityType<>("500 ppm");
    assertEquals("0.05 %", ppm.toUnit(Units.PERCENT).toString());
}
 
Example #26
Source File: SmartHomeUnitsTest.java    From openhab-core with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testDb() {
    QuantityType<Dimensionless> ratio = new QuantityType<>("100");
    assertEquals("20.0 dB", ratio.toUnit("dB").toString());
}
 
Example #27
Source File: SmartHomeUnitsTest.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testDb() {
    QuantityType<Dimensionless> ratio = new QuantityType<>("100");
    assertEquals("20.0 dB", ratio.toUnit("dB").toString());
}
 
Example #28
Source File: ConvertToBindingTest.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
@Test(expected = ConverterException.class)
public void testDecimalTypeConverterFailsToConvertQuantityType() throws ConverterException {

    DecimalTypeConverter converter = new DecimalTypeConverter();
    converter.convertToBinding(new QuantityType<Dimensionless>("99.9 %"), floatDp);
}
 
Example #29
Source File: NumberExtensions.java    From openhab-core with Eclipse Public License 2.0 4 votes vote down vote up
public static QuantityType<?> operator_divide(Number x, QuantityType<?> y) {
    QuantityType<Dimensionless> xQuantity = new QuantityType<>(x, SmartHomeUnits.ONE);
    return operator_divide(xQuantity, y);
}
 
Example #30
Source File: UnitsTest.java    From uom-systems with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Test method for {@link javax.measure.Unit#asType(java.lang.Class)}.
 */
@Test
public void testAsTypeValid() {
	one.asType(Dimensionless.class);
}