/[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 1333 by torben, Wed Apr 20 06:26:19 2011 UTC
# Line 16  import dk.thoerup.android.traininfo.comm Line 16  import dk.thoerup.android.traininfo.comm
16  import dk.thoerup.android.traininfo.common.StationBean.StationEntry;  import dk.thoerup.android.traininfo.common.StationBean.StationEntry;
17  import dk.thoerup.circuitbreaker.CircuitBreaker;  import dk.thoerup.circuitbreaker.CircuitBreaker;
18  import dk.thoerup.circuitbreaker.CircuitBreakerManager;  import dk.thoerup.circuitbreaker.CircuitBreakerManager;
 import dk.thoerup.traininfoservice.StationDAO;  
19  import dk.thoerup.traininfoservice.Statistics;  import dk.thoerup.traininfoservice.Statistics;
20    import dk.thoerup.traininfoservice.TraininfoSettings;
21    import dk.thoerup.traininfoservice.db.StationDAO;
22    
23  public class DepartureFetcher {  public class DepartureFetcher {
24                    
# Line 38  public class DepartureFetcher { Line 39  public class DepartureFetcher {
39                    
40          StationDAO stationDao = new StationDAO();          StationDAO stationDao = new StationDAO();
41                    
42          private boolean useAzureSite;  
43          private int replyTimeout;          private TraininfoSettings settings;
44                    
45          public DepartureFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) {          public DepartureFetcher(TraininfoSettings settings) {
46                  this.replyTimeout = replyTimeout;                  this.settings = settings;
47                  useAzureSite = azureSite;                  cache = new TimeoutMap<String,DepartureBean>( settings.getCacheTimeout() );
                 cache = new TimeoutMap<String,DepartureBean>(cacheTimeout);  
48          }          }
49                    
50                    
# Line 75  public class DepartureFetcher { Line 75  public class DepartureFetcher {
75                  StationEntry station = stationDao.getById(stationID);                  StationEntry station = stationDao.getById(stationID);
76                                    
77                  departureBean.stationName = station.getName();                  departureBean.stationName = station.getName();
78                    
79                  if (station.getRegional() != null && (type == FetchTrainType.REGIONAL||type == FetchTrainType.BOTH) ) {                  if (station.getRegional() != null && (type == FetchTrainType.REGIONAL||type == FetchTrainType.BOTH) ) {
80                          DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival);                          DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival);
81                          departureBean.entries.addAll( tempBean.entries );                          departureBean.entries.addAll( tempBean.entries );
# Line 92  public class DepartureFetcher { Line 92  public class DepartureFetcher {
92                          logger.info("No departures found for station " + stationID);                          logger.info("No departures found for station " + stationID);
93                  }                  }
94                                    
95                  Collections.sort( departureBean.entries );                  if (type == FetchTrainType.BOTH) { //if we have both S-tog and regional order by departure/arrival time
96                            Collections.sort( departureBean.entries );
97                    }
98    
99                                    
100                  return departureBean;                  return departureBean;
101          }          }
102                    
103          public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception {          public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception {
104                  if (useAzureSite == true) {                  if ( settings.getUseAzureSite() == true) {
105                          return lookupDeparturesAzureSite(stationcode, type, arrival);                          return lookupDeparturesAzureSite(stationcode, type, arrival);
106                  } else {                  } else {
107                          return lookupDeparturesWwwSite(stationcode, type, arrival);                          return lookupDeparturesMobileSite(stationcode, type, arrival);
108                  }                  }
109          }          }
110                    
# Line 141  public class DepartureFetcher { Line 143  public class DepartureFetcher {
143              String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning";                      String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning";        
144                            
145              logger.fine("URI: " + uri);                  logger.fine("URI: " + uri);    
146              JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout);              JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() );
147              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
148                            
149              Document page = (Document) breaker.invoke(wrapper);              Document page = (Document) breaker.invoke(wrapper);
# Line 227  public class DepartureFetcher { Line 229  public class DepartureFetcher {
229              return departureBean;              return departureBean;
230          }          }
231                    
232            public DepartureBean lookupDeparturesMobileSite(String stationcode, TrainType traintype, boolean arrival) throws Exception {
233                    
234                    DepartureBean departureBean = new DepartureBean();
235                    
236                
237                    String typeString = getTypeStringWww(traintype);
238                String arrivalDeparture = (arrival==false) ? "afgang" : "ankomst";
239                
240                stationcode = URLEncoder.encode(stationcode,"ISO-8859-1");
241    
242                //String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning";      
243                String uri = "http://mobil.bane.dk/mobilStation.asp?artikelID=5332&stat_kode=" + stationcode + "&webprofil=" + typeString  +"&beskrivelse=&mode=ankomstafgang&ankomstafgang=" + arrivalDeparture + "&gemstation=&fuldvisning=1";
244                logger.fine("URI: " + uri);    
245                JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() );
246                CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
247                
248                Document page = (Document) breaker.invoke(wrapper);
249                
250                
251                Element content = page.getElementsByClass("contentDiv").get(0);
252                
253                
254                if (content != null) {
255                        Elements tableRows =  content.child(0).children();
256                        
257    
258                        
259                        for (Element currentRow : tableRows) {
260                            if (currentRow.tagName().equals("br") ) {
261                                    break;
262                            }
263                            
264                            
265                            Element link = currentRow.child(0);    
266                            System.out.println( currentRow.text() );;
267                            
268    
269                            String parts[] = currentRow.text().split(",");
270                    
271    
272                            DepartureEntry departure = new DepartureEntry();
273    
274    /*
275    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
276    */
277                            int offset = 0;
278                            
279                            String time = parts[offset++];
280                            if (time.equals(""))
281                                    time = "0:00"; //Bane.dk bug work-around
282                            departure.setTime(time);
283    
284                            int updated = 4; //does not exist on mobile
285                            departure.setUpdated(updated);
286    
287                            String trainNumber = "-"; //extractTrainNumberAzure(fields.get(2));
288                            /*if (traintype == TrainType.STOG) //If it is S-train we need to extract the trainNumber
289                                    trainNumber = trainNumber + " " + extractTrainNumberAzure(fields.get(2));*/
290                            departure.setTrainNumber(trainNumber);
291    
292                                    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
293                                            offset++;
294                                    }
295    
296                            String destination = parts[offset++];
297                            departure.setDestination(destination);
298    
299                            String origin = "-"; // fields.get(4).text(); does not exist on mobile
300                            departure.setOrigin(origin);
301    
302                            String location = ""; // fields.get(5).text(); does not exist on mobile
303                            departure.setLocation(location);
304    
305                            String status = ""; //fields.get(6).text().trim(); - extract from url
306                            departure.setStatus(status);
307    
308                            String note = ""; //extractNote( fields.get(7) ); - extract from url
309                            departure.setNote(note);
310    
311                            departure.setType(typeString);
312    
313                            departureBean.entries.add( departure );
314    
315                        }
316                } else {
317                    logger.warning("No departures found for station=" + stationcode + ", type=" + traintype);
318                }
319                
320                return departureBean;
321            }
322            
323                    
324                    
325          public static String cleanText(String input) {          public static String cleanText(String input) {
# Line 234  public class DepartureFetcher { Line 327  public class DepartureFetcher {
327                  return input.replace((char) 0xA0, (char)0x20).trim();                  return input.replace((char) 0xA0, (char)0x20).trim();
328          }          }
329                    
330            
331            // old www site is not available any more
332            @Deprecated
333          public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception {          public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception {
334                                    
335                  DepartureBean departureBean = new DepartureBean();                  DepartureBean departureBean = new DepartureBean();
# Line 247  public class DepartureFetcher { Line 343  public class DepartureFetcher {
343              logger.fine("URI:" + uri);              logger.fine("URI:" + uri);
344                            
345    
346              JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout);              JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() );
347              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
348                            
349              Element page = (Element) breaker.invoke(wrapper);              Element page = (Element) breaker.invoke(wrapper);

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

  ViewVC Help
Powered by ViewVC 1.1.20