]> git.basschouten.com Git - openhab-addons.git/blob
0ca8599c5bab0829bdf860ab1a6fba7fe4abf6b2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.persistence.influxdb.internal;
14
15 import java.util.Optional;
16
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;
25
26 /**
27  * Utility service for using item metadata in InfluxDB
28  *
29  * @author Jan N. Klug - Initial contribution
30  */
31 @NonNullByDefault
32 @Component(service = InfluxDBMetadataService.class)
33 public class InfluxDBMetadataService {
34     private final MetadataRegistry metadataRegistry;
35
36     @Activate
37     public InfluxDBMetadataService(@Reference MetadataRegistry metadataRegistry) {
38         this.metadataRegistry = metadataRegistry;
39     }
40
41     /**
42      * get the measurement name from the item metadata or return the provided default
43      *
44      * @param itemName the item name
45      * @param defaultName the default measurement name (
46      * @return the metadata measurement name if present, defaultName otherwise
47      */
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()) {
53                 return metaName;
54             }
55         }
56
57         return defaultName;
58     }
59
60     /**
61      * get an Optional of the metadata for an item
62      *
63      * @param itemName the item name
64      * @return Optional with the metadata (may be empty)
65      */
66     public Optional<Metadata> getMetaData(String itemName) {
67         MetadataKey key = new MetadataKey(InfluxDBPersistenceService.SERVICE_NAME, itemName);
68         return Optional.ofNullable(metadataRegistry.get(key));
69     }
70 }