--- android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/MetroFetcher.java 2010/09/13 17:57:31 1042 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/MetroFetcher.java 2011/05/02 11:59:40 1409 @@ -2,44 +2,29 @@ import java.net.URL; - -import java.util.ArrayList; -import java.util.List; import java.util.Map; import java.util.logging.Logger; - import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; +import dk.thoerup.android.traininfo.common.MetroBean; +import dk.thoerup.android.traininfo.common.MetroBean.MetroEntry; +import dk.thoerup.android.traininfo.common.StationEntry; import dk.thoerup.circuitbreaker.CircuitBreaker; import dk.thoerup.circuitbreaker.CircuitBreakerManager; -import dk.thoerup.traininfoservice.StationBean; -import dk.thoerup.traininfoservice.StationDAO; -import dk.thoerup.traininfoservice.Statistics; +import dk.thoerup.genericjavautils.TimeoutMap; +import dk.thoerup.traininfoservice.db.StationDAO; public class MetroFetcher { - public static class MetroEntry { - String metro; - String destination; - String minutes; - } - - public static class MetroBean { - String head; - String operationInfo; //driftsinfo - String plan; // koereplan - - List entries = new ArrayList(); - } Logger logger = Logger.getLogger(MetroFetcher.class.getName()); StationDAO stationDAO = new StationDAO(); - Map cache = new TimeoutMap(1000); + Map cache = new TimeoutMap(60000); //TODO: make metro cache timeout configurable public MetroBean cachedLookupMetroDepartures(int stationID) throws Exception { final String key = "metro:" + stationID; @@ -50,7 +35,7 @@ metroBean = lookupMetroDepartures(stationID); cache.put(key, metroBean); } else { - Statistics.getInstance().incrementDepartureCacheHits(); + //Statistics.getInstance().incrementDepartureCacheHits(); logger.info("Metro: Cache hit " + key); //remove before production } return metroBean; @@ -58,10 +43,11 @@ public MetroBean lookupMetroDepartures(int stationID) throws Exception { - StationBean station = stationDAO.getById(stationID) ; + StationEntry station = stationDAO.getById(stationID) ; URL url = new URL("http://www.m.dk/layouts/Metro/Widgets/MetroWidget.ashx?StationId=" + station.getMetro() + "&Congestion=true&Application=MyMetro&Expiration=true"); + logger.fine("URL:" + url); JsoupInvocation wrapper = new JsoupInvocation( url, 3000); CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("metro"); @@ -75,34 +61,58 @@ Element content = doc.getElementsByClass("metro-widget-content").get(0); Elements contentElems = content.children(); - Element departureTable = content.getElementsByTag("table").get(0); - - Elements rows = departureTable.getElementsByTag("tr"); - - for (int i=1; i 0 ) { - String metro = fields.get(0).text(); - String destination = fields.get(1).text(); - Elements minuteFields = fields.get(2).children(); - String minutes =""; - - for (Element elm : minuteFields) { - minutes = minutes + elm.text() + " "; + Element departureTable = content.getElementsByTag("table").get(0); + + Elements rows = departureTable.getElementsByTag("tr"); + + for (int i=1; i