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

  ViewVC Help
Powered by ViewVC 1.1.20