/[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 1255 by torben, Mon Apr 4 10:56:44 2011 UTC revision 1368 by torben, Wed Apr 20 20:40:35 2011 UTC
# Line 16  import dk.thoerup.android.traininfo.comm Line 16  import dk.thoerup.android.traininfo.comm
16  import dk.thoerup.android.traininfo.common.TimetableEntry;  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.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;  import dk.thoerup.traininfoservice.db.StationDAO;
23    
24  public class TimetableFetcher {  public class TimetableFetcher {
# Line 29  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 45  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 57  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.getUseAzureSite() == true ){
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 88  public class TimetableFetcher { Line 89  public class TimetableFetcher {
89                  String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID;                                  String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID;                
90                  logger.fine("URL:" + url);                  logger.fine("URL:" + url);
91                            
92              JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout);              JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() );
93              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
94                            
95              Document doc = (Document) breaker.invoke(wrapper);              Document doc = (Document) breaker.invoke(wrapper);
# Line 171  public class TimetableFetcher { Line 172  public class TimetableFetcher {
172                  return timetableBean;                  return timetableBean;
173          }          }
174    
175            TimetableBean lookupTimetableMobileSite(String trainID, String type) throws Exception {
176                    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                    logger.fine( row.text() );
198                    
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            }
224            
225            @Deprecated
226          TimetableBean lookupTimetableWwwSite(String trainID, String type) throws Exception {                      TimetableBean lookupTimetableWwwSite(String trainID, String type) throws Exception {            
227                  TimetableBean timetableBean = new TimetableBean();                  TimetableBean timetableBean = new TimetableBean();
228                                    
# Line 178  public class TimetableFetcher { Line 230  public class TimetableFetcher {
230                  logger.fine("URL:" + url);                  logger.fine("URL:" + url);
231    
232                            
233              JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout);              JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() );
234              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
235                            
236              Document doc = (Document) breaker.invoke(wrapper);              Document doc = (Document) breaker.invoke(wrapper);

Legend:
Removed from v.1255  
changed lines
  Added in v.1368

  ViewVC Help
Powered by ViewVC 1.1.20