use of org.eclipse.smarthome.model.sitemap.LinkableWidget 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();
}
use of org.eclipse.smarthome.model.sitemap.LinkableWidget 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;
}
use of org.eclipse.smarthome.model.sitemap.LinkableWidget in project smarthome by eclipse.
the class SitemapResource method blockUnlessChangeOccurs.
private boolean blockUnlessChangeOccurs(String sitemapname, String pageId) {
boolean timeout = false;
Sitemap sitemap = getSitemap(sitemapname);
if (sitemap != null) {
if (pageId.equals(sitemap.getName())) {
EList<Widget> children = itemUIRegistry.getChildren(sitemap);
timeout = waitForChanges(children);
} else {
Widget pageWidget = itemUIRegistry.getWidget(sitemap, pageId);
if (pageWidget instanceof LinkableWidget) {
EList<Widget> children = itemUIRegistry.getChildren((LinkableWidget) pageWidget);
timeout = waitForChanges(children);
}
}
}
return timeout;
}
Aggregations