/[projects]/android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TritinfoFetcher.java
ViewVC logotype

Contents of /android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TritinfoFetcher.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2077 - (show annotations) (download)
Sat Nov 23 10:43:25 2013 UTC (10 years, 5 months ago) by torben
File size: 2331 byte(s)
Refactor tritinfo fetcher to seperate class
1 package dk.thoerup.traininfoservice.banedk;
2
3 import java.net.URL;
4 import java.util.logging.Logger;
5
6 import org.jsoup.nodes.Element;
7 import org.jsoup.select.Elements;
8
9 import dk.thoerup.android.traininfo.common.DepartureBean;
10 import dk.thoerup.android.traininfo.common.DepartureEntry;
11 import dk.thoerup.android.traininfo.common.StationEntry;
12 import dk.thoerup.circuitbreaker.CircuitBreaker;
13 import dk.thoerup.circuitbreaker.CircuitBreakerManager;
14 import dk.thoerup.traininfoservice.TraininfoSettings;
15
16 public class TritinfoFetcher {
17
18 Logger logger = Logger.getLogger( TritinfoFetcher.class.getName() );
19 TraininfoSettings settings;
20
21 public TritinfoFetcher(TraininfoSettings settings) {
22 this.settings = settings;
23 }
24
25
26
27
28 public void injectTritinfoData(DepartureBean departureBean, StationEntry station) throws Exception {
29 String uri = "http://tritinfo.pallas.dk/webtavle?page=stationcontent&staid=" + station.getTritStation();
30 logger.fine("URI:" + uri);
31
32
33 JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() );
34 CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("tritinfo");
35
36 Element page = (Element) breaker.invoke(wrapper);
37
38 Element table = page.getElementsByClass("passages").get(0);
39
40 Elements trains = table.getElementsByClass("train");
41
42 for (int i=0; i<trains.size(); i++) {
43 Element train = trains.get(i);
44
45 //String trainType = train.getElementsByClass("trainType").get(0).text();
46 String trainNumber = train.getElementsByClass("trainNumber").get(0).text();
47
48 Elements trackElems = train.getElementsByClass("plannedTrack");
49 if (trackElems.size() == 0) {
50 trackElems = train.getElementsByClass("expectedTrack");
51 }
52 String track = trackElems.get(0).text().trim();
53 String trackType = train.getElementsByClass("trackType").get(0).text().trim();
54
55
56 String platform = track;
57 if ( !trackType.equals("")) {
58 platform += trackType;
59 }
60
61 for (DepartureEntry entry : departureBean.entries) {
62 String entryTrainId = entry.getTrainNumber().split(" ")[1];
63 if ( entryTrainId.equals(trainNumber)) {
64
65 entry.setPlatform(platform);
66 break;
67 }
68 }
69 }
70 }
71
72 }

  ViewVC Help
Powered by ViewVC 1.1.20