Fix problem with historical Influx1 data that was generated with addon version <3.0.0 that didn't write generate item tag
Signed-off-by: Joan Pujol <joanpujol@gmail.com>
import java.util.Collections;
import java.util.Map;
+import java.util.Optional;
import java.util.StringJoiner;
import org.eclipse.jdt.annotation.NonNullByDefault;
token = (String) config.getOrDefault(TOKEN_PARAM, "");
databaseName = (String) config.getOrDefault(DATABASE_PARAM, "openhab");
retentionPolicy = (String) config.getOrDefault(RETENTION_POLICY_PARAM, "autogen");
- version = parseInfluxVersion(config.getOrDefault(VERSION_PARAM, InfluxDBVersion.V1.name()));
+ version = parseInfluxVersion((String) config.getOrDefault(VERSION_PARAM, InfluxDBVersion.V1.name()));
replaceUnderscore = getConfigBooleanValue(config, REPLACE_UNDERSCORE_PARAM, false);
addCategoryTag = getConfigBooleanValue(config, ADD_CATEGORY_TAG_PARAM, false);
}
}
- private InfluxDBVersion parseInfluxVersion(@Nullable Object value) {
+ private InfluxDBVersion parseInfluxVersion(@Nullable String value) {
try {
- return InfluxDBVersion.valueOf((String) value);
+ return Optional.ofNullable(value).map(InfluxDBVersion::valueOf).orElse(InfluxDBVersion.UNKNOWN);
} catch (RuntimeException e) {
logger.warn("Invalid version {}", value);
return InfluxDBVersion.UNKNOWN;
* @return the state of the item represented by the itemName parameter, else the string value of
* the Object parameter
*/
- public static State objectToState(Object value, String itemName, @Nullable ItemRegistry itemRegistry) {
+ public static State objectToState(@Nullable Object value, String itemName, @Nullable ItemRegistry itemRegistry) {
State state = null;
if (itemRegistry != null) {
try {
return state;
}
- public static State objectToState(Object value, Item itemToSetState) {
+ public static State objectToState(@Nullable Object value, Item itemToSetState) {
String valueStr = String.valueOf(value);
+ @Nullable
Item item = itemToSetState;
if (item instanceof GroupItem) {
item = ((GroupItem) item).getBaseItem();
*/
package org.openhab.persistence.influxdb.internal;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+
/**
* InfluxDB version
*
* @author Joan Pujol Espinar - Initial contribution
*/
+@NonNullByDefault
public enum InfluxDBVersion {
V1,
V2,
import java.time.Instant;
import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
/**
* Row data returned from database query
public class InfluxRow {
private final String itemName;
private final Instant time;
- private final Object value;
+ private final @Nullable Object value;
- public InfluxRow(Instant time, String itemName, Object value) {
+ public InfluxRow(Instant time, String itemName, @Nullable Object value) {
this.time = time;
this.itemName = itemName;
this.value = value;
return itemName;
}
- public Object getValue() {
+ public @Nullable Object getValue() {
return value;
}
}
import java.util.Collections;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
import java.util.concurrent.TimeUnit;
import org.eclipse.jdt.annotation.NonNullByDefault;
} else {
for (QueryResult.Series series : seriess) {
logger.trace("series {}", series.toString());
- String itemName = series.getName();
- List<List<Object>> valuess = series.getValues();
+ List<List<@Nullable Object>> valuess = series.getValues();
if (valuess == null) {
logger.debug("query returned no values");
} else {
throw new IllegalStateException("missing column");
}
for (int i = 0; i < valuess.size(); i++) {
- Double rawTime = (Double) valuess.get(i).get(timestampColumn);
+ Double rawTime = (Double) Objects.requireNonNull(valuess.get(i).get(timestampColumn));
Instant time = Instant.ofEpochMilli(rawTime.longValue());
+ @Nullable
Object value = valuess.get(i).get(valueColumn);
- if (itemNameColumn != null) {
- itemName = (String) valuess.get(i).get(itemNameColumn);
- }
+ var currentI = i;
+ String itemName = Optional.ofNullable(itemNameColumn)
+ .flatMap(inc -> Optional.ofNullable((String) valuess.get(currentI).get(inc)))
+ .orElse(series.getName());
logger.trace("adding historic item {}: time {} value {}", itemName, time, value);
rows.add(new InfluxRow(time, itemName, value));
}