/[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 1046 by torben, Tue Sep 14 05:33:30 2010 UTC revision 1832 by torben, Mon Aug 20 21:15:37 2012 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 11  import org.jsoup.nodes.Document; Line 12  import org.jsoup.nodes.Document;
12  import org.jsoup.nodes.Element;  import org.jsoup.nodes.Element;
13  import org.jsoup.select.Elements;  import org.jsoup.select.Elements;
14    
15    import dk.thoerup.android.traininfo.common.DepartureBean;
16    import dk.thoerup.android.traininfo.common.DepartureEntry;
17    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.StationBean;  import dk.thoerup.genericjavautils.HttpUtil;
21  import dk.thoerup.traininfoservice.StationDAO;  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 24  public class DepartureFetcher { Line 30  public class DepartureFetcher {
30                  REGIONAL                  REGIONAL
31          }          }
32                    
33            enum FetchTrainType {
34                    STOG,
35                    REGIONAL,
36                    BOTH
37            }
38            
39          Logger logger = Logger.getLogger(DepartureFetcher.class.getName());          Logger logger = Logger.getLogger(DepartureFetcher.class.getName());
40                    
41          Map<String, DepartureBean> cache;          Map<String, DepartureBean> cache;
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                    
88                                    
89                    
90          public DepartureBean cachedLookupDepartures(int stationID, boolean arrival) throws Exception {          public DepartureBean cachedLookupDepartures(int stationID, boolean arrival, FetchTrainType type) throws Exception {
91                  final String key = "" + stationID + ":" + arrival;                  
92                    final String key = "" + stationID + ":" + arrival + ":" + type.toString();
93                                    
94                  DepartureBean departureBean = cache.get(key);                  DepartureBean departureBean = cache.get(key);
95    
96                                    
97                  if (departureBean == null) {                  if (departureBean == null) {
98                          departureBean = lookupDepartures(stationID,arrival);                          departureBean = lookupDepartures(stationID, arrival, type);
99                          cache.put(key, departureBean);                          cache.put(key, departureBean);
100                  } else {                  } else {
101                          Statistics.getInstance().incrementDepartureCacheHits();                          Statistics.getInstance().incrementDepartureCacheHits();
# Line 59  public class DepartureFetcher { Line 105  public class DepartureFetcher {
105          }          }
106                                    
107    
108          public DepartureBean lookupDepartures(int stationID, boolean arrival) throws Exception {          public DepartureBean lookupDepartures(int stationID, boolean arrival, FetchTrainType type) throws Exception {
109                                    
110                  DepartureBean departureBean = new DepartureBean();                  DepartureBean departureBean = new DepartureBean();
111                                    
112                  StationBean station = stationDao.getById(stationID);                  StationEntry station = stationDao.getById(stationID);
113                                    
114                  departureBean.stationName = station.getName();                  departureBean.stationName = station.getName();
115                    
116                  if (station.getRegional() != null) {                  //TODO: FetchTraintype.Both should be removed some time after 0.9.5 release
117                    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.departureEntries.addAll( tempBean.departureEntries );                          departureBean.entries.addAll( tempBean.entries );
120                          departureBean.notifications.addAll(tempBean.notifications);                          departureBean.notifications.addAll(tempBean.notifications);
121                  }                  }
122                                    
123                  if (station.getStrain() != null) {                  if (station.getStrain() != null && (type == FetchTrainType.STOG||type == FetchTrainType.BOTH)) {
124                          DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival);                          DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival);
125                          departureBean.departureEntries.addAll( tempBean.departureEntries );                          departureBean.entries.addAll( tempBean.entries );
126                          departureBean.notifications.addAll(tempBean.notifications);                          departureBean.notifications.addAll(tempBean.notifications);
127                  }                                }              
128                                    
129                  if (departureBean.departureEntries.size() == 0) {                  if (departureBean.entries.size() == 0) {
130                          logger.info("No departures found for station " + stationID);                          logger.info("No departures found for station " + stationID);
131                  }                  }
132                                    
133                  Collections.sort( departureBean.departureEntries );                  //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                    System.out.println("Trit: " + settings.isTritinfoEnabled()  + " " +  station.getTritStation() );
139                    if ( settings.isTritinfoEnabled()  && station.getTritStation() != -1) {
140                            try {
141                                    injectTritinfoData(departureBean, station);
142                            } catch (Exception ex) { //det er ikke kritisk at vi får perron numre med
143                                    ex.printStackTrace();
144                            }
145                    }
146    
147                                    
148                  return departureBean;                  return departureBean;
149          }          }
150                    
151          public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception {          public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception {
152                  if (useAzureSite == true) {                  if ( settings.getBackend() == TraininfoSettings.Backend.Azure) {
153                          return lookupDeparturesAzureSite(stationcode, type, arrival);                          return lookupDeparturesAzureSite(stationcode, type, arrival);
154                  } else {                  } else {
155                          return lookupDeparturesWwwSite(stationcode, type, arrival);                          return lookupDeparturesMobileSite(stationcode, type, arrival);
156                  }                  }
157          }          }
158                    
# Line 131  public class DepartureFetcher { Line 190  public class DepartureFetcher {
190    
191              String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning";                      String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning";        
192                            
193              //logger.info("URI: " + uri);                        logger.fine("URI: " + uri);    
194              JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout);              JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() );
195              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
196                            
197              Document page = (Document) breaker.invoke(wrapper);              Document page = (Document) breaker.invoke(wrapper);
# Line 143  public class DepartureFetcher { Line 202  public class DepartureFetcher {
202              if (table != null) {              if (table != null) {
203                      Elements tableRows =  table.getElementsByTag("tr");                      Elements tableRows =  table.getElementsByTag("tr");
204                                            
205                      boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0);                      //boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0);
206                      boolean passedTidsstreg = false;                      //boolean passedTidsstreg = false;
207                                            
208                      for (Element currentRow : tableRows) {                      for (Element currentRow : tableRows) {
209                          String rowClass = currentRow.attr("class");                          String rowClass = currentRow.attr("class");
210                                                    /*
211                          if (tidsstregExists == true && passedTidsstreg == false) {                          if (tidsstregExists == true && passedTidsstreg == false) {
212                                  if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) {                                  if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) {
213                                          passedTidsstreg = true;                                          passedTidsstreg = true;
214                                  } else {                                  } else {
215                                          continue;                                          continue;
216                                  }                                  }
217                          }                          }*/
218                                                    
219                          if (rowClass != null && rowClass.toLowerCase().contains("station") ) {                          if (rowClass != null && rowClass.toLowerCase().contains("station") ) {
220                                                                    
# Line 193  public class DepartureFetcher { Line 252  public class DepartureFetcher {
252                                                                    
253                                  departure.setType(typeString);                                  departure.setType(typeString);
254                                                                    
255                                  departureBean.departureEntries.add( departure );                                  departureBean.entries.add( departure );
256                          }                          }
257                      }                      }
258              } else {              } else {
# Line 218  public class DepartureFetcher { Line 277  public class DepartureFetcher {
277              return departureBean;              return departureBean;
278          }          }
279                    
280            public DepartureBean lookupDeparturesMobileSite(String stationcode, TrainType traintype, boolean arrival) throws Exception {
281                    
282                    DepartureBean departureBean = new DepartureBean();
283                    
284                
285                    String typeString = getTypeStringWww(traintype);
286                String arrivalDeparture = (arrival==false) ? "afgang" : "ankomst";
287                
288                stationcode = URLEncoder.encode(stationcode,"ISO-8859-1");
289    
290                
291                String uri = "http://mobil.bane.dk/mobilStation.asp?artikelID=5332&stat_kode=" + stationcode + "&webprofil=" + typeString  +"&beskrivelse=&mode=ankomstafgang&ankomstafgang=" + arrivalDeparture + "&gemstation=&fuldvisning=1";
292                logger.fine("URI: " + uri);    
293                JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() );
294                CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
295                
296                Document page = (Document) breaker.invoke(wrapper);
297                
298                
299                Element content = page.getElementsByClass("contentDiv").get(0);
300                
301                
302                if (content != null) {
303                        Elements tableRows =  content.child(0).children();
304                        
305    
306                        
307                        for (Element currentRow : tableRows) {
308                            if (currentRow.tagName().equals("br") ) {
309                                    break;
310                            }
311                            
312                            
313                            String link = currentRow.child(0).attr("href");
314                            
315                                    logger.fine( currentRow.text() );
316                                    logger.fine("Href: " + link);
317                            
318    
319                            String parts[] = currentRow.text().split(",");
320                    
321    
322                            DepartureEntry departure = new DepartureEntry();
323                            
324                            //if we do these things upfront, then we are allowed to use continue statement when row contains no more data
325                            departure.setType(typeString);
326                            departureBean.entries.add( departure );
327    
328    /*
329    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
330    */
331                            int offset = 0;
332                            
333                            String time = parts[offset++];
334                            if (time.equals(""))
335                                    time = "0:00"; //Bane.dk bug work-around
336                            departure.setTime(time);
337    
338                            int updated = 4; //does not exist on mobile
339                            departure.setUpdated(updated);
340    
341                            String trainNumber = extractTrainNumberMobile(link);
342                            /*if (traintype == TrainType.STOG) //If it is S-train we need to extract the trainNumber
343                                    trainNumber = trainNumber + " " + extractTrainNumberAzure(fields.get(2));*/
344                            departure.setTrainNumber(trainNumber);
345    
346                                    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
347                                            String stogLine = parts[offset++].trim();
348                                            departure.setTrainNumber(stogLine + " " + trainNumber);
349                                    }
350    
351                            String destination = parts[offset++].trim();;
352                            departure.setDestination(destination);
353    
354                            String origin = "-"; // fields.get(4).text(); does not exist on mobile
355                            departure.setOrigin(origin);
356    
357                            String location = ""; // fields.get(5).text(); does not exist on mobile
358                            departure.setLocation(location);
359                            
360                            if (offset == parts.length) {
361                                    continue;
362                            }
363                            
364                            if (parts[offset].trim().equalsIgnoreCase("NB!")) {
365                                    offset++;
366                            }
367                            
368                            if (offset == parts.length) {
369                                    continue;
370                            }
371    
372                            String status = parts[offset++].trim();; //fields.get(6).text().trim(); - extract from url
373                            departure.setStatus(status);
374    
375                            String note = ""; //extractNote( fields.get(7) ); - extract from url
376                            departure.setNote(note);
377    
378                        }
379                } else {
380                    logger.warning("No departures found for station=" + stationcode + ", type=" + traintype);
381                }
382                
383                return departureBean;
384            }
385            
386                    
387                    
388          public static String cleanText(String input) {          public static String cleanText(String input) {
# Line 225  public class DepartureFetcher { Line 390  public class DepartureFetcher {
390                  return input.replace((char) 0xA0, (char)0x20).trim();                  return input.replace((char) 0xA0, (char)0x20).trim();
391          }          }
392                    
393            
394            // old www site is not available any more
395            @Deprecated
396          public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception {          public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception {
397                                    
398                  DepartureBean departureBean = new DepartureBean();                  DepartureBean departureBean = new DepartureBean();
# Line 235  public class DepartureFetcher { Line 403  public class DepartureFetcher {
403                                    
404                                                                                            
405              String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode;              String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode;
406              logger.info("URI:" + uri);              logger.fine("URI:" + uri);
407              JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout);              
408    
409                JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() );
410              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
411                            
412              Element page = (Element) breaker.invoke(wrapper);              Element page = (Element) breaker.invoke(wrapper);
# Line 249  public class DepartureFetcher { Line 419  public class DepartureFetcher {
419              if (table != null) {              if (table != null) {
420                      Elements tableRows =  table.getElementsByTag("tr");                      Elements tableRows =  table.getElementsByTag("tr");
421                                            
422                      boolean passedTidsstreg = false;                      //boolean passedTidsstreg = false;
423                      boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0);                      //boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0);
424                                            
425                      for (Element currentRow : tableRows) {                      for (Element currentRow : tableRows) {
426                          String rowClass = currentRow.attr("class");                          String rowClass = currentRow.attr("class");
427                                                    /*
428                          if (tidsstregExists == true && passedTidsstreg == false) {                          if (tidsstregExists == true && passedTidsstreg == false) {
429                                  if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) {                                  if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) {
430                                          passedTidsstreg = true;                                          passedTidsstreg = true;
431                                  } else {                                  } else {
432                                          continue;                                          continue;
433                                  }                                  }
434                          }                          }*/
435                                                    
436                                                    
437                          if (rowClass != null && rowClass.toLowerCase().contains("station") ) {                          if (rowClass != null && rowClass.toLowerCase().contains("station") ) {
# Line 301  public class DepartureFetcher { Line 471  public class DepartureFetcher {
471                                                                    
472                                  departure.setType(type);                                  departure.setType(type);
473                                                                    
474                                  departureBean.departureEntries.add(departure);                                  departureBean.entries.add(departure);
475                                                                    
476                                                                    
477                          }                          }
# Line 354  public class DepartureFetcher { Line 524  public class DepartureFetcher {
524                  return number;                  return number;
525          }          }
526                    
527            private String extractTrainNumberMobile(String link) {
528                    Map<String,String> elements = HttpUtil.decodeParams(link);
529                    
530                    return elements.get("tognummer");
531            }
532            
533          private String extractTrainNumberWww(Element trainTd) {          private String extractTrainNumberWww(Element trainTd) {
534                  String number = "";                  String number = "";
535                  Element anchorElement = trainTd.getElementsByTag("a").get(0);                  Element anchorElement = trainTd.getElementsByTag("a").get(0);
536                  String href = anchorElement.attr("href");                  String href = anchorElement.attr("href");
537                  String argstring = href.substring( href.indexOf('?') + 1);  
538                    String argstring = href.split("?")[1];
539                    Map<String,String> elements = HttpUtil.decodeParams(argstring);
540                    number = elements.get("TogNr");        
541                                    
542                    
543                    /*String argstring = href.substring( href.indexOf('?') + 1);
544                  String args[] = argstring.split("&");                  String args[] = argstring.split("&");
545                  for (String arg : args) {                  for (String arg : args) {
546                          String pair[] = arg.split("="); // Key=pair[0], Value=pair[1]                          String pair[] = arg.split("="); // Key=pair[0], Value=pair[1]
547                                                    
548                          if (pair[0].equalsIgnoreCase("TogNr"))                          if (pair[0].equalsIgnoreCase("TogNr"))
549                                  number = pair[1];                                  number = pair[1];
550                  }                  }*/
551                                                                    
552                                    
553                  return number;                  return number;
554          }          }
555            
556            
557            private void injectTritinfoData(DepartureBean departureBean, StationEntry station) throws Exception {
558                String uri = "http://tritinfo.pallas.dk/webtavle?page=stationcontent&staid=" + station.getTritStation();
559                logger.fine("URI:" + uri);
560                System.out.println("URI:" + uri);
561                
562    
563                JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() );
564                CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("tritinfo");
565                
566                Element page = (Element) breaker.invoke(wrapper);          
567                
568                Element table = page.getElementsByClass("passages").get(0);
569                
570                Elements trains = table.getElementsByClass("train");
571                
572                for (int i=0; i<trains.size(); i++) {
573                    Element train = trains.get(i);
574                    
575                    //String trainType = train.getElementsByClass("trainType").get(0).text();
576                    String trainNumber = train.getElementsByClass("trainNumber").get(0).text();            
577                    
578                    Elements trackElems = train.getElementsByClass("plannedTrack");
579                    if (trackElems.size() == 0) {
580                            trackElems = train.getElementsByClass("expectedTrack");
581                    }
582                    String track = trackElems.get(0).text();
583                    String trackType = train.getElementsByClass("trackType").get(0).text();
584                    
585                    
586                    String platform = track + " " + trackType;
587                    
588                    for (DepartureEntry entry : departureBean.entries) {
589                            String entryTrainId = entry.getTrainNumber().split(" ")[1];
590                            if ( entryTrainId.equals(trainNumber)) {
591                                    
592                                    entry.setPlatform(platform);
593                                    break;
594                            }
595                    }
596                }
597            }
598    
599                    
600          //test          //test

Legend:
Removed from v.1046  
changed lines
  Added in v.1832

  ViewVC Help
Powered by ViewVC 1.1.20