Search in sources :

Example 21 with Item

use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.

the class ItemUIRegistryImpl method processColorDefinition.

private String processColorDefinition(State state, List<ColorArray> colorList) {
    // Sanity check
    if (colorList == null) {
        return null;
    }
    if (colorList.size() == 0) {
        return null;
    }
    String colorString = null;
    // static colour
    if (colorList.size() == 1 && colorList.get(0).getState() == null) {
        colorString = colorList.get(0).getArg();
    } else {
        // with the supplied value
        for (ColorArray color : colorList) {
            // Use a local state variable in case it gets overridden below
            State cmpState = state;
            if (color.getState() == null) {
                logger.error("Error parsing color");
                continue;
            }
            // If there's an item defined here, get its state
            String itemName = color.getItem();
            if (itemName != null) {
                // Try and find the item to test.
                // If it's not found, return visible
                Item item;
                try {
                    item = itemRegistry.getItem(itemName);
                    // Get the item state
                    cmpState = item.getState();
                } catch (ItemNotFoundException e) {
                    logger.warn("Cannot retrieve color item {} for widget", color.getItem());
                }
            }
            // Handle the sign
            String value;
            if (color.getSign() != null) {
                value = color.getSign() + color.getState();
            } else {
                value = color.getState();
            }
            if (matchStateToValue(cmpState, value, color.getCondition()) == true) {
                // We have the color for this value - break!
                colorString = color.getArg();
                break;
            }
        }
    }
    // Remove quotes off the colour - if they exist
    if (colorString == null) {
        return null;
    }
    if (colorString.startsWith("\"") && colorString.endsWith("\"")) {
        colorString = colorString.substring(1, colorString.length() - 1);
    }
    return colorString;
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) CallItem(org.eclipse.smarthome.core.library.items.CallItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) DateTimeItem(org.eclipse.smarthome.core.library.items.DateTimeItem) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) LocationItem(org.eclipse.smarthome.core.library.items.LocationItem) ContactItem(org.eclipse.smarthome.core.library.items.ContactItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) StringItem(org.eclipse.smarthome.core.library.items.StringItem) PlayerItem(org.eclipse.smarthome.core.library.items.PlayerItem) ImageItem(org.eclipse.smarthome.core.library.items.ImageItem) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) State(org.eclipse.smarthome.core.types.State) ColorArray(org.eclipse.smarthome.model.sitemap.ColorArray) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 22 with Item

use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.

the class ItemUIRegistryImpl method getDynamicGroupChildren.

/**
 * This method creates a list of children for a group dynamically.
 * If there are no explicit children defined in a sitemap, the children
 * can thus be created on the fly by iterating over the members of the group item.
 *
 * @param group The group widget to get children for
 * @return a list of default widgets provided for the member items
 */
private EList<Widget> getDynamicGroupChildren(Group group) {
    EList<Widget> children = new BasicEList<Widget>();
    String itemName = group.getItem();
    try {
        if (itemName != null) {
            Item item = getItem(itemName);
            if (item instanceof GroupItem) {
                GroupItem groupItem = (GroupItem) item;
                for (Item member : groupItem.getMembers()) {
                    Widget widget = getDefaultWidget(member.getClass(), member.getName());
                    if (widget != null) {
                        widget.setItem(member.getName());
                        children.add(widget);
                    }
                }
            } else {
                logger.warn("Item '{}' is not a group.", item.getName());
            }
        } else {
            logger.warn("Group does not specify an associated item - ignoring it.");
        }
    } catch (ItemNotFoundException e) {
        logger.warn("Dynamic group with label '{}' will be ignored, because its item '{}' does not exist.", group.getLabel(), itemName);
    }
    return children;
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) CallItem(org.eclipse.smarthome.core.library.items.CallItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) DateTimeItem(org.eclipse.smarthome.core.library.items.DateTimeItem) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) LocationItem(org.eclipse.smarthome.core.library.items.LocationItem) ContactItem(org.eclipse.smarthome.core.library.items.ContactItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) StringItem(org.eclipse.smarthome.core.library.items.StringItem) PlayerItem(org.eclipse.smarthome.core.library.items.PlayerItem) ImageItem(org.eclipse.smarthome.core.library.items.ImageItem) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) BasicEList(org.eclipse.emf.common.util.BasicEList) Widget(org.eclipse.smarthome.model.sitemap.Widget) LinkableWidget(org.eclipse.smarthome.model.sitemap.LinkableWidget) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 23 with Item

use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.

the class ItemUIRegistryImpl method getVisiblity.

@Override
public boolean getVisiblity(Widget w) {
    // Default to visible if parameters not set
    List<VisibilityRule> ruleList = w.getVisibility();
    if (ruleList == null) {
        return true;
    }
    if (ruleList.size() == 0) {
        return true;
    }
    logger.debug("Checking visiblity for widget '{}'.", w.getLabel());
    for (VisibilityRule rule : w.getVisibility()) {
        String itemName = rule.getItem();
        if (itemName == null) {
            continue;
        }
        if (rule.getState() == null) {
            continue;
        }
        // Try and find the item to test.
        // If it's not found, return visible
        Item item;
        try {
            item = itemRegistry.getItem(itemName);
        } catch (ItemNotFoundException e) {
            logger.error("Cannot retrieve visibility item {} for widget {}", rule.getItem(), w.eClass().getInstanceTypeName());
            // Default to visible!
            return true;
        }
        // Get the item state
        State state = item.getState();
        // Handle the sign
        String value;
        if (rule.getSign() != null) {
            value = rule.getSign() + rule.getState();
        } else {
            value = rule.getState();
        }
        if (matchStateToValue(state, value, rule.getCondition()) == true) {
            // We have the name for this value!
            return true;
        }
    }
    logger.debug("Widget {} is not visible.", w.getLabel());
    // The state wasn't in the list, so we don't display it
    return false;
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) CallItem(org.eclipse.smarthome.core.library.items.CallItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) DateTimeItem(org.eclipse.smarthome.core.library.items.DateTimeItem) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) LocationItem(org.eclipse.smarthome.core.library.items.LocationItem) ContactItem(org.eclipse.smarthome.core.library.items.ContactItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) StringItem(org.eclipse.smarthome.core.library.items.StringItem) PlayerItem(org.eclipse.smarthome.core.library.items.PlayerItem) ImageItem(org.eclipse.smarthome.core.library.items.ImageItem) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) VisibilityRule(org.eclipse.smarthome.model.sitemap.VisibilityRule) State(org.eclipse.smarthome.core.types.State) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 24 with Item

use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.

the class ItemUIRegistryImplTest method getLabel_transformationContainingPercentS.

@Test
public void getLabel_transformationContainingPercentS() throws ItemNotFoundException {
    // It doesn't matter that "FOO" doesn't exist - this is to assert it doesn't fail before because of the two "%s"
    String testLabel = "Memory [FOO(echo %s):%s]";
    Widget w = mock(Widget.class);
    Item item = mock(Item.class);
    when(w.getLabel()).thenReturn(testLabel);
    when(w.getItem()).thenReturn("Item");
    when(registry.getItem("Item")).thenReturn(item);
    when(item.getState()).thenReturn(new StringType("State"));
    String label = uiRegistry.getLabel(w);
    assertEquals("Memory [State]", label);
}
Also used : ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) Item(org.eclipse.smarthome.core.items.Item) StringType(org.eclipse.smarthome.core.library.types.StringType) Widget(org.eclipse.smarthome.model.sitemap.Widget) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 25 with Item

use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.

the class ItemUIRegistryImplTest method getLabel_labelWithZonedDate.

@Test
public void getLabel_labelWithZonedDate() throws ItemNotFoundException {
    String testLabel = "Label [%1$td.%1$tm.%1$tY]";
    Widget w = mock(Widget.class);
    Item item = mock(Item.class);
    when(w.getLabel()).thenReturn(testLabel);
    when(w.getItem()).thenReturn("Item");
    when(registry.getItem("Item")).thenReturn(item);
    when(item.getState()).thenReturn(new DateTimeType("2011-06-01T00:00:00Z"));
    String label = uiRegistry.getLabel(w);
    assertEquals("Label [01.06.2011]", label);
}
Also used : ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) Item(org.eclipse.smarthome.core.items.Item) DateTimeType(org.eclipse.smarthome.core.library.types.DateTimeType) Widget(org.eclipse.smarthome.model.sitemap.Widget) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Aggregations

Item (org.eclipse.smarthome.core.items.Item)111 GroupItem (org.eclipse.smarthome.core.items.GroupItem)42 GenericItem (org.eclipse.smarthome.core.items.GenericItem)39 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)37 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)35 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)31 State (org.eclipse.smarthome.core.types.State)26 Test (org.junit.Test)26 ItemRegistry (org.eclipse.smarthome.core.items.ItemRegistry)14 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)14 StringItem (org.eclipse.smarthome.core.library.items.StringItem)14 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)13 DimmerItem (org.eclipse.smarthome.core.library.items.DimmerItem)12 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)10 IntentInterpretation (org.openhab.ui.habot.nlp.IntentInterpretation)10 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)9 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)9 Thing (org.eclipse.smarthome.core.thing.Thing)9 Set (java.util.Set)8 Widget (org.eclipse.smarthome.model.sitemap.Widget)8