/[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 1372 by torben, Thu Apr 21 05:51:25 2011 UTC revision 2077 by torben, Sat Nov 23 10:43:25 2013 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 40  public class DepartureFetcher { Line 41  public class DepartureFetcher {
41          Map<String, DepartureBean> cache;          Map<String, DepartureBean> cache;
42                    
43          StationDAO stationDao = new StationDAO();          StationDAO stationDao = new StationDAO();
44            TritinfoFetcher tritinfo;
45                    
46    
47          private TraininfoSettings settings;          private TraininfoSettings settings;
48                    
49            Comparator<DepartureEntry> departureTimeComparator = new Comparator<DepartureEntry>() {
50    
51                    @Override
52                    public int compare(DepartureEntry arg0, DepartureEntry arg1) {          
53                                    String timeStr1 = arg0.getTime().replace(":","").trim();
54                                    String timeStr2 = arg1.getTime().replace(":","").trim();
55                                    
56                                    int time1 = 0;
57                                    int time2 = 0;
58                                    
59                                    if (timeStr1.length() > 0)
60                                            time1 = Integer.parseInt(timeStr1);
61                                    
62                                    if (timeStr2.length() > 0)
63                                            time2 = Integer.parseInt(timeStr2);
64                                    
65                                    //work correctly when clock wraps around at midnight
66                                    if (Math.abs(time1-time2) < 1200) {
67                                            if (time1 > time2)
68                                                    return 1;
69                                            else
70                                                    return -1;
71                                    } else {
72                                            if (time1 < time2)
73                                                    return 1;
74                                            else
75                                                    return -1;
76    
77                                    }
78                                    
79                    }
80                    
81            };
82            
83          public DepartureFetcher(TraininfoSettings settings) {          public DepartureFetcher(TraininfoSettings settings) {
84                  this.settings = settings;                  this.settings = settings;
85                  cache = new TimeoutMap<String,DepartureBean>( settings.getCacheTimeout() );                  cache = new TimeoutMap<String,DepartureBean>( settings.getCacheTimeout() );
86                    
87                    tritinfo = new TritinfoFetcher(settings);
88          }          }
89                    
90                    
# Line 78  public class DepartureFetcher { Line 116  public class DepartureFetcher {
116                                    
117                  departureBean.stationName = station.getName();                  departureBean.stationName = station.getName();
118    
119                    //TODO: FetchTraintype.Both should be removed some time after 0.9.5 release
120                  if (station.getRegional() != null && (type == FetchTrainType.REGIONAL||type == FetchTrainType.BOTH) ) {                  if (station.getRegional() != null && (type == FetchTrainType.REGIONAL||type == FetchTrainType.BOTH) ) {
121                          DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival);                          DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival);
122                          departureBean.entries.addAll( tempBean.entries );                          departureBean.entries.addAll( tempBean.entries );
# Line 94  public class DepartureFetcher { Line 133  public class DepartureFetcher {
133                          logger.info("No departures found for station " + stationID);                          logger.info("No departures found for station " + stationID);
134                  }                  }
135                                    
136                    //TODO: FetchTraintype.Both should be removed some time after 0.9.5 release
137                  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
138                          Collections.sort( departureBean.entries );                          Collections.sort( departureBean.entries, departureTimeComparator);
139                    }
140                    
141                    //System.out.println("Trit: " + settings.isTritinfoEnabled()  + " " +  station.getTritStation() );
142                    if ( settings.isTritinfoEnabled()  && station.getTritStation() != -1) {
143                            try {
144                                    tritinfo.injectTritinfoData(departureBean, station);
145                            } catch (Exception ex) { //det er ikke kritisk at vi får perron numre med
146                                    logger.warning("tritinfo failed with " + ex.getClass().getName() + ": " + ex.getMessage() );
147                            }
148                  }                  }
149    
150                                    
# Line 241  public class DepartureFetcher { Line 290  public class DepartureFetcher {
290                            
291              stationcode = URLEncoder.encode(stationcode,"ISO-8859-1");              stationcode = URLEncoder.encode(stationcode,"ISO-8859-1");
292    
293              //String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning";                    
294              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";
295              logger.fine("URI: " + uri);                  logger.fine("URI: " + uri);    
296              JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() );              JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() );
# Line 472  http://mobil.bane.dk/mobilStation.asp?ar Line 521  http://mobil.bane.dk/mobilStation.asp?ar
521                  Element anchorElement = trainTd.getElementsByTag("a").get(0);                  Element anchorElement = trainTd.getElementsByTag("a").get(0);
522                  String href = anchorElement.attr("href");                  String href = anchorElement.attr("href");
523                                    
524                  int pos = href.lastIndexOf('/');                  int pos = href.lastIndexOf('=');
525                  String number = href.substring(pos+1);                  String number = href.substring(pos+1);
526                                    
527                  return number;                  return number;
# Line 506  http://mobil.bane.dk/mobilStation.asp?ar Line 555  http://mobil.bane.dk/mobilStation.asp?ar
555                                    
556                  return number;                  return number;
557          }          }
558            
559    
560                    
561          //test          //test

Legend:
Removed from v.1372  
changed lines
  Added in v.2077

  ViewVC Help
Powered by ViewVC 1.1.20