Search in sources :

Example 11 with NumberItem

use of org.eclipse.smarthome.core.library.items.NumberItem 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 12 with NumberItem

use of org.eclipse.smarthome.core.library.items.NumberItem in project smarthome by eclipse.

the class ScriptEngineOSGiTest method testDivideItemState_QuantityType.

@SuppressWarnings("null")
@Test
public void testDivideItemState_QuantityType() throws ScriptParsingException, ScriptExecutionException {
    Item numberItem = itemRegistry.get(NUMBER_ITEM_LENGTH);
    ((NumberItem) numberItem).setState(new QuantityType<>("1 m"));
    assertThat((QuantityType<?>) runScript("val length = NumberC.state as QuantityType; return length / 2 [cm];"), is(QuantityType.valueOf("50")));
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) Item(org.eclipse.smarthome.core.items.Item) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 13 with NumberItem

use of org.eclipse.smarthome.core.library.items.NumberItem in project smarthome by eclipse.

the class ScriptEngineOSGiTest method setup.

@Before
public void setup() {
    registerVolatileStorageService();
    EventPublisher eventPublisher = event -> {
    };
    registerService(eventPublisher);
    itemRegistry = getService(ItemRegistry.class);
    assertNotNull(itemRegistry);
    itemProvider = new ItemProvider() {

        @Override
        public void addProviderChangeListener(ProviderChangeListener<Item> listener) {
        }

        @Override
        public Collection<Item> getAll() {
            return Lists.newArrayList(new SwitchItem(ITEM_NAME), createNumberItem(NUMBER_ITEM_TEMPERATURE, Temperature.class), createNumberItem(NUMBER_ITEM_LENGTH, Length.class), new NumberItem(NUMBER_ITEM_DECIMAL));
        }

        @Override
        public void removeProviderChangeListener(ProviderChangeListener<Item> listener) {
        }
    };
    registerService(itemProvider);
    ScriptServiceUtil scriptServiceUtil = getService(ScriptServiceUtil.class);
    assertNotNull(scriptServiceUtil);
    scriptEngine = ScriptServiceUtil.getScriptEngine();
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) ProviderChangeListener(org.eclipse.smarthome.core.common.registry.ProviderChangeListener) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) Collection(java.util.Collection) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) ItemProvider(org.eclipse.smarthome.core.items.ItemProvider) Test(org.junit.Test) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) Item(org.eclipse.smarthome.core.items.Item) ScriptServiceUtil(org.eclipse.smarthome.model.script.ScriptServiceUtil) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) Lists(com.google.common.collect.Lists) Temperature(javax.measure.quantity.Temperature) Length(javax.measure.quantity.Length) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) After(org.junit.After) Assert(org.junit.Assert) State(org.eclipse.smarthome.core.types.State) NonNull(org.eclipse.jdt.annotation.NonNull) Before(org.junit.Before) ItemProvider(org.eclipse.smarthome.core.items.ItemProvider) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) Item(org.eclipse.smarthome.core.items.Item) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ScriptServiceUtil(org.eclipse.smarthome.model.script.ScriptServiceUtil) Collection(java.util.Collection) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Before(org.junit.Before)

Example 14 with NumberItem

use of org.eclipse.smarthome.core.library.items.NumberItem in project smarthome by eclipse.

the class ScriptEngineOSGiTest method testLessThanWithItemState.

@SuppressWarnings("null")
@Test
public void testLessThanWithItemState() throws ScriptExecutionException, ScriptParsingException {
    Item numberItem = itemRegistry.get(NUMBER_ITEM_TEMPERATURE);
    ((NumberItem) numberItem).setState(new QuantityType<>("20 °F"));
    assertTrue(runScript("NumberA.state < 20 [°C]"));
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) Item(org.eclipse.smarthome.core.items.Item) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 15 with NumberItem

use of org.eclipse.smarthome.core.library.items.NumberItem in project smarthome by eclipse.

the class ChannelStateDescriptionProviderOSGiTest method setup.

@Before
public void setup() {
    initMocks(this);
    Mockito.when(componentContext.getBundleContext()).thenReturn(bundleContext);
    registerVolatileStorageService();
    itemRegistry = getService(ItemRegistry.class);
    assertNotNull(itemRegistry);
    final TestThingHandlerFactory thingHandlerFactory = new TestThingHandlerFactory();
    thingHandlerFactory.activate(componentContext);
    registerService(thingHandlerFactory, ThingHandlerFactory.class.getName());
    final StateDescription state = new StateDescription(BigDecimal.ZERO, BigDecimal.valueOf(100), BigDecimal.TEN, "%d Peek", true, Collections.singletonList(new StateOption("SOUND", "My great sound.")));
    final StateDescription state2 = new StateDescription(BigDecimal.ZERO, BigDecimal.valueOf(256), BigDecimal.valueOf(8), null, false, null);
    final ChannelType channelType = new ChannelType(new ChannelTypeUID("hue:alarm"), false, "Number", " ", "", null, null, state, null);
    final ChannelType channelType2 = new ChannelType(new ChannelTypeUID("hue:num"), false, "Number", " ", "", null, null, state2, null);
    final ChannelType channelType3 = new ChannelType(new ChannelTypeUID("hue:info"), true, "String", " ", "", null, null, null, null);
    final ChannelType channelType4 = new ChannelType(new ChannelTypeUID("hue:color"), false, "Color", "Color", "", "ColorLight", null, null, null);
    final ChannelType channelType5 = new ChannelType(new ChannelTypeUID("hue:brightness"), false, "Dimmer", "Brightness", "", "DimmableLight", null, null, null);
    final ChannelType channelType6 = new ChannelType(new ChannelTypeUID("hue:switch"), false, "Switch", "Switch", "", "Light", null, null, null);
    final ChannelType channelType7 = new ChannelType(new ChannelTypeUID("hue:num-dynamic"), false, "Number", " ", "", "Light", null, state, null);
    List<ChannelType> channelTypes = new ArrayList<>();
    channelTypes.add(channelType);
    channelTypes.add(channelType2);
    channelTypes.add(channelType3);
    channelTypes.add(channelType4);
    channelTypes.add(channelType5);
    channelTypes.add(channelType6);
    channelTypes.add(channelType7);
    registerService(new ChannelTypeProvider() {

        @Override
        public Collection<ChannelType> getChannelTypes(Locale locale) {
            return channelTypes;
        }

        @Override
        public ChannelType getChannelType(ChannelTypeUID channelTypeUID, Locale locale) {
            for (final ChannelType channelType : channelTypes) {
                if (channelType.getUID().equals(channelTypeUID)) {
                    return channelType;
                }
            }
            return null;
        }

        @Override
        public ChannelGroupType getChannelGroupType(ChannelGroupTypeUID channelGroupTypeUID, Locale locale) {
            return null;
        }

        @Override
        public Collection<ChannelGroupType> getChannelGroupTypes(Locale locale) {
            return Collections.emptySet();
        }
    });
    registerService(new DynamicStateDescriptionProvider() {

        final StateDescription newState = new StateDescription(BigDecimal.valueOf(10), BigDecimal.valueOf(100), BigDecimal.valueOf(5), "VALUE %d", false, Arrays.asList(new StateOption("value0", "label0"), new StateOption("value1", "label1")));

        @Override
        @Nullable
        public StateDescription getStateDescription(@NonNull Channel channel, @Nullable StateDescription original, @Nullable Locale locale) {
            String id = channel.getUID().getIdWithoutGroup();
            if ("7_1".equals(id)) {
                assertEquals(channel.getChannelTypeUID(), channelType7.getUID());
                return newState;
            } else if ("7_2".equals(id)) {
                assertEquals(channel.getChannelTypeUID(), channelType7.getUID());
                StateDescription newState2 = new StateDescription(original.getMinimum().add(BigDecimal.ONE), original.getMaximum().add(BigDecimal.ONE), original.getStep().add(BigDecimal.TEN), "NEW " + original.getPattern(), true, original.getOptions());
                return newState2;
            }
            return null;
        }
    });
    List<ChannelDefinition> channelDefinitions = new ArrayList<>();
    channelDefinitions.add(new ChannelDefinition("1", channelType.getUID()));
    channelDefinitions.add(new ChannelDefinition("2", channelType2.getUID()));
    channelDefinitions.add(new ChannelDefinition("3", channelType3.getUID()));
    channelDefinitions.add(new ChannelDefinition("4", channelType4.getUID()));
    channelDefinitions.add(new ChannelDefinition("5", channelType5.getUID()));
    channelDefinitions.add(new ChannelDefinition("6", channelType6.getUID()));
    channelDefinitions.add(new ChannelDefinition("7_1", channelType7.getUID()));
    channelDefinitions.add(new ChannelDefinition("7_2", channelType7.getUID()));
    registerService(new SimpleThingTypeProvider(Collections.singleton(ThingTypeBuilder.instance(new ThingTypeUID("hue:lamp"), "label").withChannelDefinitions(channelDefinitions).build())));
    List<Item> items = new ArrayList<>();
    items.add(new NumberItem("TestItem"));
    items.add(new NumberItem("TestItem2"));
    items.add(new StringItem("TestItem3"));
    items.add(new ColorItem("TestItem4"));
    items.add(new DimmerItem("TestItem5"));
    items.add(new SwitchItem("TestItem6"));
    items.add(new NumberItem("TestItem7_1"));
    items.add(new NumberItem("TestItem7_2"));
    registerService(new TestItemProvider(items));
    linkRegistry = getService(ItemChannelLinkRegistry.class);
    stateDescriptionProvider = getService(StateDescriptionProvider.class);
    assertNotNull(stateDescriptionProvider);
}
Also used : Locale(java.util.Locale) ArrayList(java.util.ArrayList) ChannelGroupType(org.eclipse.smarthome.core.thing.type.ChannelGroupType) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) DynamicStateDescriptionProvider(org.eclipse.smarthome.core.thing.type.DynamicStateDescriptionProvider) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) Item(org.eclipse.smarthome.core.items.Item) StringItem(org.eclipse.smarthome.core.library.items.StringItem) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) ChannelTypeUID(org.eclipse.smarthome.core.thing.type.ChannelTypeUID) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) ItemChannelLinkRegistry(org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) StateDescription(org.eclipse.smarthome.core.types.StateDescription) Channel(org.eclipse.smarthome.core.thing.Channel) ChannelGroupTypeUID(org.eclipse.smarthome.core.thing.type.ChannelGroupTypeUID) BaseThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory) ThingHandlerFactory(org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory) StringItem(org.eclipse.smarthome.core.library.items.StringItem) StateOption(org.eclipse.smarthome.core.types.StateOption) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) ChannelDefinition(org.eclipse.smarthome.core.thing.type.ChannelDefinition) ChannelTypeProvider(org.eclipse.smarthome.core.thing.type.ChannelTypeProvider) Collection(java.util.Collection) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) ChannelType(org.eclipse.smarthome.core.thing.type.ChannelType) StateDescriptionProvider(org.eclipse.smarthome.core.types.StateDescriptionProvider) DynamicStateDescriptionProvider(org.eclipse.smarthome.core.thing.type.DynamicStateDescriptionProvider) Nullable(org.eclipse.jdt.annotation.Nullable) Before(org.junit.Before)

Aggregations

NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)34 Test (org.junit.Test)25 Item (org.eclipse.smarthome.core.items.Item)17 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)15 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)13 QuantityType (org.eclipse.smarthome.core.library.types.QuantityType)12 State (org.eclipse.smarthome.core.types.State)11 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)6 ArithmeticGroupFunction (org.eclipse.smarthome.core.library.types.ArithmeticGroupFunction)5 StateDescription (org.eclipse.smarthome.core.types.StateDescription)5 Temperature (javax.measure.quantity.Temperature)4 GroupItem (org.eclipse.smarthome.core.items.GroupItem)4 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)4 Mapping (org.eclipse.smarthome.model.sitemap.Mapping)4 StateDescriptionProvider (org.eclipse.smarthome.core.types.StateDescriptionProvider)3 Collection (java.util.Collection)2 Length (javax.measure.quantity.Length)2 NonNull (org.eclipse.jdt.annotation.NonNull)2 UnitProvider (org.eclipse.smarthome.core.i18n.UnitProvider)2 ItemRegistry (org.eclipse.smarthome.core.items.ItemRegistry)2