Search in sources :

Example 1 with SitemapProvider

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

the class SitemapResource method getSitemapBeans.

public Collection<SitemapDTO> getSitemapBeans(URI uri) {
    Collection<SitemapDTO> beans = new LinkedList<SitemapDTO>();
    logger.debug("Received HTTP GET request at '{}'.", UriBuilder.fromUri(uri).build().toASCIIString());
    for (SitemapProvider provider : sitemapProviders) {
        for (String modelName : provider.getSitemapNames()) {
            Sitemap sitemap = provider.getSitemap(modelName);
            if (sitemap != null) {
                SitemapDTO bean = new SitemapDTO();
                bean.name = modelName;
                bean.icon = sitemap.getIcon();
                bean.label = sitemap.getLabel();
                bean.link = UriBuilder.fromUri(uri).path(bean.name).build().toASCIIString();
                bean.homepage = new PageDTO();
                bean.homepage.link = bean.link + "/" + sitemap.getName();
                beans.add(bean);
            }
        }
    }
    return beans;
}
Also used : Sitemap(org.eclipse.smarthome.model.sitemap.Sitemap) SitemapProvider(org.eclipse.smarthome.model.sitemap.SitemapProvider) LinkedList(java.util.LinkedList)

Example 2 with SitemapProvider

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

the class PageRenderer method renderSitemapList.

public CharSequence renderSitemapList(Set<SitemapProvider> sitemapProviders) throws RenderException {
    List<String> sitemapList = new LinkedList<String>();
    for (SitemapProvider sitemapProvider : sitemapProviders) {
        Set<String> sitemaps = sitemapProvider.getSitemapNames();
        for (String sitemap : sitemaps) {
            if (!sitemap.equals("_default")) {
                sitemapList.add(sitemap);
            }
        }
    }
    String pageSnippet = getSnippet("main_static");
    String listSnippet = getSnippet("sitemaps_list");
    String sitemapSnippet = getSnippet("sitemaps_list_item");
    StringBuilder sb = new StringBuilder();
    if (sitemapList.isEmpty()) {
        String listEmptySnippet = getSnippet("sitemaps_list_empty");
        listEmptySnippet = StringUtils.replace(listEmptySnippet, "%sitemaps-list-empty.info%", localizeText("@text/sitemaps-list-empty.info"));
        sb.append(listEmptySnippet);
    } else {
        for (String sitemap : sitemapList) {
            sb.append(StringUtils.replace(sitemapSnippet, "%sitemap%", sitemap));
        }
    }
    listSnippet = StringUtils.replace(listSnippet, "%sitemaps-list.welcome%", localizeText("@text/sitemaps-list.welcome"));
    listSnippet = StringUtils.replace(listSnippet, "%sitemaps-list.available-sitemaps%", localizeText("@text/sitemaps-list.available-sitemaps"));
    listSnippet = StringUtils.replace(listSnippet, "%items%", sb.toString());
    pageSnippet = StringUtils.replace(pageSnippet, "%title%", "BasicUI");
    pageSnippet = StringUtils.replace(pageSnippet, "%htmlclass%", config.getCssClassList() + " page-welcome-sitemaps");
    pageSnippet = StringUtils.replace(pageSnippet, "%content%", listSnippet);
    return pageSnippet;
}
Also used : SitemapProvider(org.eclipse.smarthome.model.sitemap.SitemapProvider) LinkedList(java.util.LinkedList)

Example 3 with SitemapProvider

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

the class WebAppServlet method service.

@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    logger.debug("Servlet request received!");
    // read request parameters
    String sitemapName = req.getParameter("sitemap");
    String widgetId = req.getParameter("w");
    String subscriptionId = req.getParameter("subscriptionId");
    boolean async = "true".equalsIgnoreCase(req.getParameter("__async"));
    if (sitemapName == null) {
        sitemapName = config.getDefaultSitemap();
    }
    StringBuilder result = new StringBuilder();
    Sitemap sitemap = null;
    for (SitemapProvider sitemapProvider : sitemapProviders) {
        sitemap = sitemapProvider.getSitemap(sitemapName);
        if (sitemap != null) {
            break;
        }
    }
    try {
        if (sitemap == null) {
            showSitemapList(res);
            return;
        }
        logger.debug("reading sitemap {}", sitemap.getName());
        if (widgetId == null || widgetId.isEmpty() || widgetId.equals(sitemapName)) {
            // we are at the homepage, so we render the children of the sitemap root node
            if (subscriptionId != null) {
                if (subscriptions.exists(subscriptionId)) {
                    subscriptions.setPageId(subscriptionId, sitemap.getName(), sitemapName);
                } else {
                    logger.debug("Basic UI requested a non-existing event subscription id ({})", subscriptionId);
                }
            }
            String label = sitemap.getLabel() != null ? sitemap.getLabel() : sitemapName;
            EList<Widget> children = renderer.getItemUIRegistry().getChildren(sitemap);
            result.append(renderer.processPage(sitemapName, sitemapName, label, children, async));
        } else if (!widgetId.equals("Colorpicker")) {
            // we are on some subpage, so we have to render the children of the widget that has been selected
            if (subscriptionId != null) {
                if (subscriptions.exists(subscriptionId)) {
                    subscriptions.setPageId(subscriptionId, sitemap.getName(), widgetId);
                } else {
                    logger.debug("Basic UI requested a non-existing event subscription id ({})", subscriptionId);
                }
            }
            Widget w = renderer.getItemUIRegistry().getWidget(sitemap, widgetId);
            if (w != null) {
                String label = renderer.getItemUIRegistry().getLabel(w);
                if (label == null) {
                    label = "undefined";
                }
                if (!(w instanceof LinkableWidget)) {
                    throw new RenderException("Widget '" + w + "' can not have any content");
                }
                EList<Widget> children = renderer.getItemUIRegistry().getChildren((LinkableWidget) w);
                result.append(renderer.processPage(renderer.getItemUIRegistry().getWidgetId(w), sitemapName, label, children, async));
            }
        }
    } catch (RenderException e) {
        throw new ServletException(e.getMessage(), e);
    }
    if (async) {
        res.setContentType(CONTENT_TYPE_ASYNC);
    } else {
        res.setContentType(CONTENT_TYPE);
    }
    res.getWriter().append(result);
    res.getWriter().close();
}
Also used : LinkableWidget(org.eclipse.smarthome.model.sitemap.LinkableWidget) ServletException(javax.servlet.ServletException) Sitemap(org.eclipse.smarthome.model.sitemap.Sitemap) RenderException(org.eclipse.smarthome.ui.basic.render.RenderException) EList(org.eclipse.emf.common.util.EList) SitemapProvider(org.eclipse.smarthome.model.sitemap.SitemapProvider) Widget(org.eclipse.smarthome.model.sitemap.Widget) LinkableWidget(org.eclipse.smarthome.model.sitemap.LinkableWidget)

Example 4 with SitemapProvider

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

the class WebAppServlet method service.

@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    logger.debug("Servlet request received!");
    // read request parameters
    String sitemapName = req.getParameter("sitemap");
    String widgetId = req.getParameter("w");
    boolean async = "true".equalsIgnoreCase(req.getParameter("__async"));
    boolean poll = "true".equalsIgnoreCase(req.getParameter("poll"));
    // if there are no parameters, display the "default" sitemap
    if (sitemapName == null) {
        sitemapName = config.getDefaultSitemap();
    }
    StringBuilder result = new StringBuilder();
    Sitemap sitemap = null;
    for (SitemapProvider sitemapProvider : sitemapProviders) {
        sitemap = sitemapProvider.getSitemap(sitemapName);
        if (sitemap != null) {
            break;
        }
    }
    try {
        if (sitemap == null) {
            throw new RenderException("Sitemap '" + sitemapName + "' could not be found");
        }
        logger.debug("reading sitemap {} widgetId {} async {} poll {}", sitemap.getName(), widgetId, async, poll);
        if (widgetId == null || widgetId.isEmpty() || widgetId.equals("Home")) {
            // we are at the homepage, so we render the children of the sitemap root node
            String label = sitemap.getLabel() != null ? sitemap.getLabel() : sitemapName;
            EList<Widget> children = renderer.getItemUIRegistry().getChildren(sitemap);
            if (poll && waitForChanges(children) == false) {
                // we have reached the timeout, so we do not return any content as nothing has changed
                res.getWriter().append(getTimeoutResponse()).close();
                return;
            }
            result.append(renderer.processPage("Home", sitemapName, label, children, async));
        } else if (!widgetId.equals("Colorpicker")) {
            // we are on some subpage, so we have to render the children of the widget that has been selected
            Widget w = renderer.getItemUIRegistry().getWidget(sitemap, widgetId);
            if (w != null) {
                if (!(w instanceof LinkableWidget)) {
                    throw new RenderException("Widget '" + w + "' can not have any content");
                }
                LinkableWidget lw = (LinkableWidget) w;
                EList<Widget> children = renderer.getItemUIRegistry().getChildren(lw);
                EList<Widget> parentAndChildren = new BasicEList<Widget>();
                parentAndChildren.add(lw);
                parentAndChildren.addAll(children);
                if (poll && waitForChanges(parentAndChildren) == false) {
                    // we have reached the timeout, so we do not return any content as nothing has changed
                    res.getWriter().append(getTimeoutResponse()).close();
                    return;
                }
                String label = renderer.getItemUIRegistry().getLabel(w);
                if (label == null) {
                    label = "undefined";
                }
                result.append(renderer.processPage(renderer.getItemUIRegistry().getWidgetId(w), sitemapName, label, children, async));
            }
        }
    } catch (RenderException e) {
        throw new ServletException(e.getMessage(), e);
    }
    if (async) {
        res.setContentType("application/xml;charset=UTF-8");
    } else {
        res.setContentType("text/html;charset=UTF-8");
    }
    res.getWriter().append(result);
    res.getWriter().close();
}
Also used : LinkableWidget(org.eclipse.smarthome.model.sitemap.LinkableWidget) ServletException(javax.servlet.ServletException) Sitemap(org.eclipse.smarthome.model.sitemap.Sitemap) RenderException(org.eclipse.smarthome.ui.classic.render.RenderException) BasicEList(org.eclipse.emf.common.util.BasicEList) EList(org.eclipse.emf.common.util.EList) SitemapProvider(org.eclipse.smarthome.model.sitemap.SitemapProvider) Widget(org.eclipse.smarthome.model.sitemap.Widget) LinkableWidget(org.eclipse.smarthome.model.sitemap.LinkableWidget)

Aggregations

SitemapProvider (org.eclipse.smarthome.model.sitemap.SitemapProvider)4 Sitemap (org.eclipse.smarthome.model.sitemap.Sitemap)3 LinkedList (java.util.LinkedList)2 ServletException (javax.servlet.ServletException)2 EList (org.eclipse.emf.common.util.EList)2 LinkableWidget (org.eclipse.smarthome.model.sitemap.LinkableWidget)2 Widget (org.eclipse.smarthome.model.sitemap.Widget)2 BasicEList (org.eclipse.emf.common.util.BasicEList)1 RenderException (org.eclipse.smarthome.ui.basic.render.RenderException)1 RenderException (org.eclipse.smarthome.ui.classic.render.RenderException)1