use of org.eclipse.smarthome.core.items.GroupItem in project smarthome by eclipse.
the class PersistenceManagerImpl method getAllItems.
/**
* Retrieves all items for which the persistence configuration applies to.
*
* @param config the persistence configuration entry
* @return all items that this configuration applies to
*/
Iterable<Item> getAllItems(SimpleItemConfiguration config) {
// first check, if we should return them all
for (Object itemCfg : config.getItems()) {
if (itemCfg instanceof SimpleAllConfig) {
return itemRegistry.getItems();
}
}
// otherwise, go through the detailed definitions
Set<Item> items = new HashSet<Item>();
for (Object itemCfg : config.getItems()) {
if (itemCfg instanceof SimpleItemConfig) {
SimpleItemConfig singleItemConfig = (SimpleItemConfig) itemCfg;
try {
Item item = itemRegistry.getItem(singleItemConfig.getItem());
items.add(item);
} catch (ItemNotFoundException e) {
logger.debug("Item '{}' does not exist.", singleItemConfig.getItem());
}
}
if (itemCfg instanceof SimpleGroupConfig) {
SimpleGroupConfig groupItemCfg = (SimpleGroupConfig) itemCfg;
String groupName = groupItemCfg.getGroup();
try {
Item gItem = itemRegistry.getItem(groupName);
if (gItem instanceof GroupItem) {
GroupItem groupItem = (GroupItem) gItem;
items.addAll(groupItem.getAllMembers());
}
} catch (ItemNotFoundException e) {
logger.debug("Item group '{}' does not exist.", groupName);
}
}
}
return items;
}
use of org.eclipse.smarthome.core.items.GroupItem in project smarthome by eclipse.
the class DefaultChartProvider method createChart.
@Override
public BufferedImage createChart(String service, String theme, Date startTime, Date endTime, int height, int width, String items, String groups, Integer dpiValue, Boolean legend) throws ItemNotFoundException, IllegalArgumentException {
logger.debug("Rendering chart: service: '{}', theme: '{}', startTime: '{}', endTime: '{}', width: '{}', height: '{}', items: '{}', groups: '{}', dpi: '{}', legend: '{}'", service, theme, startTime, endTime, width, height, items, groups, dpiValue, legend);
QueryablePersistenceService persistenceService;
int seriesCounter = 0;
// get theme
ChartTheme chartTheme = getChartTheme(theme);
// get DPI
int dpi;
if (dpiValue != null && dpiValue > 0) {
dpi = dpiValue;
} else {
dpi = DPI_DEFAULT;
}
// Create Chart
Chart chart = new ChartBuilder().width(width).height(height).build();
// Define the time axis - the defaults are not very nice
long period = (endTime.getTime() - startTime.getTime()) / 1000;
String pattern = "HH:mm";
if (period <= 600) {
// 10 minutes
pattern = "mm:ss";
} else if (period <= 86400) {
// 1 day
pattern = "HH:mm";
} else if (period <= 604800) {
// 1 week
pattern = "EEE d";
} else {
pattern = "d MMM";
}
chart.getStyleManager().setDatePattern(pattern);
// axis
chart.getStyleManager().setAxisTickLabelsFont(chartTheme.getAxisTickLabelsFont(dpi));
chart.getStyleManager().setAxisTickLabelsColor(chartTheme.getAxisTickLabelsColor());
chart.getStyleManager().setXAxisMin(startTime.getTime());
chart.getStyleManager().setXAxisMax(endTime.getTime());
int yAxisSpacing = Math.max(height / 10, chartTheme.getAxisTickLabelsFont(dpi).getSize());
chart.getStyleManager().setYAxisTickMarkSpacingHint(yAxisSpacing);
// chart
chart.getStyleManager().setChartBackgroundColor(chartTheme.getChartBackgroundColor());
chart.getStyleManager().setChartFontColor(chartTheme.getChartFontColor());
chart.getStyleManager().setChartPadding(chartTheme.getChartPadding(dpi));
chart.getStyleManager().setPlotBackgroundColor(chartTheme.getPlotBackgroundColor());
float plotGridLinesDash = (float) chartTheme.getPlotGridLinesDash(dpi);
float[] plotGridLinesDashArray = { plotGridLinesDash, plotGridLinesDash };
chart.getStyleManager().setPlotGridLinesStroke(new BasicStroke((float) chartTheme.getPlotGridLinesWidth(dpi), 0, 2, 10, plotGridLinesDashArray, 0));
chart.getStyleManager().setPlotGridLinesColor(chartTheme.getPlotGridLinesColor());
// legend
chart.getStyleManager().setLegendBackgroundColor(chartTheme.getLegendBackgroundColor());
chart.getStyleManager().setLegendFont(chartTheme.getLegendFont(dpi));
chart.getStyleManager().setLegendSeriesLineLength(chartTheme.getLegendSeriesLineLength(dpi));
// If a persistence service is specified, find the provider
persistenceService = null;
if (service != null) {
persistenceService = getPersistenceServices().get(service);
} else {
// Otherwise, just get the first service, if one exists
Iterator<Entry<String, QueryablePersistenceService>> it = getPersistenceServices().entrySet().iterator();
if (it.hasNext()) {
persistenceService = it.next().getValue();
} else {
throw new IllegalArgumentException("No Persistence service found.");
}
}
// Did we find a service?
if (persistenceService == null) {
throw new IllegalArgumentException("Persistence service not found '" + service + "'.");
}
// Loop through all the items
if (items != null) {
String[] itemNames = items.split(",");
for (String itemName : itemNames) {
Item item = itemUIRegistry.getItem(itemName);
if (addItem(chart, persistenceService, startTime, endTime, item, seriesCounter, chartTheme, dpi)) {
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()) {
if (addItem(chart, persistenceService, startTime, endTime, member, seriesCounter, chartTheme, dpi)) {
seriesCounter++;
}
}
} else {
throw new ItemNotFoundException("Item '" + item.getName() + "' defined in groups is not a group.");
}
}
}
Boolean showLegend = null;
// If there are no series, render a blank chart
if (seriesCounter == 0) {
// always hide the legend
showLegend = false;
List<Date> xData = new ArrayList<Date>();
List<Number> yData = new ArrayList<Number>();
xData.add(startTime);
yData.add(0);
xData.add(endTime);
yData.add(0);
Series series = chart.addSeries("NONE", xData, yData);
series.setMarker(SeriesMarker.NONE);
series.setLineStyle(new BasicStroke(0f));
}
// if the legend is not already hidden, check if legend parameter is supplied, or calculate a sensible value
if (showLegend == null) {
if (legend == null) {
// more than one series, show the legend. otherwise hide it.
showLegend = seriesCounter > 1;
} else {
// take value from supplied legend parameter
showLegend = legend;
}
}
// This won't be perfect, but it's a good compromise
if (showLegend) {
if (legendPosition < 0) {
chart.getStyleManager().setLegendPosition(LegendPosition.InsideNW);
} else {
chart.getStyleManager().setLegendPosition(LegendPosition.InsideSW);
}
} else {
// hide the whole legend
chart.getStyleManager().setLegendVisible(false);
}
// Write the chart as a PNG image
BufferedImage lBufferedImage = new BufferedImage(chart.getWidth(), chart.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D lGraphics2D = lBufferedImage.createGraphics();
chart.paint(lGraphics2D);
return lBufferedImage;
}
use of org.eclipse.smarthome.core.items.GroupItem in project smarthome by eclipse.
the class GenericItemProvider2Test method testStableOrder.
@Test
public void testStableOrder() {
assertThat(itemRegistry.getAll().size(), is(0));
String model = //
"Group testGroup " + //
"Number number1 (testGroup) " + //
"Number number2 (testGroup) " + //
"Number number3 (testGroup) " + //
"Number number4 (testGroup) " + //
"Number number5 (testGroup) " + //
"Number number6 (testGroup) " + //
"Number number7 (testGroup) " + //
"Number number8 (testGroup) " + "Number number9 (testGroup) ";
modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
GroupItem groupItem = (GroupItem) itemRegistry.get("testGroup");
assertNotNull(groupItem);
int number = 0;
Iterator<Item> it = groupItem.getMembers().iterator();
while (it.hasNext()) {
Item item = it.next();
assertEquals("number" + (++number), item.getName());
}
}
use of org.eclipse.smarthome.core.items.GroupItem in project smarthome by eclipse.
the class GenericItemProvider2Test method testGroupItemIsSame.
@Test
public void testGroupItemIsSame() {
GenericItemProvider gip = new GenericItemProvider();
GroupItem g1 = new GroupItem("testGroup", new SwitchItem("test"), new ArithmeticGroupFunction.Or(OnOffType.ON, OnOffType.OFF));
GroupItem g2 = new GroupItem("testGroup", new SwitchItem("test"), new ArithmeticGroupFunction.Or(OnOffType.ON, OnOffType.OFF));
assertFalse(gip.hasItemChanged(g1, g2));
}
use of org.eclipse.smarthome.core.items.GroupItem in project smarthome by eclipse.
the class GenericItemProvider2Test method testGroupItemChangesFunctionParameters.
@Test
public void testGroupItemChangesFunctionParameters() {
GenericItemProvider gip = new GenericItemProvider();
GroupItem g1 = new GroupItem("testGroup", new SwitchItem("test"), new ArithmeticGroupFunction.Or(OnOffType.ON, OnOffType.OFF));
GroupItem g2 = new GroupItem("testGroup", new SwitchItem("test"), new ArithmeticGroupFunction.Or(OnOffType.ON, UnDefType.UNDEF));
assertTrue(gip.hasItemChanged(g1, g2));
}
Aggregations