/[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 579 by torben, Thu Oct 22 06:04:45 2009 UTC revision 580 by torben, Tue Feb 2 18:42:38 2010 UTC
# Line 24  public class DepartureFetcher { Line 24  public class DepartureFetcher {
24                    
25          Map<Integer, List<DepartureBean>> cache = new TimeoutMap<Integer,List<DepartureBean>>(120 * 1000);          Map<Integer, List<DepartureBean>> cache = new TimeoutMap<Integer,List<DepartureBean>>(120 * 1000);
26                    
27            private boolean useTempSite;
28            
29            public DepartureFetcher(boolean tempSite) {
30                    useTempSite = tempSite;
31            }
32            
33            
34                                    
35                    
36          public List<DepartureBean> cachedLookupDepartures(int stationID) throws Exception {          public List<DepartureBean> cachedLookupDepartures(int stationID) throws Exception {
# Line 78  public class DepartureFetcher { Line 85  public class DepartureFetcher {
85          }          }
86                    
87          public List<DepartureBean> lookupDepartures(String stationcode, String type) throws Exception {          public List<DepartureBean> lookupDepartures(String stationcode, String type) throws Exception {
88                    if (useTempSite == false) {
89                            return lookupDeparturesNormalSite(stationcode, type);
90                    } else {
91                            return lookupDeparturesFromTemporarySite(stationcode, type);
92                    }
93            }
94            
95            public List<DepartureBean> lookupDeparturesNormalSite(String stationcode, String type) throws Exception {
96                                    
97                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();                  List<DepartureBean> departureList = new ArrayList<DepartureBean>();
98                                    
# Line 141  public class DepartureFetcher { Line 156  public class DepartureFetcher {
156              return departureList;              return departureList;
157          }          }
158                    
159            public List<DepartureBean> lookupDeparturesFromTemporarySite(String stationcode, String type) throws Exception {
160                    
161                    List<DepartureBean> departureList = new ArrayList<DepartureBean>();
162                    
163                final WebClient webClient = new WebClient();
164                webClient.setTimeout(2500);
165                webClient.setJavaScriptEnabled(false);
166                
167    
168                String uri = "http://bane.dk/lite/station.asp?w=" + type + "&s=" + stationcode;
169                
170                BanedkInvocation wrapper = new BanedkInvocation(webClient, uri);
171                CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
172                
173                HtmlPage page = (HtmlPage) breaker.invoke(wrapper);
174                
175                HtmlElement table = page.getElementById("traf_afgang");
176                
177                if (table != null) {                        
178                        DomNodeList<HtmlElement> tableRows =  table.getElementsByTagName("tr");
179                        
180                        boolean isFirst = true;
181                        
182                        for (HtmlElement currentRow : tableRows) {
183                            if (isFirst == true) { //skip table headers
184                                    isFirst = false;
185                                    continue;
186                            }
187                            
188                            DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");
189    
190                            DepartureBean departure = new DepartureBean();
191    
192                            String time = fields.get(0).asText().trim();
193                            logger.info("time:" + time);
194                            if (time.equals(""))
195                                    time = "0:00"; //Bane.dk bug work-around
196                            departure.setTime(time);
197    
198    
199                            String trainNumber = fields.get(1).asText();
200                            if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber
201                                    trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2));
202                            departure.setTrainNumber(trainNumber);
203    
204                            String destination = fields.get(2).asText();
205                            departure.setDestination(destination);
206    
207                            String origin = fields.get(3).asText();
208                            departure.setOrigin(origin);
209    
210                            String status = fields.get(4).asText();
211                            departure.setStatus(status);
212    
213                            String note = extractNote( fields.get(5) );
214                            departure.setNote(note);
215    
216                            departureList.add(departure);
217                        }
218                } else {
219                    logger.warning("No departures found for station=" + stationcode + ", type=" + type);
220                }
221                
222                return departureList;
223            }
224    
225            
226          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"
227                  int updated = -1;                  int updated = -1;
228                                    
# Line 189  public class DepartureFetcher { Line 271  public class DepartureFetcher {
271          }          }
272                    
273          //test          //test
274            /*
275          public static void main(String args[]) throws Exception {          public static void main(String args[]) throws Exception {
276                  DepartureFetcher f = new DepartureFetcher();                  DepartureFetcher f = new DepartureFetcher();
277                  List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN");                  List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN");
# Line 198  public class DepartureFetcher { Line 281  public class DepartureFetcher {
281                  }                  }
282                                    
283                  System.out.println("--------------------------");                  System.out.println("--------------------------");
284          }          }*/
285  }  }

Legend:
Removed from v.579  
changed lines
  Added in v.580

  ViewVC Help
Powered by ViewVC 1.1.20