From 7e5c0436671644cf034dd5aa3b5af4906db08837 Mon Sep 17 00:00:00 2001 From: rusefillc Date: Wed, 22 Jun 2022 02:55:40 -0400 Subject: [PATCH] refactoring: steps towards detaching log from sensors --- .../rusefi/sensor_logs/BinarySensorLog.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/java_console/ui/src/main/java/com/rusefi/sensor_logs/BinarySensorLog.java b/java_console/ui/src/main/java/com/rusefi/sensor_logs/BinarySensorLog.java index 5a44d9573b..f890b17dc0 100644 --- a/java_console/ui/src/main/java/com/rusefi/sensor_logs/BinarySensorLog.java +++ b/java_console/ui/src/main/java/com/rusefi/sensor_logs/BinarySensorLog.java @@ -17,6 +17,7 @@ import java.util.function.Function; public class BinarySensorLog implements SensorLog { private final Function valueProvider; private final Collection entries; + private final TimeProvider timeProvider; private DataOutputStream stream; private String fileName; @@ -24,8 +25,17 @@ public class BinarySensorLog implements SensorLog { private int counter; public BinarySensorLog(Function valueProvider, Collection sensors) { - this.valueProvider = valueProvider; + this(valueProvider, sensors, System::currentTimeMillis); + } + + public BinarySensorLog(Function valueProvider, Collection sensors, TimeProvider timeProvider) { + this.valueProvider = Objects.requireNonNull(valueProvider, "valueProvider"); this.entries = Objects.requireNonNull(sensors, "entries"); + this.timeProvider = timeProvider; + } + + interface TimeProvider { + long currentTimestamp(); } @Override @@ -52,13 +62,15 @@ public class BinarySensorLog implements SensorLog { try { stream.write(0); stream.write(counter++); - stream.writeShort((int) (System.currentTimeMillis() * 100)); + stream.writeShort((int) (timeProvider.currentTimestamp() * 100)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); for (T sensor : entries) { - double value = valueProvider.apply(sensor); + Double value = valueProvider.apply(sensor); + if (value == null) + throw new NullPointerException("No value for " + sensor); sensor.writeToLog(dos, value); }