2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.persistence.influxdb.internal;
15 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.items.Metadata;
19 import org.openhab.core.items.MetadataKey;
20 import org.openhab.core.items.MetadataRegistry;
21 import org.openhab.persistence.influxdb.InfluxDBPersistenceService;
22 import org.osgi.service.component.annotations.Activate;
23 import org.osgi.service.component.annotations.Component;
24 import org.osgi.service.component.annotations.Reference;
27 * Utility service for using item metadata in InfluxDB
29 * @author Jan N. Klug - Initial contribution
32 @Component(service = InfluxDBMetadataService.class)
33 public class InfluxDBMetadataService {
34 private final MetadataRegistry metadataRegistry;
37 public InfluxDBMetadataService(@Reference MetadataRegistry metadataRegistry) {
38 this.metadataRegistry = metadataRegistry;
42 * get the measurement name from the item metadata or return the provided default
44 * @param itemName the item name
45 * @param defaultName the default measurement name (
46 * @return the metadata measurement name if present, defaultName otherwise
48 public String getMeasurementNameOrDefault(String itemName, String defaultName) {
49 Optional<Metadata> metadata = getMetaData(itemName);
50 if (metadata.isPresent()) {
51 String metaName = metadata.get().getValue();
52 if (!metaName.isBlank()) {
61 * get an Optional of the metadata for an item
63 * @param itemName the item name
64 * @return Optional with the metadata (may be empty)
66 public Optional<Metadata> getMetaData(String itemName) {
67 MetadataKey key = new MetadataKey(InfluxDBPersistenceService.SERVICE_NAME, itemName);
68 return Optional.ofNullable(metadataRegistry.get(key));