Search in sources :

Example 1 with MetadataRegistry

use of org.openhab.core.items.MetadataRegistry in project openhab-addons by openhab.

the class HomekitAccessoryFactory method addOptionalCharacteristics.

/**
 * add optional characteristic for given accessory.
 *
 * @param taggedItem main item
 * @param accessory accessory
 * @param metadataRegistry metadata registry
 */
private static void addOptionalCharacteristics(HomekitTaggedItem taggedItem, AbstractHomekitAccessoryImpl accessory, MetadataRegistry metadataRegistry) {
    Map<HomekitCharacteristicType, GenericItem> characteristics = getOptionalCharacteristics(accessory.getRootAccessory(), metadataRegistry);
    Service service = accessory.getPrimaryService();
    HashMap<String, HomekitOHItemProxy> proxyItems = new HashMap<>();
    proxyItems.put(taggedItem.getItem().getUID(), taggedItem.getProxyItem());
    // an accessory can have multiple optional characteristics. iterate over them.
    characteristics.forEach((type, item) -> {
        try {
            // check whether a proxyItem already exists, if not create one.
            final HomekitOHItemProxy proxyItem = Objects.requireNonNull(proxyItems.computeIfAbsent(item.getUID(), k -> new HomekitOHItemProxy(item)));
            final HomekitTaggedItem optionalItem = new HomekitTaggedItem(proxyItem, accessory.getRootAccessory().getAccessoryType(), type, accessory.getRootAccessory().getRootDeviceGroupItem(), getItemConfiguration(item, metadataRegistry));
            final Characteristic characteristic = HomekitCharacteristicFactory.createCharacteristic(optionalItem, accessory.getUpdater());
            // find the corresponding add method at service and call it.
            service.getClass().getMethod("addOptionalCharacteristic", characteristic.getClass()).invoke(service, characteristic);
            accessory.addCharacteristic(optionalItem);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | HomekitException e) {
            logger.warn("Unsupported optional HomeKit characteristic: service type {}, characteristic type {}", service.getType(), type.getTag());
        }
    });
}
Also used : HomekitOHItemProxy(org.openhab.io.homekit.internal.HomekitOHItemProxy) HomekitSettings(org.openhab.io.homekit.internal.HomekitSettings) Arrays(java.util.Arrays) HomekitCharacteristicType(org.openhab.io.homekit.internal.HomekitCharacteristicType) HomekitException(org.openhab.io.homekit.internal.HomekitException) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) HomekitAccessoryType(org.openhab.io.homekit.internal.HomekitAccessoryType) ArrayList(java.util.ArrayList) Nullable(org.eclipse.jdt.annotation.Nullable) GenericItem(org.openhab.core.items.GenericItem) Map(java.util.Map) Service(io.github.hapjava.services.Service) HomekitAccessory(io.github.hapjava.accessories.HomekitAccessory) SimpleEntry(java.util.AbstractMap.SimpleEntry) Metadata(org.openhab.core.items.Metadata) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) GroupItem(org.openhab.core.items.GroupItem) Logger(org.slf4j.Logger) Characteristic(io.github.hapjava.characteristics.Characteristic) MetadataRegistry(org.openhab.core.items.MetadataRegistry) Collectors(java.util.stream.Collectors) MetadataKey(org.openhab.core.items.MetadataKey) Item(org.openhab.core.items.Item) InvocationTargetException(java.lang.reflect.InvocationTargetException) HomekitTaggedItem(org.openhab.io.homekit.internal.HomekitTaggedItem) Objects(java.util.Objects) ItemRegistry(org.openhab.core.items.ItemRegistry) HomekitAccessoryUpdater(org.openhab.io.homekit.internal.HomekitAccessoryUpdater) List(java.util.List) Stream(java.util.stream.Stream) Entry(java.util.Map.Entry) Optional(java.util.Optional) Collections(java.util.Collections) HomekitCharacteristicType(org.openhab.io.homekit.internal.HomekitCharacteristicType) GenericItem(org.openhab.core.items.GenericItem) HashMap(java.util.HashMap) HomekitTaggedItem(org.openhab.io.homekit.internal.HomekitTaggedItem) Characteristic(io.github.hapjava.characteristics.Characteristic) Service(io.github.hapjava.services.Service) InvocationTargetException(java.lang.reflect.InvocationTargetException) HomekitOHItemProxy(org.openhab.io.homekit.internal.HomekitOHItemProxy) HomekitException(org.openhab.io.homekit.internal.HomekitException)

Example 2 with MetadataRegistry

use of org.openhab.core.items.MetadataRegistry in project openhab-addons by openhab.

the class HomekitAccessoryFactory method getOptionalCharacteristics.

/**
 * collect optional HomeKit characteristics for a OH item.
 *
 * @param taggedItem main OH item
 * @param metadataRegistry OH metadata registry
 * @return a map with characteristics and corresponding OH items
 */
private static Map<HomekitCharacteristicType, GenericItem> getOptionalCharacteristics(HomekitTaggedItem taggedItem, MetadataRegistry metadataRegistry) {
    Map<HomekitCharacteristicType, GenericItem> characteristicItems = new HashMap<>();
    if (taggedItem.isGroup()) {
        GroupItem groupItem = (GroupItem) taggedItem.getItem();
        groupItem.getMembers().forEach(item -> getAccessoryTypes(item, metadataRegistry).stream().filter(c -> !isRootAccessory(c)).filter(c -> !isMandatoryCharacteristic(taggedItem.getAccessoryType(), c.getValue())).forEach(characteristic -> characteristicItems.put(characteristic.getValue(), (GenericItem) item)));
    } else {
        getAccessoryTypes(taggedItem.getItem(), metadataRegistry).stream().filter(c -> !isRootAccessory(c)).filter(c -> !isMandatoryCharacteristic(taggedItem.getAccessoryType(), c.getValue())).forEach(characteristic -> characteristicItems.put(characteristic.getValue(), (GenericItem) taggedItem.getItem()));
    }
    logger.trace("Optional characteristics for item {} characteristics {}", taggedItem.getName(), characteristicItems);
    return Collections.unmodifiableMap(characteristicItems);
}
Also used : HomekitOHItemProxy(org.openhab.io.homekit.internal.HomekitOHItemProxy) HomekitSettings(org.openhab.io.homekit.internal.HomekitSettings) Arrays(java.util.Arrays) HomekitCharacteristicType(org.openhab.io.homekit.internal.HomekitCharacteristicType) HomekitException(org.openhab.io.homekit.internal.HomekitException) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) HomekitAccessoryType(org.openhab.io.homekit.internal.HomekitAccessoryType) ArrayList(java.util.ArrayList) Nullable(org.eclipse.jdt.annotation.Nullable) GenericItem(org.openhab.core.items.GenericItem) Map(java.util.Map) Service(io.github.hapjava.services.Service) HomekitAccessory(io.github.hapjava.accessories.HomekitAccessory) SimpleEntry(java.util.AbstractMap.SimpleEntry) Metadata(org.openhab.core.items.Metadata) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) GroupItem(org.openhab.core.items.GroupItem) Logger(org.slf4j.Logger) Characteristic(io.github.hapjava.characteristics.Characteristic) MetadataRegistry(org.openhab.core.items.MetadataRegistry) Collectors(java.util.stream.Collectors) MetadataKey(org.openhab.core.items.MetadataKey) Item(org.openhab.core.items.Item) InvocationTargetException(java.lang.reflect.InvocationTargetException) HomekitTaggedItem(org.openhab.io.homekit.internal.HomekitTaggedItem) Objects(java.util.Objects) ItemRegistry(org.openhab.core.items.ItemRegistry) HomekitAccessoryUpdater(org.openhab.io.homekit.internal.HomekitAccessoryUpdater) List(java.util.List) Stream(java.util.stream.Stream) Entry(java.util.Map.Entry) Optional(java.util.Optional) Collections(java.util.Collections) HomekitCharacteristicType(org.openhab.io.homekit.internal.HomekitCharacteristicType) GenericItem(org.openhab.core.items.GenericItem) HashMap(java.util.HashMap) GroupItem(org.openhab.core.items.GroupItem)

Example 3 with MetadataRegistry

use of org.openhab.core.items.MetadataRegistry in project openhab-addons by openhab.

the class ItemToStorePointCreator method addPointTags.

private void addPointTags(Item item, InfluxPoint.Builder point) {
    if (configuration.isAddCategoryTag()) {
        String categoryName = item.getCategory();
        if (categoryName == null) {
            categoryName = "n/a";
        }
        point.withTag(TAG_CATEGORY_NAME, categoryName);
    }
    if (configuration.isAddTypeTag()) {
        point.withTag(TAG_TYPE_NAME, item.getType());
    }
    if (configuration.isAddLabelTag()) {
        String labelName = item.getLabel();
        if (labelName == null) {
            labelName = "n/a";
        }
        point.withTag(TAG_LABEL_NAME, labelName);
    }
    final MetadataRegistry currentMetadataRegistry = metadataRegistry;
    if (currentMetadataRegistry != null) {
        MetadataKey key = new MetadataKey(InfluxDBPersistenceService.SERVICE_NAME, item.getName());
        Metadata metadata = currentMetadataRegistry.get(key);
        if (metadata != null) {
            metadata.getConfiguration().forEach((tagName, tagValue) -> {
                point.withTag(tagName, tagValue.toString());
            });
        }
    }
}
Also used : MetadataRegistry(org.openhab.core.items.MetadataRegistry) Metadata(org.openhab.core.items.Metadata) MetadataKey(org.openhab.core.items.MetadataKey)

Aggregations

Metadata (org.openhab.core.items.Metadata)3 MetadataKey (org.openhab.core.items.MetadataKey)3 MetadataRegistry (org.openhab.core.items.MetadataRegistry)3 HomekitAccessory (io.github.hapjava.accessories.HomekitAccessory)2 Characteristic (io.github.hapjava.characteristics.Characteristic)2 Service (io.github.hapjava.services.Service)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 SimpleEntry (java.util.AbstractMap.SimpleEntry)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)2