refactoring: steps towards detaching log from sensors
This commit is contained in:
parent
cb2d29e9a7
commit
7e5c043667
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue