33
33
import java .util .concurrent .ScheduledExecutorService ;
34
34
import java .util .concurrent .ScheduledFuture ;
35
35
import java .util .concurrent .TimeUnit ;
36
+ import java .util .function .DoubleFunction ;
36
37
import java .util .stream .Collectors ;
37
38
import java .util .stream .Stream ;
38
39
@@ -443,6 +444,8 @@ public Iterable<HistoricItem> query(FilterCriteria filter) {
443
444
long end = filterEndDate == null ? System .currentTimeMillis () / 1000
444
445
: filterEndDate .toInstant ().getEpochSecond ();
445
446
447
+ DoubleFunction <State > toState = toStateMapper (item , unit );
448
+
446
449
try {
447
450
if (filterBeginDate == null ) {
448
451
// 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) {
455
458
// we are asked only for the most recent value!
456
459
double lastValue = db .getLastDatasourceValue (DATASOURCE_STATE );
457
460
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 ()),
460
463
ZoneId .systemDefault ()));
461
464
return List .of (rrd4jItem );
462
465
} else {
@@ -486,13 +489,14 @@ public Iterable<HistoricItem> query(FilterCriteria filter) {
486
489
487
490
List <HistoricItem > items = new ArrayList <>();
488
491
long ts = result .getFirstTimestamp ();
492
+ ZonedDateTime zdt = ZonedDateTime .ofInstant (Instant .ofEpochSecond (ts ), ZoneId .systemDefault ());
489
493
long step = result .getRowCount () > 1 ? result .getStep () : 0 ;
490
494
for (double value : result .getValues (DATASOURCE_STATE )) {
491
495
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 );
494
497
items .add (rrd4jItem );
495
498
}
499
+ zdt = zdt .plusSeconds (step );
496
500
ts += step ;
497
501
}
498
502
return items ;
@@ -603,25 +607,24 @@ public ConsolFun getConsolidationFunction(RrdDb db) {
603
607
}
604
608
}
605
609
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 ) {
608
611
if (item instanceof GroupItem groupItem ) {
609
612
item = groupItem .getBaseItem ();
610
613
}
611
614
612
615
if (item instanceof SwitchItem && !(item instanceof DimmerItem )) {
613
- return OnOffType .from (value != 0.0d );
616
+ return ( value ) -> OnOffType .from (value != 0.0d );
614
617
} else if (item instanceof ContactItem ) {
615
- return value == 0.0d ? OpenClosedType .CLOSED : OpenClosedType .OPEN ;
618
+ return ( value ) -> value == 0.0d ? OpenClosedType .CLOSED : OpenClosedType .OPEN ;
616
619
} else if (item instanceof DimmerItem || item instanceof RollershutterItem || item instanceof ColorItem ) {
617
620
// 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 ));
619
622
} else if (item instanceof NumberItem ) {
620
623
if (unit != null ) {
621
- return new QuantityType (value , unit );
624
+ return ( value ) -> new QuantityType <> (value , unit );
622
625
}
623
626
}
624
- return new DecimalType ( value ) ;
627
+ return DecimalType :: new ;
625
628
}
626
629
627
630
private boolean isSupportedItemType (Item item ) {
0 commit comments