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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1060 by torben, Thu Sep 16 13:32:10 2010 UTC revision 1406 by torben, Mon May 2 09:34:53 2011 UTC
# Line 12  import org.jsoup.nodes.Document; Line 12  import org.jsoup.nodes.Document;
12  import org.jsoup.nodes.Element;  import org.jsoup.nodes.Element;
13  import org.jsoup.select.Elements;  import org.jsoup.select.Elements;
14    
15    import dk.thoerup.android.traininfo.common.TimetableBean;
16    import dk.thoerup.android.traininfo.common.TimetableEntry;
17  import dk.thoerup.circuitbreaker.CircuitBreaker;  import dk.thoerup.circuitbreaker.CircuitBreaker;
18  import dk.thoerup.circuitbreaker.CircuitBreakerManager;  import dk.thoerup.circuitbreaker.CircuitBreakerManager;
19  import dk.thoerup.traininfoservice.StationDAO;  import dk.thoerup.genericjavautils.TimeoutMap;
20  import dk.thoerup.traininfoservice.Statistics;  import dk.thoerup.traininfoservice.Statistics;
21    import dk.thoerup.traininfoservice.TraininfoSettings;
22    import dk.thoerup.traininfoservice.db.StationDAO;
23    
24  public class TimetableFetcher {  public class TimetableFetcher {
25    
# Line 27  public class TimetableFetcher { Line 31  public class TimetableFetcher {
31                    
32                    
33          Logger logger = Logger.getLogger(TimetableFetcher.class.getName());          Logger logger = Logger.getLogger(TimetableFetcher.class.getName());
34    
35            TraininfoSettings settings;    
36                    
37          private boolean useAzureSite;          public TimetableFetcher(TraininfoSettings settings) {
38          private int replyTimeout;                  this.settings = settings;
           
         public TimetableFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) {  
                 useAzureSite = azureSite;  
                 this.replyTimeout = replyTimeout;  
39                                    
40                  cache = new TimeoutMap<String,TimetableBean>(cacheTimeout);                  cache = new TimeoutMap<String,TimetableBean>( settings.getCacheTimeout() );
41                  stationCache = new TimeoutMap<String,Integer>( 3*60*60*1000 );                  stationCache = new TimeoutMap<String,Integer>( 3*60*60*1000 );
42          }          }
43                    
# Line 43  public class TimetableFetcher { Line 45  public class TimetableFetcher {
45          TimetableBean cachedLookupTimetable(String trainID, String type) throws Exception {          TimetableBean cachedLookupTimetable(String trainID, String type) throws Exception {
46                  String key = trainID+type;                  String key = trainID+type;
47                  TimetableBean list = cache.get(key);                  TimetableBean list = cache.get(key);
48                    list = null; //TODO: DEBUG
49                                    
50                  if (list == null) {                  if (list == null) {
51                          list = lookupTimetable(trainID,type);                          list = lookupTimetable(trainID,type);
# Line 55  public class TimetableFetcher { Line 58  public class TimetableFetcher {
58          }          }
59                    
60          TimetableBean lookupTimetable(String trainID, String type) throws Exception {          TimetableBean lookupTimetable(String trainID, String type) throws Exception {
61                  if (useAzureSite == true ){                  if (settings.getBackend() == TraininfoSettings.Backend.Azure ){
62                          return lookupTimetableAzureSite(trainID, type);                          return lookupTimetableAzureSite(trainID, type);
63                                                    
64                  } else {                  } else {
65                          return lookupTimetableWwwSite(trainID, type);                          return lookupTimetableMobileSite(trainID, type);
66                  }                  }
67          }          }
68                    
# Line 78  public class TimetableFetcher { Line 81  public class TimetableFetcher {
81    
82                  return id;                  return id;
83          }          }
84            
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    
92          TimetableBean lookupTimetableAzureSite(String trainID, String type) throws Exception {                    TimetableBean lookupTimetableAzureSite(String trainID, String type) throws Exception {          
93                  TimetableBean timetableBean = new TimetableBean();                  TimetableBean timetableBean = new TimetableBean();
# Line 86  public class TimetableFetcher { Line 96  public class TimetableFetcher {
96                  String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID;                                  String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID;                
97                  logger.fine("URL:" + url);                  logger.fine("URL:" + url);
98                            
99              JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout);              JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() );
100              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
101                            
102              Document doc = (Document) breaker.invoke(wrapper);              Document doc = (Document) breaker.invoke(wrapper);
# Line 117  public class TimetableFetcher { Line 127  public class TimetableFetcher {
127                                                    
128                          TimetableEntry entry = new TimetableEntry();                          TimetableEntry entry = new TimetableEntry();
129                                                    
130                          String station = fields.get(0).text() ;                          String station = correctStationName( fields.get(0).text() );
                         if (station.equals("København"))  
                                 station = "København H"; //correct inconsistency in naming  
131                                                    
132                          entry.setStation( station );                          entry.setStation( station );
133                          entry.setArrival( fields.get(1).text() );                          entry.setArrival( fields.get(1).text() );
# Line 169  public class TimetableFetcher { Line 177  public class TimetableFetcher {
177                  return timetableBean;                  return timetableBean;
178          }          }
179    
180            TimetableBean lookupTimetableMobileSite(String trainID, String type) throws Exception {
181                    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                    logger.fine( row.text() );
203                    
204                    String parts[] = row.text().split(",");
205                    
206                    TimetableEntry entry = new TimetableEntry();
207    
208                    String station = DepartureFetcher.cleanText( parts[0] ) ;
209                    station = correctStationName(station);
210    
211    
212                    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            }
229            
230            @Deprecated
231          TimetableBean lookupTimetableWwwSite(String trainID, String type) throws Exception {                      TimetableBean lookupTimetableWwwSite(String trainID, String type) throws Exception {            
232                  TimetableBean timetableBean = new TimetableBean();                  TimetableBean timetableBean = new TimetableBean();
233                                    
# Line 176  public class TimetableFetcher { Line 235  public class TimetableFetcher {
235                  logger.fine("URL:" + url);                  logger.fine("URL:" + url);
236    
237                            
238              JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout);              JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() );
239              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
240                            
241              Document doc = (Document) breaker.invoke(wrapper);              Document doc = (Document) breaker.invoke(wrapper);
# Line 208  public class TimetableFetcher { Line 267  public class TimetableFetcher {
267                          TimetableEntry entry = new TimetableEntry();                          TimetableEntry entry = new TimetableEntry();
268                                                    
269                          String station = DepartureFetcher.cleanText( fields.get(0).text() ) ;                          String station = DepartureFetcher.cleanText( fields.get(0).text() ) ;
270                          if (station.equals("København"))                          station = correctStationName(station);
271                                  station = "København H"; //correct inconsistency in naming  
272                                                    
273                          String arrival = DepartureFetcher.cleanText( fields.get(1).text() );                          String arrival = DepartureFetcher.cleanText( fields.get(1).text() );
274                          String departure = DepartureFetcher.cleanText( fields.get(2).text() );                          String departure = DepartureFetcher.cleanText( fields.get(2).text() );

Legend:
Removed from v.1060  
changed lines
  Added in v.1406

  ViewVC Help
Powered by ViewVC 1.1.20