Skip to content

Commit 30ccf89

Browse files
committed
fix(telemetry): preserve regen sign
1 parent 23513b4 commit 30ccf89

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

‎lib/src/main/java/com/evsuite/hardware/telemetry/ConsumptionCalculator.kt‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ class ConsumptionCalculator(
7777
val previous = smoothedValue
7878
val previousAt = smoothedAtMs
7979
val deltaMs = previousAt?.let { timestampMs - it }
80+
val signChanged = previous != null && previous * current < 0.0
8081
val value = if (
81-
previous == null || deltaMs == null || deltaMs <= 0L || deltaMs > MAX_SAMPLE_GAP_MS
82+
previous == null || deltaMs == null || deltaMs <= 0L ||
83+
deltaMs > MAX_SAMPLE_GAP_MS || signChanged
8284
) {
8385
current
8486
} else {

‎lib/src/test/java/com/evsuite/hardware/telemetry/ConsumptionCalculatorTest.kt‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ class ConsumptionCalculatorTest {
3636
assertEquals(-20.0, value.value!!, 1e-9)
3737
}
3838

39+
@Test fun `dashboard smoothing resets when consumption changes to regeneration`() {
40+
val calculator = ConsumptionCalculator()
41+
calculator.add(0L, 18f, 90f)
42+
43+
val regeneration = calculator.add(1_000L, -18f, 90f)
44+
45+
assertEquals(-20.0, regeneration.rawInstantaneous.value!!, 1e-9)
46+
assertEquals(-20.0, regeneration.smoothedInstantaneous.value!!, 1e-9)
47+
}
48+
3949
@Test fun `dashboard value uses a five second exponential moving average`() {
4050
val calculator = ConsumptionCalculator()
4151
calculator.add(0L, 18f, 90f)

0 commit comments

Comments
 (0)