Search in sources :

Example 91 with Item

use of org.eclipse.smarthome.core.items.Item 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);
    snippet = StringUtils.replace(snippet, "%id%", itemUIRegistry.getWidgetId(w));
    snippet = StringUtils.replace(snippet, "%category%", getCategory(w));
    snippet = StringUtils.replace(snippet, "%state%", getState(w));
    snippet = StringUtils.replace(snippet, "%format%", getFormat());
    snippet = StringUtils.replace(snippet, "%item%", w.getItem());
    snippet = StringUtils.replace(snippet, "%label%", getLabel(w));
    snippet = StringUtils.replace(snippet, "%servletname%", WebAppServlet.SERVLET_NAME);
    State state = itemUIRegistry.getState(w);
    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%", StringEscapeUtils.escapeHtml(command));
            button = StringUtils.replace(button, "%label%", label != null ? StringEscapeUtils.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)) {
                // button with red color
                buttonClass = "Warn";
            } else {
                // button with blue color
                buttonClass = "Action";
            }
            button = StringUtils.replace(button, "%type%", 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 92 with Item

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

the class ChartRenderer method renderWidget.

@Override
public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderException {
    Chart chart = (Chart) w;
    try {
        String itemParam = null;
        Item item = itemUIRegistry.getItem(chart.getItem());
        if (item instanceof GroupItem) {
            itemParam = "groups=" + chart.getItem();
        } else {
            itemParam = "items=" + chart.getItem();
        }
        String url = "/chart?" + itemParam + "&period=" + chart.getPeriod() + "&t=" + (new Date()).getTime();
        if (chart.getService() != null) {
            url += "&service=" + chart.getService();
        }
        if (chart.getLegend() != null) {
            if (chart.getLegend()) {
                url += "&legend=true";
            } else {
                url += "&legend=false";
            }
        }
        String snippet = getSnippet("image");
        if (chart.getRefresh() > 0) {
            snippet = StringUtils.replace(snippet, "%refresh%", "id=\"%id%\" data-timeout=\"" + chart.getRefresh() + "\" onload=\"startReloadImage('%url%', '%id%')\"");
        } else {
            snippet = StringUtils.replace(snippet, "%refresh%", "");
        }
        snippet = StringUtils.replace(snippet, "%id%", itemUIRegistry.getWidgetId(w));
        snippet = StringUtils.replace(snippet, "%url%", url);
        sb.append(snippet);
    } catch (ItemNotFoundException e) {
        logger.warn("Chart cannot be rendered as item '{}' does not exist.", chart.getItem());
    }
    return null;
}
Also used : Item(org.eclipse.smarthome.core.items.Item) GroupItem(org.eclipse.smarthome.core.items.GroupItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem) Chart(org.eclipse.smarthome.model.sitemap.Chart) Date(java.util.Date) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 93 with Item

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

the class ItemStateConditionHandler method isSatisfied.

@Override
public boolean isSatisfied(Map<String, Object> inputs) {
    String itemName = (String) module.getConfiguration().get(ITEM_NAME);
    String state = (String) module.getConfiguration().get(STATE);
    String operator = (String) module.getConfiguration().get(OPERATOR);
    if (operator == null || state == null || itemName == null) {
        logger.error("Module is not well configured: itemName={}  operator={}  state = {}", itemName, operator, state);
        return false;
    }
    if (itemRegistry == null) {
        logger.error("The ItemRegistry is not available to evaluate the condition.");
        return false;
    }
    try {
        Item item = itemRegistry.getItem(itemName);
        State compareState = TypeParser.parseState(item.getAcceptedDataTypes(), state);
        State itemState = item.getState();
        logger.debug("ItemStateCondition '{}'checking if {} (State={}) {} {}", module.getId(), itemName, itemState, operator, compareState);
        switch(operator) {
            case "=":
                logger.debug("ConditionSatisfied --> {}", itemState.equals(compareState));
                return itemState.equals(compareState);
            case "!=":
                return !itemState.equals(compareState);
            case "<":
                if (itemState instanceof DecimalType && compareState instanceof DecimalType) {
                    return ((DecimalType) itemState).compareTo((DecimalType) compareState) < 0;
                }
                break;
            case "<=":
            case "=<":
                if (itemState instanceof DecimalType && compareState instanceof DecimalType) {
                    return ((DecimalType) itemState).compareTo((DecimalType) compareState) <= 0;
                }
                break;
            case ">":
                if (itemState instanceof DecimalType && compareState instanceof DecimalType) {
                    return ((DecimalType) itemState).compareTo((DecimalType) compareState) > 0;
                }
                break;
            case ">=":
            case "=>":
                if (itemState instanceof DecimalType && compareState instanceof DecimalType) {
                    return ((DecimalType) itemState).compareTo((DecimalType) compareState) >= 0;
                }
                break;
            default:
                break;
        }
    } catch (ItemNotFoundException e) {
        logger.error("Item with Name {} not found in itemRegistry", itemName);
        return false;
    }
    return false;
}
Also used : Item(org.eclipse.smarthome.core.items.Item) State(org.eclipse.smarthome.core.types.State) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 94 with Item

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

the class ScriptBusEvent method postUpdate.

/**
 * Posts a status update for a specified item to the event bus.
 *
 * @param itemName the name of the item to send the status update for
 * @param stateAsString the new state of the item
 */
public Object postUpdate(String itemName, String stateString) {
    if (eventPublisher != null && itemRegistry != null) {
        try {
            Item item = itemRegistry.getItem(itemName);
            State state = TypeParser.parseState(item.getAcceptedDataTypes(), stateString);
            eventPublisher.post(ItemEventFactory.createStateEvent(itemName, state));
        } catch (ItemNotFoundException e) {
            LoggerFactory.getLogger(ScriptBusEvent.class).warn("Item '{}' does not exist.", itemName);
        }
    }
    return null;
}
Also used : Item(org.eclipse.smarthome.core.items.Item) GroupItem(org.eclipse.smarthome.core.items.GroupItem) State(org.eclipse.smarthome.core.types.State) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 95 with Item

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

the class ChannelItemProvider method disableChannelItemProvider.

private synchronized void disableChannelItemProvider() {
    if (executor != null) {
        executor.shutdownNow();
        executor = null;
    }
    for (ProviderChangeListener<Item> listener : listeners) {
        for (Item item : getAll()) {
            listener.removed(this, item);
        }
    }
    removeRegistryChangeListeners();
    initialized = false;
    items = null;
}
Also used : GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item)

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