Commit 705a274be09572666e36e27aca402b0ba55f2a2c

Authored by Aymeric Cretin
0 parents
Exists in master

Let's start a new story!

Showing 3 changed files with 232 additions and 0 deletions Side-by-side Diff

... ... @@ -0,0 +1,3 @@
  1 +.idea
  2 +target
  3 +workspace
0 4 \ No newline at end of file
... ... @@ -0,0 +1,50 @@
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<project xmlns="http://maven.apache.org/POM/4.0.0"
  3 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5 + <modelVersion>4.0.0</modelVersion>
  6 +
  7 + <groupId>com.fdit</groupId>
  8 + <artifactId>nmea-extractor</artifactId>
  9 + <version>1.0-SNAPSHOT</version>
  10 + <build>
  11 + <plugins>
  12 + <plugin>
  13 + <groupId>org.apache.maven.plugins</groupId>
  14 + <artifactId>maven-compiler-plugin</artifactId>
  15 + <configuration>
  16 + <source>8</source>
  17 + <target>8</target>
  18 + </configuration>
  19 + </plugin>
  20 + </plugins>
  21 + </build>
  22 +
  23 + <dependencies>
  24 + <dependency>
  25 + <groupId>com.google.guava</groupId>
  26 + <artifactId>guava</artifactId>
  27 + <version>21.0</version>
  28 + </dependency>
  29 + <dependency>
  30 + <groupId>dk.dma.ais.lib</groupId>
  31 + <artifactId>ais-lib-messages</artifactId>
  32 + <version>2.4-SNAPSHOT</version>
  33 + </dependency>
  34 + <dependency>
  35 + <groupId>dk.dma.ais.lib</groupId>
  36 + <artifactId>ais-lib-communication</artifactId>
  37 + <version>2.4-SNAPSHOT</version>
  38 + </dependency>
  39 + <dependency>
  40 + <groupId>dk.dma.commons</groupId>
  41 + <artifactId>dma-commons-util</artifactId>
  42 + <version>0.5-SNAPSHOT</version>
  43 + </dependency>
  44 + <dependency>
  45 + <groupId>dk.dma.enav</groupId>
  46 + <artifactId>enav-model</artifactId>
  47 + <version>0.6-SNAPSHOT</version>
  48 + </dependency>
  49 + </dependencies>
  50 +</project>
0 51 \ No newline at end of file
src/main/java/fr/femto/extractor/track/Main.java View file @ 705a274
... ... @@ -0,0 +1,179 @@
  1 +package fr.femto.extractor.track;
  2 +
  3 +import dk.dma.ais.message.AisMessage;
  4 +import dk.dma.ais.sentence.Vdm;
  5 +
  6 +import java.io.*;
  7 +import java.text.SimpleDateFormat;
  8 +import java.util.List;
  9 +
  10 +import static com.google.common.collect.Lists.newArrayList;
  11 +import static java.lang.Integer.parseInt;
  12 +import static java.lang.Long.parseLong;
  13 +import static java.lang.System.nanoTime;
  14 +import static java.time.Instant.now;
  15 +import static java.util.Arrays.asList;
  16 +import static java.util.Date.from;
  17 +
  18 +public class Main {
  19 +
  20 + private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM_hh-mm-ss");
  21 +
  22 + public static void main(String[] args) throws Exception {
  23 + final long startTime = nanoTime();
  24 + if (args.length == 2 && args[0].compareToIgnoreCase("-mm") == 0) {
  25 + final File input = new File(args[1]);
  26 + printMinMaxTimestamps(input);
  27 + }
  28 + if (args.length == 3) {
  29 + if (args[0].compareToIgnoreCase("-ev") == 0) {
  30 + final File input = new File(args[1]);
  31 + final List<Integer> userIDs = newArrayList();
  32 + asList(args[2].split(",")).forEach(id -> userIDs.add(parseInt(id)));
  33 + extractVessels(
  34 + input,
  35 + new File(input.getParentFile(), getDateStr() + "_" + input.getName()),
  36 + userIDs);
  37 + }
  38 + if (args[0].compareToIgnoreCase("-fp") == 0) {
  39 + final File input = new File(args[1]);
  40 + filterOutPattern(
  41 + input,
  42 + new File(input.getParentFile(), getDateStr() + "_" + input.getName()),
  43 + args[2]);
  44 + }
  45 + if (args[0].compareToIgnoreCase("-ep") == 0) {
  46 + final File input = new File(args[1]);
  47 + extractPattern(
  48 + input,
  49 + new File(input.getParentFile(), getDateStr() + "_" + input.getName()),
  50 + args[2]);
  51 + }
  52 + }
  53 + if (args.length == 4 && args[0].compareToIgnoreCase("-tw") == 0) {
  54 + final File input = new File(args[1]);
  55 + final long lowerTimestamp = parseLong(args[2]);
  56 + extractTimeWindow(
  57 + input,
  58 + new File(input.getParentFile(), getDateStr() + "_" + input.getName()),
  59 + lowerTimestamp,
  60 + lowerTimestamp + parseLong(args[3]) * 3600);
  61 + }
  62 + final long endTime = nanoTime();
  63 + System.out.println("Execution time in milliseconds: " + (endTime - startTime) / 1000000);
  64 + }
  65 +
  66 + private static String getDateStr() {
  67 + return DATE_FORMAT.format(from(now()));
  68 + }
  69 +
  70 + private static void printMinMaxTimestamps(final File input) throws IOException {
  71 + try (final FileReader fileReader = new FileReader(input);
  72 + final BufferedReader bufferedReader = new BufferedReader(fileReader)) {
  73 + String currentMessage = bufferedReader.readLine();
  74 + long maxTimestamp = Long.MIN_VALUE;
  75 + long minTimestamp = Long.MAX_VALUE;
  76 + while (currentMessage != null) {
  77 + if (!currentMessage.isEmpty()) {
  78 + try {
  79 + final long timestamp = parseLong(currentMessage.substring(3).split(",")[0]);
  80 + if (timestamp < minTimestamp) {
  81 + minTimestamp = timestamp;
  82 + }
  83 + if (timestamp > maxTimestamp) {
  84 + maxTimestamp = timestamp;
  85 + }
  86 + } catch (final Exception ignored) {
  87 + }
  88 + }
  89 + currentMessage = bufferedReader.readLine();
  90 + }
  91 + System.out.println("Min timestamp: " + minTimestamp);
  92 + System.out.println("Max timestamp: " + maxTimestamp);
  93 + }
  94 + }
  95 +
  96 + private static void extractVessels(final File input,
  97 + final File output,
  98 + final List<Integer> MMSIs) throws IOException {
  99 + try (final FileReader fileReader = new FileReader(input);
  100 + final BufferedReader bufferedReader = new BufferedReader(fileReader);
  101 + final FileWriter fileWriter = new FileWriter(output);
  102 + final BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) {
  103 + String currentMessage = bufferedReader.readLine();
  104 + while (currentMessage != null) {
  105 + if (!currentMessage.isEmpty()) {
  106 + final Vdm vdm = new Vdm();
  107 + try {
  108 + vdm.parse(currentMessage);
  109 + if (MMSIs.contains(AisMessage.getInstance(vdm).getUserId())) {
  110 + bufferedWriter.write(currentMessage + "\n");
  111 + }
  112 + } catch (final Exception ignored) {
  113 +
  114 + }
  115 + }
  116 + currentMessage = bufferedReader.readLine();
  117 + }
  118 + }
  119 + }
  120 +
  121 + private static void filterOutPattern(final File input,
  122 + final File output,
  123 + final String pattern) throws IOException {
  124 + try (final FileReader fileReader = new FileReader(input);
  125 + final BufferedReader bufferedReader = new BufferedReader(fileReader);
  126 + final FileWriter fileWriter = new FileWriter(output);
  127 + final BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) {
  128 + String currentMessage = bufferedReader.readLine();
  129 + while (currentMessage != null) {
  130 + if (!currentMessage.isEmpty()) {
  131 + if (!currentMessage.contains(pattern)) {
  132 + bufferedWriter.write(currentMessage + "\n");
  133 + }
  134 + }
  135 + currentMessage = bufferedReader.readLine();
  136 + }
  137 + }
  138 + }
  139 +
  140 + private static void extractPattern(final File input,
  141 + final File output,
  142 + final String pattern) throws IOException {
  143 + try (final FileReader fileReader = new FileReader(input);
  144 + final BufferedReader bufferedReader = new BufferedReader(fileReader);
  145 + final FileWriter fileWriter = new FileWriter(output);
  146 + final BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) {
  147 + String currentMessage = bufferedReader.readLine();
  148 + while (currentMessage != null) {
  149 + if (!currentMessage.isEmpty()) {
  150 + if (currentMessage.contains(pattern)) {
  151 + bufferedWriter.write(currentMessage + "\n");
  152 + }
  153 + }
  154 + currentMessage = bufferedReader.readLine();
  155 + }
  156 + }
  157 + }
  158 +
  159 + private static void extractTimeWindow(final File input,
  160 + final File output,
  161 + final long lowerTimestamp,
  162 + final long upperTimestamp) throws IOException {
  163 + try (final FileReader fileReader = new FileReader(input);
  164 + final BufferedReader bufferedReader = new BufferedReader(fileReader);
  165 + final FileWriter fileWriter = new FileWriter(output);
  166 + final BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) {
  167 + String currentMessage = bufferedReader.readLine();
  168 + while (currentMessage != null) {
  169 + if (!currentMessage.isEmpty()) {
  170 + final long timestamp = parseLong(currentMessage.substring(3).split(",")[0]);
  171 + if (lowerTimestamp <= timestamp && timestamp <= upperTimestamp) {
  172 + bufferedWriter.write(currentMessage + "\n");
  173 + }
  174 + }
  175 + currentMessage = bufferedReader.readLine();
  176 + }
  177 + }
  178 + }
  179 +}
0 180 \ No newline at end of file