Search in sources :

Example 6 with NumberItem

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

the class SwitchRenderer method renderWidget.

@Override
public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderException {
    Switch s = (Switch) w;
    String snippetName = null;
    Item item = null;
    try {
        item = itemUIRegistry.getItem(w.getItem());
        if (s.getMappings().size() == 0) {
            if (item instanceof RollershutterItem) {
                snippetName = "rollerblind";
            } else if (item instanceof GroupItem && ((GroupItem) item).getBaseItem() instanceof RollershutterItem) {
                snippetName = "rollerblind";
            } else {
                snippetName = "switch";
            }
        } else {
            snippetName = "buttons";
        }
    } catch (ItemNotFoundException e) {
        logger.warn("Cannot determine item type of '{}'", w.getItem(), e);
        snippetName = "switch";
    }
    String snippet = getSnippet(snippetName);
    State state = itemUIRegistry.getState(w);
    snippet = preprocessSnippet(snippet, w);
    snippet = StringUtils.replace(snippet, "%count%", Integer.toString(s.getMappings().size()));
    if (s.getMappings().size() == 0) {
        if (state.equals(OnOffType.ON)) {
            snippet = snippet.replaceAll("%checked%", "checked=true");
        } else {
            snippet = snippet.replaceAll("%checked%", "");
        }
    } else {
        StringBuilder buttons = new StringBuilder();
        for (Mapping mapping : s.getMappings()) {
            String button = getSnippet("button");
            String command = mapping.getCmd();
            String label = mapping.getLabel();
            if (item instanceof NumberItem && ((NumberItem) item).getDimension() != null) {
                String unit = getUnitForWidget(w);
                command = StringUtils.replace(command, UnitUtils.UNIT_PLACEHOLDER, unit);
                label = StringUtils.replace(label, UnitUtils.UNIT_PLACEHOLDER, unit);
                // Special treatment for °C since uom library uses a single character: ℃
                // This will ensure the current state matches the cmd and the buttonClass is set accordingly.
                command = StringUtils.replace(command, "°C", "℃");
            }
            button = StringUtils.replace(button, "%item%", w.getItem());
            button = StringUtils.replace(button, "%cmd%", escapeHtml(command));
            button = StringUtils.replace(button, "%label%", label != null ? escapeHtml(label) : "");
            String buttonClass;
            State compareMappingState = state;
            if (state instanceof QuantityType) {
                // convert the item state to the command value for proper
                // comparison and buttonClass calculation
                compareMappingState = convertStateToLabelUnit((QuantityType<?>) state, command);
            }
            if (s.getMappings().size() > 1 && compareMappingState.toString().equals(command)) {
                buttonClass = "mdl-button--accent";
            } else {
                buttonClass = "mdl-button";
            }
            button = StringUtils.replace(button, "%class%", buttonClass);
            buttons.append(button);
        }
        snippet = StringUtils.replace(snippet, "%buttons%", buttons.toString());
    }
    // Process the color tags
    snippet = processColor(w, snippet);
    sb.append(snippet);
    return null;
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) Item(org.eclipse.smarthome.core.items.Item) GroupItem(org.eclipse.smarthome.core.items.GroupItem) Switch(org.eclipse.smarthome.model.sitemap.Switch) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) State(org.eclipse.smarthome.core.types.State) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem) Mapping(org.eclipse.smarthome.model.sitemap.Mapping) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 7 with NumberItem

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

the class ChannelItemProviderTest method testItemRemoval_itemFromOtherProvider.

@Test
public void testItemRemoval_itemFromOtherProvider() throws Exception {
    provider.linkRegistryListener.added(new ItemChannelLink(ITEM_NAME, CHANNEL_UID));
    resetAndPrepareListener();
    provider.itemRegistryListener.beforeAdding(new NumberItem(ITEM_NAME));
    verify(listener, only()).removed(same(provider), same(ITEM));
    verify(listener, never()).added(same(provider), same(ITEM));
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) ItemChannelLink(org.eclipse.smarthome.core.thing.link.ItemChannelLink) Test(org.junit.Test)

Example 8 with NumberItem

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

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

the class GroupItemTest method createNumberItem.

private NumberItem createNumberItem(String name, Class<? extends Quantity<?>> dimension, State state) {
    NumberItem item = new NumberItem(CoreItemFactory.NUMBER + ":" + dimension.getSimpleName(), name);
    item.setUnitProvider(unitProvider);
    item.setState(state);
    return item;
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem)

Example 10 with NumberItem

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

the class QuantityTypeArithmeticGroupFunctionTest method createNumberItem.

private NumberItem createNumberItem(String name, Class<? extends Quantity<?>> dimension, State state) {
    NumberItem item = new NumberItem(CoreItemFactory.NUMBER + ":" + dimension.getSimpleName(), name);
    item.setUnitProvider(unitProvider);
    item.setState(state);
    return item;
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem)

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