Search in sources :

Example 11 with ItemNotFoundException

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

the class SelectionRenderer method renderWidget.

@Override
public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderException {
    String snippet = getSnippet("selection");
    snippet = preprocessSnippet(snippet, w);
    snippet = StringUtils.replace(snippet, "%value_map%", getMappingsJSON((Selection) w));
    snippet = StringUtils.replace(snippet, "%label_header%", getLabel(w));
    State state = itemUIRegistry.getState(w);
    Selection selection = (Selection) w;
    String mappingLabel = null;
    Item item = null;
    try {
        item = itemUIRegistry.getItem(w.getItem());
    } catch (ItemNotFoundException e) {
        logger.debug("Failed to retrieve item during widget rendering: {}", e.getMessage());
    }
    StringBuilder rowSB = new StringBuilder();
    for (Mapping mapping : selection.getMappings()) {
        String rowSnippet = getSnippet("selection_row");
        String command = mapping.getCmd() != null ? 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", "℃");
        }
        rowSnippet = StringUtils.replace(rowSnippet, "%item%", w.getItem() != null ? w.getItem() : "");
        rowSnippet = StringUtils.replace(rowSnippet, "%cmd%", escapeHtml(command));
        rowSnippet = StringUtils.replace(rowSnippet, "%label%", label != null ? escapeHtml(label) : "");
        State compareMappingState = state;
        if (state instanceof QuantityType) {
            // convert the item state to the command value for proper
            // comparison and "checked" attribute calculation
            compareMappingState = convertStateToLabelUnit((QuantityType<?>) state, command);
        }
        if (compareMappingState.toString().equals(command)) {
            mappingLabel = label;
            rowSnippet = StringUtils.replace(rowSnippet, "%checked%", "checked=\"true\"");
        } else {
            rowSnippet = StringUtils.replace(rowSnippet, "%checked%", "");
        }
        rowSB.append(rowSnippet);
    }
    snippet = StringUtils.replace(snippet, "%rows%", rowSB.toString());
    snippet = StringUtils.replace(snippet, "%value_header%", mappingLabel != null ? mappingLabel : "");
    // Process the color tags
    snippet = processColor(w, snippet);
    sb.append(snippet);
    return null;
}
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) Selection(org.eclipse.smarthome.model.sitemap.Selection) Mapping(org.eclipse.smarthome.model.sitemap.Mapping) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 12 with ItemNotFoundException

use of org.eclipse.smarthome.core.items.ItemNotFoundException 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 13 with ItemNotFoundException

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

the class CmdServlet method service.

@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    for (Object key : req.getParameterMap().keySet()) {
        String itemName = key.toString();
        if (!itemName.startsWith("__")) {
            // all additional webapp params start with "__" and should be ignored
            String commandName = req.getParameter(itemName);
            try {
                Item item = itemRegistry.getItem(itemName);
                // into real commands by the webapp.
                if ((item instanceof SwitchItem || item instanceof GroupItem) && commandName.equals("TOGGLE")) {
                    commandName = OnOffType.ON.equals(item.getStateAs(OnOffType.class)) ? "OFF" : "ON";
                }
                Command command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), commandName);
                if (command != null) {
                    eventPublisher.post(ItemEventFactory.createCommandEvent(itemName, command));
                } else {
                    logger.warn("Received unknown command '{}' for item '{}'", commandName, itemName);
                }
            } catch (ItemNotFoundException e) {
                logger.warn("Received command '{}' for item '{}', but the item does not exist in the registry", commandName, itemName);
            }
        }
    }
}
Also used : SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Item(org.eclipse.smarthome.core.items.Item) GroupItem(org.eclipse.smarthome.core.items.GroupItem) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) Command(org.eclipse.smarthome.core.types.Command) GroupItem(org.eclipse.smarthome.core.items.GroupItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 14 with ItemNotFoundException

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

the class CmdServlet method service.

@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    res.setContentType("text/plain");
    for (Object key : req.getParameterMap().keySet()) {
        String itemName = key.toString();
        if (!itemName.startsWith("__")) {
            // all additional webapp params start with "__" and should be ignored
            String commandName = req.getParameter(itemName);
            try {
                Item item = itemRegistry.getItem(itemName);
                // into real commands by the webapp.
                if ((item instanceof SwitchItem || item instanceof GroupItem) && commandName.equals("TOGGLE")) {
                    commandName = OnOffType.ON.equals(item.getStateAs(OnOffType.class)) ? "OFF" : "ON";
                }
                Command command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), commandName);
                if (command != null) {
                    eventPublisher.post(ItemEventFactory.createCommandEvent(itemName, command));
                } else {
                    logger.warn("Received unknown command '{}' for item '{}'", commandName, itemName);
                }
            } catch (ItemNotFoundException e) {
                logger.warn("Received command '{}' for item '{}', but the item does not exist in the registry", commandName, itemName);
            }
        }
    }
}
Also used : SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Item(org.eclipse.smarthome.core.items.Item) GroupItem(org.eclipse.smarthome.core.items.GroupItem) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) Command(org.eclipse.smarthome.core.types.Command) GroupItem(org.eclipse.smarthome.core.items.GroupItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 15 with ItemNotFoundException

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

the class WebAppServlet method addItemWithName.

private void addItemWithName(Set<GenericItem> items, String itemName) {
    if (itemName != null) {
        try {
            Item item = renderer.getItemUIRegistry().getItem(itemName);
            if (item instanceof GenericItem) {
                final GenericItem gItem = (GenericItem) item;
                items.add(gItem);
            }
        } catch (ItemNotFoundException e) {
        // ignore
        }
    }
}
Also used : GroupItem(org.eclipse.smarthome.core.items.GroupItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) GenericItem(org.eclipse.smarthome.core.items.GenericItem) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Aggregations

ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)41 Item (org.eclipse.smarthome.core.items.Item)36 GroupItem (org.eclipse.smarthome.core.items.GroupItem)22 GenericItem (org.eclipse.smarthome.core.items.GenericItem)17 State (org.eclipse.smarthome.core.types.State)15 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)8 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)8 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)8 Command (org.eclipse.smarthome.core.types.Command)7 ItemNotUniqueException (org.eclipse.smarthome.core.items.ItemNotUniqueException)5 QuantityType (org.eclipse.smarthome.core.library.types.QuantityType)5 Mapping (org.eclipse.smarthome.model.sitemap.Mapping)5 Date (java.util.Date)4 CallItem (org.eclipse.smarthome.core.library.items.CallItem)4 DateTimeItem (org.eclipse.smarthome.core.library.items.DateTimeItem)4 StringItem (org.eclipse.smarthome.core.library.items.StringItem)4 Widget (org.eclipse.smarthome.model.sitemap.Widget)4 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)3 ContactItem (org.eclipse.smarthome.core.library.items.ContactItem)3 DimmerItem (org.eclipse.smarthome.core.library.items.DimmerItem)3