Search in sources :

Example 1 with MetadataKey

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

the class MetadataConsoleCommandExtension method removeMetadata.

private void removeMetadata(Console console, String itemName, @Nullable String namespace) {
    if (itemRegistry.get(itemName) == null) {
        console.println("Warning: Item " + itemName + " does not exist, removing metadata anyway.");
    }
    if (namespace == null) {
        metadataRegistry.stream().filter(MetadataPredicates.ofItem(itemName)).map(Metadata::getUID).forEach(key -> removeMetadata(console, key));
    } else {
        MetadataKey key = new MetadataKey(namespace, itemName);
        removeMetadata(console, key);
    }
}
Also used : MetadataKey(org.eclipse.smarthome.core.items.MetadataKey)

Example 2 with MetadataKey

use of org.eclipse.smarthome.core.items.MetadataKey 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 MetadataKey

use of org.eclipse.smarthome.core.items.MetadataKey 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 MetadataKey

use of org.eclipse.smarthome.core.items.MetadataKey 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 MetadataKey

use of org.eclipse.smarthome.core.items.MetadataKey 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

MetadataKey (org.eclipse.smarthome.core.items.MetadataKey)15 Metadata (org.eclipse.smarthome.core.items.Metadata)13 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 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 HashMap (java.util.HashMap)2 RolesAllowed (javax.annotation.security.RolesAllowed)2 Path (javax.ws.rs.Path)2 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)2 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)2 JsonObject (com.google.gson.JsonObject)1 BigDecimal (java.math.BigDecimal)1 Consumes (javax.ws.rs.Consumes)1 DELETE (javax.ws.rs.DELETE)1