diff --git a/src/main/java/com/rusefi/can/ByteRateOfChange.java b/src/main/java/com/rusefi/can/ByteRateOfChange.java index 3bdbb66..caec21c 100644 --- a/src/main/java/com/rusefi/can/ByteRateOfChange.java +++ b/src/main/java/com/rusefi/can/ByteRateOfChange.java @@ -9,7 +9,6 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; import java.util.*; -import java.util.concurrent.atomic.AtomicReference; public class ByteRateOfChange { @@ -40,7 +39,9 @@ public class ByteRateOfChange { ps.close(); - return new TraceReport(simpleFileName, statistics); + TraceReport traceReport = new TraceReport(simpleFileName, statistics); + traceReport.createMegaLogViewer(packets); + return traceReport; } public static String dualSid(int sid) { @@ -78,6 +79,10 @@ public class ByteRateOfChange { this.index = index; } + private String getLogKey() { + return sid + "_" + index; + } + @Override public boolean equals(Object o) { if (this == o) return true; @@ -117,21 +122,26 @@ public class ByteRateOfChange { return statistics; } - public void createMegaLogViewer() { + public void createMegaLogViewer(List packets) { Map entries = new HashMap<>(); for (ByteId key : statistics.keySet()) { - entries.put(key, BinaryLogEntry.createFloatLogEntry(key.sid + "_" + key.index, Integer.toBinaryString(key.sid))); + entries.put(key, BinaryLogEntry.createFloatLogEntry(key.getLogKey(), Integer.toBinaryString(key.sid))); } - Map values = new HashMap<>(); - AtomicReference time = new AtomicReference<>(); - BinarySensorLog log = new BinarySensorLog<>(o -> { - Double value = values.get(o.getName()); - if (value == null) - return 0.0; - return value; - }, entries.values(), time::get, "haha" + LoggingStrategy.MLG); + LoggingContext context = new LoggingContext(); + BinarySensorLog log = context.getBinaryLogEntryBinarySensorLog(entries.values(), simpleFileName + LoggingStrategy.MLG); + + + context.processPackets(packets, log, packetContent -> { + for (int i = 0; i < packetContent.getData().length; i++) { + int value = packetContent.getData()[i] & 0xFF; + + String name = new ByteId(packetContent.getId(), i).getLogKey(); + context.values.put(name, (double) value); + } + return true; + }); } } } diff --git a/src/main/java/com/rusefi/can/LoggingContext.java b/src/main/java/com/rusefi/can/LoggingContext.java new file mode 100644 index 0000000..a8e7b98 --- /dev/null +++ b/src/main/java/com/rusefi/can/LoggingContext.java @@ -0,0 +1,38 @@ +package com.rusefi.can; + +import com.rusefi.sensor_logs.BinaryLogEntry; +import com.rusefi.sensor_logs.BinarySensorLog; + +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; + +class LoggingContext { + Map values = new HashMap<>(); + AtomicReference time = new AtomicReference<>(); + + BinarySensorLog getBinaryLogEntryBinarySensorLog(Collection entries, String outputFileName) { + return new BinarySensorLog<>(o -> { + Double value = this.values.get(o.getName()); + if (value == null) + return 0.0; + return value; + }, entries, getTimeProvider(), outputFileName); + } + + public void processPackets(List packets, BinarySensorLog log, LoggingStrategy.PacketLogger logger) { + for (CANPacket packetContent : packets) { + this.time.set((long) (packetContent.getTimeStamp() * 1000)); + boolean needLine = logger.takeValues(packetContent); + if (needLine) + log.writeSensorLogLine(); + } + log.close(); + } + + public BinarySensorLog.TimeProvider getTimeProvider() { + return () -> this.time.get(); + } +} diff --git a/src/main/java/com/rusefi/can/LoggingStrategy.java b/src/main/java/com/rusefi/can/LoggingStrategy.java index bb78b97..8d8f321 100644 --- a/src/main/java/com/rusefi/can/LoggingStrategy.java +++ b/src/main/java/com/rusefi/can/LoggingStrategy.java @@ -7,10 +7,7 @@ import com.rusefi.sensor_logs.BinaryLogEntry; import com.rusefi.sensor_logs.BinarySensorLog; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; -import java.util.concurrent.atomic.AtomicReference; public class LoggingStrategy { public static final String MLG = ".mlg"; @@ -31,26 +28,25 @@ public class LoggingStrategy { public static void writeLog(DbcFile dbc, List packets, String outputFileName) { List entries = dbc.getFieldNameEntries(); - Map values = new HashMap<>(); - AtomicReference time = new AtomicReference<>(); - BinarySensorLog log = new BinarySensorLog<>(o -> { - Double value = values.get(o.getName()); - if (value == null) - return 0.0; - return value; - }, entries, time::get, outputFileName); + LoggingContext context = new LoggingContext(); + BinarySensorLog log = context.getBinaryLogEntryBinarySensorLog(entries, outputFileName); - for (CANPacket packetContent : packets) { + PacketLogger logger = packetContent -> { DbcPacket packetMeta = dbc.findPacket(packetContent.getId()); if (packetMeta == null) - continue; + return false; - time.set((long) (packetContent.getTimeStamp() * 1000)); for (DbcField field : packetMeta.getFields()) { - values.put(field.getName(), field.getValue(packetContent)); + context.values.put(field.getName(), field.getValue(packetContent)); } - log.writeSensorLogLine(); - } - log.close(); + return true; + }; + + context.processPackets(packets, log, logger); } + + interface PacketLogger { + boolean takeValues(CANPacket packetContent); + } + } diff --git a/src/main/java/com/rusefi/sensor_logs/BinarySensorLog.java b/src/main/java/com/rusefi/sensor_logs/BinarySensorLog.java index c39de60..16c3a87 100644 --- a/src/main/java/com/rusefi/sensor_logs/BinarySensorLog.java +++ b/src/main/java/com/rusefi/sensor_logs/BinarySensorLog.java @@ -142,6 +142,7 @@ public class BinarySensorLog implements SensorLog, Aut @Override public void close() { + System.out.println("Finishing " + fileName); close(stream); stream = null; }