Search in sources :

Example 1 with Chart

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

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

Example 3 with Chart

use of org.eclipse.smarthome.model.sitemap.Chart 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 chartUrl = "/chart?" + itemParam + "&period=" + chart.getPeriod();
        if (chart.getService() != null) {
            chartUrl += "&service=" + chart.getService();
        }
        // if legend parameter is given, add corresponding GET parameter
        if (chart.getLegend() != null) {
            if (chart.getLegend()) {
                chartUrl += "&legend=true";
            } else {
                chartUrl += "&legend=false";
            }
        }
        // add theme GET parameter
        String chartTheme = null;
        switch(config.getTheme()) {
            case WebAppConfig.THEME_NAME_DEFAULT:
                chartTheme = "bright";
                break;
            case WebAppConfig.THEME_NAME_DARK:
                chartTheme = "dark";
                break;
        }
        if (chartTheme != null) {
            chartUrl += "&theme=" + chartTheme;
        }
        String url;
        boolean ignoreRefresh;
        if (!itemUIRegistry.getVisiblity(w)) {
            url = URL_NONE_ICON;
            ignoreRefresh = true;
        } else {
            // add timestamp to circumvent browser cache
            url = chartUrl + "&t=" + (new Date()).getTime();
            ignoreRefresh = false;
        }
        String snippet = getSnippet("chart");
        snippet = preprocessSnippet(snippet, w);
        if (chart.getRefresh() > 0) {
            snippet = StringUtils.replace(snippet, "%update_interval%", Integer.toString(chart.getRefresh()));
        } else {
            snippet = StringUtils.replace(snippet, "%update_interval%", "0");
        }
        snippet = StringUtils.replace(snippet, "%id%", itemUIRegistry.getWidgetId(w));
        snippet = StringUtils.replace(snippet, "%proxied_url%", chartUrl);
        snippet = StringUtils.replace(snippet, "%valid_url%", "true");
        snippet = StringUtils.replace(snippet, "%ignore_refresh%", ignoreRefresh ? "true" : "false");
        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)

Aggregations

Item (org.eclipse.smarthome.core.items.Item)3 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)3 Chart (org.eclipse.smarthome.model.sitemap.Chart)3 Date (java.util.Date)2 GroupItem (org.eclipse.smarthome.core.items.GroupItem)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 EList (org.eclipse.emf.common.util.EList)1 GenericItem (org.eclipse.smarthome.core.items.GenericItem)1 Frame (org.eclipse.smarthome.model.sitemap.Frame)1 Image (org.eclipse.smarthome.model.sitemap.Image)1 LinkableWidget (org.eclipse.smarthome.model.sitemap.LinkableWidget)1 List (org.eclipse.smarthome.model.sitemap.List)1 Mapping (org.eclipse.smarthome.model.sitemap.Mapping)1 Mapview (org.eclipse.smarthome.model.sitemap.Mapview)1 Selection (org.eclipse.smarthome.model.sitemap.Selection)1 Setpoint (org.eclipse.smarthome.model.sitemap.Setpoint)1 Slider (org.eclipse.smarthome.model.sitemap.Slider)1 Switch (org.eclipse.smarthome.model.sitemap.Switch)1 Video (org.eclipse.smarthome.model.sitemap.Video)1