refactoring: steps towards detaching log from sensors

This commit is contained in:
rusefillc 2022-06-22 02:55:40 -04:00
parent cb2d29e9a7
commit 7e5c043667
1 changed files with 15 additions and 3 deletions

View File

@ -17,6 +17,7 @@ import java.util.function.Function;
public class BinarySensorLog<T extends BinaryLogEntry> implements SensorLog {
private final Function<T, Double> valueProvider;
private final Collection<T> entries;
private final TimeProvider timeProvider;
private DataOutputStream stream;
private String fileName;
@ -24,8 +25,17 @@ public class BinarySensorLog<T extends BinaryLogEntry> implements SensorLog {
private int counter;
public BinarySensorLog(Function<T, Double> valueProvider, Collection<T> sensors) {
this.valueProvider = valueProvider;
this(valueProvider, sensors, System::currentTimeMillis);
}
public BinarySensorLog(Function<T, Double> valueProvider, Collection<T> 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<T extends BinaryLogEntry> 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);
}