1 package au.gov.amsa.ais.message;
2
3 import java.util.Calendar;
4 import java.util.TimeZone;
5
6 import com.google.common.annotations.VisibleForTesting;
7
8 import au.gov.amsa.ais.AisExtractor;
9 import au.gov.amsa.ais.AisExtractorFactory;
10 import au.gov.amsa.ais.AisMessage;
11 import au.gov.amsa.ais.AisMessageType;
12 import au.gov.amsa.ais.HasMmsi;
13 import au.gov.amsa.ais.Util;
14
15
16
17
18
19
20
21 public class AisBaseStation implements AisMessage, HasMmsi {
22 @VisibleForTesting
23 static final int MIN_LENGTH = 168;
24 private final AisExtractor extractor;
25 private final String source;
26 private final int messageId;
27
28
29
30
31
32
33 public AisBaseStation(String message, int padBits) {
34 this(message, null, padBits);
35 }
36
37
38
39
40
41
42
43 public AisBaseStation(String message, String source, int padBits) {
44 this(Util.getAisExtractorFactory(), message, source, padBits);
45 }
46
47
48
49
50
51
52
53
54
55 public AisBaseStation(AisExtractorFactory factory, String message, String source, int padBits) {
56 this.source = source;
57 this.extractor = factory.create(message, MIN_LENGTH, padBits);
58 messageId = extractor.getMessageId();
59 Util.checkMessageId(messageId, AisMessageType.BASE_STATION_REPORT);
60 Util.checkLatLong(getLatitude(), getLongitude());
61
62 }
63
64 @Override
65 public int getMessageId() {
66 return messageId;
67 }
68
69 public int getRepeatIndicator() {
70 return extractor.getValue(6, 8);
71 }
72
73 @Override
74 public int getMmsi() {
75 return extractor.getValue(8, 38);
76 }
77
78 public int getYear() {
79 return extractor.getValue(38, 52);
80 }
81
82 public int getMonth() {
83 return extractor.getValue(52, 56);
84 }
85
86 public int getDay() {
87 return extractor.getValue(56, 61);
88 }
89
90 public int getHour() {
91 return extractor.getValue(61, 66);
92 }
93
94 public int getMinute() {
95 return extractor.getValue(66, 72);
96 }
97
98 public int getSecond() {
99 return extractor.getValue(72, 78);
100 }
101
102 public int getPositionAccuracy() {
103 return extractor.getValue(78, 79);
104 }
105
106 private long calculateTimestamp() {
107 Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
108 cal.set(getYear(), getMonth() - 1, getDay(), getHour(), getMinute(), getSecond());
109 cal.set(Calendar.MILLISECOND, 0);
110 return cal.getTimeInMillis();
111 }
112
113 public long getTimestamp() {
114 return calculateTimestamp();
115 }
116
117 public double getLatitude() {
118 return extractor.getSignedValue(107, 134) / 600000.0;
119 }
120
121 public double getLongitude() {
122 return extractor.getSignedValue(79, 107) / 600000.0;
123 }
124
125 public int getDeviceType() {
126 return extractor.getValue(134, 138);
127 }
128
129 @Override
130 public String getSource() {
131 return source;
132 }
133
134 @Override
135 public String toString() {
136 StringBuilder builder = new StringBuilder();
137 builder.append("AisBaseStation [source=");
138 builder.append(source);
139 builder.append(", year=");
140 builder.append(getYear());
141 builder.append(", messageId=");
142 builder.append(messageId);
143 builder.append(", repeatIndicator=");
144 builder.append(getRepeatIndicator());
145 builder.append(", mmsi=");
146 builder.append(getMmsi());
147 builder.append(", month=");
148 builder.append(getMonth());
149 builder.append(", day=");
150 builder.append(getDay());
151 builder.append(", hour=");
152 builder.append(getHour());
153 builder.append(", minute=");
154 builder.append(getMinute());
155 builder.append(", second=");
156 builder.append(getSecond());
157 builder.append(", positionAccuracy=");
158 builder.append(getPositionAccuracy());
159 builder.append(", timestamp=");
160 builder.append(getTimestamp());
161 builder.append(", latitude=");
162 builder.append(getLatitude());
163 builder.append(", longitude=");
164 builder.append(getLongitude());
165 builder.append(", deviceType=");
166 builder.append(getDeviceType());
167 builder.append("]");
168 return builder.toString();
169 }
170
171 }