generated from Grigo/AndroidTemplate
73 lines
3.4 KiB
Java
73 lines
3.4 KiB
Java
import java.util.regex.Pattern;
|
|
import java.util.regex.Matcher;
|
|
|
|
public class TestAISDecoding {
|
|
|
|
public static void main(String[] args) {
|
|
TestAISDecoding test = new TestAISDecoding();
|
|
|
|
// Тестируем AIS сообщение типа 1
|
|
String ais1 = "17RdG7V04d039I5wwj2kh30d050l";
|
|
System.out.println("=== Тест AIS типа 1 ===");
|
|
test.testDecodeAISField(ais1, 8, 30, "MMSI");
|
|
test.testDecodeAISField(ais1, 38, 4, "Navigation Status");
|
|
test.testDecodeAISField(ais1, 50, 10, "Speed");
|
|
test.testDecodeAISField(ais1, 61, 28, "Longitude");
|
|
test.testDecodeAISField(ais1, 89, 27, "Latitude");
|
|
test.testDecodeAISField(ais1, 116, 12, "Course");
|
|
|
|
// Тестируем AIS сообщение типа 5 (собранное из фрагментов)
|
|
String ais5 = "57RdG7T1M>wh4U?62204U?62222222222222220R:0D5?1Uf4<Q1APEC588888888888882";
|
|
System.out.println("\n=== Тест AIS типа 5 ===");
|
|
test.testDecodeAISField(ais5, 8, 30, "MMSI");
|
|
test.testDecodeAISField(ais5, 38, 2, "AIS Version");
|
|
test.testDecodeAISField(ais5, 40, 30, "IMO");
|
|
test.testDecodeAISField(ais5, 70, 42, "Call Sign");
|
|
test.testDecodeAISField(ais5, 112, 120, "Vessel Name");
|
|
test.testDecodeAISField(ais5, 232, 8, "Ship Type");
|
|
test.testDecodeAISField(ais5, 256, 10, "Length");
|
|
test.testDecodeAISField(ais5, 266, 10, "Width");
|
|
test.testDecodeAISField(ais5, 276, 8, "Draft");
|
|
}
|
|
|
|
private void testDecodeAISField(String payload, int startBit, int length, String fieldName) {
|
|
String bits = decodeAISField(payload, startBit, length);
|
|
System.out.printf("%s (биты %d-%d): %s (длина: %d)%n",
|
|
fieldName, startBit, startBit + length - 1, bits, bits.length());
|
|
|
|
if (length <= 30) {
|
|
try {
|
|
int value = Integer.parseInt(bits, 2);
|
|
System.out.printf(" Десятичное значение: %d%n", value);
|
|
} catch (NumberFormatException e) {
|
|
System.out.printf(" Ошибка парсинга: %s%n", e.getMessage());
|
|
}
|
|
}
|
|
}
|
|
|
|
private String decodeAISField(String payload, int startBit, int length) {
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
// Преобразуем каждый символ payload в 6-битное значение
|
|
for (int i = 0; i < payload.length(); i++) {
|
|
char c = payload.charAt(i);
|
|
int value = c - 48; // AIS использует ASCII 48-119 для значений 0-71
|
|
if (value < 0) value += 64;
|
|
|
|
String binary = String.format("%6s", Integer.toBinaryString(value)).replace(' ', '0');
|
|
result.append(binary);
|
|
}
|
|
|
|
String fullBinary = result.toString();
|
|
|
|
// Вырезаем нужный диапазон битов
|
|
if (startBit + length <= fullBinary.length()) {
|
|
return fullBinary.substring(startBit, startBit + length);
|
|
} else {
|
|
System.out.printf(" ВНИМАНИЕ: AIS поле выходит за границы: startBit=%d, length=%d, payloadLength=%d, binaryLength=%d%n",
|
|
startBit, length, payload.length(), fullBinary.length());
|
|
return fullBinary.substring(startBit, Math.min(startBit + length, fullBinary.length()));
|
|
}
|
|
}
|
|
}
|