Search in sources :

Example 6 with QuantityType

use of org.eclipse.smarthome.core.library.types.QuantityType in project smarthome by eclipse.

the class GroupItemTest method assertThatNumberGroupItemWithDifferentDimensionsCalculatesCorrectState.

@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")));
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) ArithmeticGroupFunction(org.eclipse.smarthome.core.library.types.ArithmeticGroupFunction) Temperature(javax.measure.quantity.Temperature) Dimensionless(javax.measure.quantity.Dimensionless) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) GroupFunctionDTO(org.eclipse.smarthome.core.items.dto.GroupFunctionDTO) Pressure(javax.measure.quantity.Pressure) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 7 with QuantityType

use of org.eclipse.smarthome.core.library.types.QuantityType in project smarthome by eclipse.

the class StateUtil method getAllStates.

public static List<State> getAllStates() {
    LinkedList<State> states = new LinkedList<>();
    DateTimeType dateTime = new DateTimeType();
    states.add(dateTime);
    DecimalType decimal = new DecimalType(23);
    states.add(decimal);
    PercentType percent = new PercentType(50);
    states.add(percent);
    HSBType hsb = new HSBType("50,75,42");
    states.add(hsb);
    states.add(OnOffType.ON);
    states.add(OnOffType.OFF);
    states.add(OpenClosedType.OPEN);
    states.add(OpenClosedType.CLOSED);
    states.add(PlayPauseType.PLAY);
    states.add(PlayPauseType.PAUSE);
    PointType point = new PointType("42.23,23.5");
    states.add(point);
    RawType raw = new RawType(new byte[0], "application/octet-stream");
    states.add(raw);
    states.add(RewindFastforwardType.REWIND);
    states.add(RewindFastforwardType.FASTFORWARD);
    StringListType stringList = new StringListType(new String[] { "foo", "bar" });
    states.add(stringList);
    StringType string = new StringType("foo");
    states.add(string);
    states.add(UnDefType.NULL);
    states.add(UnDefType.UNDEF);
    states.add(UpDownType.UP);
    states.add(UpDownType.DOWN);
    QuantityType<Temperature> quantityType = new QuantityType<Temperature>("12 °C");
    states.add(quantityType);
    return states;
}
Also used : Temperature(javax.measure.quantity.Temperature) StringType(org.eclipse.smarthome.core.library.types.StringType) PercentType(org.eclipse.smarthome.core.library.types.PercentType) LinkedList(java.util.LinkedList) DateTimeType(org.eclipse.smarthome.core.library.types.DateTimeType) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) State(org.eclipse.smarthome.core.types.State) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) PointType(org.eclipse.smarthome.core.library.types.PointType) RawType(org.eclipse.smarthome.core.library.types.RawType) HSBType(org.eclipse.smarthome.core.library.types.HSBType) StringListType(org.eclipse.smarthome.core.library.types.StringListType)

Example 8 with QuantityType

use of org.eclipse.smarthome.core.library.types.QuantityType in project smarthome by eclipse.

the class ItemStateConverterImpl method convertToAcceptedState.

@Override
@NonNull
public State convertToAcceptedState(@Nullable State state, @Nullable Item item) {
    if (state == null) {
        logger.error("A conversion of null was requested:", new IllegalArgumentException("State must not be null."));
        return UnDefType.NULL;
    }
    if (item != null && !isAccepted(item, state)) {
        for (Class<? extends State> acceptedType : item.getAcceptedDataTypes()) {
            State convertedState = state.as(acceptedType);
            if (convertedState != null) {
                logger.debug("Converting {} '{}' to {} '{}' for item '{}'", state.getClass().getSimpleName(), state, convertedState.getClass().getSimpleName(), convertedState, item.getName());
                return convertedState;
            }
        }
    }
    if (item instanceof NumberItem && state instanceof QuantityType) {
        NumberItem numberItem = (NumberItem) item;
        if (numberItem.getDimension() != null) {
            QuantityType<?> quantityState = (QuantityType<?>) state;
            // in case the item does define a unit it takes precedence over all other conversions:
            Unit<?> itemUnit = parseItemUnit(numberItem);
            if (itemUnit != null) {
                if (!itemUnit.equals(quantityState.getUnit())) {
                    return convertOrUndef(quantityState, itemUnit);
                }
                return quantityState;
            }
            Class<? extends Quantity<?>> dimension = numberItem.getDimension();
            @SuppressWarnings({ "unchecked", "rawtypes" }) Unit<? extends Quantity<?>> conversionUnit = unitProvider.getUnit((Class<Quantity>) dimension);
            if (conversionUnit != null && UnitUtils.isDifferentMeasurementSystem(conversionUnit, quantityState.getUnit())) {
                return convertOrUndef(quantityState, conversionUnit);
            }
            return state;
        } else {
            return state.as(DecimalType.class);
        }
    }
    return state;
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) State(org.eclipse.smarthome.core.types.State) Quantity(javax.measure.Quantity) NonNull(org.eclipse.jdt.annotation.NonNull)

Example 9 with QuantityType

use of org.eclipse.smarthome.core.library.types.QuantityType in project smarthome by eclipse.

the class ItemStateConverterImplTest method numberItemWitDimensionShouldConvertToLocaleBasedUnit.

@Test
public void numberItemWitDimensionShouldConvertToLocaleBasedUnit() {
    NumberItem item = mock(NumberItem.class);
    doReturn(Temperature.class).when(item).getDimension();
    UnitProvider unitProvider = mock(UnitProvider.class);
    when(unitProvider.getUnit(Temperature.class)).thenReturn(ImperialUnits.FAHRENHEIT);
    itemStateConverter.setUnitProvider(unitProvider);
    State originalState = new QuantityType<>("12.34 °C");
    State convertedState = itemStateConverter.convertToAcceptedState(originalState, item);
    assertThat(convertedState, is(new QuantityType<>("54.212 °F")));
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) UnitProvider(org.eclipse.smarthome.core.i18n.UnitProvider) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) State(org.eclipse.smarthome.core.types.State) Test(org.junit.Test)

Example 10 with QuantityType

use of org.eclipse.smarthome.core.library.types.QuantityType in project smarthome by eclipse.

the class ItemStateConverterImplTest method numberItemWithoutDimensionShouldConvertToDecimalType.

@Test
public void numberItemWithoutDimensionShouldConvertToDecimalType() {
    Item item = new NumberItem("number");
    State originalState = new QuantityType<>("12.34 °C");
    State convertedState = itemStateConverter.convertToAcceptedState(originalState, item);
    assertThat(convertedState, is(new DecimalType("12.34")));
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) Item(org.eclipse.smarthome.core.items.Item) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) State(org.eclipse.smarthome.core.types.State) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) Test(org.junit.Test)

Aggregations

QuantityType (org.eclipse.smarthome.core.library.types.QuantityType)15 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)12 State (org.eclipse.smarthome.core.types.State)12 Item (org.eclipse.smarthome.core.items.Item)6 Test (org.junit.Test)6 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)5 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)4 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)4 Mapping (org.eclipse.smarthome.model.sitemap.Mapping)4 Temperature (javax.measure.quantity.Temperature)3 GroupItem (org.eclipse.smarthome.core.items.GroupItem)3 PercentType (org.eclipse.smarthome.core.library.types.PercentType)3 Switch (org.eclipse.smarthome.model.sitemap.Switch)3 UnitProvider (org.eclipse.smarthome.core.i18n.UnitProvider)2 GroupFunctionDTO (org.eclipse.smarthome.core.items.dto.GroupFunctionDTO)2 ArithmeticGroupFunction (org.eclipse.smarthome.core.library.types.ArithmeticGroupFunction)2 DateTimeType (org.eclipse.smarthome.core.library.types.DateTimeType)2 OnOffType (org.eclipse.smarthome.core.library.types.OnOffType)2 StringType (org.eclipse.smarthome.core.library.types.StringType)2 StateDescription (org.eclipse.smarthome.core.types.StateDescription)2