From 705a274be09572666e36e27aca402b0ba55f2a2c Mon Sep 17 00:00:00 2001 From: Aymeric Cretin Date: Tue, 5 Nov 2019 15:14:22 +0100 Subject: [PATCH] Let's start a new story! --- .gitignore | 3 + pom.xml | 50 ++++++ src/main/java/fr/femto/extractor/track/Main.java | 179 ++++++++++++++++++++++ 3 files changed, 232 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/fr/femto/extractor/track/Main.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..66d8220 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +target +workspace \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..c3db6b2 --- /dev/null +++ b/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + com.fdit + nmea-extractor + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + + + com.google.guava + guava + 21.0 + + + dk.dma.ais.lib + ais-lib-messages + 2.4-SNAPSHOT + + + dk.dma.ais.lib + ais-lib-communication + 2.4-SNAPSHOT + + + dk.dma.commons + dma-commons-util + 0.5-SNAPSHOT + + + dk.dma.enav + enav-model + 0.6-SNAPSHOT + + + \ No newline at end of file diff --git a/src/main/java/fr/femto/extractor/track/Main.java b/src/main/java/fr/femto/extractor/track/Main.java new file mode 100644 index 0000000..9805b87 --- /dev/null +++ b/src/main/java/fr/femto/extractor/track/Main.java @@ -0,0 +1,179 @@ +package fr.femto.extractor.track; + +import dk.dma.ais.message.AisMessage; +import dk.dma.ais.sentence.Vdm; + +import java.io.*; +import java.text.SimpleDateFormat; +import java.util.List; + +import static com.google.common.collect.Lists.newArrayList; +import static java.lang.Integer.parseInt; +import static java.lang.Long.parseLong; +import static java.lang.System.nanoTime; +import static java.time.Instant.now; +import static java.util.Arrays.asList; +import static java.util.Date.from; + +public class Main { + + private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM_hh-mm-ss"); + + public static void main(String[] args) throws Exception { + final long startTime = nanoTime(); + if (args.length == 2 && args[0].compareToIgnoreCase("-mm") == 0) { + final File input = new File(args[1]); + printMinMaxTimestamps(input); + } + if (args.length == 3) { + if (args[0].compareToIgnoreCase("-ev") == 0) { + final File input = new File(args[1]); + final List userIDs = newArrayList(); + asList(args[2].split(",")).forEach(id -> userIDs.add(parseInt(id))); + extractVessels( + input, + new File(input.getParentFile(), getDateStr() + "_" + input.getName()), + userIDs); + } + if (args[0].compareToIgnoreCase("-fp") == 0) { + final File input = new File(args[1]); + filterOutPattern( + input, + new File(input.getParentFile(), getDateStr() + "_" + input.getName()), + args[2]); + } + if (args[0].compareToIgnoreCase("-ep") == 0) { + final File input = new File(args[1]); + extractPattern( + input, + new File(input.getParentFile(), getDateStr() + "_" + input.getName()), + args[2]); + } + } + if (args.length == 4 && args[0].compareToIgnoreCase("-tw") == 0) { + final File input = new File(args[1]); + final long lowerTimestamp = parseLong(args[2]); + extractTimeWindow( + input, + new File(input.getParentFile(), getDateStr() + "_" + input.getName()), + lowerTimestamp, + lowerTimestamp + parseLong(args[3]) * 3600); + } + final long endTime = nanoTime(); + System.out.println("Execution time in milliseconds: " + (endTime - startTime) / 1000000); + } + + private static String getDateStr() { + return DATE_FORMAT.format(from(now())); + } + + private static void printMinMaxTimestamps(final File input) throws IOException { + try (final FileReader fileReader = new FileReader(input); + final BufferedReader bufferedReader = new BufferedReader(fileReader)) { + String currentMessage = bufferedReader.readLine(); + long maxTimestamp = Long.MIN_VALUE; + long minTimestamp = Long.MAX_VALUE; + while (currentMessage != null) { + if (!currentMessage.isEmpty()) { + try { + final long timestamp = parseLong(currentMessage.substring(3).split(",")[0]); + if (timestamp < minTimestamp) { + minTimestamp = timestamp; + } + if (timestamp > maxTimestamp) { + maxTimestamp = timestamp; + } + } catch (final Exception ignored) { + } + } + currentMessage = bufferedReader.readLine(); + } + System.out.println("Min timestamp: " + minTimestamp); + System.out.println("Max timestamp: " + maxTimestamp); + } + } + + private static void extractVessels(final File input, + final File output, + final List MMSIs) throws IOException { + try (final FileReader fileReader = new FileReader(input); + final BufferedReader bufferedReader = new BufferedReader(fileReader); + final FileWriter fileWriter = new FileWriter(output); + final BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) { + String currentMessage = bufferedReader.readLine(); + while (currentMessage != null) { + if (!currentMessage.isEmpty()) { + final Vdm vdm = new Vdm(); + try { + vdm.parse(currentMessage); + if (MMSIs.contains(AisMessage.getInstance(vdm).getUserId())) { + bufferedWriter.write(currentMessage + "\n"); + } + } catch (final Exception ignored) { + + } + } + currentMessage = bufferedReader.readLine(); + } + } + } + + private static void filterOutPattern(final File input, + final File output, + final String pattern) throws IOException { + try (final FileReader fileReader = new FileReader(input); + final BufferedReader bufferedReader = new BufferedReader(fileReader); + final FileWriter fileWriter = new FileWriter(output); + final BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) { + String currentMessage = bufferedReader.readLine(); + while (currentMessage != null) { + if (!currentMessage.isEmpty()) { + if (!currentMessage.contains(pattern)) { + bufferedWriter.write(currentMessage + "\n"); + } + } + currentMessage = bufferedReader.readLine(); + } + } + } + + private static void extractPattern(final File input, + final File output, + final String pattern) throws IOException { + try (final FileReader fileReader = new FileReader(input); + final BufferedReader bufferedReader = new BufferedReader(fileReader); + final FileWriter fileWriter = new FileWriter(output); + final BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) { + String currentMessage = bufferedReader.readLine(); + while (currentMessage != null) { + if (!currentMessage.isEmpty()) { + if (currentMessage.contains(pattern)) { + bufferedWriter.write(currentMessage + "\n"); + } + } + currentMessage = bufferedReader.readLine(); + } + } + } + + private static void extractTimeWindow(final File input, + final File output, + final long lowerTimestamp, + final long upperTimestamp) throws IOException { + try (final FileReader fileReader = new FileReader(input); + final BufferedReader bufferedReader = new BufferedReader(fileReader); + final FileWriter fileWriter = new FileWriter(output); + final BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) { + String currentMessage = bufferedReader.readLine(); + while (currentMessage != null) { + if (!currentMessage.isEmpty()) { + final long timestamp = parseLong(currentMessage.substring(3).split(",")[0]); + if (lowerTimestamp <= timestamp && timestamp <= upperTimestamp) { + bufferedWriter.write(currentMessage + "\n"); + } + } + currentMessage = bufferedReader.readLine(); + } + } + } +} \ No newline at end of file -- 1.7.10.4