/[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 1411 - (hide annotations) (download)
Mon May 2 12:19:12 2011 UTC (13 years, 1 month ago) by torben
File size: 9081 byte(s)
Adapt fetcher to get entire stationentry instead of just the stationID
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 1411 StationEntry getStationId(String name) {
71     StationEntry station = stationCache.get(name);
72 torben 836
73 torben 1411 if (station == null) {
74 torben 836 try {
75 torben 1411 StationBean bean = stationDao.getByName(name);
76     if (bean.entries.size() == 1) {
77     station = bean.entries.get(0);
78     stationCache.put(name,station);
79     }
80 torben 836 } catch (SQLException e) {
81     logger.log(Level.SEVERE, "getStationId failed", e);
82     }
83     }
84 torben 350
85 torben 1411 return station;
86 torben 836 }
87 torben 1406
88     String correctStationName(String name) {
89     if (name.equals("København"))
90     name = "København H"; //correct inconsistency in naming
91    
92     return name;
93     }
94 torben 836
95 torben 1060 TimetableBean lookupTimetableAzureSite(String trainID, String type) throws Exception {
96     TimetableBean timetableBean = new TimetableBean();
97 torben 350
98 torben 1048
99 torben 970 String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID;
100 torben 1048 logger.fine("URL:" + url);
101 torben 350
102 torben 1303 JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() );
103 torben 421 CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
104 torben 350
105 torben 992 Document doc = (Document) breaker.invoke(wrapper);
106 torben 421
107 torben 350
108     boolean currentStation = false;
109     boolean currentStationSaved = false;
110    
111 torben 992 Elements tables = doc.getElementsByClass("Rute");
112    
113 torben 350 if (tables.size() == 1) {
114 torben 992 Element timetable = tables.get(0);
115     Elements rows = timetable.getElementsByTag("tr");
116 torben 350
117     for (int i=0; i<rows.size(); i++) {
118     if (i==0) //First row is column headers
119     continue;
120    
121    
122 torben 992 Element row = rows.get(i);
123     Elements fields = row.getElementsByTag("td");
124    
125 torben 350
126 torben 992 if (currentStationSaved == false && fields.get(0).attr("class").equalsIgnoreCase("Tidsstreg")) {
127 torben 350 currentStation = true;
128     continue;
129     }
130    
131 torben 1060 TimetableEntry entry = new TimetableEntry();
132 torben 836
133 torben 1406 String station = correctStationName( fields.get(0).text() );
134 torben 836
135 torben 1060 entry.setStation( station );
136     entry.setArrival( fields.get(1).text() );
137     entry.setDeparture( fields.get(2).text() );
138 torben 350
139 torben 992 boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst");
140 torben 1060 entry.setCancelled(cancelled);
141 torben 975
142 torben 350 if (currentStation == true && currentStationSaved == false ) {
143 torben 1060 entry.setCurrent(currentStation);
144 torben 350 currentStationSaved = true;
145     }
146    
147 torben 1060 entry.setStationId( getStationId( station ));
148 torben 836
149 torben 1060 timetableBean.entries.add(entry);
150 torben 350 }
151    
152 torben 977 //TODO: There is an off-by-one error in this cancelled parser thingie
153 torben 975 final String cancelledString = "Aflyst";
154 torben 1060 for (int i=0;i<timetableBean.entries.size(); i++) { //handle cancelled labels
155     final int lastIdx = (timetableBean.entries.size() - 1);
156 torben 975
157 torben 1060 TimetableEntry current = timetableBean.entries.get(i);
158 torben 976 if (current.isCancelled()) {
159     if (i == 0) {
160     current.setDeparture(cancelledString);
161     } else if (i == lastIdx) {
162     current.setArrival(cancelledString);
163     } else if (i>0 && i<lastIdx) {
164 torben 1060 TimetableEntry next = timetableBean.entries.get(i+1);
165     TimetableEntry prev = timetableBean.entries.get(i-1);
166 torben 976
167     if (next.isCancelled())
168     current.setDeparture(cancelledString);
169     if (prev.isCancelled())
170     current.setArrival(cancelledString);
171     }
172 torben 975 }
173     }
174    
175 torben 350 } else {
176     logger.warning("No time table found, trainID=" + trainID + " type=" + type);
177     }
178 torben 992
179 torben 350
180 torben 1060 return timetableBean;
181 torben 350 }
182 torben 1036
183 torben 1331 TimetableBean lookupTimetableMobileSite(String trainID, String type) throws Exception {
184 torben 1367 TimetableBean timetableBean = new TimetableBean();
185    
186     String url = "http://mobil.bane.dk/mobilStation.asp?artikelID=5332&tognummer=" + trainID + "&webprofil=" + type + "&mode=rute";
187     logger.fine("URL:" + url);
188    
189    
190     JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() );
191     CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
192    
193     Document doc = (Document) breaker.invoke(wrapper);
194    
195     Element content = doc.getElementsByClass("contentDiv").get(1);
196     Element dlist = content.child(0);
197    
198    
199     Elements rows = dlist.getElementsByTag("dt");
200    
201     for (int i=0; i<rows.size(); i++) {
202    
203     Element row = rows.get(i);
204    
205 torben 1368 logger.fine( row.text() );
206 torben 1367
207     String parts[] = row.text().split(",");
208    
209     TimetableEntry entry = new TimetableEntry();
210    
211     String station = DepartureFetcher.cleanText( parts[0] ) ;
212 torben 1406 station = correctStationName(station);
213 torben 1367
214 torben 1406
215 torben 1367 String arrival = DepartureFetcher.cleanText( parts[1] );
216     String departure = DepartureFetcher.cleanText( "" );
217    
218     entry.setStation( station );
219     entry.setArrival( arrival );
220     entry.setDeparture( departure );
221    
222    
223     entry.setStationId( getStationId( station ));
224    
225     timetableBean.entries.add(entry);
226     }
227    
228    
229     return timetableBean;
230    
231 torben 1331 }
232    
233     @Deprecated
234 torben 1060 TimetableBean lookupTimetableWwwSite(String trainID, String type) throws Exception {
235     TimetableBean timetableBean = new TimetableBean();
236 torben 1036
237     String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276";
238 torben 1048 logger.fine("URL:" + url);
239 torben 1036
240    
241 torben 1303 JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() );
242 torben 1036 CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
243    
244     Document doc = (Document) breaker.invoke(wrapper);
245    
246    
247     boolean currentStation = false;
248     boolean currentStationSaved = false;
249    
250     Elements tables = doc.getElementsByClass("Rute");
251    
252     if (tables.size() == 1) {
253     Element timetable = tables.get(0);
254     Elements rows = timetable.getElementsByTag("tr");
255    
256     for (int i=0; i<rows.size(); i++) {
257     if (i==0) //First row is column headers
258     continue;
259    
260    
261     Element row = rows.get(i);
262     Elements fields = row.getElementsByTag("td");
263    
264    
265     if (currentStationSaved == false && fields.get(0).attr("class").equalsIgnoreCase("Tidsstreg")) {
266     currentStation = true;
267     continue;
268     }
269    
270 torben 1060 TimetableEntry entry = new TimetableEntry();
271 torben 1036
272 torben 1040 String station = DepartureFetcher.cleanText( fields.get(0).text() ) ;
273 torben 1406 station = correctStationName(station);
274    
275 torben 1036
276 torben 1040 String arrival = DepartureFetcher.cleanText( fields.get(1).text() );
277     String departure = DepartureFetcher.cleanText( fields.get(2).text() );
278    
279 torben 1060 entry.setStation( station );
280     entry.setArrival( arrival );
281     entry.setDeparture( departure );
282 torben 1036
283    
284     if (currentStation == true && currentStationSaved == false ) {
285 torben 1060 entry.setCurrent(currentStation);
286 torben 1036 currentStationSaved = true;
287     }
288    
289 torben 1060 entry.setStationId( getStationId( station ));
290 torben 1036
291 torben 1060 timetableBean.entries.add(entry);
292 torben 1036 }
293    
294     } else {
295     logger.warning("No time table found, trainID=" + trainID + " type=" + type);
296     }
297    
298    
299 torben 1060 return timetableBean;
300 torben 1036 }
301 torben 350
302     }

  ViewVC Help
Powered by ViewVC 1.1.20