30 |
|
|
31 |
StationDAO stationDao = new StationDAO(); |
StationDAO stationDao = new StationDAO(); |
32 |
|
|
33 |
private boolean useTempSite; |
private boolean useAzureSite; |
34 |
private int replyTimeout; |
private int replyTimeout; |
35 |
|
|
36 |
public DepartureFetcher(boolean tempSite, int cacheTimeout, int replyTimeout) { |
public DepartureFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) { |
37 |
this.replyTimeout = replyTimeout; |
this.replyTimeout = replyTimeout; |
38 |
useTempSite = tempSite; |
useAzureSite = azureSite; |
39 |
cache = new TimeoutMap<String,DepartureBean>(cacheTimeout); |
cache = new TimeoutMap<String,DepartureBean>(cacheTimeout); |
40 |
} |
} |
41 |
|
|
86 |
} |
} |
87 |
|
|
88 |
public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception { |
public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception { |
89 |
if (useTempSite == false) { |
if (useAzureSite == true) { |
90 |
return lookupDeparturesNormalSite(stationcode, type, arrival); |
return lookupDeparturesAzureSite(stationcode, type, arrival); |
91 |
} else { |
} else { |
92 |
//return lookupDeparturesFromTemporarySite(stationcode, type); |
return lookupDeparturesWwwSite(stationcode, type, arrival); |
|
//TODO: find out what to to if they ever put a temp site up on trafikinfo.bane.dk |
|
|
return null; |
|
93 |
} |
} |
94 |
} |
} |
95 |
|
|
96 |
private String getTypeString(TrainType type) { |
private String getTypeStringAzure(TrainType type) { |
97 |
switch (type) { |
switch (type) { |
98 |
case STOG: |
case STOG: |
99 |
return "S-Tog"; |
return "S-Tog"; |
104 |
} |
} |
105 |
} |
} |
106 |
|
|
107 |
public DepartureBean lookupDeparturesNormalSite(String stationcode, TrainType type, boolean arrival) throws Exception { |
private String getTypeStringWww(TrainType type) { |
108 |
|
switch (type) { |
109 |
|
case STOG: |
110 |
|
return "S2"; |
111 |
|
case REGIONAL: |
112 |
|
return "FJRN"; |
113 |
|
default: |
114 |
|
return ""; //Can not happen |
115 |
|
} |
116 |
|
} |
117 |
|
|
118 |
|
public DepartureBean lookupDeparturesAzureSite(String stationcode, TrainType type, boolean arrival) throws Exception { |
119 |
|
|
120 |
DepartureBean departureBean = new DepartureBean(); |
DepartureBean departureBean = new DepartureBean(); |
121 |
|
|
122 |
|
|
123 |
String typeString = getTypeString(type); |
String typeString = getTypeStringAzure(type); |
124 |
String arrivalDeparture = (arrival==false) ? "Afgang" : "Ankomst"; |
String arrivalDeparture = (arrival==false) ? "Afgang" : "Ankomst"; |
125 |
|
|
126 |
stationcode = URLEncoder.encode(stationcode,"ISO-8859-1"); |
stationcode = URLEncoder.encode(stationcode,"ISO-8859-1"); |
|
//String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode; |
|
|
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
|
127 |
|
|
128 |
|
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
129 |
|
|
130 |
//logger.info("URI: " + uri); |
//logger.info("URI: " + uri); |
131 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
214 |
return departureBean; |
return departureBean; |
215 |
} |
} |
216 |
|
|
217 |
/* |
public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception { |
|
@Deprecated |
|
|
public List<DepartureBean> lookupDeparturesFromTemporarySite(String stationcode, String type) throws Exception { |
|
218 |
|
|
219 |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
DepartureBean departureBean = new DepartureBean(); |
220 |
|
|
221 |
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3); |
String type = getTypeStringWww(trainType); |
222 |
webClient.setTimeout(2500); |
|
223 |
webClient.setJavaScriptEnabled(false); |
|
224 |
|
String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode; |
225 |
|
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
|
String uri = "http://bane.dk/lite/station.asp?w=" + type + "&s=" + stationcode; |
|
|
|
|
|
HtmlunitInvocation wrapper = new HtmlunitInvocation(webClient, uri); |
|
226 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
227 |
|
|
228 |
HtmlPage page = (HtmlPage) breaker.invoke(wrapper); |
Element page = (Element) breaker.invoke(wrapper); |
229 |
|
|
230 |
HtmlElement table = page.getElementById("traf_afgang"); |
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel"; |
231 |
|
Element table = page.getElementById(tableName); |
232 |
|
|
233 |
if (table != null) { |
if (table != null) { |
234 |
DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr"); |
Elements tableRows = table.getElementsByTag("tr"); |
|
|
|
|
boolean isFirst = true; |
|
235 |
|
|
236 |
for (HtmlElement currentRow : tableRows) { |
for (Element currentRow : tableRows) { |
237 |
if (isFirst == true) { //skip table headers |
String rowClass = currentRow.attr("class"); |
238 |
isFirst = false; |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
239 |
continue; |
Elements fields = currentRow.getElementsByTag("td"); |
240 |
} |
|
241 |
|
DepartureEntry departure = new DepartureEntry(); |
242 |
DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td"); |
|
|
|
|
|
DepartureBean departure = new DepartureBean(); |
|
|
|
|
|
String time = fields.get(0).asText().trim(); |
|
|
|
|
|
if (time.equals("")) |
|
|
time = "0:00"; //Bane.dk bug work-around |
|
|
departure.setTime(time); |
|
|
|
|
|
|
|
|
String trainNumber = fields.get(1).asText(); |
|
|
departure.setTrainNumber(trainNumber); |
|
|
|
|
|
String destination = fields.get(2).asText(); |
|
|
departure.setDestination(destination); |
|
|
|
|
|
String origin = fields.get(3).asText(); |
|
|
departure.setOrigin(origin); |
|
|
|
|
|
String status = fields.get(4).asText(); |
|
|
departure.setStatus(status); |
|
|
|
|
|
String note = fields.get(5).asText(); |
|
|
departure.setNote(note); |
|
243 |
|
|
244 |
departureList.add(departure); |
|
245 |
|
String time = fields.get(0).getAllElements().get(2).text(); |
246 |
|
if (time.equals("")) |
247 |
|
time = "0:00"; //Bane.dk bug work-around |
248 |
|
departure.setTime(time); |
249 |
|
|
250 |
|
int updated = extractUpdated( fields.get(1) ); |
251 |
|
departure.setUpdated(updated); |
252 |
|
|
253 |
|
String trainNumber = fields.get(2).text(); |
254 |
|
if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber |
255 |
|
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
256 |
|
departure.setTrainNumber(trainNumber); |
257 |
|
|
258 |
|
String destination = fields.get(3).text(); |
259 |
|
departure.setDestination(destination); |
260 |
|
|
261 |
|
String origin = fields.get(4).text(); |
262 |
|
departure.setOrigin(origin); |
263 |
|
|
264 |
|
String location = fields.get(5).text(); |
265 |
|
departure.setLocation(location); |
266 |
|
|
267 |
|
String status = fields.get(6).text().trim(); |
268 |
|
departure.setStatus(status); |
269 |
|
|
270 |
|
String note = extractNote( fields.get(7) ); |
271 |
|
departure.setNote(note); |
272 |
|
|
273 |
|
departure.setType(type); |
274 |
|
|
275 |
|
departureBean.departureEntries.add(departure); |
276 |
|
|
277 |
|
|
278 |
|
} |
279 |
} |
} |
280 |
} else { |
} else { |
281 |
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
282 |
} |
} |
|
webClient.closeAllWindows(); |
|
283 |
|
|
284 |
|
|
285 |
return departureList; |
return departureBean; |
286 |
}*/ |
} |
287 |
|
|
288 |
|
|
289 |
private int extractUpdated(Element updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif" |
private int extractUpdated(Element updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif" |
290 |
int updated = -1; |
int updated = -1; |