/[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 1366 by torben, Wed Apr 20 20:04:05 2011 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 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.genericjavautils.HttpUtil;  import dk.thoerup.genericjavautils.HttpUtil;
# Line 44  public class DepartureFetcher { Line 45  public class DepartureFetcher {
45    
46          private TraininfoSettings settings;          private TraininfoSettings settings;
47                    
48            Comparator<DepartureEntry> departureTimeComparator = new Comparator<DepartureEntry>() {
49    
50                    @Override
51                    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) {          public DepartureFetcher(TraininfoSettings settings) {
83                  this.settings = settings;                  this.settings = settings;
84                  cache = new TimeoutMap<String,DepartureBean>( settings.getCacheTimeout() );                  cache = new TimeoutMap<String,DepartureBean>( settings.getCacheTimeout() );
# Line 78  public class DepartureFetcher { Line 113  public class DepartureFetcher {
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 94  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                    //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                  if (type == FetchTrainType.BOTH) { //if we have both S-tog and regional order by departure/arrival time
135                          Collections.sort( departureBean.entries );                          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                                    
# Line 103  public class DepartureFetcher { Line 149  public class DepartureFetcher {
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 ( settings.getUseAzureSite() == true) {                  if ( settings.getBackend() == TraininfoSettings.Backend.Azure) {
153                          return lookupDeparturesAzureSite(stationcode, type, arrival);                          return lookupDeparturesAzureSite(stationcode, type, arrival);
154                  } else {                  } else {
155                          return lookupDeparturesMobileSite(stationcode, type, arrival);                          return lookupDeparturesMobileSite(stationcode, type, arrival);
# Line 241  public class DepartureFetcher { Line 287  public class DepartureFetcher {
287                            
288              stationcode = URLEncoder.encode(stationcode,"ISO-8859-1");              stationcode = URLEncoder.encode(stationcode,"ISO-8859-1");
289    
290              //String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning";                    
291              String uri = "http://mobil.bane.dk/mobilStation.asp?artikelID=5332&stat_kode=" + stationcode + "&webprofil=" + typeString  +"&beskrivelse=&mode=ankomstafgang&ankomstafgang=" + arrivalDeparture + "&gemstation=&fuldvisning=1";              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);                  logger.fine("URI: " + uri);    
293              JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() );              JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() );
# Line 506  http://mobil.bane.dk/mobilStation.asp?ar Line 552  http://mobil.bane.dk/mobilStation.asp?ar
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.1366  
changed lines
  Added in v.1832

  ViewVC Help
Powered by ViewVC 1.1.20