View Javadoc
1   package au.gov.amsa.ais;
2   
3   import com.google.common.annotations.VisibleForTesting;
4   
5   /**
6    * Provides the fields inside the Communications portion of an AIS Message.
7    * 
8    * @author dxm
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  	 * Constructor.
24  	 * 
25  	 * @param extractor
26  	 * @param startIndex
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  	 * Returns received stations as per 1371-4.pdf.
42  	 * 
43  	 * @param extractor
44  	 * @param slotTimeout
45  	 * @param startIndex
46  	 * @return
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  	 * Returns slot number as per 1371-4.pdf.
59  	 * 
60  	 * @param extractor
61  	 * @param slotTimeout
62  	 * @param startIndex
63  	 * @return
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  	 * Returns hour UTC as per 1371-4.pdf.
76  	 * 
77  	 * @param extractor
78  	 * @param slotTimeout
79  	 * @param startIndex
80  	 * @return
81  	 */
82  	private static Integer getHourUtc(AisExtractor extractor, int slotTimeout,
83  			int startIndex) {
84  		if (slotTimeout == 1) {
85  			// skip the msb bit
86  			int hours = extractor.getValue(startIndex + 5, startIndex + 10);
87  			return hours;
88  		} else
89  			return null;
90  	}
91  
92  	/**
93  	 * Returns minute UTC as per 1371-4.pdf.
94  	 * 
95  	 * @param extractor
96  	 * @param slotTimeout
97  	 * @param startIndex
98  	 * @return
99  	 */
100 	private static Integer getMinuteUtc(AisExtractor extractor,
101 			int slotTimeout, int startIndex) {
102 		if (slotTimeout == 1) {
103 			// skip the msb bit
104 			int minutes = extractor.getValue(startIndex + 10, startIndex + 17);
105 			return minutes;
106 		} else
107 			return null;
108 	}
109 
110 	/**
111 	 * Returns slot offset as per 1371-4.pdf.
112 	 * 
113 	 * @param extractor
114 	 * @param slotTimeout
115 	 * @param startIndex
116 	 * @return
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 	 * Returns sync state as per 1371-4.pdf.
128 	 * 
129 	 * @return
130 	 */
131 	public int getSyncState() {
132 		return syncState;
133 	}
134 
135 	/**
136 	 * Returns slot timeout as per 1371-4.pdf.
137 	 * 
138 	 * @return
139 	 */
140 	public int getSlotTimeout() {
141 		return slotTimeout;
142 	}
143 
144 	/**
145 	 * Returns received stations as per 1371-4.pdf.
146 	 * 
147 	 * @return
148 	 */
149 	public Integer getReceivedStations() {
150 		return receivedStations;
151 	}
152 
153 	/**
154 	 * Returns slot number as per 1371-4.pdf.
155 	 * 
156 	 * @return
157 	 */
158 	public Integer getSlotNumber() {
159 		return slotNumber;
160 	}
161 
162 	/**
163 	 * Returns hour UTC as per 1371-4.pdf.
164 	 * 
165 	 * @return
166 	 */
167 	public Integer getHourUtc() {
168 		return hourUtc;
169 	}
170 
171 	/**
172 	 * Returns minute UTC as per 1371-4.pdf.
173 	 * 
174 	 * @return
175 	 */
176 	public Integer getMinuteUtc() {
177 		return minuteUtc;
178 	}
179 
180 	/**
181 	 * Returns total minutes (60* hour + minute) in UTC.
182 	 * 
183 	 * @return
184 	 */
185 	public Integer getMinutesUtc() {
186 		return getMinutesUtc(slotTimeout, hourUtc, minuteUtc);
187 	}
188 
189 	/**
190 	 * Returns minutes UTC (60* hour + minute) in UTC if slot timeout is 1, null
191 	 * otherwise.
192 	 * 
193 	 * @param slotTimeout
194 	 * @param hour
195 	 * @param minute
196 	 * @return
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 	 * Returns slot offset as per 1371-4.pdf.
208 	 * 
209 	 * @return
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 }