/[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 1255 by torben, Mon Apr 4 10:56:44 2011 UTC revision 1355 by torben, Wed Apr 20 19:01:33 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;
19    import dk.thoerup.genericjavautils.TimeoutMap;
20  import dk.thoerup.traininfoservice.Statistics;  import dk.thoerup.traininfoservice.Statistics;
21    import dk.thoerup.traininfoservice.TraininfoSettings;
22  import dk.thoerup.traininfoservice.db.StationDAO;  import dk.thoerup.traininfoservice.db.StationDAO;
23    
24  public class DepartureFetcher {  public class DepartureFetcher {
# Line 38  public class DepartureFetcher { Line 40  public class DepartureFetcher {
40                    
41          StationDAO stationDao = new StationDAO();          StationDAO stationDao = new StationDAO();
42                    
43          private boolean useAzureSite;  
44          private int replyTimeout;          private TraininfoSettings settings;
45                    
46          public DepartureFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) {          public DepartureFetcher(TraininfoSettings settings) {
47                  this.replyTimeout = replyTimeout;                  this.settings = settings;
48                  useAzureSite = azureSite;                  cache = new TimeoutMap<String,DepartureBean>( settings.getCacheTimeout() );
                 cache = new TimeoutMap<String,DepartureBean>(cacheTimeout);  
49          }          }
50                    
51                    
# Line 101  public class DepartureFetcher { Line 102  public class DepartureFetcher {
102          }          }
103                    
104          public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception {          public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception {
105                  if (useAzureSite == true) {                  if ( settings.getUseAzureSite() == true) {
106                          return lookupDeparturesAzureSite(stationcode, type, arrival);                          return lookupDeparturesAzureSite(stationcode, type, arrival);
107                  } else {                  } else {
108                          return lookupDeparturesWwwSite(stationcode, type, arrival);                          return lookupDeparturesMobileSite(stationcode, type, arrival);
109                  }                  }
110          }          }
111                    
# Line 143  public class DepartureFetcher { Line 144  public class DepartureFetcher {
144              String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning";                      String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning";        
145                            
146              logger.fine("URI: " + uri);                  logger.fine("URI: " + uri);    
147              JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout);              JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() );
148              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
149                            
150              Document page = (Document) breaker.invoke(wrapper);              Document page = (Document) breaker.invoke(wrapper);
# Line 229  public class DepartureFetcher { Line 230  public class DepartureFetcher {
230              return departureBean;              return departureBean;
231          }          }
232                    
233            public DepartureBean lookupDeparturesMobileSite(String stationcode, TrainType traintype, boolean arrival) throws Exception {
234                    
235                    DepartureBean departureBean = new DepartureBean();
236                    
237                
238                    String typeString = getTypeStringWww(traintype);
239                String arrivalDeparture = (arrival==false) ? "afgang" : "ankomst";
240                
241                stationcode = URLEncoder.encode(stationcode,"ISO-8859-1");
242    
243                //String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning";      
244                String uri = "http://mobil.bane.dk/mobilStation.asp?artikelID=5332&stat_kode=" + stationcode + "&webprofil=" + typeString  +"&beskrivelse=&mode=ankomstafgang&ankomstafgang=" + arrivalDeparture + "&gemstation=&fuldvisning=1";
245                logger.fine("URI: " + uri);    
246                JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() );
247                CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
248                
249                Document page = (Document) breaker.invoke(wrapper);
250                
251                
252                Element content = page.getElementsByClass("contentDiv").get(0);
253                
254                
255                if (content != null) {
256                        Elements tableRows =  content.child(0).children();
257                        
258    
259                        
260                        for (Element currentRow : tableRows) {
261                            if (currentRow.tagName().equals("br") ) {
262                                    break;
263                            }
264                            
265                            
266                            String link = currentRow.child(0).attr("href");
267                            
268                                    logger.fine( currentRow.text() );
269                                    logger.fine("Href: " + link);
270                            
271    
272                            String parts[] = currentRow.text().split(",");
273                    
274    
275                            DepartureEntry departure = new DepartureEntry();
276                            
277                            //if we do these things upfront, then we are allowed to use continue statement when row contains no more data
278                            departure.setType(typeString);
279                            departureBean.entries.add( departure );
280    
281    /*
282    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
283    */
284                            int offset = 0;
285                            
286                            String time = parts[offset++];
287                            if (time.equals(""))
288                                    time = "0:00"; //Bane.dk bug work-around
289                            departure.setTime(time);
290    
291                            int updated = 4; //does not exist on mobile
292                            departure.setUpdated(updated);
293    
294                            String trainNumber = "-"; //extractTrainNumberAzure(fields.get(2));
295                            /*if (traintype == TrainType.STOG) //If it is S-train we need to extract the trainNumber
296                                    trainNumber = trainNumber + " " + extractTrainNumberAzure(fields.get(2));*/
297                            departure.setTrainNumber(trainNumber);
298    
299                                    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
300                                            offset++;
301                                    }
302    
303                            String destination = parts[offset++];
304                            departure.setDestination(destination);
305    
306                            String origin = "-"; // fields.get(4).text(); does not exist on mobile
307                            departure.setOrigin(origin);
308    
309                            String location = ""; // fields.get(5).text(); does not exist on mobile
310                            departure.setLocation(location);
311                            
312                            if (offset == parts.length) {
313                                    continue;
314                            }
315                            
316                            if (parts[offset].trim().equalsIgnoreCase("NB!")) {
317                                    offset++;
318                            }
319                            
320                            if (offset == parts.length) {
321                                    continue;
322                            }
323    
324                            String status = parts[offset++]; //fields.get(6).text().trim(); - extract from url
325                            departure.setStatus(status);
326    
327                            String note = ""; //extractNote( fields.get(7) ); - extract from url
328                            departure.setNote(note);
329    
330                        }
331                } else {
332                    logger.warning("No departures found for station=" + stationcode + ", type=" + traintype);
333                }
334                
335                return departureBean;
336            }
337            
338                    
339                    
340          public static String cleanText(String input) {          public static String cleanText(String input) {
# Line 236  public class DepartureFetcher { Line 342  public class DepartureFetcher {
342                  return input.replace((char) 0xA0, (char)0x20).trim();                  return input.replace((char) 0xA0, (char)0x20).trim();
343          }          }
344                    
345            
346            // old www site is not available any more
347            @Deprecated
348          public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception {          public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception {
349                                    
350                  DepartureBean departureBean = new DepartureBean();                  DepartureBean departureBean = new DepartureBean();
# Line 249  public class DepartureFetcher { Line 358  public class DepartureFetcher {
358              logger.fine("URI:" + uri);              logger.fine("URI:" + uri);
359                            
360    
361              JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout);              JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() );
362              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
363                            
364              Element page = (Element) breaker.invoke(wrapper);              Element page = (Element) breaker.invoke(wrapper);

Legend:
Removed from v.1255  
changed lines
  Added in v.1355

  ViewVC Help
Powered by ViewVC 1.1.20