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());
}
});
}
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);
}
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());
});
}
}
}
Aggregations