Skip to content

Commit e3d5e5d

Browse files
authored
Improved querying the RRD4jPersistenceService (openhab#16360)
Signed-off-by: Jörg Sautter <[email protected]>
1 parent f45cff8 commit e3d5e5d

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

bundles/org.openhab.persistence.rrd4j/src/main/java/org/openhab/persistence/rrd4j/internal/RRD4jPersistenceService.java

+14-11
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.concurrent.ScheduledExecutorService;
3434
import java.util.concurrent.ScheduledFuture;
3535
import java.util.concurrent.TimeUnit;
36+
import java.util.function.DoubleFunction;
3637
import java.util.stream.Collectors;
3738
import java.util.stream.Stream;
3839

@@ -443,6 +444,8 @@ public Iterable<HistoricItem> query(FilterCriteria filter) {
443444
long end = filterEndDate == null ? System.currentTimeMillis() / 1000
444445
: filterEndDate.toInstant().getEpochSecond();
445446

447+
DoubleFunction<State> toState = toStateMapper(item, unit);
448+
446449
try {
447450
if (filterBeginDate == null) {
448451
// as rrd goes back for years and gets more and more inaccurate, we only support descending order
@@ -455,8 +458,8 @@ public Iterable<HistoricItem> query(FilterCriteria filter) {
455458
// we are asked only for the most recent value!
456459
double lastValue = db.getLastDatasourceValue(DATASOURCE_STATE);
457460
if (!Double.isNaN(lastValue)) {
458-
HistoricItem rrd4jItem = new RRD4jItem(itemName, mapToState(lastValue, item, unit),
459-
ZonedDateTime.ofInstant(Instant.ofEpochMilli(db.getLastArchiveUpdateTime() * 1000),
461+
HistoricItem rrd4jItem = new RRD4jItem(itemName, toState.apply(lastValue),
462+
ZonedDateTime.ofInstant(Instant.ofEpochSecond(db.getLastArchiveUpdateTime()),
460463
ZoneId.systemDefault()));
461464
return List.of(rrd4jItem);
462465
} else {
@@ -486,13 +489,14 @@ public Iterable<HistoricItem> query(FilterCriteria filter) {
486489

487490
List<HistoricItem> items = new ArrayList<>();
488491
long ts = result.getFirstTimestamp();
492+
ZonedDateTime zdt = ZonedDateTime.ofInstant(Instant.ofEpochSecond(ts), ZoneId.systemDefault());
489493
long step = result.getRowCount() > 1 ? result.getStep() : 0;
490494
for (double value : result.getValues(DATASOURCE_STATE)) {
491495
if (!Double.isNaN(value) && (((ts >= start) && (ts <= end)) || (start == end))) {
492-
RRD4jItem rrd4jItem = new RRD4jItem(itemName, mapToState(value, item, unit),
493-
ZonedDateTime.ofInstant(Instant.ofEpochSecond(ts), ZoneId.systemDefault()));
496+
RRD4jItem rrd4jItem = new RRD4jItem(itemName, toState.apply(value), zdt);
494497
items.add(rrd4jItem);
495498
}
499+
zdt = zdt.plusSeconds(step);
496500
ts += step;
497501
}
498502
return items;
@@ -603,25 +607,24 @@ public ConsolFun getConsolidationFunction(RrdDb db) {
603607
}
604608
}
605609

606-
@SuppressWarnings({ "unchecked", "rawtypes" })
607-
private State mapToState(double value, @Nullable Item item, @Nullable Unit unit) {
610+
private <Q extends Quantity<Q>> DoubleFunction<State> toStateMapper(@Nullable Item item, @Nullable Unit<Q> unit) {
608611
if (item instanceof GroupItem groupItem) {
609612
item = groupItem.getBaseItem();
610613
}
611614

612615
if (item instanceof SwitchItem && !(item instanceof DimmerItem)) {
613-
return OnOffType.from(value != 0.0d);
616+
return (value) -> OnOffType.from(value != 0.0d);
614617
} else if (item instanceof ContactItem) {
615-
return value == 0.0d ? OpenClosedType.CLOSED : OpenClosedType.OPEN;
618+
return (value) -> value == 0.0d ? OpenClosedType.CLOSED : OpenClosedType.OPEN;
616619
} else if (item instanceof DimmerItem || item instanceof RollershutterItem || item instanceof ColorItem) {
617620
// make sure Items that need PercentTypes instead of DecimalTypes do receive the right information
618-
return new PercentType((int) Math.round(value * 100));
621+
return (value) -> new PercentType((int) Math.round(value * 100));
619622
} else if (item instanceof NumberItem) {
620623
if (unit != null) {
621-
return new QuantityType(value, unit);
624+
return (value) -> new QuantityType<>(value, unit);
622625
}
623626
}
624-
return new DecimalType(value);
627+
return DecimalType::new;
625628
}
626629

627630
private boolean isSupportedItemType(Item item) {

0 commit comments

Comments
 (0)