diff --git a/src/main/java/com/rusefi/can/HandleFolder.java b/src/main/java/com/rusefi/can/HandleFolder.java new file mode 100644 index 0000000..5423ddc --- /dev/null +++ b/src/main/java/com/rusefi/can/HandleFolder.java @@ -0,0 +1,31 @@ +package com.rusefi.can; + +import com.rusefi.can.reader.dbc.DbcFile; +import com.rusefi.can.reader.impl.PcanTrcReader; + +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Objects; + +public class HandleFolder { + public static void doJob(String dbcFileName, String inputFolderName, String outputFolder) throws IOException { + System.out.println("Dbc file " + dbcFileName); + System.out.println("inputFolderName " + inputFolderName); + System.out.println("outputFolder " + outputFolder); + DbcFile dbc = DbcFile.readFromFile(dbcFileName); + + File inputFolder = new File(inputFolderName); + for (String inputFile : Objects.requireNonNull(inputFolder.list((dir, name) -> name.endsWith(".trc")))) { + System.out.println("Handling " + inputFile); + + String fullInputFile = inputFolderName + File.separator + inputFile; + + List packets = new PcanTrcReader().readFile(fullInputFile); + + String outputFileName = outputFolder + File.separator + inputFile; + + LoggingStrategy.writeLog(dbc, packets, outputFileName); + } + } +} diff --git a/src/main/java/com/rusefi/can/LoggingStrategy.java b/src/main/java/com/rusefi/can/LoggingStrategy.java index da1ff26..909fa58 100644 --- a/src/main/java/com/rusefi/can/LoggingStrategy.java +++ b/src/main/java/com/rusefi/can/LoggingStrategy.java @@ -59,8 +59,9 @@ public class LoggingStrategy { return entries; } - public static void writeLog(DbcFile dbc, List entries, List packets) { + public static void writeLog(DbcFile dbc, List packets, String outputFileName) { Map values = new HashMap<>(); + List entries = dbc.getFieldNameEntries(); AtomicReference time = new AtomicReference<>(); BinarySensorLog log = new BinarySensorLog<>(o -> { @@ -68,8 +69,7 @@ public class LoggingStrategy { if (value == null) return 0.0; return value; - }, entries, time::get); - + }, entries, time::get, outputFileName); for (CANPacket packetContent : packets) { DbcPacket packetMeta = dbc.findPacket(packetContent.getId()); diff --git a/src/main/java/com/rusefi/can/TrcToMlq.java b/src/main/java/com/rusefi/can/TrcToMlq.java index 0064d93..8ac97fb 100644 --- a/src/main/java/com/rusefi/can/TrcToMlq.java +++ b/src/main/java/com/rusefi/can/TrcToMlq.java @@ -2,28 +2,32 @@ package com.rusefi.can; import com.rusefi.can.reader.dbc.DbcFile; import com.rusefi.can.reader.impl.PcanTrcReader; -import com.rusefi.sensor_logs.BinaryLogEntry; import java.io.IOException; import java.util.List; public class TrcToMlq { public static void main(String[] args) throws IOException { - if (args.length != 2) { + LoggingStrategy.LOG_ONLY_TRANSLATED_FIELDS = true; + + if (args.length == 3) { + String dbcFileName = args[0]; + String inputFolder = args[1]; + String outputFolder = args[1]; + HandleFolder.doJob(dbcFileName, inputFolder, outputFolder); + } else if (args.length != 2) { System.err.println("Two arguments expected - DBC file name and TRC file name"); System.exit(-1); + } else { + String dbcFileName = args[0]; + String inputFileName = args[1]; + + DbcFile dbc = DbcFile.readFromFile(dbcFileName); + + List packets = new PcanTrcReader().readFile(inputFileName); + + String outputFileName = System.getProperty("mlq_file_name", "gauges.mlg"); + LoggingStrategy.writeLog(dbc, packets, outputFileName); } - String dbcFileName = args[0]; - String trcFileName = args[1]; - - DbcFile dbc = DbcFile.readFromFile(dbcFileName); - - LoggingStrategy.LOG_ONLY_TRANSLATED_FIELDS = true; - List entries = LoggingStrategy.getFieldNameEntries(dbc); - - List packets = new PcanTrcReader().readFile(trcFileName); - System.out.println("Got " + packets.size() + " CAN packets"); - - LoggingStrategy.writeLog(dbc, entries, packets); } } diff --git a/src/main/java/com/rusefi/can/reader/CANLineReader.java b/src/main/java/com/rusefi/can/reader/CANLineReader.java index b7b49a4..3991ddd 100644 --- a/src/main/java/com/rusefi/can/reader/CANLineReader.java +++ b/src/main/java/com/rusefi/can/reader/CANLineReader.java @@ -27,6 +27,7 @@ public interface CANLineReader { if (packet != null) result.add(packet); }); + System.out.println("Got " + result.size() + " CAN packets from " + fileName); return result; } } diff --git a/src/main/java/com/rusefi/can/reader/dbc/DbcFile.java b/src/main/java/com/rusefi/can/reader/dbc/DbcFile.java index c68307f..596b60a 100644 --- a/src/main/java/com/rusefi/can/reader/dbc/DbcFile.java +++ b/src/main/java/com/rusefi/can/reader/dbc/DbcFile.java @@ -1,5 +1,8 @@ package com.rusefi.can.reader.dbc; +import com.rusefi.can.LoggingStrategy; +import com.rusefi.sensor_logs.BinaryLogEntry; + import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; @@ -13,6 +16,8 @@ public class DbcFile { private static final boolean debugEnabled = false; + private List list; + public static DbcFile readFromFile(String fileName) throws IOException { DbcFile dbc = new DbcFile(); { @@ -113,4 +118,12 @@ public class DbcFile { public DbcPacket findPacket(int canId) { return packets.get(canId); } + + + public List getFieldNameEntries() { + if (list == null) { + list = LoggingStrategy.getFieldNameEntries(this); + } + return list; + } } diff --git a/src/main/java/com/rusefi/sensor_logs/BinarySensorLog.java b/src/main/java/com/rusefi/sensor_logs/BinarySensorLog.java index 333767b..c39de60 100644 --- a/src/main/java/com/rusefi/sensor_logs/BinarySensorLog.java +++ b/src/main/java/com/rusefi/sensor_logs/BinarySensorLog.java @@ -20,18 +20,15 @@ public class BinarySensorLog implements SensorLog, Aut private final TimeProvider timeProvider; private DataOutputStream stream; - private final String fileName = System.getProperty("mlq_file_name", "gauges.mlg"); + private final String fileName; private int counter; - public BinarySensorLog(Function valueProvider, Collection sensors) { - this(valueProvider, sensors, System::currentTimeMillis); - } - - public BinarySensorLog(Function valueProvider, Collection sensors, TimeProvider timeProvider) { + public BinarySensorLog(Function valueProvider, Collection sensors, TimeProvider timeProvider, String fileName) { this.valueProvider = Objects.requireNonNull(valueProvider, "valueProvider"); this.entries = Objects.requireNonNull(sensors, "entries"); this.timeProvider = timeProvider; + this.fileName = fileName; } public interface TimeProvider { diff --git a/src/test/java/com/rusefi/can/reader/impl/TrcToMlqSandbox.java b/src/test/java/com/rusefi/can/reader/impl/TrcToMlqSandbox.java index dc0d38a..ac6b249 100644 --- a/src/test/java/com/rusefi/can/reader/impl/TrcToMlqSandbox.java +++ b/src/test/java/com/rusefi/can/reader/impl/TrcToMlqSandbox.java @@ -24,13 +24,10 @@ public class TrcToMlqSandbox { dbc.read(reader); } - List entries = LoggingStrategy.getFieldNameEntries(dbc); - List packets = new PcanTrcReader().readFile(trcFileName); System.out.println(packets.size() + " packets"); - - LoggingStrategy.writeLog(dbc, entries, packets); + LoggingStrategy.writeLog(dbc, packets, "gauges.mlg"); } }