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

  ViewVC Help
Powered by ViewVC 1.1.20