refactoring: better names

This commit is contained in:
rusefillc 2022-11-12 20:59:47 -05:00
parent da6ef4d19d
commit e50e24344b
5 changed files with 24 additions and 25 deletions

View File

@ -148,12 +148,12 @@ public class ByteRateOfChange {
BinarySensorLog<BinaryLogEntry> log = context.getBinaryLogEntryBinarySensorLog(entries.values(), simpleFileName + LoggingStrategy.MLG); BinarySensorLog<BinaryLogEntry> log = context.getBinaryLogEntryBinarySensorLog(entries.values(), simpleFileName + LoggingStrategy.MLG);
context.processPackets(packets, log, packetContent -> { context.writeLogContent(packets, log, packetContent -> {
for (int i = 0; i < packetContent.getData().length; i++) { for (int i = 0; i < packetContent.getData().length; i++) {
int value = packetContent.getData()[i] & 0xFF; int value = packetContent.getData()[i] & 0xFF;
String name = new ByteId(packetContent.getId(), i).getLogKey(); String name = new ByteId(packetContent.getId(), i).getLogKey();
context.values.put(name, (double) value); context.currentSnapshot.put(name, (double) value);
} }
return true; return true;
}); });

View File

@ -1,7 +1,6 @@
package com.rusefi.mlv; package com.rusefi.mlv;
import com.rusefi.can.CANPacket; import com.rusefi.can.CANPacket;
import com.rusefi.mlv.LoggingStrategy;
import com.rusefi.sensor_logs.BinaryLogEntry; import com.rusefi.sensor_logs.BinaryLogEntry;
import com.rusefi.sensor_logs.BinarySensorLog; import com.rusefi.sensor_logs.BinarySensorLog;
@ -12,21 +11,21 @@ import java.util.Map;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
public class LoggingContext { public class LoggingContext {
public Map<String, Double> values = new HashMap<>(); public Map<String, Double> currentSnapshot = new HashMap<>();
AtomicReference<Long> time = new AtomicReference<>(); AtomicReference<Long> currentTime = new AtomicReference<>();
public BinarySensorLog<BinaryLogEntry> getBinaryLogEntryBinarySensorLog(Collection<BinaryLogEntry> entries, String outputFileName) { public BinarySensorLog<BinaryLogEntry> getBinaryLogEntryBinarySensorLog(Collection<BinaryLogEntry> entries, String outputFileName) {
return new BinarySensorLog<>(o -> { return new BinarySensorLog<>(o -> {
Double value = this.values.get(o.getName()); Double value = this.currentSnapshot.get(o.getName());
if (value == null) if (value == null)
return 0.0; return 0.0;
return value; return value;
}, entries, getTimeProvider(), outputFileName); }, entries, getTimeProvider(), outputFileName);
} }
public void processPackets(List<CANPacket> packets, BinarySensorLog<BinaryLogEntry> log, LoggingStrategy.PacketLogger logger) { public void writeLogContent(List<CANPacket> packets, BinarySensorLog<BinaryLogEntry> log, LoggingStrategy.PacketLogger logger) {
for (CANPacket packetContent : packets) { for (CANPacket packetContent : packets) {
this.time.set((long) (packetContent.getTimeStamp() * 1000)); currentTime.set((long) (packetContent.getTimeStamp() * 1000));
boolean needLine = logger.takeValues(packetContent); boolean needLine = logger.takeValues(packetContent);
if (needLine) if (needLine)
log.writeSensorLogLine(); log.writeSensorLogLine();
@ -35,6 +34,6 @@ public class LoggingContext {
} }
public BinarySensorLog.TimeProvider getTimeProvider() { public BinarySensorLog.TimeProvider getTimeProvider() {
return () -> this.time.get(); return () -> this.currentTime.get();
} }
} }

View File

@ -38,12 +38,12 @@ public class LoggingStrategy {
return false; return false;
for (DbcField field : packetMeta.getFields()) { for (DbcField field : packetMeta.getFields()) {
context.values.put(field.getName(), field.getValue(packetContent)); context.currentSnapshot.put(field.getName(), field.getValue(packetContent));
} }
return true; return true;
}; };
context.processPackets(packets, log, logger); context.writeLogContent(packets, log, logger);
} }
public interface PacketLogger { public interface PacketLogger {

View File

@ -20,15 +20,15 @@ public class BinarySensorLog<T extends BinaryLogEntry> implements SensorLog, Aut
private final TimeProvider timeProvider; private final TimeProvider timeProvider;
private DataOutputStream stream; private DataOutputStream stream;
private final String fileName; private final String outputFileName;
private int counter; private int lineCounter;
public BinarySensorLog(Function<T, Double> valueProvider, Collection<T> sensors, TimeProvider timeProvider, String fileName) { public BinarySensorLog(Function<T, Double> valueProvider, Collection<T> sensors, TimeProvider timeProvider, String outputFileName) {
this.valueProvider = Objects.requireNonNull(valueProvider, "valueProvider"); this.valueProvider = Objects.requireNonNull(valueProvider, "valueProvider");
this.entries = Objects.requireNonNull(sensors, "entries"); this.entries = Objects.requireNonNull(sensors, "entries");
this.timeProvider = timeProvider; this.timeProvider = timeProvider;
this.fileName = fileName; this.outputFileName = outputFileName;
} }
public interface TimeProvider { public interface TimeProvider {
@ -43,10 +43,10 @@ public class BinarySensorLog<T extends BinaryLogEntry> implements SensorLog, Aut
@Override @Override
public void writeSensorLogLine() { public void writeSensorLogLine() {
if (stream == null) { if (stream == null) {
System.out.println("Writing to " + fileName); System.out.println("Writing to " + outputFileName);
try { try {
stream = new DataOutputStream(new FileOutputStream(fileName)); stream = new DataOutputStream(new FileOutputStream(outputFileName));
writeHeader(); writeHeader();
} catch (Throwable e) { } catch (Throwable e) {
e.printStackTrace(); e.printStackTrace();
@ -57,7 +57,7 @@ public class BinarySensorLog<T extends BinaryLogEntry> implements SensorLog, Aut
if (stream != null) { if (stream != null) {
try { try {
stream.write(0); stream.write(0);
stream.write(counter++); stream.write(lineCounter++);
stream.writeShort((int) (timeProvider.currentTimestamp() * 100)); stream.writeShort((int) (timeProvider.currentTimestamp() * 100));
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
@ -78,7 +78,7 @@ public class BinarySensorLog<T extends BinaryLogEntry> implements SensorLog, Aut
stream.write(byteArray); stream.write(byteArray);
stream.write(checkSum); stream.write(checkSum);
if (counter % 20 == 0) { if (lineCounter % 20 == 0) {
// for not flush on each block of data but still flush // for not flush on each block of data but still flush
stream.flush(); stream.flush();
} }
@ -142,7 +142,7 @@ public class BinarySensorLog<T extends BinaryLogEntry> implements SensorLog, Aut
@Override @Override
public void close() { public void close() {
System.out.println("Finishing " + fileName); System.out.println("Finishing " + outputFileName);
close(stream); close(stream);
stream = null; stream = null;
} }
@ -157,8 +157,8 @@ public class BinarySensorLog<T extends BinaryLogEntry> implements SensorLog, Aut
} }
} }
public String getFileName() { public String getOutputFileName() {
return fileName; return outputFileName;
} }
private void writeLine(DataOutputStream stream, String name, int length) throws IOException { private void writeLine(DataOutputStream stream, String name, int length) throws IOException {

View File

@ -13,15 +13,15 @@ public class ByteRateOfChangeSandbox {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
ReaderTypeHolder.INSTANCE.type = ReaderType.PCAN; ReaderTypeHolder.INSTANCE.type = ReaderType.PCAN;
String folder = "C:\\stuff\\rusefi_documentation\\OEM-Docs\\Nissan\\2011_Xterra\\CAN-Nov-2022"; String inputFileName = "C:\\stuff\\rusefi_documentation\\OEM-Docs\\Nissan\\2011_Xterra\\CAN-Nov-2022";
String reportDestinationFolder = folder + File.separator + "processed"; String reportDestinationFolder = inputFileName + File.separator + "processed";
new File(reportDestinationFolder).mkdirs(); new File(reportDestinationFolder).mkdirs();
List<ByteRateOfChange.TraceReport> reports = new ArrayList<>(); List<ByteRateOfChange.TraceReport> reports = new ArrayList<>();
FolderUtil.handleFolder(folder, (simpleFileName, fullFileName) -> { FolderUtil.handleFolder(inputFileName, (simpleFileName, fullFileName) -> {
ByteRateOfChange.TraceReport report = ByteRateOfChange.process(fullFileName, reportDestinationFolder, simpleFileName); ByteRateOfChange.TraceReport report = ByteRateOfChange.process(fullFileName, reportDestinationFolder, simpleFileName);
reports.add(report); reports.add(report);
}, "pcan.trc"); }, "pcan.trc");