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

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

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

revision 1042 by torben, Mon Sep 13 17:57:31 2010 UTC revision 1409 by torben, Mon May 2 11:59:40 2011 UTC
# Line 2  package dk.thoerup.traininfoservice.bane Line 2  package dk.thoerup.traininfoservice.bane
2    
3    
4  import java.net.URL;  import java.net.URL;
   
 import java.util.ArrayList;  
 import java.util.List;  
5  import java.util.Map;  import java.util.Map;
6  import java.util.logging.Logger;  import java.util.logging.Logger;
7    
   
8  import org.jsoup.nodes.Document;  import org.jsoup.nodes.Document;
9  import org.jsoup.nodes.Element;  import org.jsoup.nodes.Element;
10  import org.jsoup.select.Elements;  import org.jsoup.select.Elements;
11    
12    import dk.thoerup.android.traininfo.common.MetroBean;
13    import dk.thoerup.android.traininfo.common.MetroBean.MetroEntry;
14    import dk.thoerup.android.traininfo.common.StationEntry;
15  import dk.thoerup.circuitbreaker.CircuitBreaker;  import dk.thoerup.circuitbreaker.CircuitBreaker;
16  import dk.thoerup.circuitbreaker.CircuitBreakerManager;  import dk.thoerup.circuitbreaker.CircuitBreakerManager;
17  import dk.thoerup.traininfoservice.StationBean;  import dk.thoerup.genericjavautils.TimeoutMap;
18  import dk.thoerup.traininfoservice.StationDAO;  import dk.thoerup.traininfoservice.db.StationDAO;
 import dk.thoerup.traininfoservice.Statistics;  
19    
20    
21    
22  public class MetroFetcher {  public class MetroFetcher {
         public static class MetroEntry {  
                 String metro;  
                 String destination;  
                 String minutes;          
         }  
           
         public static class MetroBean {  
                 String head;  
                 String operationInfo; //driftsinfo  
                 String plan; // koereplan  
                   
                 List<MetroEntry> entries = new ArrayList<MetroEntry>();  
         }  
23                    
24          Logger logger = Logger.getLogger(MetroFetcher.class.getName());          Logger logger = Logger.getLogger(MetroFetcher.class.getName());
25          StationDAO stationDAO = new StationDAO();          StationDAO stationDAO = new StationDAO();
26                    
27          Map<String, MetroBean> cache = new TimeoutMap<String,MetroBean>(1000);          Map<String, MetroBean> cache = new TimeoutMap<String,MetroBean>(60000); //TODO: make metro cache timeout configurable
28                    
29          public MetroBean cachedLookupMetroDepartures(int stationID) throws Exception {          public MetroBean cachedLookupMetroDepartures(int stationID) throws Exception {
30                  final String key = "metro:" + stationID;                  final String key = "metro:" + stationID;
# Line 50  public class MetroFetcher { Line 35  public class MetroFetcher {
35                          metroBean = lookupMetroDepartures(stationID);                          metroBean = lookupMetroDepartures(stationID);
36                          cache.put(key, metroBean);                          cache.put(key, metroBean);
37                  } else {                  } else {
38                          Statistics.getInstance().incrementDepartureCacheHits();                          //Statistics.getInstance().incrementDepartureCacheHits();
39                          logger.info("Metro: Cache hit " + key); //remove before production                          logger.info("Metro: Cache hit " + key); //remove before production
40                  }                  }
41                  return metroBean;                  return metroBean;
# Line 58  public class MetroFetcher { Line 43  public class MetroFetcher {
43    
44          public MetroBean lookupMetroDepartures(int stationID) throws Exception {          public MetroBean lookupMetroDepartures(int stationID) throws Exception {
45                                    
46                  StationBean station = stationDAO.getById(stationID) ;                  StationEntry station = stationDAO.getById(stationID) ;
47                                                    
48                                    
49                  URL url = new URL("http://www.m.dk/layouts/Metro/Widgets/MetroWidget.ashx?StationId=" + station.getMetro() + "&Congestion=true&Application=MyMetro&Expiration=true");                  URL url = new URL("http://www.m.dk/layouts/Metro/Widgets/MetroWidget.ashx?StationId=" + station.getMetro() + "&Congestion=true&Application=MyMetro&Expiration=true");
50                    logger.fine("URL:" + url);
51                                    
52              JsoupInvocation wrapper = new JsoupInvocation( url, 3000);              JsoupInvocation wrapper = new JsoupInvocation( url, 3000);
53              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("metro");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("metro");
# Line 75  public class MetroFetcher { Line 61  public class MetroFetcher {
61                  Element content = doc.getElementsByClass("metro-widget-content").get(0);                  Element content = doc.getElementsByClass("metro-widget-content").get(0);
62                  Elements contentElems = content.children();                  Elements contentElems = content.children();
63                                                                                                                    
64                  Element departureTable = content.getElementsByTag("table").get(0);                  Elements contentTables = content.getElementsByTag("table");
65                    if ( contentTables.size() > 0 ) {
                 Elements rows = departureTable.getElementsByTag("tr");  
   
                 for (int i=1; i<rows.size(); i++) {  
                         Element row = rows.get(i);        
                         Elements fields = row.children();  
66                                                    
67                          String metro = fields.get(0).text();                          Element departureTable = content.getElementsByTag("table").get(0);
68                          String destination = fields.get(1).text();          
69                          Elements minuteFields = fields.get(2).children();                          Elements rows = departureTable.getElementsByTag("tr");
70                          String minutes ="";          
71                                                    for (int i=1; i<rows.size(); i++) {
72                          for (Element elm : minuteFields) {                                  Element row = rows.get(i);      
73                                  minutes = minutes + elm.text() + " ";                                  Elements fields = row.children();
74            
75                                    
76                                    
77                                    Elements metroFields = fields.get(0).children();                        
78                                    String metro ="";                      
79                                    for (Element elm : metroFields) {
80                                            metro = metro + elm.text() + " ";
81                                    }
82                                    
83                                    
84                                    
85                                    String destination = fields.get(1).text();
86                                    
87                                    Elements minuteFields = fields.get(2).children();                      
88                                    String minutes ="";                    
89                                    for (Element elm : minuteFields) {
90                                            minutes = minutes + elm.text() + " ";
91                                    }
92                                    
93                                    MetroEntry entry = new MetroEntry();
94                                    entry.metro = metro.trim();
95                                    entry.destination = destination.trim();
96                                    entry.minutes = minutes.trim();
97                                    
98                                    bean.entries.add(entry);                        
99                          }                          }
                           
                         MetroEntry entry = new MetroEntry();  
                         entry.metro = metro;  
                         entry.destination = destination;  
                         entry.minutes = minutes;  
                           
                         bean.entries.add(entry);                          
100                  }                  }
101                                                                    
102                  bean.operationInfo = contentElems.get(3).text();                  
103                  bean.plan = contentElems.get(5).text();                  for (int i=0; i<contentElems.size(); i++) {
104                            String currentText = contentElems.get(i).text().trim();
105                            if (currentText.equalsIgnoreCase("driftsinformation")) {                                
106                                    bean.operationInfo = contentElems.get(i+1).text();
107                            }
108                            
109                            if (currentText.equalsIgnoreCase("køreplan")) {                                
110                                    bean.plan = contentElems.get(i+1).text();
111                            }
112                    }
113                                    
114    
115    
116                  return bean;                  return bean;
117          }          }
118                    

Legend:
Removed from v.1042  
changed lines
  Added in v.1409

  ViewVC Help
Powered by ViewVC 1.1.20