1 package au.gov.amsa.ais;
2
3 import static java.lang.Integer.parseInt;
4
5 import java.util.Calendar;
6 import java.util.regex.Pattern;
7
8 import au.gov.amsa.util.nmea.NmeaMessage;
9 import au.gov.amsa.util.nmea.NmeaMessageParseException;
10 import au.gov.amsa.util.nmea.NmeaUtil;
11
12 import com.google.common.annotations.VisibleForTesting;
13
14
15
16
17
18
19
20
21 public class NmeaMessageExactEarthTimestamp {
22
23 private final NmeaMessage nmea;
24 private final long time;
25 private final String followingSequenceChecksum;
26
27
28
29
30
31
32 public NmeaMessageExactEarthTimestamp(String line) {
33 nmea = NmeaUtil.parseNmea(line);
34 try {
35 Util.checkArgument(isPghp(line), "not an ExactEarth timestamp: "
36 + line);
37 getFollowingSequenceChecksum();
38 int year = parseInt(getItem(2));
39 int month = parseInt(getItem(3));
40 int day = parseInt(getItem(4));
41 int hour = parseInt(getItem(5));
42 int minute = parseInt(getItem(6));
43 int second = parseInt(getItem(7));
44 int millisecond = parseInt(getItem(8));
45 time = getTime(year, month, day, hour, minute, second, millisecond);
46 followingSequenceChecksum = extractFollowingSequenceChecksum();
47 } catch (RuntimeException e) {
48 throw new AisParseException(e);
49 }
50 }
51
52 private static final Pattern pghpPattern = Pattern
53 .compile("^(\\\\.*\\\\)?\\$PGHP.*$");
54
55
56
57
58
59
60
61
62 @VisibleForTesting
63 static boolean isPghp(String line) {
64 if (line == null)
65 return false;
66 else
67
68 return pghpPattern.matcher(line).matches();
69 }
70
71
72
73
74
75
76
77 public static boolean isExactEarthTimestamp(String line) {
78 try {
79 new NmeaMessageExactEarthTimestamp(line);
80 return true;
81 } catch (AisParseException e) {
82 return false;
83 } catch (NmeaMessageParseException e) {
84 return false;
85 }
86 }
87
88
89
90
91
92
93
94 private String getItem(int index) {
95 return nmea.getItems().get(index);
96 }
97
98
99
100
101
102
103 private static long getTime(int year, int month, int day, int hour,
104 int minute, int second, int millisecond) {
105 Calendar cal = Calendar.getInstance(Util.TIME_ZONE_UTC);
106 cal.set(year, month - 1, day, hour, minute, second);
107 cal.set(Calendar.MILLISECOND, millisecond);
108 return cal.getTimeInMillis();
109 }
110
111
112
113
114
115
116 public long getTime() {
117 return time;
118 }
119
120
121
122
123
124
125 public String getFollowingSequenceChecksum() {
126 return followingSequenceChecksum;
127 }
128
129
130
131
132
133
134 private String extractFollowingSequenceChecksum() {
135 return getItem(13);
136 }
137 }