can-log-tools/src/main/java/com/rusefi/can/reader/CANLineReader.java

78 lines
2.5 KiB
Java
Raw Normal View History

2022-02-02 19:37:29 -08:00
package com.rusefi.can.reader;
import com.rusefi.can.CANPacket;
2022-09-12 16:59:10 -07:00
import com.rusefi.can.reader.impl.CANoeReader;
2022-09-12 23:01:23 -07:00
import com.rusefi.can.reader.impl.CanHackerReader;
2023-10-14 09:17:23 -07:00
import com.rusefi.can.reader.impl.*;
2022-02-02 19:37:29 -08:00
2022-02-15 13:17:28 -08:00
import java.io.IOException;
2022-06-20 18:29:27 -07:00
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
2022-02-15 13:17:28 -08:00
import java.util.List;
2022-09-12 23:01:23 -07:00
import java.util.function.Consumer;
2022-06-20 18:29:27 -07:00
import java.util.stream.Stream;
2022-02-15 13:17:28 -08:00
2022-02-02 19:37:29 -08:00
public interface CANLineReader {
2022-11-12 19:13:12 -08:00
// locale drama is here? todo: more flexibility?
char FLOAT_DIVIDER = Double.toString(0).charAt(1);
2022-09-12 23:01:23 -07:00
2022-02-15 13:17:28 -08:00
static byte[] readHexArray(String[] tokens, int start, int size) {
byte[] data = new byte[size];
for (int i = 0; i < size; i++)
data[i] = (byte) Integer.parseInt(tokens[start + i], 16);
return data;
}
2022-09-12 16:59:10 -07:00
static CANLineReader getReader() {
2022-11-10 15:12:02 -08:00
switch (ReaderTypeHolder.INSTANCE.getType()) {
2022-09-12 16:59:10 -07:00
case CANOE:
return CANoeReader.INSTANCE;
2022-09-12 23:01:23 -07:00
case CANHACKER:
return CanHackerReader.INSTANCE;
2023-10-14 09:17:23 -07:00
case PCAN:
2022-09-12 16:59:10 -07:00
default:
2023-12-30 16:22:37 -08:00
return AutoFormatReader.INSTANCE;
2022-09-12 16:59:10 -07:00
}
}
2023-07-19 20:29:47 -07:00
default CANPacket readLine(String line) {
return readLine(line, "no-file-name");
}
2023-12-15 07:38:17 -08:00
default CANPacket readLine(String line, String fileName) {
return readLine(line, fileName, -1);
}
CANPacket readLine(String line, String fileName, int lineIndex);
2022-02-15 13:17:28 -08:00
2022-06-20 18:29:27 -07:00
default List<CANPacket> readFile(String fileName) throws IOException {
2022-09-12 23:01:23 -07:00
return skipHeaderAndRead(fileName, 0);
}
default List<CANPacket> skipHeaderAndRead(String fileName, final int skipCount) throws IOException {
2022-06-20 18:29:27 -07:00
List<CANPacket> result = new ArrayList<>();
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
2022-09-12 23:01:23 -07:00
stream.forEach(new Consumer<String>() {
int index = 0;
@Override
public void accept(String s) {
if (index++ < skipCount)
return;
2023-12-15 07:43:39 -08:00
CANPacket packet = readLine(s, fileName, index);
2022-09-12 23:01:23 -07:00
if (packet != null)
result.add(packet);
}
2022-06-20 18:29:27 -07:00
});
return result;
}
}
2022-09-12 23:01:23 -07:00
static String attemptToFixLocalization(String maybeInvalidString) {
if (FLOAT_DIVIDER == '.')
return maybeInvalidString.replace(',', '.'); // converting RU file on EN device
return maybeInvalidString.replace('.', ',');// converting US file on RU device
}
2022-02-02 19:37:29 -08:00
}