/[projects]/android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/MetroFetcher.java
ViewVC logotype

Contents of /android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/MetroFetcher.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1042 - (show annotations) (download)
Mon Sep 13 17:57:31 2010 UTC (13 years, 8 months ago) by torben
File size: 3022 byte(s)
Add backend for fetching metro data
1 package dk.thoerup.traininfoservice.banedk;
2
3
4 import java.net.URL;
5
6 import java.util.ArrayList;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.logging.Logger;
10
11
12 import org.jsoup.nodes.Document;
13 import org.jsoup.nodes.Element;
14 import org.jsoup.select.Elements;
15
16 import dk.thoerup.circuitbreaker.CircuitBreaker;
17 import dk.thoerup.circuitbreaker.CircuitBreakerManager;
18 import dk.thoerup.traininfoservice.StationBean;
19 import dk.thoerup.traininfoservice.StationDAO;
20 import dk.thoerup.traininfoservice.Statistics;
21
22
23
24 public class MetroFetcher {
25 public static class MetroEntry {
26 String metro;
27 String destination;
28 String minutes;
29 }
30
31 public static class MetroBean {
32 String head;
33 String operationInfo; //driftsinfo
34 String plan; // koereplan
35
36 List<MetroEntry> entries = new ArrayList<MetroEntry>();
37 }
38
39 Logger logger = Logger.getLogger(MetroFetcher.class.getName());
40 StationDAO stationDAO = new StationDAO();
41
42 Map<String, MetroBean> cache = new TimeoutMap<String,MetroBean>(1000);
43
44 public MetroBean cachedLookupMetroDepartures(int stationID) throws Exception {
45 final String key = "metro:" + stationID;
46
47 MetroBean metroBean = cache.get(key);
48
49 if (metroBean == null) {
50 metroBean = lookupMetroDepartures(stationID);
51 cache.put(key, metroBean);
52 } else {
53 Statistics.getInstance().incrementDepartureCacheHits();
54 logger.info("Metro: Cache hit " + key); //remove before production
55 }
56 return metroBean;
57 }
58
59 public MetroBean lookupMetroDepartures(int stationID) throws Exception {
60
61 StationBean station = stationDAO.getById(stationID) ;
62
63
64 URL url = new URL("http://www.m.dk/layouts/Metro/Widgets/MetroWidget.ashx?StationId=" + station.getMetro() + "&Congestion=true&Application=MyMetro&Expiration=true");
65
66 JsoupInvocation wrapper = new JsoupInvocation( url, 3000);
67 CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("metro");
68
69 Document doc = (Document) breaker.invoke(wrapper);
70
71 MetroBean bean = new MetroBean();
72
73 bean.head = doc.getElementsByClass("metro-widget-headline").get(0).text();
74
75 Element content = doc.getElementsByClass("metro-widget-content").get(0);
76 Elements contentElems = content.children();
77
78 Element departureTable = content.getElementsByTag("table").get(0);
79
80 Elements rows = departureTable.getElementsByTag("tr");
81
82 for (int i=1; i<rows.size(); i++) {
83 Element row = rows.get(i);
84 Elements fields = row.children();
85
86 String metro = fields.get(0).text();
87 String destination = fields.get(1).text();
88 Elements minuteFields = fields.get(2).children();
89 String minutes ="";
90
91 for (Element elm : minuteFields) {
92 minutes = minutes + elm.text() + " ";
93 }
94
95 MetroEntry entry = new MetroEntry();
96 entry.metro = metro;
97 entry.destination = destination;
98 entry.minutes = minutes;
99
100 bean.entries.add(entry);
101 }
102
103 bean.operationInfo = contentElems.get(3).text();
104 bean.plan = contentElems.get(5).text();
105
106 return bean;
107 }
108
109 }

  ViewVC Help
Powered by ViewVC 1.1.20