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

    String snippetName = "slider";

    String snippet = getSnippet(snippetName);

    // set the default send-update frequency to 200ms
    String frequency = s.getFrequency() == 0 ? "200" : Integer.toString(s.getFrequency());

    snippet = StringUtils.replace(snippet, "%id%", itemUIRegistry.getWidgetId(s));
    snippet = StringUtils.replace(snippet, "%icon%", escapeURLPath(itemUIRegistry.getIcon(s)));
    snippet = StringUtils.replace(snippet, "%item%", w.getItem());
    snippet = StringUtils.replace(snippet, "%label%", getLabel(s));
    snippet = StringUtils.replace(snippet, "%state%", itemUIRegistry.getState(s).toString());
    snippet = StringUtils.replace(snippet, "%frequency%", frequency);
    snippet = StringUtils.replace(snippet, "%switch%", s.isSwitchEnabled() ? "1" : "0");
    snippet = StringUtils.replace(snippet, "%servletname%", WebAppServlet.SERVLET_PATH);

    // Process the color tags
    snippet = processColor(w, snippet);

    sb.append(snippet);
    return null;
  }
  /** {@inheritDoc} */
  public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderException {
    Colorpicker cp = (Colorpicker) w;

    String snippetName = "colorpicker";

    String snippet = getSnippet(snippetName);

    // set the default send-update frequency to 200ms
    String frequency = cp.getFrequency() == 0 ? "200" : Integer.toString(cp.getFrequency());

    // get RGB hex value
    State state = itemUIRegistry.getState(cp);
    String hexValue = "#ffffff";
    if (state instanceof HSBType) {
      HSBType hsbState = (HSBType) state;
      Color color = hsbState.toColor();
      hexValue = "#" + Integer.toHexString(color.getRGB()).substring(2);
    }
    String label = getLabel(cp);
    String purelabel = label;
    if (label.contains("<span>")) {
      purelabel = purelabel.substring(0, label.indexOf("<span>"));
    }

    snippet = StringUtils.replace(snippet, "%id%", itemUIRegistry.getWidgetId(cp));
    snippet = StringUtils.replace(snippet, "%icon%", escapeURLPath(itemUIRegistry.getIcon(cp)));
    snippet = StringUtils.replace(snippet, "%item%", w.getItem());
    snippet = StringUtils.replace(snippet, "%label%", label);
    snippet = StringUtils.replace(snippet, "%purelabel%", purelabel);
    snippet = StringUtils.replace(snippet, "%state%", hexValue);
    snippet = StringUtils.replace(snippet, "%frequency%", frequency);
    snippet = StringUtils.replace(snippet, "%servletname%", WebAppServlet.SERVLET_NAME);

    String style = "";
    String color = itemUIRegistry.getLabelColor(w);
    if (color != null) {
      style = "color:" + color;
    }
    snippet = StringUtils.replace(snippet, "%labelstyle%", style);

    style = "";
    color = itemUIRegistry.getValueColor(w);
    if (color != null) {
      style = "color:" + color;
    }
    snippet = StringUtils.replace(snippet, "%valuestyle%", style);

    sb.append(snippet);
    return null;
  }
Esempio n. 3
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;
  }
Esempio n. 4
0
 public static PageBean getPageBean(String sitemapName, String pageId, URI uri) {
   ItemUIRegistry itemUIRegistry = RESTApplication.getItemUIRegistry();
   Sitemap sitemap = getSitemap(sitemapName);
   if (sitemap != null) {
     if (pageId.equals(sitemap.getName())) {
       return createPageBean(
           sitemapName,
           sitemap.getLabel(),
           sitemap.getIcon(),
           sitemap.getName(),
           sitemap.getChildren(),
           false,
           isLeaf(sitemap.getChildren()),
           uri);
     } else {
       Widget pageWidget = itemUIRegistry.getWidget(sitemap, pageId);
       if (pageWidget instanceof LinkableWidget) {
         EList<Widget> children = itemUIRegistry.getChildren((LinkableWidget) pageWidget);
         PageBean pageBean =
             createPageBean(
                 sitemapName,
                 itemUIRegistry.getLabel(pageWidget),
                 itemUIRegistry.getIcon(pageWidget),
                 pageId,
                 children,
                 false,
                 isLeaf(children),
                 uri);
         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);
           pageBean.parent.widgets = null;
           pageBean.parent.parent = null;
         } else if (parentPage instanceof Sitemap) {
           pageBean.parent = getPageBean(sitemapName, sitemap.getName(), uri);
           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);
   }
 }
 @Override
 public String getIcon(Widget w) {
   return itemUIRegistry.getIcon(w.getItem());
 }
  /** {@inheritDoc} */
  public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderException {
    Switch s = (Switch) w;

    String snippetName = null;
    Item item;
    try {
      item = itemUIRegistry.getItem(w.getItem());
      if (s.getMappings().size() == 0) {
        if (item instanceof RollershutterItem) {
          snippetName = "rollerblind";
        } else if (item instanceof GroupItem
            && ((GroupItem) item).getBaseItem() instanceof RollershutterItem) {
          snippetName = "rollerblind";
        } else {
          snippetName = "switch";
        }
      } else {
        snippetName = "buttons";
      }
    } catch (ItemNotFoundException e) {
      logger.warn("Cannot determine item type of '{}'", w.getItem(), e);
      snippetName = "switch";
    }

    String snippet = getSnippet(snippetName);

    snippet = StringUtils.replace(snippet, "%id%", itemUIRegistry.getWidgetId(w));
    System.out.println("\nSwitchRenderer->getICon->" + itemUIRegistry.getIcon(w));
    snippet = StringUtils.replace(snippet, "%icon%", escapeURLPath(itemUIRegistry.getIcon(w)));
    snippet = StringUtils.replace(snippet, "%item%", w.getItem());
    snippet = StringUtils.replace(snippet, "%label%", getLabel(w));
    snippet = StringUtils.replace(snippet, "%servletname%", WebAppServlet.SERVLET_NAME);

    State state = itemUIRegistry.getState(w);

    System.out.println("\nSwitchRenderer->getState->" + state.toString());
    if (s.getMappings().size() == 0) {

      if (state instanceof PercentType) {
        state = ((PercentType) state).intValue() > 0 ? OnOffType.ON : OnOffType.OFF;
      }
      if (state.equals(OnOffType.ON)) {
        snippet = snippet.replaceAll("%checked%", "checked=true");
      } else {
        snippet = snippet.replaceAll("%checked%", "");
      }
    } else {
      StringBuilder buttons = new StringBuilder();
      for (Mapping mapping : s.getMappings()) {
        String button = getSnippet("button");
        button = StringUtils.replace(button, "%item%", w.getItem());
        button = StringUtils.replace(button, "%cmd%", mapping.getCmd());
        button = StringUtils.replace(button, "%label%", mapping.getLabel());
        if (s.getMappings().size() > 1 && state.toString().equals(mapping.getCmd())) {
          button = StringUtils.replace(button, "%type%", "Warn"); // button with red color
        } else {
          button = StringUtils.replace(button, "%type%", "Action"); // button with blue color
        }
        buttons.insert(0, button);
      }
      snippet = StringUtils.replace(snippet, "%buttons%", buttons.toString());
    }

    // Process the color tags
    snippet = processColor(w, snippet);

    sb.append(snippet);
    return null;
  }