Commit 84aa0381695a980d3587072d8b65f85ad0d17af1

Authored by Aymeric Cretin
1 parent 705a274be0
Exists in master

[TW] Ignore malformed message

Showing 1 changed file with 7 additions and 3 deletions Inline Diff

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