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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 829 - (show annotations) (download)
Thu Jun 10 22:26:09 2010 UTC (13 years, 11 months ago) by torben
File size: 9320 byte(s)
Add backend support for reading arrivals
1 package dk.thoerup.traininfoservice.banedk;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.logging.Logger;
8
9 import com.gargoylesoftware.htmlunit.BrowserVersion;
10 import com.gargoylesoftware.htmlunit.WebClient;
11 import com.gargoylesoftware.htmlunit.html.DomNodeList;
12 import com.gargoylesoftware.htmlunit.html.HtmlElement;
13 import com.gargoylesoftware.htmlunit.html.HtmlPage;
14
15 import dk.thoerup.circuitbreaker.CircuitBreaker;
16 import dk.thoerup.circuitbreaker.CircuitBreakerManager;
17 import dk.thoerup.traininfoservice.StationBean;
18 import dk.thoerup.traininfoservice.StationDAO;
19 import dk.thoerup.traininfoservice.Statistics;
20
21 public class DepartureFetcher {
22
23 Logger logger = Logger.getLogger(DepartureFetcher.class.getName());
24
25 Map<String, List<DepartureBean>> cache;
26
27 StationDAO stationDao = new StationDAO();
28
29 private boolean useTempSite;
30
31 public DepartureFetcher(boolean tempSite, int cacheTimeout) {
32 useTempSite = tempSite;
33 cache = new TimeoutMap<String,List<DepartureBean>>(cacheTimeout);
34 }
35
36
37
38
39 public List<DepartureBean> cachedLookupDepartures(int stationID, boolean arrival) throws Exception {
40 final String key = "" + stationID + ":" + arrival;
41
42 List<DepartureBean> list = cache.get(key);
43
44
45 if (list == null) {
46 list = lookupDepartures(stationID,arrival);
47 cache.put(key, list);
48 } else {
49 Statistics.getInstance().incrementDepartureCacheHits();
50 logger.info("Departure: Cache hit " + key); //remove before production
51 }
52 return list;
53 }
54
55
56 public List<DepartureBean> lookupDepartures(int stationID, boolean arrival) throws Exception {
57 List<DepartureBean> departureList = new ArrayList<DepartureBean>();
58
59 StationBean station = stationDao.getById(stationID);
60
61 if (station.getRegional() != null) {
62 List<DepartureBean> list = lookupDepartures(station.getRegional(), "FJRN", arrival);
63 departureList.addAll(list);
64 }
65
66 if (station.getStrain() != null) {
67 List<DepartureBean> list = lookupDepartures(station.getStrain(), "S2", arrival);
68 departureList.addAll(list);
69 }
70
71 Collections.sort( departureList );
72
73
74 return departureList;
75 }
76
77 public List<DepartureBean> lookupDepartures(String stationcode, String type, boolean arrival) throws Exception {
78 if (useTempSite == false) {
79 return lookupDeparturesNormalSite(stationcode, type, arrival);
80 } else {
81 return lookupDeparturesFromTemporarySite(stationcode, type);
82 }
83 }
84
85 public List<DepartureBean> lookupDeparturesNormalSite(String stationcode, String type, boolean arrival) throws Exception {
86
87 List<DepartureBean> departureList = new ArrayList<DepartureBean>();
88
89 final WebClient webClient = new WebClient( BrowserVersion.FIREFOX_3 );
90 webClient.setTimeout(2500);
91 webClient.setJavaScriptEnabled(false);
92
93 String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode;
94 BanedkInvocation wrapper = new BanedkInvocation(webClient, uri);
95 CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
96
97 HtmlPage page = (HtmlPage) breaker.invoke(wrapper);
98
99 String tableName = arrival == false ? "afgangtabel" : "ankomsttabel";
100
101 System.out.println(uri);
102 System.out.println(tableName);
103
104 HtmlElement table = page.getElementById(tableName);
105
106 if (table != null) {
107 DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr");
108
109 for (HtmlElement currentRow : tableRows) {
110 String rowClass = currentRow.getAttribute("class");
111 if (rowClass != null && rowClass.toLowerCase().contains("station") ) {
112 DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");
113
114 DepartureBean departure = new DepartureBean();
115
116 String time = fields.get(0).asText();
117 if (time.equals(""))
118 time = "0:00"; //Bane.dk bug work-around
119 departure.setTime(time);
120
121 int updated = extractUpdated( fields.get(1) );
122 departure.setUpdated(updated);
123
124 String trainNumber = fields.get(2).asText();
125 if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber
126 trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2));
127 departure.setTrainNumber(trainNumber);
128
129 String destination = fields.get(3).asText();
130 departure.setDestination(destination);
131
132 String origin = fields.get(4).asText();
133 departure.setOrigin(origin);
134
135 String location = fields.get(5).asText();
136 departure.setLocation(location);
137
138 String status = fields.get(6).asText();
139 departure.setStatus(status);
140
141 String note = extractNote( fields.get(7) );
142 departure.setNote(note);
143
144 departure.setType(type);
145
146 departureList.add(departure);
147 }
148 }
149 } else {
150 logger.warning("No departures found for station=" + stationcode + ", type=" + type);
151 }
152 webClient.closeAllWindows();
153
154 return departureList;
155 }
156
157 public List<DepartureBean> lookupDeparturesFromTemporarySite(String stationcode, String type) throws Exception {
158
159 List<DepartureBean> departureList = new ArrayList<DepartureBean>();
160
161 final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3);
162 webClient.setTimeout(2500);
163 webClient.setJavaScriptEnabled(false);
164
165
166 String uri = "http://bane.dk/lite/station.asp?w=" + type + "&s=" + stationcode;
167
168 BanedkInvocation wrapper = new BanedkInvocation(webClient, uri);
169 CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
170
171 HtmlPage page = (HtmlPage) breaker.invoke(wrapper);
172
173 HtmlElement table = page.getElementById("traf_afgang");
174
175 if (table != null) {
176 DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr");
177
178 boolean isFirst = true;
179
180 for (HtmlElement currentRow : tableRows) {
181 if (isFirst == true) { //skip table headers
182 isFirst = false;
183 continue;
184 }
185
186 DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");
187
188 DepartureBean departure = new DepartureBean();
189
190 String time = fields.get(0).asText().trim();
191
192 if (time.equals(""))
193 time = "0:00"; //Bane.dk bug work-around
194 departure.setTime(time);
195
196
197 String trainNumber = fields.get(1).asText();
198 departure.setTrainNumber(trainNumber);
199
200 String destination = fields.get(2).asText();
201 departure.setDestination(destination);
202
203 String origin = fields.get(3).asText();
204 departure.setOrigin(origin);
205
206 String status = fields.get(4).asText();
207 departure.setStatus(status);
208
209 String note = fields.get(5).asText();
210 departure.setNote(note);
211
212 departureList.add(departure);
213 }
214 } else {
215 logger.warning("No departures found for station=" + stationcode + ", type=" + type);
216 }
217 webClient.closeAllWindows();
218
219
220 return departureList;
221 }
222
223
224 private int extractUpdated(HtmlElement updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif"
225 int updated = -1;
226
227 DomNodeList<HtmlElement> updatedImgs = updatedTd.getElementsByTagName("img");
228 String updatedStr = updatedImgs.get(0).getAttribute("src");
229
230 if (updatedStr != null) {
231 for (int i=0; i<updatedStr.length(); i++) {
232 char c = updatedStr.charAt(i);
233 if ( Character.isDigit(c)) {
234 updated = Character.digit(c, 10);
235 break;
236 }
237 }
238 }
239 return updated;
240 }
241
242 private String extractNote(HtmlElement noteTd) {
243 String note = noteTd.asText().trim();
244
245 List<HtmlElement> elems = noteTd.getElementsByAttribute("span", "class", "bemtype");
246 if (elems.size() > 0 && note.charAt(note.length()-1) == 'i')
247 note = note.substring(0,note.length() -1 );
248
249 return note;
250 }
251
252 private String extractTrainNumber(HtmlElement trainTd) {
253 String number = "";
254 HtmlElement anchorElement = trainTd.getElementsByTagName("a").get(0);
255 String href = anchorElement.getAttribute("href");
256 String argstring = href.substring( href.indexOf('?') + 1);
257
258 String args[] = argstring.split("&");
259 for (String arg : args) {
260 String pair[] = arg.split("="); // Key=pair[0], Value=pair[1]
261
262 if (pair[0].equalsIgnoreCase("TogNr"))
263 number = pair[1];
264 }
265
266
267 return number;
268 }
269
270 //test
271 /*
272 public static void main(String args[]) throws Exception {
273 DepartureFetcher f = new DepartureFetcher();
274 List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN");
275 for(DepartureBean d : deps) {
276 System.out.println( d.getTime() + ";" + d.getUpdated() + ";" + d.getTrainNumber() + ";" +
277 d.getDestination() + ";" + d.getOrigin() + ";" + d.getLocation() + ";" + d.getStatus() + ";" + d.getNote() );
278 }
279
280 System.out.println("--------------------------");
281 }*/
282 }

  ViewVC Help
Powered by ViewVC 1.1.20