refactoring: steps towards detaching log from sensors

This commit is contained in:
rusefillc 2022-06-22 01:44:01 -04:00
parent 3d05a72753
commit cb2d29e9a7
7 changed files with 64 additions and 91 deletions

View File

@ -2,6 +2,7 @@ package com.rusefi.core;
import com.rusefi.config.FieldType;
import com.rusefi.config.generated.Fields;
import com.rusefi.sensor_logs.BinaryLogEntry;
import org.jetbrains.annotations.Nullable;
import java.io.DataOutputStream;
@ -19,7 +20,7 @@ import static com.rusefi.config.generated.Fields.*;
* 2/11/13
* @see GetOutputsCommand#OUTPUT_SIZE
*/
public enum Sensor {
public enum Sensor implements BinaryLogEntry {
/**
* Please note that these enum names are used to make 'set_mock_XXX_voltage' commands
*/
@ -220,6 +221,8 @@ public enum Sensor {
offset = -1;
}
public static Collection<Sensor> getSensorsForCategory(String category) {
final Set<Sensor> sensors = new TreeSet<>(Comparator.comparing(o -> o.getName().toLowerCase()));
@ -280,6 +283,31 @@ public enum Sensor {
return name;
}
@Override
public String getUnit() {
return units;
}
@Override
public int getByteSize() {
switch (getType()) {
case UINT8:
return 0;
case INT8:
return 1;
case UINT16:
return 2;
case INT16:
return 3;
case INT:
return 4;
case FLOAT:
return 7;
default:
throw new UnsupportedOperationException("" + getType());
}
}
public SensorCategory getCategory() {
return category;
}
@ -312,6 +340,7 @@ public enum Sensor {
return value;
}
@Override
public void writeToLog(DataOutputStream dos, double value) throws IOException {
switch (type) {
case INT8:

View File

@ -0,0 +1,14 @@
package com.rusefi.sensor_logs;
import java.io.DataOutputStream;
import java.io.IOException;
public interface BinaryLogEntry {
String getName();
String getUnit();
int getByteSize();
void writeToLog(DataOutputStream dos, double value) throws IOException;
}

View File

@ -1,25 +0,0 @@
package com.rusefi.sensor_logs;
public class BinaryLogEntry {
private final String name;
private final String unit;
private final int byteSize;
public BinaryLogEntry(String name, String unit, int byteSize) {
this.name = name;
this.unit = unit;
this.byteSize = byteSize;
}
public String getName() {
return name;
}
public String getUnit() {
return unit;
}
public int getByteSize() {
return byteSize;
}
}

View File

@ -3,42 +3,29 @@ package com.rusefi.sensor_logs;
import com.opensr5.Logger;
import com.rusefi.FileLog;
import com.rusefi.config.generated.Fields;
import com.rusefi.core.Sensor;
import com.rusefi.core.SensorCentral;
import com.rusefi.rusEFIVersion;
import java.io.*;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* MLV .mlq binary log file
* </p>
* Andrey Belomutskiy, (c) 2013-2020
*/
public class BinarySensorLog implements SensorLog {
private final Function<Sensor, Double> valueProvider;
private final Collection<Sensor> sensors;
private final List<BinaryLogEntry> entries;
public class BinarySensorLog<T extends BinaryLogEntry> implements SensorLog {
private final Function<T, Double> valueProvider;
private final Collection<T> entries;
private DataOutputStream stream;
private String fileName;
private int counter;
public BinarySensorLog() {
this(sensor -> SensorCentral.getInstance().getValue(sensor), SensorLogger.SENSORS);
}
public BinarySensorLog(Function<Sensor, Double> valueProvider, Sensor... sensors) {
public BinarySensorLog(Function<T, Double> valueProvider, Collection<T> sensors) {
this.valueProvider = valueProvider;
this.sensors = filterOutSensorsWithoutType(Objects.requireNonNull(sensors, "sensors"));
entries = SensorHelper.values(this.sensors);
}
private static Collection<Sensor> filterOutSensorsWithoutType(Sensor[] sensors) {
return Arrays.stream(sensors).filter(sensor -> sensor.getType() != null).collect(Collectors.toCollection(ArrayList::new));
this.entries = Objects.requireNonNull(sensors, "entries");
}
@Override
@ -70,7 +57,7 @@ public class BinarySensorLog implements SensorLog {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
for (Sensor sensor : sensors) {
for (T sensor : entries) {
double value = valueProvider.apply(sensor);
sensor.writeToLog(dos, value);
}

View File

@ -10,8 +10,12 @@ import com.rusefi.tools.online.UploadResult;
import com.rusefi.ui.AuthTokenPanel;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
public class BinarySensorLogRestarter implements SensorLog {
private final static Executor UPLOAD_EXECUTOR = Executors.newSingleThreadExecutor(new NamedThreadFactory("BinarySensorLogRestarter"));
@ -37,11 +41,17 @@ public class BinarySensorLogRestarter implements SensorLog {
}
if (logger == null) {
logger = new BinarySensorLog();
Collection<Sensor> sensorsForLogging = filterOutSensorsWithoutType(SensorLogger.SENSORS);
logger = new BinarySensorLog<>(sensor -> SensorCentral.getInstance().getValue(sensor), sensorsForLogging);
}
logger.writeSensorLogLine();
}
private static Collection<Sensor> filterOutSensorsWithoutType(Sensor[] sensors) {
return Arrays.stream(sensors).filter(sensor -> sensor.getType() != null).collect(Collectors.toCollection(ArrayList::new));
}
@Override
public synchronized void close() {
if (logger != null) {

View File

@ -2,6 +2,7 @@ package com.rusefi.sensor_logs;
import com.rusefi.core.Sensor;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
@ -18,6 +19,7 @@ public class BinarySensorLogSandbox {
};
BinarySensorLog l = new BinarySensorLog(valueProvider,
Arrays.asList(
Sensor.TIME_SECONDS,
Sensor.RPMValue,
Sensor.vehicleSpeedKph,
@ -31,7 +33,7 @@ public class BinarySensorLogSandbox {
Sensor.Lambda,
Sensor.TARGET_AFR,
Sensor.FIRMWARE_VERSION,
Sensor.CLT);
Sensor.CLT));
values.put(Sensor.TIME_SECONDS, 1.0);
values.put(Sensor.RPMValue, 0.0);

View File

@ -1,44 +0,0 @@
package com.rusefi.sensor_logs;
import com.rusefi.core.Sensor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class SensorHelper {
static int getSensorValueSize(Sensor sensor) {
switch (sensor.getType()) {
case UINT8:
return 0;
case INT8:
return 1;
case UINT16:
return 2;
case INT16:
return 3;
case INT:
return 4;
case FLOAT:
return 7;
default:
throw new UnsupportedOperationException("" + sensor.getType());
}
}
public static BinaryLogEntry valueOf(Sensor sensor) {
String name = SensorLogger.getSensorName(sensor, 0);
String unit = sensor.getUnits();
return new BinaryLogEntry(name, unit, getSensorValueSize(sensor));
}
public static List<BinaryLogEntry> values(Collection<Sensor> sensors) {
List<BinaryLogEntry> result = new ArrayList<>();
for (Sensor sensor : sensors) {
result.add(valueOf(sensor));
}
return result;
}
}