Avoid ArithmeticException if timeSpan is zero (#1562)
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>pull/1572/head
parent
27dcce5207
commit
4585dac84a
|
@ -48,6 +48,8 @@ import org.slf4j.LoggerFactory;
|
||||||
@Component(immediate = true)
|
@Component(immediate = true)
|
||||||
public class PersistenceExtensions {
|
public class PersistenceExtensions {
|
||||||
|
|
||||||
|
private static final BigDecimal BIG_DECIMAL_TWO = BigDecimal.valueOf(2);
|
||||||
|
|
||||||
private static PersistenceServiceRegistry registry;
|
private static PersistenceServiceRegistry registry;
|
||||||
private static TimeZoneProvider timeZoneProvider;
|
private static TimeZoneProvider timeZoneProvider;
|
||||||
|
|
||||||
|
@ -432,9 +434,9 @@ public class PersistenceExtensions {
|
||||||
if (firstTimestamp == null || lastState == null) {
|
if (firstTimestamp == null || lastState == null) {
|
||||||
firstTimestamp = thisTimestamp;
|
firstTimestamp = thisTimestamp;
|
||||||
} else {
|
} else {
|
||||||
avgValue = (thisState.toBigDecimal().add(lastState.toBigDecimal())).divide(BigDecimal.valueOf(2),
|
avgValue = thisState.toBigDecimal().add(lastState.toBigDecimal()).divide(BIG_DECIMAL_TWO,
|
||||||
MathContext.DECIMAL64);
|
MathContext.DECIMAL64);
|
||||||
timeSpan = thisTimestamp.subtract(lastTimestamp);
|
timeSpan = thisTimestamp.subtract(lastTimestamp, MathContext.DECIMAL64);
|
||||||
total = total.add(avgValue.multiply(timeSpan, MathContext.DECIMAL64));
|
total = total.add(avgValue.multiply(timeSpan, MathContext.DECIMAL64));
|
||||||
}
|
}
|
||||||
lastTimestamp = thisTimestamp;
|
lastTimestamp = thisTimestamp;
|
||||||
|
@ -446,21 +448,22 @@ public class PersistenceExtensions {
|
||||||
thisState = item.getStateAs(DecimalType.class);
|
thisState = item.getStateAs(DecimalType.class);
|
||||||
if (thisState != null) {
|
if (thisState != null) {
|
||||||
thisTimestamp = BigDecimal.valueOf(Instant.now().toEpochMilli());
|
thisTimestamp = BigDecimal.valueOf(Instant.now().toEpochMilli());
|
||||||
avgValue = (thisState.toBigDecimal().add(lastState.toBigDecimal())).divide(BigDecimal.valueOf(2),
|
avgValue = thisState.toBigDecimal().add(lastState.toBigDecimal()).divide(BIG_DECIMAL_TWO,
|
||||||
MathContext.DECIMAL64);
|
MathContext.DECIMAL64);
|
||||||
timeSpan = thisTimestamp.subtract(lastTimestamp);
|
timeSpan = thisTimestamp.subtract(lastTimestamp, MathContext.DECIMAL64);
|
||||||
total = total.add(avgValue.multiply(timeSpan, MathContext.DECIMAL64));
|
total = total.add(avgValue.multiply(timeSpan, MathContext.DECIMAL64));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (thisTimestamp != null) {
|
if (thisTimestamp != null) {
|
||||||
timeSpan = thisTimestamp.subtract(firstTimestamp, MathContext.DECIMAL64);
|
timeSpan = thisTimestamp.subtract(firstTimestamp, MathContext.DECIMAL64);
|
||||||
|
// avoid ArithmeticException if timeSpan is zero
|
||||||
BigDecimal average = total.divide(timeSpan, MathContext.DECIMAL64);
|
if (!BigDecimal.ZERO.equals(timeSpan)) {
|
||||||
return new DecimalType(average);
|
BigDecimal average = total.divide(timeSpan, MathContext.DECIMAL64);
|
||||||
} else {
|
return new DecimalType(average);
|
||||||
return null;
|
}
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue