Esempio n. 1
0
  /** {@inheritDoc} */
  public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderException {
    Chart chart = (Chart) w;

    if (chart.getService() != null && !chart.getService().equals("rrd4j")) {
      logger.warn(
          "Chart service '{}' is not supported by the Classic UI, using rrd4j as fallback instead.");
    }

    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 = "/rrdchart.png?" + itemParam + "&period=" + chart.getPeriod() + "&random=1";

      String snippet = getSnippet("image");

      if (chart.getRefresh() > 0) {
        snippet =
            StringUtils.replace(
                snippet,
                "%setrefresh%",
                "<script type=\"text/javascript\">imagesToRefreshOnPage=1</script>");
        snippet =
            StringUtils.replace(
                snippet,
                "%refresh%",
                "id=\"%id%\" onload=\"setTimeout('reloadImage(\\'%url%\\', \\'%id%\\')', "
                    + chart.getRefresh()
                    + ")\"");
      } else {
        snippet = StringUtils.replace(snippet, "%setrefresh%", "");
        snippet = StringUtils.replace(snippet, "%refresh%", "");
      }

      snippet = StringUtils.replace(snippet, "%id%", itemUIRegistry.getWidgetId(w));
      snippet = StringUtils.replace(snippet, "%url%", url);
      snippet = StringUtils.replace(snippet, "%refresh%", Integer.toString(chart.getRefresh()));

      sb.append(snippet);
    } catch (ItemNotFoundException e) {
      logger.warn("Chart cannot be rendered as item '{}' does not exist.", chart.getItem());
    }
    return null;
  }
Esempio n. 2
0
  private static WidgetBean createWidgetBean(
      String sitemapName, Widget widget, boolean drillDown, URI uri, String widgetId) {
    ItemUIRegistry itemUIRegistry = RESTApplication.getItemUIRegistry();

    // Test visibility
    if (itemUIRegistry.getVisiblity(widget) == false) return null;

    WidgetBean bean = new WidgetBean();
    if (widget.getItem() != null) {
      Item item = ItemResource.getItem(widget.getItem());
      if (item != null) {
        bean.item =
            ItemResource.createItemBean(
                item, false, UriBuilder.fromUri(uri).build().toASCIIString());
      }
    }
    bean.widgetId = widgetId;
    bean.icon = itemUIRegistry.getIcon(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) {
        int cntWidget = 0;
        for (Widget child : children) {
          widgetId += "_" + cntWidget;
          WidgetBean subWidget = createWidgetBean(sitemapName, child, drillDown, uri, widgetId);
          if (subWidget != null) {
            bean.widgets.add(subWidget);
            cntWidget++;
          }
        }
      } else if (children.size() > 0) {
        String pageName = itemUIRegistry.getWidgetId(linkableWidget);
        bean.linkedPage =
            createPageBean(
                sitemapName,
                itemUIRegistry.getLabel(widget),
                itemUIRegistry.getIcon(widget),
                pageName,
                drillDown ? children : null,
                drillDown,
                isLeaf(children),
                uri);
      }
    }
    if (widget instanceof Switch) {
      Switch switchWidget = (Switch) widget;
      for (Mapping mapping : switchWidget.getMappings()) {
        MappingBean mappingBean = new MappingBean();
        // Remove quotes - if they exist
        if (mapping.getCmd() != null) {
          if (mapping.getCmd().startsWith("\"") && mapping.getCmd().endsWith("\"")) {
            mappingBean.command = mapping.getCmd().substring(1, mapping.getCmd().length() - 1);
          } else {
            mappingBean.command = mapping.getCmd();
          }
        } else {
          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()) {
        MappingBean mappingBean = new MappingBean();
        // Remove quotes - if they exist
        if (mapping.getCmd() != null) {
          if (mapping.getCmd().startsWith("\"") && mapping.getCmd().endsWith("\"")) {
            mappingBean.command = mapping.getCmd().substring(1, mapping.getCmd().length() - 1);
          } else {
            mappingBean.command = mapping.getCmd();
          }
        } else {
          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) {
      Image imageWidget = (Image) widget;
      String wId = itemUIRegistry.getWidgetId(widget);
      if (uri.getPort() < 0 || uri.getPort() == 80) {
        bean.url =
            uri.getScheme()
                + "://"
                + uri.getHost()
                + "/proxy?sitemap="
                + sitemapName
                + ".sitemap&widgetId="
                + wId;
      } else {
        bean.url =
            uri.getScheme()
                + "://"
                + uri.getHost()
                + ":"
                + uri.getPort()
                + "/proxy?sitemap="
                + sitemapName
                + ".sitemap&widgetId="
                + wId;
      }
      if (imageWidget.getRefresh() > 0) {
        bean.refresh = imageWidget.getRefresh();
      }
    }
    if (widget instanceof Video) {
      Video videoWidget = (Video) widget;
      String wId = itemUIRegistry.getWidgetId(widget);
      if (videoWidget.getEncoding() != null) {
        bean.encoding = videoWidget.getEncoding();
      }
      if (uri.getPort() < 0 || uri.getPort() == 80) {
        bean.url =
            uri.getScheme()
                + "://"
                + uri.getHost()
                + "/proxy?sitemap="
                + sitemapName
                + ".sitemap&widgetId="
                + wId;
      } else {
        bean.url =
            uri.getScheme()
                + "://"
                + uri.getHost()
                + ":"
                + uri.getPort()
                + "/proxy?sitemap="
                + sitemapName
                + ".sitemap&widgetId="
                + wId;
      }
    }
    if (widget instanceof Webview) {
      Webview webViewWidget = (Webview) widget;
      bean.url = webViewWidget.getUrl();
      bean.height = webViewWidget.getHeight();
    }
    if (widget instanceof Chart) {
      Chart chartWidget = (Chart) widget;
      bean.service = chartWidget.getService();
      bean.period = chartWidget.getPeriod();
      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;
  }