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 { public class BinarySensorLog<T extends BinaryLogEntry> implements SensorLog {
private final Function<T, Double> valueProvider; private final Function<T, Double> valueProvider;
private final Collection<T> entries; private final Collection<T> entries;
private final TimeProvider timeProvider;
private DataOutputStream stream; private DataOutputStream stream;
private String fileName; private String fileName;
@ -24,8 +25,17 @@ public class BinarySensorLog<T extends BinaryLogEntry> implements SensorLog {
private int counter; private int counter;
public BinarySensorLog(Function<T, Double> valueProvider, Collection<T> sensors) { 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.entries = Objects.requireNonNull(sensors, "entries");
this.timeProvider = timeProvider;
}
interface TimeProvider {
long currentTimestamp();
} }
@Override @Override
@ -52,13 +62,15 @@ public class BinarySensorLog<T extends BinaryLogEntry> implements SensorLog {
try { try {
stream.write(0); stream.write(0);
stream.write(counter++); stream.write(counter++);
stream.writeShort((int) (System.currentTimeMillis() * 100)); stream.writeShort((int) (timeProvider.currentTimestamp() * 100));
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos); DataOutputStream dos = new DataOutputStream(baos);
for (T sensor : entries) { 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); sensor.writeToLog(dos, value);
} }