can-log-tools/src/main/java/com/rusefi/can/analysis/ByteRateOfChange.java

163 lines
5.3 KiB
Java
Raw Normal View History

2022-11-12 09:29:20 -08:00
package com.rusefi.can.analysis;
2022-11-10 15:12:02 -08:00
2022-11-12 09:29:20 -08:00
import com.rusefi.can.CANPacket;
import com.rusefi.mlv.LoggingContext;
import com.rusefi.mlv.LoggingStrategy;
2022-11-10 15:12:02 -08:00
import com.rusefi.can.reader.CANLineReader;
2022-11-10 20:38:46 -08:00
import com.rusefi.sensor_logs.BinaryLogEntry;
import com.rusefi.sensor_logs.BinarySensorLog;
2022-11-10 15:12:02 -08:00
2022-11-10 16:23:51 -08:00
import java.io.File;
import java.io.FileOutputStream;
2022-11-10 15:12:02 -08:00
import java.io.IOException;
2022-11-10 16:23:51 -08:00
import java.io.PrintStream;
2022-11-10 15:12:02 -08:00
import java.util.*;
public class ByteRateOfChange {
2022-11-10 16:23:51 -08:00
public static TraceReport process(String fullFileName, String reportDestinationFolder, String simpleFileName) throws IOException {
2022-11-10 15:12:02 -08:00
List<CANPacket> packets = CANLineReader.getReader().readFile(fullFileName);
2022-11-12 10:07:13 -08:00
PerSidDump.handle(packets, simpleFileName);
2022-11-10 15:12:02 -08:00
HashMap<ByteId, ByteStatistics> statistics = new HashMap<>();
for (CANPacket packet : packets) {
for (int index = 0; index < packet.getData().length; index++) {
ByteId key = new ByteId(packet.getId(), index);
ByteStatistics stats = statistics.computeIfAbsent(key, byteId -> new ByteStatistics(key));
stats.uniqueValues.add((int) packet.getData()[index]);
}
}
List<ByteStatistics> allStats = new ArrayList<>(statistics.values());
allStats.sort((o1, o2) -> o2.getUniqueValues() - o1.getUniqueValues());
System.out.println(allStats);
2022-11-10 16:23:51 -08:00
PrintStream ps = new PrintStream(new FileOutputStream(reportDestinationFolder + File.separator + simpleFileName + ".txt", false));
2022-11-10 15:12:02 -08:00
for (ByteStatistics byteStatistics : allStats) {
ByteId key = byteStatistics.key;
2022-11-10 16:23:51 -08:00
ps.println(dualSid(key.sid) + " at index " + key.index + " has " + byteStatistics.getUniqueValues() + " unique value(s)");
2022-11-10 15:12:02 -08:00
}
2022-11-10 16:23:51 -08:00
ps.close();
2022-11-10 15:12:02 -08:00
2022-11-12 09:29:20 -08:00
double duration = packets.isEmpty() ? 0 : packets.get(packets.size() - 1).getTimeStamp() - packets.get(0).getTimeStamp();
TraceReport traceReport = new TraceReport(simpleFileName, statistics, duration);
2022-11-11 22:53:31 -08:00
traceReport.createMegaLogViewer(packets);
return traceReport;
2022-11-10 15:12:02 -08:00
}
public static String dualSid(int sid) {
2022-11-12 10:39:32 -08:00
return dualSid(sid, "/");
}
public static String dualSid(int sid, String separator) {
return String.format("%d%s0x%x", sid, separator, sid);
2022-11-10 15:12:02 -08:00
}
2022-11-12 09:29:20 -08:00
public static class ByteStatistics {
2022-11-10 15:12:02 -08:00
HashSet<Integer> uniqueValues = new HashSet<>();
private final ByteId key;
public ByteStatistics(ByteId key) {
this.key = key;
}
public int getUniqueValues() {
return uniqueValues.size();
}
@Override
public String toString() {
return "ByteStatistics{" +
"counter=" + uniqueValues.size() +
", key=" + key +
'}';
}
}
2022-11-12 09:29:20 -08:00
public static class ByteId {
2022-11-10 15:12:02 -08:00
final int sid;
final int index;
public ByteId(int sid, int index) {
this.sid = sid;
this.index = index;
}
2022-11-11 22:53:31 -08:00
private String getLogKey() {
2022-11-11 23:12:28 -08:00
return dualSid(sid) + "_byte_" + index + "_bit_" + (index * 8);
2022-11-11 22:53:31 -08:00
}
2022-11-10 15:12:02 -08:00
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ByteId byteId = (ByteId) o;
return sid == byteId.sid && index == byteId.index;
}
@Override
public int hashCode() {
return Objects.hash(sid, index);
}
@Override
public String toString() {
2022-11-11 23:12:28 -08:00
return getLogKey();
2022-11-10 15:12:02 -08:00
}
}
2022-11-10 16:23:51 -08:00
public static class TraceReport extends HashMap<ByteId, ByteStatistics> {
private final String simpleFileName;
private final HashMap<ByteId, ByteStatistics> statistics;
2022-11-12 10:07:13 -08:00
private final double durationMs;
2022-11-10 16:23:51 -08:00
2022-11-12 09:29:20 -08:00
public TraceReport(String simpleFileName, HashMap<ByteId, ByteStatistics> statistics, double duration) {
2022-11-10 16:23:51 -08:00
this.simpleFileName = simpleFileName;
this.statistics = statistics;
2022-11-12 10:07:13 -08:00
this.durationMs = duration;
2022-11-12 09:29:20 -08:00
}
String getSummary() {
2022-11-12 10:07:13 -08:00
return getSimpleFileName() + " (duration=" + (int)(durationMs / 1000) + "secs)";
2022-11-10 16:23:51 -08:00
}
public String getSimpleFileName() {
return simpleFileName;
}
public HashMap<ByteId, ByteStatistics> getStatistics() {
return statistics;
}
2022-11-10 20:38:46 -08:00
2022-11-11 22:53:31 -08:00
public void createMegaLogViewer(List<CANPacket> packets) {
2022-11-10 20:38:46 -08:00
Map<ByteId, BinaryLogEntry> entries = new HashMap<>();
for (ByteId key : statistics.keySet()) {
2022-11-11 22:53:31 -08:00
entries.put(key, BinaryLogEntry.createFloatLogEntry(key.getLogKey(), Integer.toBinaryString(key.sid)));
2022-11-10 20:38:46 -08:00
}
2022-11-11 22:53:31 -08:00
LoggingContext context = new LoggingContext();
BinarySensorLog<BinaryLogEntry> log = context.getBinaryLogEntryBinarySensorLog(entries.values(), simpleFileName + LoggingStrategy.MLG);
2022-11-12 17:59:47 -08:00
context.writeLogContent(packets, log, packetContent -> {
2022-11-11 22:53:31 -08:00
for (int i = 0; i < packetContent.getData().length; i++) {
int value = packetContent.getData()[i] & 0xFF;
String name = new ByteId(packetContent.getId(), i).getLogKey();
2022-11-12 17:59:47 -08:00
context.currentSnapshot.put(name, (double) value);
2022-11-11 22:53:31 -08:00
}
return true;
});
2022-11-10 20:38:46 -08:00
}
2022-11-10 16:23:51 -08:00
}
2022-11-10 15:12:02 -08:00
}