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

  ViewVC Help
Powered by ViewVC 1.1.20