use of org.eclipse.smarthome.core.items.GroupItem in project smarthome by eclipse.
the class GenericItemProvider2Test method testStableReloadOrder.
@Test
public void testStableReloadOrder() {
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()));
assertThat(itemRegistry.getAll().size(), is(10));
model = //
"Group testGroup " + //
"Number number1 (testGroup) " + //
"Number number2 (testGroup) " + //
"Number number3 (testGroup) " + //
"Number number4 (testGroup) " + //
"Number number5 (testGroup) " + //
"Number number6 (testGroup) " + //
"Number number7 \"Number Seven\" (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());
if (number == 7) {
assertEquals("Number Seven", item.getLabel());
}
}
}
use of org.eclipse.smarthome.core.items.GroupItem in project smarthome by eclipse.
the class GenericItemProvider method createItemFromModelItem.
private Item createItemFromModelItem(ModelItem modelItem) {
GenericItem item = null;
if (modelItem instanceof ModelGroupItem) {
ModelGroupItem modelGroupItem = (ModelGroupItem) modelItem;
GenericItem baseItem;
try {
baseItem = createItemOfType(modelGroupItem.getType(), modelGroupItem.getName());
} catch (IllegalArgumentException e) {
logger.debug("Error creating base item for group item '{}', item will be ignored: {}", modelGroupItem.getName(), e.getMessage());
return null;
}
if (baseItem != null) {
// if the user did not specify a function the first value of the enum in xtext (EQUAL) will be used
ModelGroupFunction function = modelGroupItem.getFunction();
item = applyGroupFunction(baseItem, modelGroupItem, function);
} else {
item = new GroupItem(modelGroupItem.getName());
}
} else {
ModelNormalItem normalItem = (ModelNormalItem) modelItem;
try {
item = createItemOfType(normalItem.getType(), normalItem.getName());
} catch (IllegalArgumentException e) {
logger.debug("Error creating item '{}', item will be ignored: {}", normalItem.getName(), e.getMessage());
return null;
}
}
if (item != null) {
String label = modelItem.getLabel();
String format = StringUtils.substringBetween(label, "[", "]");
if (format != null) {
label = StringUtils.substringBefore(label, "[").trim();
stateDescriptions.put(modelItem.getName(), new StateDescription(null, null, null, format, false, null));
}
item.setLabel(label);
item.setCategory(modelItem.getIcon());
assignTags(modelItem, item);
return item;
} else {
return null;
}
}
use of org.eclipse.smarthome.core.items.GroupItem in project smarthome by eclipse.
the class GenericItemProvider method applyGroupFunction.
private GroupItem applyGroupFunction(GenericItem baseItem, ModelGroupItem modelGroupItem, ModelGroupFunction function) {
GroupFunctionDTO dto = new GroupFunctionDTO();
dto.name = function.getName();
dto.params = modelGroupItem.getArgs().toArray(new String[modelGroupItem.getArgs().size()]);
GroupFunction groupFunction = ItemDTOMapper.mapFunction(baseItem, dto);
return new GroupItem(modelGroupItem.getName(), baseItem, groupFunction);
}
use of org.eclipse.smarthome.core.items.GroupItem in project smarthome by eclipse.
the class GenericItemProvider method hasGroupItemChanged.
private boolean hasGroupItemChanged(Item item1, Item item2) {
GroupItem gItem1 = null;
GroupItem gItem2 = null;
if (item1 instanceof GroupItem) {
gItem1 = (GroupItem) item1;
}
if (item2 instanceof GroupItem) {
gItem2 = (GroupItem) item2;
}
if (gItem1 == null && gItem2 == null) {
return false;
}
if ((gItem1 != null && gItem2 == null) || (gItem1 == null && gItem2 != null)) {
return true;
}
boolean sameBaseItemClass = Objects.equals(gItem1.getBaseItem(), gItem2.getBaseItem());
boolean sameFunction = false;
GroupFunction gf1 = gItem1.getFunction();
GroupFunction gf2 = gItem2.getFunction();
if (gf1 != null && gf2 != null) {
if (Objects.equals(gf1.getClass(), gf2.getClass())) {
sameFunction = Arrays.equals(gf1.getParameters(), gf2.getParameters());
}
} else if (gf1 == null && gf2 == null) {
sameFunction = true;
}
return !(sameBaseItemClass && sameFunction);
}
use of org.eclipse.smarthome.core.items.GroupItem in project habot by ghys.
the class AbstractItemIntentInterpreter method findItems.
/**
* Returns the items matching the entities in the intent by looking for tags prefixed by "object:" or "location:".
* Group items are expanded and the tags, and tags are inherited to members.
*
* The resulting items should match the object AND the location if both are provided.
*
* @param intent the {@link Intent} containing the entities to match to items' tags.
* @return the set of matching items
*/
protected Set<Item> findItems(Intent intent) {
Collection<Item> itemsWithLocationTag = null;
if (intent.entities.containsKey("location")) {
itemsWithLocationTag = itemRegistry.getItemsByTag("location:" + intent.entities.get("location"));
}
Collection<Item> itemsWithObjectTag = null;
if (intent.entities.containsKey("object")) {
itemsWithObjectTag = itemRegistry.getItemsByTag("object:" + intent.entities.get("object"));
}
HashSet<Item> itemsMatchingLocationSlot = null;
if (itemsWithLocationTag != null) {
itemsMatchingLocationSlot = new HashSet<Item>();
for (Item item : itemsWithLocationTag) {
if (item instanceof GroupItem) {
GroupItem groupItem = (GroupItem) item;
for (Item member : groupItem.getAllMembers()) {
itemsMatchingLocationSlot.add(member);
}
} else {
itemsMatchingLocationSlot.add(item);
}
}
}
HashSet<Item> itemsMatchingObjectSlot = null;
if (itemsWithObjectTag != null) {
itemsMatchingObjectSlot = new HashSet<Item>();
for (Item item : itemsWithObjectTag) {
if (item instanceof GroupItem) {
GroupItem groupItem = (GroupItem) item;
for (Item member : groupItem.getAllMembers()) {
itemsMatchingObjectSlot.add(member);
}
} else {
itemsMatchingObjectSlot.add(item);
}
}
}
if (itemsMatchingLocationSlot == null && itemsMatchingObjectSlot == null) {
return null;
} else if (itemsMatchingObjectSlot == null) {
return itemsMatchingLocationSlot;
} else if (itemsMatchingLocationSlot == null) {
return itemsMatchingObjectSlot;
} else {
return itemsMatchingLocationSlot.stream().filter(itemsMatchingObjectSlot::contains).collect(Collectors.toSet());
}
}
Aggregations