From 3c15fc279d3d1682daccb39a109602446f5468f2 Mon Sep 17 00:00:00 2001 From: rusefillc Date: Wed, 22 Jun 2022 03:19:09 -0400 Subject: [PATCH] progress --- .../com/rusefi/can/reader/dbc/DbcFile.java | 9 ++++ .../rusefi/sensor_logs/BinarySensorLog.java | 18 ++++++-- .../can/reader/impl/GetValueFromTrc.java | 4 ++ .../com/rusefi/can/reader/impl/ParseDBC.java | 2 +- .../can/reader/impl/TrcToMlqSandbox.java | 46 ++++++++++++++----- 5 files changed, 64 insertions(+), 15 deletions(-) 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 75e405c..0c3eeea 100644 --- a/src/main/java/com/rusefi/can/reader/dbc/DbcFile.java +++ b/src/main/java/com/rusefi/can/reader/dbc/DbcFile.java @@ -54,4 +54,13 @@ public class DbcFile { if (currentState != null) this.packets.add(currentState); } + + // todo: performance optimization SOON + public DbcPacket findPacket(int i) { + for (DbcPacket packet : packets) { + if (packet.getId() == i) + return packet; + } + return null; + } } diff --git a/src/main/java/com/rusefi/sensor_logs/BinarySensorLog.java b/src/main/java/com/rusefi/sensor_logs/BinarySensorLog.java index 8473ec8..05f207c 100644 --- a/src/main/java/com/rusefi/sensor_logs/BinarySensorLog.java +++ b/src/main/java/com/rusefi/sensor_logs/BinarySensorLog.java @@ -12,6 +12,7 @@ import java.util.function.Function; public class BinarySensorLog implements SensorLog { private final Function valueProvider; private final Collection entries; + private final TimeProvider timeProvider; private DataOutputStream stream; private String fileName; @@ -19,8 +20,17 @@ public class BinarySensorLog implements SensorLog { private int counter; public BinarySensorLog(Function valueProvider, Collection sensors) { - this.valueProvider = valueProvider; + this(valueProvider, sensors, System::currentTimeMillis); + } + + public BinarySensorLog(Function valueProvider, Collection sensors, TimeProvider timeProvider) { + this.valueProvider = Objects.requireNonNull(valueProvider, "valueProvider"); this.entries = Objects.requireNonNull(sensors, "entries"); + this.timeProvider = timeProvider; + } + + public interface TimeProvider { + long currentTimestamp(); } @Override @@ -46,13 +56,15 @@ public class BinarySensorLog implements SensorLog { try { stream.write(0); stream.write(counter++); - stream.writeShort((int) (System.currentTimeMillis() * 100)); + stream.writeShort((int) (timeProvider.currentTimestamp() * 100)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); 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); } diff --git a/src/test/java/com/rusefi/can/reader/impl/GetValueFromTrc.java b/src/test/java/com/rusefi/can/reader/impl/GetValueFromTrc.java index d6c7d5e..9ffbc5b 100644 --- a/src/test/java/com/rusefi/can/reader/impl/GetValueFromTrc.java +++ b/src/test/java/com/rusefi/can/reader/impl/GetValueFromTrc.java @@ -10,7 +10,9 @@ import java.io.IOException; import java.io.StringReader; import static com.rusefi.can.reader.impl.ParseDBC.VAG_MOTOR_1; +import static junit.framework.TestCase.assertNull; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; public class GetValueFromTrc { @@ -23,6 +25,8 @@ public class GetValueFromTrc { BufferedReader reader = new BufferedReader(new StringReader(VAG_MOTOR_1)); dbc.read(reader); } + assertNotNull(dbc.findPacket(640)); + assertNull(dbc.findPacket(1640)); String trcLine = " 3769) 2117.7 Rx 0280 8 01 1D DF 12 1E 00 1A 1E "; diff --git a/src/test/java/com/rusefi/can/reader/impl/ParseDBC.java b/src/test/java/com/rusefi/can/reader/impl/ParseDBC.java index f79f5c0..b39b231 100644 --- a/src/test/java/com/rusefi/can/reader/impl/ParseDBC.java +++ b/src/test/java/com/rusefi/can/reader/impl/ParseDBC.java @@ -15,7 +15,7 @@ public class ParseDBC { public static final String VAG_MOTOR_1 = "BO_ 640 Motor_1: 8 XXX\n" + " SG_ Fahrerwunschmoment : 56|8@1+ (0.39,0) [0|99] \"MDI\" XXX\n" + " SG_ mechanisches_Motor_Verlustmomen : 48|8@1+ (0.39,0) [0|99] \"MDI\" XXX\n" + - " SG_ Fahrpedalwert_oder_Drosselklapp : 40|8@1+ (0.4,0) [0|101.6] \"%\" XXX\n" + + " SG_ PPS_TPS : 40|8@1+ (0.4,0) [0|101.6] \"%\" XXX\n" + " SG_ inneres_Motor_Moment_ohne_exter : 32|8@1+ (0.39,0) [0|99] \"MDI\" XXX\n" + " SG_ RPM : 16|16@1+ (0.25,0) [0|16256] \"U/min\" XXX\n" + " SG_ inneres_Motor_Moment : 8|8@1+ (0.39,0) [0|99] \"MDI\" XXX\n" + 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 be480f4..f6058b8 100644 --- a/src/test/java/com/rusefi/can/reader/impl/TrcToMlqSandbox.java +++ b/src/test/java/com/rusefi/can/reader/impl/TrcToMlqSandbox.java @@ -12,12 +12,18 @@ import java.io.DataOutputStream; import java.io.IOException; import java.io.StringReader; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; -import java.util.function.Function; +import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; import static com.rusefi.can.reader.impl.ParseDBC.VAG_MOTOR_1; public class TrcToMlqSandbox { + + // private static final String fileName = "C:\\stuff\\rusefi_documentation\\OEM-Docs\\VAG\\2006-Passat-B6\\passat-b6-stock-ecu-ecu-ptcan-not-running-pedal-up-and-down.trc"; + private static final String fileName = "C:\\stuff\\rusefi_documentation\\OEM-Docs\\VAG\\2006-Passat-B6\\passat-b6-stock-ecu-ecu-ptcan-parked-revving.trc"; + public static void main(String[] args) throws IOException { DbcFile dbc = new DbcFile(); { @@ -48,24 +54,42 @@ public class TrcToMlqSandbox { public void writeToLog(DataOutputStream dos, double value) throws IOException { dos.writeFloat((float) value); } - }); + @Override + public String toString() { + return getName(); + } + }); } } PcanTrcReader reader = new PcanTrcReader(); - List packets = reader.readFile("C:\\stuff\\rusefi_documentation\\OEM-Docs\\VAG\\2006-Passat-B6\\passat-b6-stock-ecu-ecu-ptcan-not-running-pedal-up-and-down.trc"); + List packets = reader.readFile(fileName); System.out.println(packets.size() + " packets"); - BinarySensorLog log = new BinarySensorLog(new Function() { - @Override - public Double apply(BinaryLogEntry o) { - System.out.println("apply"); - return null; + 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); + + + for (CANPacket packetContent : packets) { + DbcPacket packetMeta = dbc.findPacket(packetContent.getId()); + if (packetMeta == null) + continue; + + time.set((long) (packetContent.getTimeStamp() * 1000)); + for (DbcField field : packetMeta.getFields()) { + values.put(field.getName(), field.getValue(packetContent)); } - }, entries); - - + log.writeSensorLogLine(); + } + log.close(); } }