2022-11-10 15:12:02 -08:00
|
|
|
package com.rusefi.can;
|
|
|
|
|
|
|
|
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.*;
|
2022-11-10 20:38:46 -08:00
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
2022-11-10 15:12:02 -08:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
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-10 16:23:51 -08:00
|
|
|
return new TraceReport(simpleFileName, statistics);
|
2022-11-10 15:12:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public static String dualSid(int sid) {
|
|
|
|
return String.format("%d/0x%x", sid, sid);
|
|
|
|
}
|
|
|
|
|
|
|
|
static class ByteStatistics {
|
|
|
|
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 +
|
|
|
|
'}';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static class ByteId {
|
|
|
|
final int sid;
|
|
|
|
final int index;
|
|
|
|
|
|
|
|
public ByteId(int sid, int index) {
|
|
|
|
this.sid = sid;
|
|
|
|
this.index = index;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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() {
|
|
|
|
return "ByteId{" +
|
|
|
|
"sid=" + sid +
|
|
|
|
", index=" + index +
|
|
|
|
'}';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
public TraceReport(String simpleFileName, HashMap<ByteId, ByteStatistics> statistics) {
|
|
|
|
this.simpleFileName = simpleFileName;
|
|
|
|
this.statistics = statistics;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getSimpleFileName() {
|
|
|
|
return simpleFileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public HashMap<ByteId, ByteStatistics> getStatistics() {
|
|
|
|
return statistics;
|
|
|
|
}
|
2022-11-10 20:38:46 -08:00
|
|
|
|
|
|
|
public void createMegaLogViewer() {
|
|
|
|
Map<ByteId, BinaryLogEntry> entries = new HashMap<>();
|
|
|
|
|
|
|
|
for (ByteId key : statistics.keySet()) {
|
|
|
|
entries.put(key, BinaryLogEntry.createFloatLogEntry(key.sid + "_" + key.index, Integer.toBinaryString(key.sid)));
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, Double> values = new HashMap<>();
|
|
|
|
AtomicReference<Long> time = new AtomicReference<>();
|
|
|
|
BinarySensorLog<BinaryLogEntry> 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);
|
|
|
|
}
|
2022-11-10 16:23:51 -08:00
|
|
|
}
|
2022-11-10 15:12:02 -08:00
|
|
|
}
|