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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20