Search in sources :

Example 11 with GroupItem

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());
        }
    }
}
Also used : SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) Item(org.eclipse.smarthome.core.items.Item) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ByteArrayInputStream(java.io.ByteArrayInputStream) GroupItem(org.eclipse.smarthome.core.items.GroupItem) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 12 with GroupItem

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;
    }
}
Also used : ModelGroupItem(org.eclipse.smarthome.model.items.ModelGroupItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) ModelGroupFunction(org.eclipse.smarthome.model.items.ModelGroupFunction) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ModelGroupItem(org.eclipse.smarthome.model.items.ModelGroupItem) ModelNormalItem(org.eclipse.smarthome.model.items.ModelNormalItem) StateDescription(org.eclipse.smarthome.core.types.StateDescription)

Example 13 with GroupItem

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);
}
Also used : GroupFunction(org.eclipse.smarthome.core.items.GroupFunction) ModelGroupFunction(org.eclipse.smarthome.model.items.ModelGroupFunction) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ModelGroupItem(org.eclipse.smarthome.model.items.ModelGroupItem) GroupFunctionDTO(org.eclipse.smarthome.core.items.dto.GroupFunctionDTO)

Example 14 with GroupItem

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);
}
Also used : GroupFunction(org.eclipse.smarthome.core.items.GroupFunction) ModelGroupFunction(org.eclipse.smarthome.model.items.ModelGroupFunction) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ModelGroupItem(org.eclipse.smarthome.model.items.ModelGroupItem)

Example 15 with GroupItem

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());
    }
}
Also used : Item(org.eclipse.smarthome.core.items.Item) GroupItem(org.eclipse.smarthome.core.items.GroupItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem)

Aggregations

GroupItem (org.eclipse.smarthome.core.items.GroupItem)29 Item (org.eclipse.smarthome.core.items.Item)18 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)13 GenericItem (org.eclipse.smarthome.core.items.GenericItem)12 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)11 Test (org.junit.Test)8 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)7 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)7 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)5 ArithmeticGroupFunction (org.eclipse.smarthome.core.library.types.ArithmeticGroupFunction)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Date (java.util.Date)3 GroupFunction (org.eclipse.smarthome.core.items.GroupFunction)3 HistoricItem (org.eclipse.smarthome.core.persistence.HistoricItem)3 ModelGroupFunction (org.eclipse.smarthome.model.items.ModelGroupFunction)3 ModelGroupItem (org.eclipse.smarthome.model.items.ModelGroupItem)3 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2