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

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

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

revision 389 by torben, Fri Oct 2 17:18:31 2009 UTC revision 584 by torben, Fri Feb 5 13:57:39 2010 UTC
# Line 6  import java.sql.Statement; Line 6  import java.sql.Statement;
6  import java.util.ArrayList;  import java.util.ArrayList;
7  import java.util.Collections;  import java.util.Collections;
8  import java.util.List;  import java.util.List;
9    import java.util.Map;
10  import java.util.logging.Logger;  import java.util.logging.Logger;
11    
   
12  import com.gargoylesoftware.htmlunit.WebClient;  import com.gargoylesoftware.htmlunit.WebClient;
13  import com.gargoylesoftware.htmlunit.html.DomNodeList;  import com.gargoylesoftware.htmlunit.html.DomNodeList;
14  import com.gargoylesoftware.htmlunit.html.HtmlElement;  import com.gargoylesoftware.htmlunit.html.HtmlElement;
15  import com.gargoylesoftware.htmlunit.html.HtmlPage;  import com.gargoylesoftware.htmlunit.html.HtmlPage;
16    
17    import dk.thoerup.circuitbreaker.CircuitBreaker;
18    import dk.thoerup.circuitbreaker.CircuitBreakerManager;
19  import dk.thoerup.traininfoservice.DBConnection;  import dk.thoerup.traininfoservice.DBConnection;
20    
21  public class DepartureFetcher {  public class DepartureFetcher {
22                    
23          Logger logger = Logger.getLogger(DepartureFetcher.class.getName());          Logger logger = Logger.getLogger(DepartureFetcher.class.getName());
24                    
25          TimeoutCache<Integer, List<DepartureBean>> cache = new TimeoutCache<Integer,List<DepartureBean>>(120 * 1000);          Map<Integer, List<DepartureBean>> cache;
26            
27            private boolean useTempSite;
28            
29            public DepartureFetcher(boolean tempSite, int cacheTimeout) {
30                    useTempSite = tempSite;
31                    cache = new TimeoutMap<Integer,List<DepartureBean>>(cacheTimeout);
32            }
33            
34                    
35                                    
36                    
# Line 76  public class DepartureFetcher { Line 86  public class DepartureFetcher {
86          }          }
87                    
88          public List<DepartureBean> lookupDepartures(String stationcode, String type) throws Exception {          public List<DepartureBean> lookupDepartures(String stationcode, String type) throws Exception {
89                    if (useTempSite == false) {
90                            return lookupDeparturesNormalSite(stationcode, type);
91                    } else {
92                            return lookupDeparturesFromTemporarySite(stationcode, type);
93                    }
94            }
95            
96            public List<DepartureBean> lookupDeparturesNormalSite(String stationcode, String type) throws Exception {
97                                    
98                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();
99                                    
# Line 83  public class DepartureFetcher { Line 101  public class DepartureFetcher {
101              webClient.setTimeout(2500);              webClient.setTimeout(2500);
102              webClient.setJavaScriptEnabled(false);              webClient.setJavaScriptEnabled(false);
103                                                            
104                String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode;
105                BanedkInvocation wrapper = new BanedkInvocation(webClient, uri);
106                CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
107                            
108              final HtmlPage page = webClient.getPage("http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode);              HtmlPage page = (HtmlPage) breaker.invoke(wrapper);
109                            
110              HtmlElement table = page.getElementById("afgangtabel");              HtmlElement table = page.getElementById("afgangtabel");
111                            
# Line 107  public class DepartureFetcher { Line 128  public class DepartureFetcher {
128                                  departure.setUpdated(updated);                                  departure.setUpdated(updated);
129                                                                    
130                                  String trainNumber = fields.get(2).asText();                                  String trainNumber = fields.get(2).asText();
131                                  if (trainNumber.trim().length() == 1)                                  if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber
132                                          trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2));                                          trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2));
133                                  departure.setTrainNumber(trainNumber);                                  departure.setTrainNumber(trainNumber);
134                                                                    
# Line 136  public class DepartureFetcher { Line 157  public class DepartureFetcher {
157              return departureList;              return departureList;
158          }          }
159                    
160            public List<DepartureBean> lookupDeparturesFromTemporarySite(String stationcode, String type) throws Exception {
161                    
162                    List<DepartureBean> departureList = new ArrayList<DepartureBean>();
163                    
164                final WebClient webClient = new WebClient();
165                webClient.setTimeout(2500);
166                webClient.setJavaScriptEnabled(false);
167                
168    
169                String uri = "http://bane.dk/lite/station.asp?w=" + type + "&s=" + stationcode;
170                
171                BanedkInvocation wrapper = new BanedkInvocation(webClient, uri);
172                CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
173                
174                HtmlPage page = (HtmlPage) breaker.invoke(wrapper);
175                
176                HtmlElement table = page.getElementById("traf_afgang");
177                
178                if (table != null) {                        
179                        DomNodeList<HtmlElement> tableRows =  table.getElementsByTagName("tr");
180                        
181                        boolean isFirst = true;
182                        
183                        for (HtmlElement currentRow : tableRows) {
184                            if (isFirst == true) { //skip table headers
185                                    isFirst = false;
186                                    continue;
187                            }
188                            
189                            DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");
190    
191                            DepartureBean departure = new DepartureBean();
192    
193                            String time = fields.get(0).asText().trim();
194    
195                            if (time.equals(""))
196                                    time = "0:00"; //Bane.dk bug work-around
197                            departure.setTime(time);
198    
199    
200                            String trainNumber = fields.get(1).asText();
201                            departure.setTrainNumber(trainNumber);
202    
203                            String destination = fields.get(2).asText();
204                            departure.setDestination(destination);
205    
206                            String origin = fields.get(3).asText();
207                            departure.setOrigin(origin);
208    
209                            String status = fields.get(4).asText();
210                            departure.setStatus(status);
211    
212                            String note = fields.get(5).asText();
213                            departure.setNote(note);
214    
215                            departureList.add(departure);
216                        }
217                } else {
218                    logger.warning("No departures found for station=" + stationcode + ", type=" + type);
219                }
220                
221                return departureList;
222            }
223    
224            
225          private int extractUpdated(HtmlElement updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif"          private int extractUpdated(HtmlElement updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif"
226                  int updated = -1;                  int updated = -1;
227                                    
# Line 184  public class DepartureFetcher { Line 270  public class DepartureFetcher {
270          }          }
271                    
272          //test          //test
273          public static void main(String args[]) throws Exception{          /*
274            public static void main(String args[]) throws Exception {
275                  DepartureFetcher f = new DepartureFetcher();                  DepartureFetcher f = new DepartureFetcher();
276                  List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN");                  List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN");
277                  for(DepartureBean d : deps) {                  for(DepartureBean d : deps) {
# Line 193  public class DepartureFetcher { Line 280  public class DepartureFetcher {
280                  }                  }
281                                    
282                  System.out.println("--------------------------");                  System.out.println("--------------------------");
283          }          }*/
284  }  }

Legend:
Removed from v.389  
changed lines
  Added in v.584

  ViewVC Help
Powered by ViewVC 1.1.20