Search in sources :

Example 1 with Frame

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

the class SitemapResource method getPageBean.

private PageDTO getPageBean(String sitemapName, String pageId, URI uri, Locale locale, boolean timeout) {
    Sitemap sitemap = getSitemap(sitemapName);
    if (sitemap != null) {
        if (pageId.equals(sitemap.getName())) {
            EList<Widget> children = itemUIRegistry.getChildren(sitemap);
            return createPageBean(sitemapName, sitemap.getLabel(), sitemap.getIcon(), sitemap.getName(), children, false, isLeaf(children), uri, locale, timeout);
        } else {
            Widget pageWidget = itemUIRegistry.getWidget(sitemap, pageId);
            if (pageWidget instanceof LinkableWidget) {
                EList<Widget> children = itemUIRegistry.getChildren((LinkableWidget) pageWidget);
                PageDTO pageBean = createPageBean(sitemapName, itemUIRegistry.getLabel(pageWidget), itemUIRegistry.getCategory(pageWidget), pageId, children, false, isLeaf(children), uri, locale, timeout);
                EObject parentPage = pageWidget.eContainer();
                while (parentPage instanceof Frame) {
                    parentPage = parentPage.eContainer();
                }
                if (parentPage instanceof Widget) {
                    String parentId = itemUIRegistry.getWidgetId((Widget) parentPage);
                    pageBean.parent = getPageBean(sitemapName, parentId, uri, locale, timeout);
                    pageBean.parent.widgets = null;
                    pageBean.parent.parent = null;
                } else if (parentPage instanceof Sitemap) {
                    pageBean.parent = getPageBean(sitemapName, sitemap.getName(), uri, locale, timeout);
                    pageBean.parent.widgets = null;
                }
                return pageBean;
            } else {
                if (logger.isDebugEnabled()) {
                    if (pageWidget == null) {
                        logger.debug("Received HTTP GET request at '{}' for the unknown page id '{}'.", uri, pageId);
                    } else {
                        logger.debug("Received HTTP GET request at '{}' for the page id '{}'. " + "This id refers to a non-linkable widget and is therefore no valid page id.", uri, pageId);
                    }
                }
                throw new WebApplicationException(404);
            }
        }
    } else {
        logger.info("Received HTTP GET request at '{}' for the unknown sitemap '{}'.", uri, sitemapName);
        throw new WebApplicationException(404);
    }
}
Also used : LinkableWidget(org.eclipse.smarthome.model.sitemap.LinkableWidget) Sitemap(org.eclipse.smarthome.model.sitemap.Sitemap) Frame(org.eclipse.smarthome.model.sitemap.Frame) WebApplicationException(javax.ws.rs.WebApplicationException) EObject(org.eclipse.emf.ecore.EObject) Widget(org.eclipse.smarthome.model.sitemap.Widget) LinkableWidget(org.eclipse.smarthome.model.sitemap.LinkableWidget)

Example 2 with Frame

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

the class SitemapResource method getAllItems.

/**
 * Collects all items that are represented by a given list of widgets
 *
 * @param widgets
 *            the widget list to get the items for added to all bundles containing REST resources
 * @return all items that are represented by the list of widgets
 */
private Set<GenericItem> getAllItems(EList<Widget> widgets) {
    Set<GenericItem> items = new HashSet<GenericItem>();
    if (itemUIRegistry != null) {
        for (Widget widget : widgets) {
            String itemName = widget.getItem();
            if (itemName != null) {
                try {
                    Item item = itemUIRegistry.getItem(itemName);
                    if (item instanceof GenericItem) {
                        items.add((GenericItem) item);
                    }
                } catch (ItemNotFoundException e) {
                // ignore
                }
            }
            // Consider all items inside the frame
            if (widget instanceof Frame) {
                items.addAll(getAllItems(((Frame) widget).getChildren()));
            }
            // Consider items involved in any visibility, labelcolor and valuecolor condition
            items.addAll(getItemsInVisibilityCond(widget.getVisibility()));
            items.addAll(getItemsInColorCond(widget.getLabelColor()));
            items.addAll(getItemsInColorCond(widget.getValueColor()));
        }
    }
    return items;
}
Also used : GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) Frame(org.eclipse.smarthome.model.sitemap.Frame) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Widget(org.eclipse.smarthome.model.sitemap.Widget) LinkableWidget(org.eclipse.smarthome.model.sitemap.LinkableWidget) HashSet(java.util.HashSet) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 3 with Frame

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

the class PageRenderer method processChildren.

private void processChildren(StringBuilder sb_pre, StringBuilder sb_post, EList<Widget> children) throws RenderException {
    // put a single frame around all children widgets, if there are no explicit frames
    if (!children.isEmpty()) {
        EObject firstChild = children.get(0);
        EObject parent = itemUIRegistry.getParent((Widget) firstChild);
        if (!(firstChild instanceof Frame || parent instanceof Frame || parent instanceof Sitemap || parent instanceof org.eclipse.smarthome.model.sitemap.List)) {
            String frameSnippet = getSnippet("frame");
            frameSnippet = StringUtils.replace(frameSnippet, "%widget_id%", "");
            frameSnippet = StringUtils.replace(frameSnippet, "%label%", "");
            frameSnippet = StringUtils.replace(frameSnippet, "%frame_class%", "mdl-form--no-label");
            String[] parts = frameSnippet.split("%children%");
            if (parts.length > 1) {
                sb_pre.append(parts[0]);
                sb_post.insert(0, parts[1]);
            }
            if (parts.length > 2) {
                logger.error("Snippet 'frame' contains multiple %children% sections, but only one is allowed!");
            }
        }
    }
    for (Widget w : children) {
        StringBuilder newPre = new StringBuilder();
        StringBuilder newPost = new StringBuilder();
        StringBuilder widgetSB = new StringBuilder();
        EList<Widget> nextChildren = renderWidget(w, widgetSB);
        if (nextChildren != null) {
            String[] parts = widgetSB.toString().split("%children%");
            // no %children% placeholder found or at the end
            if (parts.length == 1) {
                newPre.append(widgetSB);
            }
            // %children% section found
            if (parts.length > 1) {
                newPre.append(parts[0]);
                newPost.insert(0, parts[1]);
            }
            // occurance
            if (parts.length > 2) {
                String widgetType = w.eClass().getInstanceTypeName().substring(w.eClass().getInstanceTypeName().lastIndexOf(".") + 1);
                logger.error("Snippet for widget '{}' contains multiple %children% sections, but only one is allowed!", widgetType);
            }
            processChildren(newPre, newPost, nextChildren);
            sb_pre.append(newPre);
            sb_pre.append(newPost);
        } else {
            sb_pre.append(widgetSB);
        }
    }
}
Also used : Sitemap(org.eclipse.smarthome.model.sitemap.Sitemap) Frame(org.eclipse.smarthome.model.sitemap.Frame) EObject(org.eclipse.emf.ecore.EObject) Widget(org.eclipse.smarthome.model.sitemap.Widget)

Example 4 with Frame

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

the class PageRenderer method processChildren.

private void processChildren(StringBuilder sb_pre, StringBuilder sb_post, EList<Widget> children) throws RenderException {
    // put a single frame around all children widgets, if there are no explicit frames
    if (!children.isEmpty()) {
        boolean frameRequired = false;
        for (Widget w : children) {
            EObject parent = itemUIRegistry.getParent(w);
            if (!(w instanceof Frame || parent instanceof Frame || parent instanceof List)) {
                frameRequired = true;
            }
        }
        if (frameRequired) {
            String frameSnippet = getSnippet("frame");
            frameSnippet = StringUtils.replace(frameSnippet, "%label%", "");
            String[] parts = frameSnippet.split("%children%");
            if (parts.length > 1) {
                sb_pre.append(parts[0]);
            }
            if (parts.length > 2) {
                sb_post.insert(0, parts[1]);
            }
            if (parts.length > 2) {
                logger.error("Snippet 'frame' contains multiple %children% sections, but only one is allowed!");
            }
        }
    }
    for (Widget w : children) {
        StringBuilder newPre = new StringBuilder();
        StringBuilder newPost = new StringBuilder();
        StringBuilder widgetSB = new StringBuilder();
        EList<Widget> nextChildren = renderWidget(w, widgetSB);
        if (nextChildren != null) {
            String[] parts = widgetSB.toString().split("%children%");
            // no %children% placeholder found or at the end
            if (parts.length == 1) {
                newPre.append(widgetSB);
            }
            // %children% section found
            if (parts.length > 1) {
                newPre.append(parts[0]);
                newPost.insert(0, parts[1]);
            }
            // occurance
            if (parts.length > 2) {
                String widgetType = w.eClass().getInstanceTypeName().substring(w.eClass().getInstanceTypeName().lastIndexOf(".") + 1);
                logger.error("Snippet for widget '{}' contains multiple %children% sections, but only one is allowed!", widgetType);
            }
            processChildren(newPre, newPost, nextChildren);
            sb_pre.append(newPre);
            sb_pre.append(newPost);
        } else {
            sb_pre.append(widgetSB);
        }
    }
}
Also used : Frame(org.eclipse.smarthome.model.sitemap.Frame) EObject(org.eclipse.emf.ecore.EObject) Widget(org.eclipse.smarthome.model.sitemap.Widget) EList(org.eclipse.emf.common.util.EList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with Frame

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

Frame (org.eclipse.smarthome.model.sitemap.Frame)6 Widget (org.eclipse.smarthome.model.sitemap.Widget)6 EObject (org.eclipse.emf.ecore.EObject)3 GenericItem (org.eclipse.smarthome.core.items.GenericItem)3 Item (org.eclipse.smarthome.core.items.Item)3 LinkableWidget (org.eclipse.smarthome.model.sitemap.LinkableWidget)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 EList (org.eclipse.emf.common.util.EList)2 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)2 Sitemap (org.eclipse.smarthome.model.sitemap.Sitemap)2 LinkedList (java.util.LinkedList)1 List (java.util.List)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 GroupItem (org.eclipse.smarthome.core.items.GroupItem)1 Chart (org.eclipse.smarthome.model.sitemap.Chart)1 Image (org.eclipse.smarthome.model.sitemap.Image)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