]> git.basschouten.com Git - openhab-addons.git/blob
25929d80926f87127cadfe83b8eb147b74972e9e
[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 static org.openhab.persistence.influxdb.internal.InfluxDBConstants.TAG_CATEGORY_NAME;
16 import static org.openhab.persistence.influxdb.internal.InfluxDBConstants.TAG_ITEM_NAME;
17 import static org.openhab.persistence.influxdb.internal.InfluxDBConstants.TAG_LABEL_NAME;
18 import static org.openhab.persistence.influxdb.internal.InfluxDBConstants.TAG_TYPE_NAME;
19
20 import java.time.Instant;
21 import java.util.Objects;
22 import java.util.Optional;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.core.items.Item;
27 import org.openhab.core.types.State;
28 import org.openhab.core.types.UnDefType;
29
30 /**
31  * Logic to create an InfluxDB {@link InfluxPoint} from an openHAB {@link Item}
32  *
33  * @author Joan Pujol Espinar - Initial contribution
34  */
35 @NonNullByDefault
36 public class ItemToStorePointCreator {
37     private final InfluxDBConfiguration configuration;
38     private final InfluxDBMetadataService influxDBMetadataService;
39
40     public ItemToStorePointCreator(InfluxDBConfiguration configuration,
41             InfluxDBMetadataService influxDBMetadataService) {
42         this.configuration = configuration;
43         this.influxDBMetadataService = influxDBMetadataService;
44     }
45
46     public @Nullable InfluxPoint convert(Item item, @Nullable String storeAlias) {
47         if (item.getState() instanceof UnDefType) {
48             return null;
49         }
50
51         String measurementName = calculateMeasurementName(item, storeAlias);
52         String itemName = item.getName();
53         State state = getItemState(item);
54
55         Object value = InfluxDBStateConvertUtils.stateToObject(state);
56
57         InfluxPoint.Builder pointBuilder = InfluxPoint.newBuilder(measurementName).withTime(Instant.now())
58                 .withValue(value).withTag(TAG_ITEM_NAME, itemName);
59
60         addPointTags(item, pointBuilder);
61
62         return pointBuilder.build();
63     }
64
65     private String calculateMeasurementName(Item item, @Nullable String storeAlias) {
66         String name = storeAlias != null && !storeAlias.isBlank() ? storeAlias : item.getName();
67         name = influxDBMetadataService.getMeasurementNameOrDefault(item.getName(), name);
68
69         if (configuration.isReplaceUnderscore()) {
70             name = name.replace('_', '.');
71         }
72
73         return name;
74     }
75
76     private State getItemState(Item item) {
77         return calculateDesiredTypeConversionToStore(item)
78                 .map(desiredClass -> Objects.requireNonNullElseGet(item.getStateAs(desiredClass), item::getState))
79                 .orElseGet(item::getState);
80     }
81
82     private Optional<Class<? extends State>> calculateDesiredTypeConversionToStore(Item item) {
83         return item.getAcceptedCommandTypes().stream().filter(commandType -> commandType.isAssignableFrom(State.class))
84                 .findFirst().map(commandType -> commandType.asSubclass(State.class));
85     }
86
87     private void addPointTags(Item item, InfluxPoint.Builder pointBuilder) {
88         if (configuration.isAddCategoryTag()) {
89             String categoryName = Objects.requireNonNullElse(item.getCategory(), "n/a");
90             pointBuilder.withTag(TAG_CATEGORY_NAME, categoryName);
91         }
92
93         if (configuration.isAddTypeTag()) {
94             pointBuilder.withTag(TAG_TYPE_NAME, item.getType());
95         }
96
97         if (configuration.isAddLabelTag()) {
98             String labelName = Objects.requireNonNullElse(item.getLabel(), "n/a");
99             pointBuilder.withTag(TAG_LABEL_NAME, labelName);
100         }
101
102         influxDBMetadataService.getMetaData(item.getName())
103                 .ifPresent(metadata -> metadata.getConfiguration().forEach(pointBuilder::withTag));
104     }
105 }