Search in sources :

Example 16 with ItemNotFoundException

use of org.openhab.core.items.ItemNotFoundException in project openhab1-addons by openhab.

the class RRD4jChartServlet method createChart.

@Override
public BufferedImage createChart(String service, String theme, Date startTime, Date endTime, int height, int width, String items, String groups) throws ItemNotFoundException {
    RrdGraphDef graphDef = new RrdGraphDef();
    long period = (startTime.getTime() - endTime.getTime()) / 1000;
    graphDef.setWidth(width);
    graphDef.setHeight(height);
    graphDef.setAntiAliasing(true);
    graphDef.setImageFormat("PNG");
    graphDef.setStartTime(period);
    graphDef.setTextAntiAliasing(true);
    graphDef.setLargeFont(new Font("SansSerif", Font.PLAIN, 15));
    graphDef.setSmallFont(new Font("SansSerif", Font.PLAIN, 11));
    int seriesCounter = 0;
    // Loop through all the items
    if (items != null) {
        String[] itemNames = items.split(",");
        for (String itemName : itemNames) {
            Item item = itemUIRegistry.getItem(itemName);
            addLine(graphDef, item, seriesCounter++);
        }
    }
    // Loop through all the groups and add each item from each group
    if (groups != null) {
        String[] groupNames = groups.split(",");
        for (String groupName : groupNames) {
            Item item = itemUIRegistry.getItem(groupName);
            if (item instanceof GroupItem) {
                GroupItem groupItem = (GroupItem) item;
                for (Item member : groupItem.getMembers()) {
                    addLine(graphDef, member, seriesCounter++);
                }
            } else {
                throw new ItemNotFoundException("Item '" + item.getName() + "' defined in groups is not a group.");
            }
        }
    }
    // Write the chart as a PNG image
    RrdGraph graph;
    try {
        graph = new RrdGraph(graphDef);
        BufferedImage bi = new BufferedImage(graph.getRrdGraphInfo().getWidth(), graph.getRrdGraphInfo().getHeight(), BufferedImage.TYPE_INT_RGB);
        graph.render(bi.getGraphics());
        return bi;
    } catch (IOException e) {
        logger.error("Error generating graph: {}", e);
    }
    return null;
}
Also used : RrdGraphDef(org.rrd4j.graph.RrdGraphDef) NumberItem(org.openhab.core.library.items.NumberItem) GroupItem(org.openhab.core.items.GroupItem) Item(org.openhab.core.items.Item) RrdGraph(org.rrd4j.graph.RrdGraph) GroupItem(org.openhab.core.items.GroupItem) IOException(java.io.IOException) Font(java.awt.Font) BufferedImage(java.awt.image.BufferedImage) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Aggregations

ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)16 Item (org.openhab.core.items.Item)15 NumberItem (org.openhab.core.library.items.NumberItem)7 ContactItem (org.openhab.core.library.items.ContactItem)6 DimmerItem (org.openhab.core.library.items.DimmerItem)6 State (org.openhab.core.types.State)6 RollershutterItem (org.openhab.core.library.items.RollershutterItem)5 SwitchItem (org.openhab.core.library.items.SwitchItem)5 DecimalType (org.openhab.core.library.types.DecimalType)5 ArrayList (java.util.ArrayList)4 IOException (java.io.IOException)3 SappBindingConfigContactItem (org.openhab.binding.sapp.internal.configs.SappBindingConfigContactItem)3 SappBindingConfigDimmerItem (org.openhab.binding.sapp.internal.configs.SappBindingConfigDimmerItem)3 SappBindingConfigNumberItem (org.openhab.binding.sapp.internal.configs.SappBindingConfigNumberItem)3 SappBindingConfigRollershutterItem (org.openhab.binding.sapp.internal.configs.SappBindingConfigRollershutterItem)3 SappBindingConfigSwitchItem (org.openhab.binding.sapp.internal.configs.SappBindingConfigSwitchItem)3 SappAddressDecimal (org.openhab.binding.sapp.internal.model.SappAddressDecimal)3 SappAddressDimmer (org.openhab.binding.sapp.internal.model.SappAddressDimmer)3 GroupItem (org.openhab.core.items.GroupItem)3 ColorItem (org.openhab.core.library.items.ColorItem)3