13 |
|
|
14 |
import dk.thoerup.android.traininfo.common.DepartureBean; |
import dk.thoerup.android.traininfo.common.DepartureBean; |
15 |
import dk.thoerup.android.traininfo.common.DepartureEntry; |
import dk.thoerup.android.traininfo.common.DepartureEntry; |
16 |
import dk.thoerup.android.traininfo.common.StationBean.StationEntry; |
import dk.thoerup.android.traininfo.common.StationEntry; |
17 |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
18 |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
19 |
import dk.thoerup.traininfoservice.StationDAO; |
import dk.thoerup.genericjavautils.HttpUtil; |
20 |
|
import dk.thoerup.genericjavautils.TimeoutMap; |
21 |
import dk.thoerup.traininfoservice.Statistics; |
import dk.thoerup.traininfoservice.Statistics; |
22 |
|
import dk.thoerup.traininfoservice.TraininfoSettings; |
23 |
|
import dk.thoerup.traininfoservice.db.StationDAO; |
24 |
|
|
25 |
public class DepartureFetcher { |
public class DepartureFetcher { |
26 |
|
|
29 |
REGIONAL |
REGIONAL |
30 |
} |
} |
31 |
|
|
32 |
|
enum FetchTrainType { |
33 |
|
STOG, |
34 |
|
REGIONAL, |
35 |
|
BOTH |
36 |
|
} |
37 |
|
|
38 |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
39 |
|
|
40 |
Map<String, DepartureBean> cache; |
Map<String, DepartureBean> cache; |
41 |
|
|
42 |
StationDAO stationDao = new StationDAO(); |
StationDAO stationDao = new StationDAO(); |
43 |
|
|
44 |
private boolean useAzureSite; |
|
45 |
private int replyTimeout; |
private TraininfoSettings settings; |
46 |
|
|
47 |
public DepartureFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) { |
public DepartureFetcher(TraininfoSettings settings) { |
48 |
this.replyTimeout = replyTimeout; |
this.settings = settings; |
49 |
useAzureSite = azureSite; |
cache = new TimeoutMap<String,DepartureBean>( settings.getCacheTimeout() ); |
|
cache = new TimeoutMap<String,DepartureBean>(cacheTimeout); |
|
50 |
} |
} |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
public DepartureBean cachedLookupDepartures(int stationID, boolean arrival) throws Exception { |
public DepartureBean cachedLookupDepartures(int stationID, boolean arrival, FetchTrainType type) throws Exception { |
56 |
final String key = "" + stationID + ":" + arrival; |
|
57 |
|
final String key = "" + stationID + ":" + arrival + ":" + type.toString(); |
58 |
|
|
59 |
DepartureBean departureBean = cache.get(key); |
DepartureBean departureBean = cache.get(key); |
60 |
|
|
61 |
|
|
62 |
if (departureBean == null) { |
if (departureBean == null) { |
63 |
departureBean = lookupDepartures(stationID,arrival); |
departureBean = lookupDepartures(stationID, arrival, type); |
64 |
cache.put(key, departureBean); |
cache.put(key, departureBean); |
65 |
} else { |
} else { |
66 |
Statistics.getInstance().incrementDepartureCacheHits(); |
Statistics.getInstance().incrementDepartureCacheHits(); |
70 |
} |
} |
71 |
|
|
72 |
|
|
73 |
public DepartureBean lookupDepartures(int stationID, boolean arrival) throws Exception { |
public DepartureBean lookupDepartures(int stationID, boolean arrival, FetchTrainType type) throws Exception { |
74 |
|
|
75 |
DepartureBean departureBean = new DepartureBean(); |
DepartureBean departureBean = new DepartureBean(); |
76 |
|
|
77 |
StationEntry station = stationDao.getById(stationID); |
StationEntry station = stationDao.getById(stationID); |
78 |
|
|
79 |
departureBean.stationName = station.getName(); |
departureBean.stationName = station.getName(); |
80 |
|
|
81 |
if (station.getRegional() != null) { |
if (station.getRegional() != null && (type == FetchTrainType.REGIONAL||type == FetchTrainType.BOTH) ) { |
82 |
DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival); |
DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival); |
83 |
departureBean.entries.addAll( tempBean.entries ); |
departureBean.entries.addAll( tempBean.entries ); |
84 |
departureBean.notifications.addAll(tempBean.notifications); |
departureBean.notifications.addAll(tempBean.notifications); |
85 |
} |
} |
86 |
|
|
87 |
if (station.getStrain() != null) { |
if (station.getStrain() != null && (type == FetchTrainType.STOG||type == FetchTrainType.BOTH)) { |
88 |
DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival); |
DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival); |
89 |
departureBean.entries.addAll( tempBean.entries ); |
departureBean.entries.addAll( tempBean.entries ); |
90 |
departureBean.notifications.addAll(tempBean.notifications); |
departureBean.notifications.addAll(tempBean.notifications); |
94 |
logger.info("No departures found for station " + stationID); |
logger.info("No departures found for station " + stationID); |
95 |
} |
} |
96 |
|
|
97 |
Collections.sort( departureBean.entries ); |
if (type == FetchTrainType.BOTH) { //if we have both S-tog and regional order by departure/arrival time |
98 |
|
Collections.sort( departureBean.entries ); |
99 |
|
} |
100 |
|
|
101 |
|
|
102 |
return departureBean; |
return departureBean; |
103 |
} |
} |
104 |
|
|
105 |
public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception { |
public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception { |
106 |
if (useAzureSite == true) { |
if ( settings.getBackend() == TraininfoSettings.Backend.Azure) { |
107 |
return lookupDeparturesAzureSite(stationcode, type, arrival); |
return lookupDeparturesAzureSite(stationcode, type, arrival); |
108 |
} else { |
} else { |
109 |
return lookupDeparturesWwwSite(stationcode, type, arrival); |
return lookupDeparturesMobileSite(stationcode, type, arrival); |
110 |
} |
} |
111 |
} |
} |
112 |
|
|
145 |
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
146 |
|
|
147 |
logger.fine("URI: " + uri); |
logger.fine("URI: " + uri); |
148 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() ); |
149 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
150 |
|
|
151 |
Document page = (Document) breaker.invoke(wrapper); |
Document page = (Document) breaker.invoke(wrapper); |
231 |
return departureBean; |
return departureBean; |
232 |
} |
} |
233 |
|
|
234 |
|
public DepartureBean lookupDeparturesMobileSite(String stationcode, TrainType traintype, boolean arrival) throws Exception { |
235 |
|
|
236 |
|
DepartureBean departureBean = new DepartureBean(); |
237 |
|
|
238 |
|
|
239 |
|
String typeString = getTypeStringWww(traintype); |
240 |
|
String arrivalDeparture = (arrival==false) ? "afgang" : "ankomst"; |
241 |
|
|
242 |
|
stationcode = URLEncoder.encode(stationcode,"ISO-8859-1"); |
243 |
|
|
244 |
|
//String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
245 |
|
String uri = "http://mobil.bane.dk/mobilStation.asp?artikelID=5332&stat_kode=" + stationcode + "&webprofil=" + typeString +"&beskrivelse=&mode=ankomstafgang&ankomstafgang=" + arrivalDeparture + "&gemstation=&fuldvisning=1"; |
246 |
|
logger.fine("URI: " + uri); |
247 |
|
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() ); |
248 |
|
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
249 |
|
|
250 |
|
Document page = (Document) breaker.invoke(wrapper); |
251 |
|
|
252 |
|
|
253 |
|
Element content = page.getElementsByClass("contentDiv").get(0); |
254 |
|
|
255 |
|
|
256 |
|
if (content != null) { |
257 |
|
Elements tableRows = content.child(0).children(); |
258 |
|
|
259 |
|
|
260 |
|
|
261 |
|
for (Element currentRow : tableRows) { |
262 |
|
if (currentRow.tagName().equals("br") ) { |
263 |
|
break; |
264 |
|
} |
265 |
|
|
266 |
|
|
267 |
|
String link = currentRow.child(0).attr("href"); |
268 |
|
|
269 |
|
logger.fine( currentRow.text() ); |
270 |
|
logger.fine("Href: " + link); |
271 |
|
|
272 |
|
|
273 |
|
String parts[] = currentRow.text().split(","); |
274 |
|
|
275 |
|
|
276 |
|
DepartureEntry departure = new DepartureEntry(); |
277 |
|
|
278 |
|
//if we do these things upfront, then we are allowed to use continue statement when row contains no more data |
279 |
|
departure.setType(typeString); |
280 |
|
departureBean.entries.add( departure ); |
281 |
|
|
282 |
|
/* |
283 |
|
http://mobil.bane.dk/mobilStation.asp?artikelID=5332&tognummer=111&webprofil=FJRN&mode=rute&strBemaerkning=Afg%E5r+fra+%C5rhus+H+kl%2E07%3A21++&strRefURL=%2FmobilStation%2Easp%3FartikelID%3D5332%26stat%5Fkode%3DAR%26webprofil%3DFJRN%26beskrivelse%3D%25C5rhus%2BH%26mode%3Dankomstafgang%26ankomstafgang%3Dafgang%26gemstation%3D |
284 |
|
*/ |
285 |
|
int offset = 0; |
286 |
|
|
287 |
|
String time = parts[offset++]; |
288 |
|
if (time.equals("")) |
289 |
|
time = "0:00"; //Bane.dk bug work-around |
290 |
|
departure.setTime(time); |
291 |
|
|
292 |
|
int updated = 4; //does not exist on mobile |
293 |
|
departure.setUpdated(updated); |
294 |
|
|
295 |
|
String trainNumber = extractTrainNumberMobile(link); |
296 |
|
/*if (traintype == TrainType.STOG) //If it is S-train we need to extract the trainNumber |
297 |
|
trainNumber = trainNumber + " " + extractTrainNumberAzure(fields.get(2));*/ |
298 |
|
departure.setTrainNumber(trainNumber); |
299 |
|
|
300 |
|
if (traintype == TrainType.STOG) { //if it is stog the next vield is the "Line" code - this should be used somewhere, but skippint ahead for now |
301 |
|
String stogLine = parts[offset++].trim(); |
302 |
|
departure.setTrainNumber(stogLine + " " + trainNumber); |
303 |
|
} |
304 |
|
|
305 |
|
String destination = parts[offset++].trim();; |
306 |
|
departure.setDestination(destination); |
307 |
|
|
308 |
|
String origin = "-"; // fields.get(4).text(); does not exist on mobile |
309 |
|
departure.setOrigin(origin); |
310 |
|
|
311 |
|
String location = ""; // fields.get(5).text(); does not exist on mobile |
312 |
|
departure.setLocation(location); |
313 |
|
|
314 |
|
if (offset == parts.length) { |
315 |
|
continue; |
316 |
|
} |
317 |
|
|
318 |
|
if (parts[offset].trim().equalsIgnoreCase("NB!")) { |
319 |
|
offset++; |
320 |
|
} |
321 |
|
|
322 |
|
if (offset == parts.length) { |
323 |
|
continue; |
324 |
|
} |
325 |
|
|
326 |
|
String status = parts[offset++].trim();; //fields.get(6).text().trim(); - extract from url |
327 |
|
departure.setStatus(status); |
328 |
|
|
329 |
|
String note = ""; //extractNote( fields.get(7) ); - extract from url |
330 |
|
departure.setNote(note); |
331 |
|
|
332 |
|
} |
333 |
|
} else { |
334 |
|
logger.warning("No departures found for station=" + stationcode + ", type=" + traintype); |
335 |
|
} |
336 |
|
|
337 |
|
return departureBean; |
338 |
|
} |
339 |
|
|
340 |
|
|
341 |
|
|
342 |
public static String cleanText(String input) { |
public static String cleanText(String input) { |
344 |
return input.replace((char) 0xA0, (char)0x20).trim(); |
return input.replace((char) 0xA0, (char)0x20).trim(); |
345 |
} |
} |
346 |
|
|
347 |
|
|
348 |
|
// old www site is not available any more |
349 |
|
@Deprecated |
350 |
public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception { |
public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception { |
351 |
|
|
352 |
DepartureBean departureBean = new DepartureBean(); |
DepartureBean departureBean = new DepartureBean(); |
360 |
logger.fine("URI:" + uri); |
logger.fine("URI:" + uri); |
361 |
|
|
362 |
|
|
363 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() ); |
364 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
365 |
|
|
366 |
Element page = (Element) breaker.invoke(wrapper); |
Element page = (Element) breaker.invoke(wrapper); |
478 |
return number; |
return number; |
479 |
} |
} |
480 |
|
|
481 |
|
private String extractTrainNumberMobile(String link) { |
482 |
|
Map<String,String> elements = HttpUtil.decodeParams(link); |
483 |
|
|
484 |
|
return elements.get("tognummer"); |
485 |
|
} |
486 |
|
|
487 |
private String extractTrainNumberWww(Element trainTd) { |
private String extractTrainNumberWww(Element trainTd) { |
488 |
String number = ""; |
String number = ""; |
489 |
Element anchorElement = trainTd.getElementsByTag("a").get(0); |
Element anchorElement = trainTd.getElementsByTag("a").get(0); |
490 |
String href = anchorElement.attr("href"); |
String href = anchorElement.attr("href"); |
491 |
String argstring = href.substring( href.indexOf('?') + 1); |
|
492 |
|
String argstring = href.split("?")[1]; |
493 |
|
Map<String,String> elements = HttpUtil.decodeParams(argstring); |
494 |
|
number = elements.get("TogNr"); |
495 |
|
|
496 |
|
|
497 |
|
/*String argstring = href.substring( href.indexOf('?') + 1); |
498 |
String args[] = argstring.split("&"); |
String args[] = argstring.split("&"); |
499 |
for (String arg : args) { |
for (String arg : args) { |
500 |
String pair[] = arg.split("="); // Key=pair[0], Value=pair[1] |
String pair[] = arg.split("="); // Key=pair[0], Value=pair[1] |
501 |
|
|
502 |
if (pair[0].equalsIgnoreCase("TogNr")) |
if (pair[0].equalsIgnoreCase("TogNr")) |
503 |
number = pair[1]; |
number = pair[1]; |
504 |
} |
}*/ |
505 |
|
|
506 |
|
|
507 |
return number; |
return number; |
508 |
} |
} |