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 {
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue