1 package au.gov.amsa.ais;
2
3 import com.google.common.annotations.VisibleForTesting;
4
5
6
7
8
9
10
11 public class Communications {
12
13 private final int startIndex;
14 private final int syncState;
15 private final int slotTimeout;
16 private final Integer receivedStations;
17 private final Integer slotNumber;
18 private final Integer hourUtc;
19 private final Integer minuteUtc;
20 private final Integer slotOffset;
21
22
23
24
25
26
27
28 public Communications(AisExtractor extractor, int startIndex) {
29 this.startIndex = startIndex;
30 syncState = extractor.getValue(startIndex, startIndex + 2);
31 slotTimeout = extractor.getValue(startIndex + 2, startIndex + 5);
32 receivedStations = getReceivedStations(extractor, slotTimeout,
33 startIndex);
34 slotNumber = getSlotNumber(extractor, slotTimeout, startIndex);
35 hourUtc = getHourUtc(extractor, slotTimeout, startIndex);
36 minuteUtc = getMinuteUtc(extractor, slotTimeout, startIndex);
37 slotOffset = getSlotOffset(extractor, slotTimeout, startIndex);
38 }
39
40
41
42
43
44
45
46
47
48 @VisibleForTesting
49 static Integer getReceivedStations(AisExtractor extractor, int slotTimeout,
50 int startIndex) {
51 if (slotTimeout == 3 || slotTimeout == 5 || slotTimeout == 7)
52 return extractor.getValue(startIndex + 5, startIndex + 19);
53 else
54 return null;
55 }
56
57
58
59
60
61
62
63
64
65 @VisibleForTesting
66 static Integer getSlotNumber(AisExtractor extractor, int slotTimeout,
67 int startIndex) {
68 if (slotTimeout == 2 || slotTimeout == 4 || slotTimeout == 6)
69 return extractor.getValue(startIndex + 5, startIndex + 19);
70 else
71 return null;
72 }
73
74
75
76
77
78
79
80
81
82 private static Integer getHourUtc(AisExtractor extractor, int slotTimeout,
83 int startIndex) {
84 if (slotTimeout == 1) {
85
86 int hours = extractor.getValue(startIndex + 5, startIndex + 10);
87 return hours;
88 } else
89 return null;
90 }
91
92
93
94
95
96
97
98
99
100 private static Integer getMinuteUtc(AisExtractor extractor,
101 int slotTimeout, int startIndex) {
102 if (slotTimeout == 1) {
103
104 int minutes = extractor.getValue(startIndex + 10, startIndex + 17);
105 return minutes;
106 } else
107 return null;
108 }
109
110
111
112
113
114
115
116
117
118 private static Integer getSlotOffset(AisExtractor extractor,
119 int slotTimeout, int startIndex) {
120 if (slotTimeout == 0)
121 return extractor.getValue(startIndex + 5, startIndex + 19);
122 else
123 return null;
124 }
125
126
127
128
129
130
131 public int getSyncState() {
132 return syncState;
133 }
134
135
136
137
138
139
140 public int getSlotTimeout() {
141 return slotTimeout;
142 }
143
144
145
146
147
148
149 public Integer getReceivedStations() {
150 return receivedStations;
151 }
152
153
154
155
156
157
158 public Integer getSlotNumber() {
159 return slotNumber;
160 }
161
162
163
164
165
166
167 public Integer getHourUtc() {
168 return hourUtc;
169 }
170
171
172
173
174
175
176 public Integer getMinuteUtc() {
177 return minuteUtc;
178 }
179
180
181
182
183
184
185 public Integer getMinutesUtc() {
186 return getMinutesUtc(slotTimeout, hourUtc, minuteUtc);
187 }
188
189
190
191
192
193
194
195
196
197
198 private static Integer getMinutesUtc(int slotTimeout, Integer hour,
199 Integer minute) {
200 if (slotTimeout == 1) {
201 return hour * 60 + minute;
202 } else
203 return null;
204 }
205
206
207
208
209
210
211 public Integer getSlotOffset() {
212 return slotOffset;
213 }
214
215 @Override
216 public String toString() {
217 StringBuilder builder = new StringBuilder();
218 builder.append("Communications [startIndex=");
219 builder.append(startIndex);
220 builder.append(", syncState=");
221 builder.append(syncState);
222 builder.append(", slotTimeout=");
223 builder.append(slotTimeout);
224 builder.append(", receivedStations=");
225 builder.append(receivedStations);
226 builder.append(", slotNumber=");
227 builder.append(slotNumber);
228 builder.append(", hourUtc=");
229 builder.append(hourUtc);
230 builder.append(", minuteUtc=");
231 builder.append(minuteUtc);
232 builder.append(", slotOffset=");
233 builder.append(slotOffset);
234 builder.append("]");
235 return builder.toString();
236 }
237
238 }