Search in sources :

Example 6 with Switch

use of org.eclipse.smarthome.model.sitemap.Switch 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 7 with Switch

use of org.eclipse.smarthome.model.sitemap.Switch in project smarthome by eclipse.

the class SitemapResource method createWidgetBean.

private WidgetDTO createWidgetBean(String sitemapName, Widget widget, boolean drillDown, URI uri, String widgetId, Locale locale) {
    // Test visibility
    if (itemUIRegistry.getVisiblity(widget) == false) {
        return null;
    }
    WidgetDTO bean = new WidgetDTO();
    if (widget.getItem() != null) {
        try {
            Item item = itemUIRegistry.getItem(widget.getItem());
            String widgetTypeName = widget.eClass().getInstanceTypeName().substring(widget.eClass().getInstanceTypeName().lastIndexOf(".") + 1);
            boolean isMapview = "mapview".equalsIgnoreCase(widgetTypeName);
            Predicate<Item> itemFilter = (i -> i.getType().equals(CoreItemFactory.LOCATION));
            bean.item = EnrichedItemDTOMapper.map(item, isMapview, itemFilter, UriBuilder.fromUri(uri).build(), locale);
            bean.state = itemUIRegistry.getState(widget).toFullString();
            // In case the widget state is identical to the item state, its value is set to null.
            if (bean.state != null && bean.state.equals(bean.item.state)) {
                bean.state = null;
            }
        } catch (ItemNotFoundException e) {
            logger.debug("{}", e.getMessage());
        }
    }
    bean.widgetId = widgetId;
    bean.icon = itemUIRegistry.getCategory(widget);
    bean.labelcolor = itemUIRegistry.getLabelColor(widget);
    bean.valuecolor = itemUIRegistry.getValueColor(widget);
    bean.label = itemUIRegistry.getLabel(widget);
    bean.type = widget.eClass().getName();
    if (widget instanceof LinkableWidget) {
        LinkableWidget linkableWidget = (LinkableWidget) widget;
        EList<Widget> children = itemUIRegistry.getChildren(linkableWidget);
        if (widget instanceof Frame) {
            for (Widget child : children) {
                String wID = itemUIRegistry.getWidgetId(child);
                WidgetDTO subWidget = createWidgetBean(sitemapName, child, drillDown, uri, wID, locale);
                if (subWidget != null) {
                    bean.widgets.add(subWidget);
                }
            }
        } else if (children.size() > 0) {
            String pageName = itemUIRegistry.getWidgetId(linkableWidget);
            bean.linkedPage = createPageBean(sitemapName, itemUIRegistry.getLabel(widget), itemUIRegistry.getCategory(widget), pageName, drillDown ? children : null, drillDown, isLeaf(children), uri, locale, false);
        }
    }
    if (widget instanceof Switch) {
        Switch switchWidget = (Switch) widget;
        for (Mapping mapping : switchWidget.getMappings()) {
            MappingDTO mappingBean = new MappingDTO();
            mappingBean.command = mapping.getCmd();
            mappingBean.label = mapping.getLabel();
            bean.mappings.add(mappingBean);
        }
    }
    if (widget instanceof Selection) {
        Selection selectionWidget = (Selection) widget;
        for (Mapping mapping : selectionWidget.getMappings()) {
            MappingDTO mappingBean = new MappingDTO();
            mappingBean.command = mapping.getCmd();
            mappingBean.label = mapping.getLabel();
            bean.mappings.add(mappingBean);
        }
    }
    if (widget instanceof Slider) {
        Slider sliderWidget = (Slider) widget;
        bean.sendFrequency = sliderWidget.getFrequency();
        bean.switchSupport = sliderWidget.isSwitchEnabled();
    }
    if (widget instanceof List) {
        List listWidget = (List) widget;
        bean.separator = listWidget.getSeparator();
    }
    if (widget instanceof Image) {
        bean.url = buildProxyUrl(sitemapName, widget, uri);
        Image imageWidget = (Image) widget;
        if (imageWidget.getRefresh() > 0) {
            bean.refresh = imageWidget.getRefresh();
        }
    }
    if (widget instanceof Video) {
        Video videoWidget = (Video) widget;
        if (videoWidget.getEncoding() != null) {
            bean.encoding = videoWidget.getEncoding();
        }
        if (videoWidget.getEncoding() != null && videoWidget.getEncoding().toLowerCase().contains("hls")) {
            bean.url = videoWidget.getUrl();
        } else {
            bean.url = buildProxyUrl(sitemapName, videoWidget, uri);
        }
    }
    if (widget instanceof Webview) {
        Webview webViewWidget = (Webview) widget;
        bean.url = webViewWidget.getUrl();
        bean.height = webViewWidget.getHeight();
    }
    if (widget instanceof Mapview) {
        Mapview mapViewWidget = (Mapview) widget;
        bean.height = mapViewWidget.getHeight();
    }
    if (widget instanceof Chart) {
        Chart chartWidget = (Chart) widget;
        bean.service = chartWidget.getService();
        bean.period = chartWidget.getPeriod();
        bean.legend = chartWidget.getLegend();
        if (chartWidget.getRefresh() > 0) {
            bean.refresh = chartWidget.getRefresh();
        }
    }
    if (widget instanceof Setpoint) {
        Setpoint setpointWidget = (Setpoint) widget;
        bean.minValue = setpointWidget.getMinValue();
        bean.maxValue = setpointWidget.getMaxValue();
        bean.step = setpointWidget.getStep();
    }
    return bean;
}
Also used : Frame(org.eclipse.smarthome.model.sitemap.Frame) Slider(org.eclipse.smarthome.model.sitemap.Slider) Selection(org.eclipse.smarthome.model.sitemap.Selection) Mapview(org.eclipse.smarthome.model.sitemap.Mapview) Widget(org.eclipse.smarthome.model.sitemap.Widget) LinkableWidget(org.eclipse.smarthome.model.sitemap.LinkableWidget) Setpoint(org.eclipse.smarthome.model.sitemap.Setpoint) Mapping(org.eclipse.smarthome.model.sitemap.Mapping) Webview(org.eclipse.smarthome.model.sitemap.Webview) Image(org.eclipse.smarthome.model.sitemap.Image) LinkableWidget(org.eclipse.smarthome.model.sitemap.LinkableWidget) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) Switch(org.eclipse.smarthome.model.sitemap.Switch) Video(org.eclipse.smarthome.model.sitemap.Video) List(org.eclipse.smarthome.model.sitemap.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) EList(org.eclipse.emf.common.util.EList) Chart(org.eclipse.smarthome.model.sitemap.Chart) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Aggregations

Switch (org.eclipse.smarthome.model.sitemap.Switch)7 Mapping (org.eclipse.smarthome.model.sitemap.Mapping)6 State (org.eclipse.smarthome.core.types.State)5 Item (org.eclipse.smarthome.core.items.Item)3 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)3 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)3 QuantityType (org.eclipse.smarthome.core.library.types.QuantityType)3 GroupItem (org.eclipse.smarthome.core.items.GroupItem)2 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)2 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)2 HSBType (org.eclipse.smarthome.core.library.types.HSBType)2 Slider (org.eclipse.smarthome.model.sitemap.Slider)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 BasicEList (org.eclipse.emf.common.util.BasicEList)1 EList (org.eclipse.emf.common.util.EList)1 GenericItem (org.eclipse.smarthome.core.items.GenericItem)1 OnOffType (org.eclipse.smarthome.core.library.types.OnOffType)1 PercentType (org.eclipse.smarthome.core.library.types.PercentType)1