This commit is contained in:
rusefillc 2022-06-22 02:05:04 -04:00
parent 8127f376e7
commit f367a16570
5 changed files with 251 additions and 0 deletions

View File

@ -21,6 +21,10 @@ public class DbcPacket {
return name;
}
public List<DbcField> getFields() {
return fields;
}
@Override
public String toString() {
return "DbcPacket{" +

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

@ -0,0 +1,156 @@
package com.rusefi.sensor_logs;
import java.io.*;
import java.util.*;
import java.util.function.Function;
/**
* MLV .mlq binary log file
* </p>
* Andrey Belomutskiy, (c) 2013-2020
*/
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(Function<T, Double> valueProvider, Collection<T> sensors) {
this.valueProvider = valueProvider;
this.entries = Objects.requireNonNull(sensors, "entries");
}
@Override
public double getSecondsSinceFileStart() {
throw new UnsupportedOperationException();
}
@Override
public void writeSensorLogLine() {
if (stream == null) {
fileName = "gauges_" + ".mlg";
try {
stream = new DataOutputStream(new FileOutputStream(fileName));
writeHeader();
} catch (Throwable e) {
e.printStackTrace();
stream = null;
}
}
if (stream != null) {
try {
stream.write(0);
stream.write(counter++);
stream.writeShort((int) (System.currentTimeMillis() * 100));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
for (T sensor : entries) {
double value = valueProvider.apply(sensor);
sensor.writeToLog(dos, value);
}
byte[] byteArray = baos.toByteArray();
byte checkSum = 0;
for (byte b : byteArray) {
checkSum += b;
}
stream.write(byteArray);
stream.write(checkSum);
if (counter % 20 == 0) {
// for not flush on each block of data but still flush
stream.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void writeHeader() throws IOException {
String headerText = "\"rusEFI " + "sdsr" + "\"\n" +
"\"Capture Date: " + new Date() + "\"\n";
for (char c : "MLVLG\0".toCharArray()) {
stream.write(c);
}
int fieldsDataSize = 0;
for (BinaryLogEntry entry : entries) {
fieldsDataSize += entry.getByteSize();
}
// 0006h Format version = 01
stream.write(0);
stream.write(1);
// 0008h Timestamp
stream.writeInt((int) (System.currentTimeMillis() / 1000));
// 000ch
int offsetToText = Fields.MLQ_HEADER_SIZE + Fields.MLQ_FIELD_HEADER_SIZE * entries.size();
stream.writeShort(offsetToText);
stream.writeShort(0); // reserved?
// 0010h = offset_to_data
stream.writeShort(offsetToText + headerText.length());
// 0012h
stream.writeShort(fieldsDataSize);
// 0014h number of fields
stream.writeShort(entries.size());
for (BinaryLogEntry sensor : entries) {
String name = sensor.getName();
String unit = sensor.getUnit();
// 0000h
stream.write(sensor.getByteSize());
// 0001h
writeLine(stream, name, 34);
// 0023h
writeLine(stream, unit, 11);
// 002Eh scale
stream.writeFloat(1); // todo: multiplier?
// 0032h zeroes
stream.writeInt(0);
// 0036h precision
stream.write(2);
}
if (stream.size() != offsetToText)
throw new IllegalStateException("We are doing something wrong :( stream.size=" + stream.size());
writeLine(stream, headerText, headerText.length());
}
@Override
public void close() {
close(stream);
stream = null;
}
public static void close(Closeable closeable) {
try {
if (closeable != null) {
closeable.close();
}
} catch (IOException e) {
// ignoring
}
}
public String getFileName() {
return fileName;
}
private void writeLine(DataOutputStream stream, String name, int length) throws IOException {
for (int i = 0; i < Math.min(name.length(), length); i++) {
stream.write(name.charAt(i));
}
for (int i = name.length(); i < length; i++)
stream.write(0);
}
}

View File

@ -0,0 +1,6 @@
package com.rusefi.sensor_logs;
public class Fields {
public static final int MLQ_FIELD_HEADER_SIZE = 55;
public static final int MLQ_HEADER_SIZE = 22;
}

View File

@ -0,0 +1,71 @@
package com.rusefi.can.reader.impl;
import com.rusefi.can.CANPacket;
import com.rusefi.can.reader.dbc.DbcField;
import com.rusefi.can.reader.dbc.DbcFile;
import com.rusefi.can.reader.dbc.DbcPacket;
import com.rusefi.sensor_logs.BinaryLogEntry;
import com.rusefi.sensor_logs.BinarySensorLog;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import static com.rusefi.can.reader.impl.ParseDBC.VAG_MOTOR_1;
public class TrcToMlqSandbox {
public static void main(String[] args) throws IOException {
DbcFile dbc = new DbcFile();
{
BufferedReader reader = new BufferedReader(new StringReader(VAG_MOTOR_1));
dbc.read(reader);
}
List<BinaryLogEntry> entries = new ArrayList<>();
for (DbcPacket packet : dbc.packets) {
for (DbcField field : packet.getFields()) {
entries.add(new BinaryLogEntry() {
@Override
public String getName() {
return field.getName();
}
@Override
public String getUnit() {
return "x";
}
@Override
public int getByteSize() {
return 4;
}
@Override
public void writeToLog(DataOutputStream dos, double value) throws IOException {
dos.writeFloat((float) value);
}
});
}
}
PcanTrcReader reader = new PcanTrcReader();
List<CANPacket> 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");
System.out.println(packets.size() + " packets");
BinarySensorLog log = new BinarySensorLog(new Function<BinaryLogEntry, Double>() {
@Override
public Double apply(BinaryLogEntry o) {
System.out.println("apply");
return null;
}
}, entries);
}
}