Search in sources :

Example 1 with Video

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

the class ProxyServletService method uriFromRequest.

/**
 * Determine which URI to address based on the request contents.
 *
 * @param request the servlet request. New attributes may be added to the request in order to cache the result for
 *            future calls.
 * @return the URI indicated by the request, or <code>null</code> if not possible
 */
URI uriFromRequest(HttpServletRequest request) {
    try {
        // Return any URI we've already saved for this request
        URI uri = (URI) request.getAttribute(ATTR_URI);
        if (uri != null) {
            return uri;
        } else {
            ProxyServletException pse = (ProxyServletException) request.getAttribute(ATTR_SERVLET_EXCEPTION);
            if (pse != null) {
                // If we errored on this request before, there is no point continuing
                return null;
            }
        }
        String sitemapName = request.getParameter("sitemap");
        if (sitemapName == null) {
            throw new ProxyServletException(HttpServletResponse.SC_BAD_REQUEST, "Parameter 'sitemap' must be provided!");
        }
        String widgetId = request.getParameter("widgetId");
        if (widgetId == null) {
            throw new ProxyServletException(HttpServletResponse.SC_BAD_REQUEST, "Parameter 'widgetId' must be provided!");
        }
        Sitemap sitemap = (Sitemap) modelRepository.getModel(sitemapName);
        if (sitemap == null) {
            throw new ProxyServletException(HttpServletResponse.SC_NOT_FOUND, String.format("Sitemap '%s' could not be found!", sitemapName));
        }
        Widget widget = itemUIRegistry.getWidget(sitemap, widgetId);
        if (widget == null) {
            throw new ProxyServletException(HttpServletResponse.SC_NOT_FOUND, String.format("Widget '%s' could not be found!", widgetId));
        }
        String uriString = null;
        if (widget instanceof Image) {
            uriString = ((Image) widget).getUrl();
        } else if (widget instanceof Video) {
            uriString = ((Video) widget).getUrl();
        } else {
            throw new ProxyServletException(HttpServletResponse.SC_FORBIDDEN, String.format("Widget type '%s' is not supported!", widget.getClass().getName()));
        }
        String itemName = widget.getItem();
        if (itemName != null) {
            State state = itemUIRegistry.getItemState(itemName);
            if (state != null && state instanceof StringType) {
                try {
                    uri = URI.create(state.toString());
                    request.setAttribute(ATTR_URI, uri);
                    return uri;
                } catch (IllegalArgumentException ex) {
                // fall thru
                }
            }
        }
        try {
            uri = URI.create(uriString);
            request.setAttribute(ATTR_URI, uri);
            return uri;
        } catch (IllegalArgumentException iae) {
            throw new ProxyServletException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, String.format("URI '%s' is not a valid URI.", uriString));
        }
    } catch (ProxyServletException pse) {
        request.setAttribute(ATTR_SERVLET_EXCEPTION, pse);
        return null;
    }
}
Also used : Sitemap(org.eclipse.smarthome.model.sitemap.Sitemap) StringType(org.eclipse.smarthome.core.library.types.StringType) Video(org.eclipse.smarthome.model.sitemap.Video) State(org.eclipse.smarthome.core.types.State) Widget(org.eclipse.smarthome.model.sitemap.Widget) Image(org.eclipse.smarthome.model.sitemap.Image) URI(java.net.URI)

Example 2 with Video

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

the class VideoRenderer method renderWidget.

@Override
public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderException {
    Video videoWidget = (Video) w;
    String snippet = null;
    String widgetId = itemUIRegistry.getWidgetId(w);
    String sitemap = w.eResource().getURI().path();
    if (videoWidget.getEncoding() != null && videoWidget.getEncoding().toLowerCase().contains("mjpeg")) {
        // we handle mjpeg streams as an html image as browser can usually handle this
        snippet = getSnippet("image");
        snippet = StringUtils.replace(snippet, "%setrefresh%", "");
        snippet = StringUtils.replace(snippet, "%refresh%", "");
    } else {
        snippet = getSnippet("video");
    }
    String url = "../proxy?sitemap=" + sitemap + "&widgetId=" + widgetId;
    String mediaType = "";
    if (videoWidget.getEncoding() != null && videoWidget.getEncoding().toLowerCase().contains("hls")) {
        // For HTTP Live Stream we don't proxy the URL and we set the appropriate media type
        State state = itemUIRegistry.getState(w);
        url = (state instanceof StringType) ? state.toString() : videoWidget.getUrl();
        mediaType = "type=\"application/vnd.apple.mpegurl\"";
    }
    snippet = StringUtils.replace(snippet, "%url%", url);
    snippet = StringUtils.replace(snippet, "%media_type%", mediaType);
    sb.append(snippet);
    return null;
}
Also used : StringType(org.eclipse.smarthome.core.library.types.StringType) Video(org.eclipse.smarthome.model.sitemap.Video) State(org.eclipse.smarthome.core.types.State)

Example 3 with Video

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

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

the class VideoRenderer method renderWidget.

@Override
public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderException {
    Video videoWidget = (Video) w;
    String snippet = null;
    String widgetId = itemUIRegistry.getWidgetId(w);
    String sitemap = w.eResource().getURI().path();
    // we handle mjpeg streams as an html image as browser can usually handle this
    String snippetName = (videoWidget.getEncoding() != null && videoWidget.getEncoding().toLowerCase().contains("mjpeg")) ? "image" : "video";
    snippet = getSnippet(snippetName);
    snippet = preprocessSnippet(snippet, w);
    State state = itemUIRegistry.getState(w);
    String url;
    if (snippetName.equals("image")) {
        boolean validUrl = false;
        if (videoWidget.getUrl() != null && !videoWidget.getUrl().isEmpty()) {
            try {
                URI.create(videoWidget.getUrl());
                validUrl = true;
            } catch (IllegalArgumentException ex) {
            }
        }
        String proxiedUrl = "../proxy?sitemap=" + sitemap + "&amp;widgetId=" + widgetId;
        if (!itemUIRegistry.getVisiblity(w)) {
            url = URL_NONE_ICON;
        } else if ((sitemap != null) && ((state instanceof StringType) || validUrl)) {
            url = proxiedUrl + "&amp;t=" + (new Date()).getTime();
        } else {
            url = URL_NONE_ICON;
        }
        snippet = StringUtils.replace(snippet, "%valid_url%", validUrl ? "true" : "false");
        snippet = StringUtils.replace(snippet, "%proxied_url%", proxiedUrl);
        snippet = StringUtils.replace(snippet, "%update_interval%", "0");
        snippet = StringUtils.replace(snippet, "%ignore_refresh%", "true");
        snippet = StringUtils.replace(snippet, "%url%", url);
    } else {
        String mediaType;
        if (videoWidget.getEncoding() != null && videoWidget.getEncoding().toLowerCase().contains("hls")) {
            // For HTTP Live Stream we don't proxy the URL and we set the appropriate media type
            url = (state instanceof StringType) ? state.toString() : videoWidget.getUrl();
            mediaType = "type=\"application/vnd.apple.mpegurl\"";
        } else {
            url = "../proxy?sitemap=" + sitemap + "&widgetId=" + widgetId;
            mediaType = "";
        }
        snippet = StringUtils.replace(snippet, "%url%", url);
        snippet = StringUtils.replace(snippet, "%media_type%", mediaType);
    }
    sb.append(snippet);
    return null;
}
Also used : StringType(org.eclipse.smarthome.core.library.types.StringType) Video(org.eclipse.smarthome.model.sitemap.Video) State(org.eclipse.smarthome.core.types.State) Date(java.util.Date)

Aggregations

Video (org.eclipse.smarthome.model.sitemap.Video)4 StringType (org.eclipse.smarthome.core.library.types.StringType)3 State (org.eclipse.smarthome.core.types.State)3 Image (org.eclipse.smarthome.model.sitemap.Image)2 Widget (org.eclipse.smarthome.model.sitemap.Widget)2 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 EList (org.eclipse.emf.common.util.EList)1 GenericItem (org.eclipse.smarthome.core.items.GenericItem)1 Item (org.eclipse.smarthome.core.items.Item)1 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)1 Chart (org.eclipse.smarthome.model.sitemap.Chart)1 Frame (org.eclipse.smarthome.model.sitemap.Frame)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