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;
}
Aggregations