Search in sources :

Example 1 with Metadata

use of org.eclipse.smarthome.core.items.Metadata in project smarthome by eclipse.

the class GenericMetadataProvider method removeMetadata.

/**
 * Removes all meta-data for a given item name
 *
 * @param itemName
 */
public void removeMetadata(String itemName) {
    Set<Metadata> toBeRemoved;
    try {
        lock.writeLock().lock();
        toBeRemoved = metadata.stream().filter(MetadataPredicates.ofItem(itemName)).collect(toSet());
        metadata.removeAll(toBeRemoved);
    } finally {
        lock.writeLock().unlock();
    }
    for (Metadata m : toBeRemoved) {
        notifyListenersAboutRemovedElement(m);
    }
}
Also used : Metadata(org.eclipse.smarthome.core.items.Metadata)

Example 2 with Metadata

use of org.eclipse.smarthome.core.items.Metadata in project smarthome by eclipse.

the class GenericItemProvider2Test method testMetadataUpdate.

@Test
public void testMetadataUpdate() {
    modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream("Switch s { meta=\"foo\" }".getBytes()));
    Metadata metadata1 = metadataRegistry.get(new MetadataKey("meta", "s"));
    assertNotNull(metadata1);
    assertEquals("foo", metadata1.getValue());
    modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream("Switch s { meta=\"bar\" }".getBytes()));
    Metadata metadata2 = metadataRegistry.get(new MetadataKey("meta", "s"));
    assertNotNull(metadata2);
    assertEquals("bar", metadata2.getValue());
    modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream("Switch s".getBytes()));
    Metadata metadata3 = metadataRegistry.get(new MetadataKey("meta", "s"));
    assertNull(metadata3);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Metadata(org.eclipse.smarthome.core.items.Metadata) MetadataKey(org.eclipse.smarthome.core.items.MetadataKey) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 3 with Metadata

use of org.eclipse.smarthome.core.items.Metadata in project smarthome by eclipse.

the class GenericItemProvider2Test method testMetadata_simple.

@Test
public void testMetadata_simple() {
    String model = "Switch simple { namespace=\"value\" } ";
    modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
    Item item = itemRegistry.get("simple");
    assertNotNull(item);
    Metadata res = metadataRegistry.get(new MetadataKey("namespace", "simple"));
    assertNotNull(res);
    assertEquals("value", res.getValue());
    assertNotNull(res.getConfiguration());
}
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) Metadata(org.eclipse.smarthome.core.items.Metadata) MetadataKey(org.eclipse.smarthome.core.items.MetadataKey) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 4 with Metadata

use of org.eclipse.smarthome.core.items.Metadata in project smarthome by eclipse.

the class GenericItemProvider2Test method testMetadata_configured.

@Test
public void testMetadata_configured() {
    String model = // 
    "Switch simple { namespace=\"value\" } " + "Switch configured { foo=\"bar\" [ answer=42 ] } ";
    modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
    Item item = itemRegistry.get("configured");
    assertNotNull(item);
    Metadata res = metadataRegistry.get(new MetadataKey("foo", "configured"));
    assertNotNull(res);
    assertEquals("bar", res.getValue());
    assertEquals(new BigDecimal(42), res.getConfiguration().get("answer"));
    modelRepository.removeModel(TESTMODEL_NAME);
    res = metadataRegistry.get(new MetadataKey("foo", "configured"));
    assertNull(res);
}
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) Metadata(org.eclipse.smarthome.core.items.Metadata) MetadataKey(org.eclipse.smarthome.core.items.MetadataKey) BigDecimal(java.math.BigDecimal) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 5 with Metadata

use of org.eclipse.smarthome.core.items.Metadata in project smarthome by eclipse.

the class ItemResource method addMetadata.

private void addMetadata(EnrichedItemDTO dto, Set<String> namespaces, @Nullable Predicate<Metadata> filter) {
    Map<String, Object> metadata = new HashMap<>();
    for (String namespace : namespaces) {
        MetadataKey key = new MetadataKey(namespace, dto.name);
        Metadata md = metadataRegistry.get(key);
        if (md != null && (filter == null || filter.test(md))) {
            MetadataDTO mdDto = new MetadataDTO();
            mdDto.value = md.getValue();
            mdDto.config = md.getConfiguration().isEmpty() ? null : md.getConfiguration();
            metadata.put(namespace, mdDto);
        }
    }
    if (dto instanceof EnrichedGroupItemDTO) {
        for (EnrichedItemDTO member : ((EnrichedGroupItemDTO) dto).members) {
            addMetadata(member, namespaces, filter);
        }
    }
    if (!metadata.isEmpty()) {
        // we only set it in the dto if there is really data available
        dto.metadata = metadata;
    }
}
Also used : HashMap(java.util.HashMap) EnrichedItemDTO(org.eclipse.smarthome.io.rest.core.item.EnrichedItemDTO) Metadata(org.eclipse.smarthome.core.items.Metadata) JsonObject(com.google.gson.JsonObject) MetadataKey(org.eclipse.smarthome.core.items.MetadataKey) MetadataDTO(org.eclipse.smarthome.core.items.dto.MetadataDTO) EnrichedGroupItemDTO(org.eclipse.smarthome.io.rest.core.item.EnrichedGroupItemDTO)

Aggregations

Metadata (org.eclipse.smarthome.core.items.Metadata)15 MetadataKey (org.eclipse.smarthome.core.items.MetadataKey)14 Test (org.junit.Test)6 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)5 GenericItem (org.eclipse.smarthome.core.items.GenericItem)4 GroupItem (org.eclipse.smarthome.core.items.GroupItem)4 Item (org.eclipse.smarthome.core.items.Item)4 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 HashMap (java.util.HashMap)3 JsonObject (com.google.gson.JsonObject)2 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 RolesAllowed (javax.annotation.security.RolesAllowed)2 Consumes (javax.ws.rs.Consumes)2 PUT (javax.ws.rs.PUT)2 Path (javax.ws.rs.Path)2 Response (javax.ws.rs.core.Response)2 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)2 MetadataDTO (org.eclipse.smarthome.core.items.dto.MetadataDTO)2