--- android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java 2010/09/16 13:32:10 1060 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java 2011/05/02 09:34:53 1406 @@ -12,10 +12,14 @@ import org.jsoup.nodes.Element; import org.jsoup.select.Elements; +import dk.thoerup.android.traininfo.common.TimetableBean; +import dk.thoerup.android.traininfo.common.TimetableEntry; import dk.thoerup.circuitbreaker.CircuitBreaker; import dk.thoerup.circuitbreaker.CircuitBreakerManager; -import dk.thoerup.traininfoservice.StationDAO; +import dk.thoerup.genericjavautils.TimeoutMap; import dk.thoerup.traininfoservice.Statistics; +import dk.thoerup.traininfoservice.TraininfoSettings; +import dk.thoerup.traininfoservice.db.StationDAO; public class TimetableFetcher { @@ -27,15 +31,13 @@ Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); + + TraininfoSettings settings; - private boolean useAzureSite; - private int replyTimeout; - - public TimetableFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) { - useAzureSite = azureSite; - this.replyTimeout = replyTimeout; + public TimetableFetcher(TraininfoSettings settings) { + this.settings = settings; - cache = new TimeoutMap(cacheTimeout); + cache = new TimeoutMap( settings.getCacheTimeout() ); stationCache = new TimeoutMap( 3*60*60*1000 ); } @@ -43,6 +45,7 @@ TimetableBean cachedLookupTimetable(String trainID, String type) throws Exception { String key = trainID+type; TimetableBean list = cache.get(key); + list = null; //TODO: DEBUG if (list == null) { list = lookupTimetable(trainID,type); @@ -55,11 +58,11 @@ } TimetableBean lookupTimetable(String trainID, String type) throws Exception { - if (useAzureSite == true ){ + if (settings.getBackend() == TraininfoSettings.Backend.Azure ){ return lookupTimetableAzureSite(trainID, type); } else { - return lookupTimetableWwwSite(trainID, type); + return lookupTimetableMobileSite(trainID, type); } } @@ -78,6 +81,13 @@ return id; } + + String correctStationName(String name) { + if (name.equals("København")) + name = "København H"; //correct inconsistency in naming + + return name; + } TimetableBean lookupTimetableAzureSite(String trainID, String type) throws Exception { TimetableBean timetableBean = new TimetableBean(); @@ -86,7 +96,7 @@ String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID; logger.fine("URL:" + url); - JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout); + JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() ); CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); Document doc = (Document) breaker.invoke(wrapper); @@ -117,9 +127,7 @@ TimetableEntry entry = new TimetableEntry(); - String station = fields.get(0).text() ; - if (station.equals("København")) - station = "København H"; //correct inconsistency in naming + String station = correctStationName( fields.get(0).text() ); entry.setStation( station ); entry.setArrival( fields.get(1).text() ); @@ -169,6 +177,57 @@ return timetableBean; } + TimetableBean lookupTimetableMobileSite(String trainID, String type) throws Exception { + TimetableBean timetableBean = new TimetableBean(); + + String url = "http://mobil.bane.dk/mobilStation.asp?artikelID=5332&tognummer=" + trainID + "&webprofil=" + type + "&mode=rute"; + logger.fine("URL:" + url); + + + JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() ); + CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); + + Document doc = (Document) breaker.invoke(wrapper); + + Element content = doc.getElementsByClass("contentDiv").get(1); + Element dlist = content.child(0); + + + Elements rows = dlist.getElementsByTag("dt"); + + for (int i=0; i