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

Annotation of /android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1417 - (hide annotations) (download)
Mon May 2 16:21:37 2011 UTC (13 years, 1 month ago) by torben
File size: 9034 byte(s)
Use a simpler getbyname
1 torben 350 package dk.thoerup.traininfoservice.banedk;
2    
3    
4 torben 992
5 torben 350 import java.net.URL;
6 torben 836 import java.sql.SQLException;
7 torben 428 import java.util.Map;
8 torben 836 import java.util.logging.Level;
9 torben 350 import java.util.logging.Logger;
10    
11 torben 992 import org.jsoup.nodes.Document;
12     import org.jsoup.nodes.Element;
13     import org.jsoup.select.Elements;
14 torben 350
15 torben 1411 import dk.thoerup.android.traininfo.common.StationBean;
16     import dk.thoerup.android.traininfo.common.StationEntry;
17 torben 1061 import dk.thoerup.android.traininfo.common.TimetableBean;
18     import dk.thoerup.android.traininfo.common.TimetableEntry;
19 torben 468 import dk.thoerup.circuitbreaker.CircuitBreaker;
20     import dk.thoerup.circuitbreaker.CircuitBreakerManager;
21 torben 1355 import dk.thoerup.genericjavautils.TimeoutMap;
22 torben 711 import dk.thoerup.traininfoservice.Statistics;
23 torben 1303 import dk.thoerup.traininfoservice.TraininfoSettings;
24 torben 1255 import dk.thoerup.traininfoservice.db.StationDAO;
25 torben 421
26 torben 350 public class TimetableFetcher {
27 torben 992
28 torben 350
29 torben 1060 Map<String, TimetableBean> cache;
30 torben 1411 Map<String, StationEntry> stationCache;
31 torben 836
32     StationDAO stationDao = new StationDAO();
33 torben 350
34 torben 387
35 torben 350 Logger logger = Logger.getLogger(TimetableFetcher.class.getName());
36 torben 1303
37     TraininfoSettings settings;
38 torben 387
39 torben 1303 public TimetableFetcher(TraininfoSettings settings) {
40     this.settings = settings;
41 torben 584
42 torben 1303 cache = new TimeoutMap<String,TimetableBean>( settings.getCacheTimeout() );
43 torben 1411 stationCache = new TimeoutMap<String,StationEntry>( 3*60*60*1000 );
44 torben 581 }
45    
46    
47 torben 1060 TimetableBean cachedLookupTimetable(String trainID, String type) throws Exception {
48 torben 387 String key = trainID+type;
49 torben 1060 TimetableBean list = cache.get(key);
50 torben 387
51     if (list == null) {
52     list = lookupTimetable(trainID,type);
53     cache.put(key, list);
54     } else {
55 torben 711 Statistics.getInstance().incrementTimetableCacheHits();
56 torben 389 logger.info("Timetable: Cache hit " + trainID);
57 torben 387 }
58     return list;
59     }
60 torben 581
61 torben 1060 TimetableBean lookupTimetable(String trainID, String type) throws Exception {
62 torben 1372 if (settings.getBackend() == TraininfoSettings.Backend.Azure ){
63 torben 1036 return lookupTimetableAzureSite(trainID, type);
64    
65 torben 581 } else {
66 torben 1331 return lookupTimetableMobileSite(trainID, type);
67 torben 581 }
68     }
69 torben 836
70 torben 1417 StationEntry getStation(String name) {
71 torben 1411 StationEntry station = stationCache.get(name);
72 torben 836
73 torben 1411 if (station == null) {
74 torben 836 try {
75 torben 1417 station = stationDao.getSimpleByName(name);
76     if (station != null) {
77 torben 1411 stationCache.put(name,station);
78     }
79 torben 836 } catch (SQLException e) {
80     logger.log(Level.SEVERE, "getStationId failed", e);
81     }
82     }
83 torben 350
84 torben 1411 return station;
85 torben 836 }
86 torben 1406
87     String correctStationName(String name) {
88     if (name.equals("København"))
89     name = "København H"; //correct inconsistency in naming
90    
91     return name;
92     }
93 torben 836
94 torben 1060 TimetableBean lookupTimetableAzureSite(String trainID, String type) throws Exception {
95     TimetableBean timetableBean = new TimetableBean();
96 torben 350
97 torben 1048
98 torben 970 String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID;
99 torben 1048 logger.fine("URL:" + url);
100 torben 350
101 torben 1303 JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() );
102 torben 421 CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
103 torben 350
104 torben 992 Document doc = (Document) breaker.invoke(wrapper);
105 torben 421
106 torben 350
107     boolean currentStation = false;
108     boolean currentStationSaved = false;
109    
110 torben 992 Elements tables = doc.getElementsByClass("Rute");
111    
112 torben 350 if (tables.size() == 1) {
113 torben 992 Element timetable = tables.get(0);
114     Elements rows = timetable.getElementsByTag("tr");
115 torben 350
116     for (int i=0; i<rows.size(); i++) {
117     if (i==0) //First row is column headers
118     continue;
119    
120    
121 torben 992 Element row = rows.get(i);
122     Elements fields = row.getElementsByTag("td");
123    
124 torben 350
125 torben 992 if (currentStationSaved == false && fields.get(0).attr("class").equalsIgnoreCase("Tidsstreg")) {
126 torben 350 currentStation = true;
127     continue;
128     }
129    
130 torben 1060 TimetableEntry entry = new TimetableEntry();
131 torben 836
132 torben 1406 String station = correctStationName( fields.get(0).text() );
133 torben 836
134 torben 1060 entry.setStation( station );
135     entry.setArrival( fields.get(1).text() );
136     entry.setDeparture( fields.get(2).text() );
137 torben 350
138 torben 992 boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst");
139 torben 1060 entry.setCancelled(cancelled);
140 torben 975
141 torben 350 if (currentStation == true && currentStationSaved == false ) {
142 torben 1060 entry.setCurrent(currentStation);
143 torben 350 currentStationSaved = true;
144     }
145    
146 torben 1417 entry.setStationEntry( getStation( station ));
147 torben 836
148 torben 1060 timetableBean.entries.add(entry);
149 torben 350 }
150    
151 torben 977 //TODO: There is an off-by-one error in this cancelled parser thingie
152 torben 975 final String cancelledString = "Aflyst";
153 torben 1060 for (int i=0;i<timetableBean.entries.size(); i++) { //handle cancelled labels
154     final int lastIdx = (timetableBean.entries.size() - 1);
155 torben 975
156 torben 1060 TimetableEntry current = timetableBean.entries.get(i);
157 torben 976 if (current.isCancelled()) {
158     if (i == 0) {
159     current.setDeparture(cancelledString);
160     } else if (i == lastIdx) {
161     current.setArrival(cancelledString);
162     } else if (i>0 && i<lastIdx) {
163 torben 1060 TimetableEntry next = timetableBean.entries.get(i+1);
164     TimetableEntry prev = timetableBean.entries.get(i-1);
165 torben 976
166     if (next.isCancelled())
167     current.setDeparture(cancelledString);
168     if (prev.isCancelled())
169     current.setArrival(cancelledString);
170     }
171 torben 975 }
172     }
173    
174 torben 350 } else {
175     logger.warning("No time table found, trainID=" + trainID + " type=" + type);
176     }
177 torben 992
178 torben 350
179 torben 1060 return timetableBean;
180 torben 350 }
181 torben 1036
182 torben 1331 TimetableBean lookupTimetableMobileSite(String trainID, String type) throws Exception {
183 torben 1367 TimetableBean timetableBean = new TimetableBean();
184    
185     String url = "http://mobil.bane.dk/mobilStation.asp?artikelID=5332&tognummer=" + trainID + "&webprofil=" + type + "&mode=rute";
186     logger.fine("URL:" + url);
187    
188    
189     JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() );
190     CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
191    
192     Document doc = (Document) breaker.invoke(wrapper);
193    
194     Element content = doc.getElementsByClass("contentDiv").get(1);
195     Element dlist = content.child(0);
196    
197    
198     Elements rows = dlist.getElementsByTag("dt");
199    
200     for (int i=0; i<rows.size(); i++) {
201    
202     Element row = rows.get(i);
203    
204 torben 1368 logger.fine( row.text() );
205 torben 1367
206     String parts[] = row.text().split(",");
207    
208     TimetableEntry entry = new TimetableEntry();
209    
210     String station = DepartureFetcher.cleanText( parts[0] ) ;
211 torben 1406 station = correctStationName(station);
212 torben 1367
213 torben 1406
214 torben 1367 String arrival = DepartureFetcher.cleanText( parts[1] );
215     String departure = DepartureFetcher.cleanText( "" );
216    
217     entry.setStation( station );
218     entry.setArrival( arrival );
219     entry.setDeparture( departure );
220    
221    
222 torben 1417 entry.setStationEntry( getStation( station ));
223 torben 1367
224     timetableBean.entries.add(entry);
225     }
226    
227    
228     return timetableBean;
229    
230 torben 1331 }
231    
232     @Deprecated
233 torben 1060 TimetableBean lookupTimetableWwwSite(String trainID, String type) throws Exception {
234     TimetableBean timetableBean = new TimetableBean();
235 torben 1036
236     String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276";
237 torben 1048 logger.fine("URL:" + url);
238 torben 1036
239    
240 torben 1303 JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() );
241 torben 1036 CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
242    
243     Document doc = (Document) breaker.invoke(wrapper);
244    
245    
246     boolean currentStation = false;
247     boolean currentStationSaved = false;
248    
249     Elements tables = doc.getElementsByClass("Rute");
250    
251     if (tables.size() == 1) {
252     Element timetable = tables.get(0);
253     Elements rows = timetable.getElementsByTag("tr");
254    
255     for (int i=0; i<rows.size(); i++) {
256     if (i==0) //First row is column headers
257     continue;
258    
259    
260     Element row = rows.get(i);
261     Elements fields = row.getElementsByTag("td");
262    
263    
264     if (currentStationSaved == false && fields.get(0).attr("class").equalsIgnoreCase("Tidsstreg")) {
265     currentStation = true;
266     continue;
267     }
268    
269 torben 1060 TimetableEntry entry = new TimetableEntry();
270 torben 1036
271 torben 1040 String station = DepartureFetcher.cleanText( fields.get(0).text() ) ;
272 torben 1406 station = correctStationName(station);
273    
274 torben 1036
275 torben 1040 String arrival = DepartureFetcher.cleanText( fields.get(1).text() );
276     String departure = DepartureFetcher.cleanText( fields.get(2).text() );
277    
278 torben 1060 entry.setStation( station );
279     entry.setArrival( arrival );
280     entry.setDeparture( departure );
281 torben 1036
282    
283     if (currentStation == true && currentStationSaved == false ) {
284 torben 1060 entry.setCurrent(currentStation);
285 torben 1036 currentStationSaved = true;
286     }
287    
288 torben 1417 entry.setStationEntry( getStation( station ));
289 torben 1036
290 torben 1060 timetableBean.entries.add(entry);
291 torben 1036 }
292    
293     } else {
294     logger.warning("No time table found, trainID=" + trainID + " type=" + type);
295     }
296    
297    
298 torben 1060 return timetableBean;
299 torben 1036 }
300 torben 350
301     }

  ViewVC Help
Powered by ViewVC 1.1.20