/[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 1248 by torben, Thu Mar 31 17:13:19 2011 UTC revision 1562 by torben, Fri Jul 8 16:26:09 2011 UTC
# Line 4  package dk.thoerup.traininfoservice.bane Line 4  package dk.thoerup.traininfoservice.bane
4  import java.net.URL;  import java.net.URL;
5  import java.net.URLEncoder;  import java.net.URLEncoder;
6  import java.util.Collections;  import java.util.Collections;
7    import java.util.Comparator;
8  import java.util.Map;  import java.util.Map;
9  import java.util.logging.Logger;  import java.util.logging.Logger;
10    
# Line 13  import org.jsoup.select.Elements; Line 14  import org.jsoup.select.Elements;
14    
15  import dk.thoerup.android.traininfo.common.DepartureBean;  import dk.thoerup.android.traininfo.common.DepartureBean;
16  import dk.thoerup.android.traininfo.common.DepartureEntry;  import dk.thoerup.android.traininfo.common.DepartureEntry;
17  import dk.thoerup.android.traininfo.common.StationBean.StationEntry;  import dk.thoerup.android.traininfo.common.StationEntry;
18  import dk.thoerup.circuitbreaker.CircuitBreaker;  import dk.thoerup.circuitbreaker.CircuitBreaker;
19  import dk.thoerup.circuitbreaker.CircuitBreakerManager;  import dk.thoerup.circuitbreaker.CircuitBreakerManager;
20  import dk.thoerup.traininfoservice.StationDAO;  import dk.thoerup.genericjavautils.HttpUtil;
21    import dk.thoerup.genericjavautils.TimeoutMap;
22  import dk.thoerup.traininfoservice.Statistics;  import dk.thoerup.traininfoservice.Statistics;
23    import dk.thoerup.traininfoservice.TraininfoSettings;
24    import dk.thoerup.traininfoservice.db.StationDAO;
25    
26  public class DepartureFetcher {  public class DepartureFetcher {
27                    
# Line 38  public class DepartureFetcher { Line 42  public class DepartureFetcher {
42                    
43          StationDAO stationDao = new StationDAO();          StationDAO stationDao = new StationDAO();
44                    
45          private boolean useAzureSite;  
46          private int replyTimeout;          private TraininfoSettings settings;
47                    
48          public DepartureFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) {          Comparator<DepartureEntry> departureTimeComparator = new Comparator<DepartureEntry>() {
49                  this.replyTimeout = replyTimeout;  
50                  useAzureSite = azureSite;                  @Override
51                  cache = new TimeoutMap<String,DepartureBean>(cacheTimeout);                  public int compare(DepartureEntry arg0, DepartureEntry arg1) {          
52                                    String timeStr1 = arg0.getTime().replace(":","").trim();
53                                    String timeStr2 = arg1.getTime().replace(":","").trim();
54                                    
55                                    int time1 = 0;
56                                    int time2 = 0;
57                                    
58                                    if (timeStr1.length() > 0)
59                                            time1 = Integer.parseInt(timeStr1);
60                                    
61                                    if (timeStr2.length() > 0)
62                                            time2 = Integer.parseInt(timeStr2);
63                                    
64                                    //work correctly when clock wraps around at midnight
65                                    if (Math.abs(time1-time2) < 1200) {
66                                            if (time1 > time2)
67                                                    return 1;
68                                            else
69                                                    return -1;
70                                    } else {
71                                            if (time1 < time2)
72                                                    return 1;
73                                            else
74                                                    return -1;
75    
76                                    }
77                                    
78                    }
79                    
80            };
81            
82            public DepartureFetcher(TraininfoSettings settings) {
83                    this.settings = settings;
84                    cache = new TimeoutMap<String,DepartureBean>( settings.getCacheTimeout() );
85          }          }
86                    
87                    
# Line 75  public class DepartureFetcher { Line 112  public class DepartureFetcher {
112                  StationEntry station = stationDao.getById(stationID);                  StationEntry station = stationDao.getById(stationID);
113                                    
114                  departureBean.stationName = station.getName();                  departureBean.stationName = station.getName();
115                    
116                    //TODO: FetchTraintype.Both should be removed some time after 0.9.5 release
117                  if (station.getRegional() != null && (type == FetchTrainType.REGIONAL||type == FetchTrainType.BOTH) ) {                  if (station.getRegional() != null && (type == FetchTrainType.REGIONAL||type == FetchTrainType.BOTH) ) {
118                          DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival);                          DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival);
119                          departureBean.entries.addAll( tempBean.entries );                          departureBean.entries.addAll( tempBean.entries );
# Line 92  public class DepartureFetcher { Line 130  public class DepartureFetcher {
130                          logger.info("No departures found for station " + stationID);                          logger.info("No departures found for station " + stationID);
131                  }                  }
132                                    
133                  Collections.sort( departureBean.entries );                  //TODO: FetchTraintype.Both should be removed some time after 0.9.5 release
134                    if (type == FetchTrainType.BOTH) { //if we have both S-tog and regional order by departure/arrival time
135                            Collections.sort( departureBean.entries, departureTimeComparator);
136                    }
137    
138                                    
139                  return departureBean;                  return departureBean;
140          }          }
141                    
142          public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception {          public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception {
143                  if (useAzureSite == true) {                  if ( settings.getBackend() == TraininfoSettings.Backend.Azure) {
144                          return lookupDeparturesAzureSite(stationcode, type, arrival);                          return lookupDeparturesAzureSite(stationcode, type, arrival);
145                  } else {                  } else {
146                          return lookupDeparturesWwwSite(stationcode, type, arrival);                          return lookupDeparturesMobileSite(stationcode, type, arrival);
147                  }                  }
148          }          }
149                    
# Line 141  public class DepartureFetcher { Line 182  public class DepartureFetcher {
182              String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning";                      String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning";        
183                            
184              logger.fine("URI: " + uri);                  logger.fine("URI: " + uri);    
185              JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout);              JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() );
186              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
187                            
188              Document page = (Document) breaker.invoke(wrapper);              Document page = (Document) breaker.invoke(wrapper);
# Line 227  public class DepartureFetcher { Line 268  public class DepartureFetcher {
268              return departureBean;              return departureBean;
269          }          }
270                    
271            public DepartureBean lookupDeparturesMobileSite(String stationcode, TrainType traintype, boolean arrival) throws Exception {
272                    
273                    DepartureBean departureBean = new DepartureBean();
274                    
275                
276                    String typeString = getTypeStringWww(traintype);
277                String arrivalDeparture = (arrival==false) ? "afgang" : "ankomst";
278                
279                stationcode = URLEncoder.encode(stationcode,"ISO-8859-1");
280    
281                
282                String uri = "http://mobil.bane.dk/mobilStation.asp?artikelID=5332&stat_kode=" + stationcode + "&webprofil=" + typeString  +"&beskrivelse=&mode=ankomstafgang&ankomstafgang=" + arrivalDeparture + "&gemstation=&fuldvisning=1";
283                logger.fine("URI: " + uri);    
284                JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() );
285                CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
286                
287                Document page = (Document) breaker.invoke(wrapper);
288                
289                
290                Element content = page.getElementsByClass("contentDiv").get(0);
291                
292                
293                if (content != null) {
294                        Elements tableRows =  content.child(0).children();
295                        
296    
297                        
298                        for (Element currentRow : tableRows) {
299                            if (currentRow.tagName().equals("br") ) {
300                                    break;
301                            }
302                            
303                            
304                            String link = currentRow.child(0).attr("href");
305                            
306                                    logger.fine( currentRow.text() );
307                                    logger.fine("Href: " + link);
308                            
309    
310                            String parts[] = currentRow.text().split(",");
311                    
312    
313                            DepartureEntry departure = new DepartureEntry();
314                            
315                            //if we do these things upfront, then we are allowed to use continue statement when row contains no more data
316                            departure.setType(typeString);
317                            departureBean.entries.add( departure );
318    
319    /*
320    http://mobil.bane.dk/mobilStation.asp?artikelID=5332&tognummer=111&webprofil=FJRN&mode=rute&strBemaerkning=Afg%E5r+fra+%C5rhus+H+kl%2E07%3A21++&strRefURL=%2FmobilStation%2Easp%3FartikelID%3D5332%26stat%5Fkode%3DAR%26webprofil%3DFJRN%26beskrivelse%3D%25C5rhus%2BH%26mode%3Dankomstafgang%26ankomstafgang%3Dafgang%26gemstation%3D
321    */
322                            int offset = 0;
323                            
324                            String time = parts[offset++];
325                            if (time.equals(""))
326                                    time = "0:00"; //Bane.dk bug work-around
327                            departure.setTime(time);
328    
329                            int updated = 4; //does not exist on mobile
330                            departure.setUpdated(updated);
331    
332                            String trainNumber = extractTrainNumberMobile(link);
333                            /*if (traintype == TrainType.STOG) //If it is S-train we need to extract the trainNumber
334                                    trainNumber = trainNumber + " " + extractTrainNumberAzure(fields.get(2));*/
335                            departure.setTrainNumber(trainNumber);
336    
337                                    if (traintype == TrainType.STOG) { //if it is stog the next vield is the "Line" code - this should be used somewhere, but skippint ahead for now
338                                            String stogLine = parts[offset++].trim();
339                                            departure.setTrainNumber(stogLine + " " + trainNumber);
340                                    }
341    
342                            String destination = parts[offset++].trim();;
343                            departure.setDestination(destination);
344    
345                            String origin = "-"; // fields.get(4).text(); does not exist on mobile
346                            departure.setOrigin(origin);
347    
348                            String location = ""; // fields.get(5).text(); does not exist on mobile
349                            departure.setLocation(location);
350                            
351                            if (offset == parts.length) {
352                                    continue;
353                            }
354                            
355                            if (parts[offset].trim().equalsIgnoreCase("NB!")) {
356                                    offset++;
357                            }
358                            
359                            if (offset == parts.length) {
360                                    continue;
361                            }
362    
363                            String status = parts[offset++].trim();; //fields.get(6).text().trim(); - extract from url
364                            departure.setStatus(status);
365    
366                            String note = ""; //extractNote( fields.get(7) ); - extract from url
367                            departure.setNote(note);
368    
369                        }
370                } else {
371                    logger.warning("No departures found for station=" + stationcode + ", type=" + traintype);
372                }
373                
374                return departureBean;
375            }
376            
377                    
378                    
379          public static String cleanText(String input) {          public static String cleanText(String input) {
# Line 234  public class DepartureFetcher { Line 381  public class DepartureFetcher {
381                  return input.replace((char) 0xA0, (char)0x20).trim();                  return input.replace((char) 0xA0, (char)0x20).trim();
382          }          }
383                    
384            
385            // old www site is not available any more
386            @Deprecated
387          public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception {          public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception {
388                                    
389                  DepartureBean departureBean = new DepartureBean();                  DepartureBean departureBean = new DepartureBean();
# Line 247  public class DepartureFetcher { Line 397  public class DepartureFetcher {
397              logger.fine("URI:" + uri);              logger.fine("URI:" + uri);
398                            
399    
400              JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout);              JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() );
401              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
402                            
403              Element page = (Element) breaker.invoke(wrapper);              Element page = (Element) breaker.invoke(wrapper);
# Line 365  public class DepartureFetcher { Line 515  public class DepartureFetcher {
515                  return number;                  return number;
516          }          }
517                    
518            private String extractTrainNumberMobile(String link) {
519                    Map<String,String> elements = HttpUtil.decodeParams(link);
520                    
521                    return elements.get("tognummer");
522            }
523            
524          private String extractTrainNumberWww(Element trainTd) {          private String extractTrainNumberWww(Element trainTd) {
525                  String number = "";                  String number = "";
526                  Element anchorElement = trainTd.getElementsByTag("a").get(0);                  Element anchorElement = trainTd.getElementsByTag("a").get(0);
527                  String href = anchorElement.attr("href");                  String href = anchorElement.attr("href");
528                  String argstring = href.substring( href.indexOf('?') + 1);  
529                    String argstring = href.split("?")[1];
530                    Map<String,String> elements = HttpUtil.decodeParams(argstring);
531                    number = elements.get("TogNr");        
532                    
533                                    
534                    /*String argstring = href.substring( href.indexOf('?') + 1);
535                  String args[] = argstring.split("&");                  String args[] = argstring.split("&");
536                  for (String arg : args) {                  for (String arg : args) {
537                          String pair[] = arg.split("="); // Key=pair[0], Value=pair[1]                          String pair[] = arg.split("="); // Key=pair[0], Value=pair[1]
538                                                    
539                          if (pair[0].equalsIgnoreCase("TogNr"))                          if (pair[0].equalsIgnoreCase("TogNr"))
540                                  number = pair[1];                                  number = pair[1];
541                  }                  }*/
542                                                                    
543                                    
544                  return number;                  return number;
545          }          }

Legend:
Removed from v.1248  
changed lines
  Added in v.1562

  ViewVC Help
Powered by ViewVC 1.1.20