11 |
import org.jsoup.nodes.Element; |
import org.jsoup.nodes.Element; |
12 |
import org.jsoup.select.Elements; |
import org.jsoup.select.Elements; |
13 |
|
|
14 |
|
import dk.thoerup.android.traininfo.common.DepartureBean; |
15 |
|
import dk.thoerup.android.traininfo.common.DepartureEntry; |
16 |
|
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; |
|
import dk.thoerup.traininfoservice.StationBean; |
|
|
import dk.thoerup.traininfoservice.StationDAO; |
|
19 |
import dk.thoerup.traininfoservice.Statistics; |
import dk.thoerup.traininfoservice.Statistics; |
20 |
|
import dk.thoerup.traininfoservice.TraininfoSettings; |
21 |
|
import dk.thoerup.traininfoservice.db.StationDAO; |
22 |
|
|
23 |
public class DepartureFetcher { |
public class DepartureFetcher { |
24 |
|
|
27 |
REGIONAL |
REGIONAL |
28 |
} |
} |
29 |
|
|
30 |
|
enum FetchTrainType { |
31 |
|
STOG, |
32 |
|
REGIONAL, |
33 |
|
BOTH |
34 |
|
} |
35 |
|
|
36 |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
37 |
|
|
38 |
Map<String, DepartureBean> cache; |
Map<String, DepartureBean> cache; |
39 |
|
|
40 |
StationDAO stationDao = new StationDAO(); |
StationDAO stationDao = new StationDAO(); |
41 |
|
|
42 |
private boolean useAzureSite; |
|
43 |
private int replyTimeout; |
private TraininfoSettings settings; |
44 |
|
|
45 |
public DepartureFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) { |
public DepartureFetcher(TraininfoSettings settings) { |
46 |
this.replyTimeout = replyTimeout; |
this.settings = settings; |
47 |
useAzureSite = azureSite; |
cache = new TimeoutMap<String,DepartureBean>( settings.getCacheTimeout() ); |
|
cache = new TimeoutMap<String,DepartureBean>(cacheTimeout); |
|
48 |
} |
} |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
public DepartureBean cachedLookupDepartures(int stationID, boolean arrival) throws Exception { |
public DepartureBean cachedLookupDepartures(int stationID, boolean arrival, FetchTrainType type) throws Exception { |
54 |
final String key = "" + stationID + ":" + arrival; |
|
55 |
|
final String key = "" + stationID + ":" + arrival + ":" + type.toString(); |
56 |
|
|
57 |
DepartureBean departureBean = cache.get(key); |
DepartureBean departureBean = cache.get(key); |
58 |
|
|
59 |
|
|
60 |
if (departureBean == null) { |
if (departureBean == null) { |
61 |
departureBean = lookupDepartures(stationID,arrival); |
departureBean = lookupDepartures(stationID, arrival, type); |
62 |
cache.put(key, departureBean); |
cache.put(key, departureBean); |
63 |
} else { |
} else { |
64 |
Statistics.getInstance().incrementDepartureCacheHits(); |
Statistics.getInstance().incrementDepartureCacheHits(); |
68 |
} |
} |
69 |
|
|
70 |
|
|
71 |
public DepartureBean lookupDepartures(int stationID, boolean arrival) throws Exception { |
public DepartureBean lookupDepartures(int stationID, boolean arrival, FetchTrainType type) throws Exception { |
72 |
|
|
73 |
DepartureBean departureBean = new DepartureBean(); |
DepartureBean departureBean = new DepartureBean(); |
74 |
|
|
75 |
StationBean station = stationDao.getById(stationID); |
StationEntry station = stationDao.getById(stationID); |
76 |
|
|
77 |
departureBean.stationName = station.getName(); |
departureBean.stationName = station.getName(); |
78 |
|
|
79 |
if (station.getRegional() != null) { |
if (station.getRegional() != null && (type == FetchTrainType.REGIONAL||type == FetchTrainType.BOTH) ) { |
80 |
DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival); |
DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival); |
81 |
departureBean.departureEntries.addAll( tempBean.departureEntries ); |
departureBean.entries.addAll( tempBean.entries ); |
82 |
departureBean.notifications.addAll(tempBean.notifications); |
departureBean.notifications.addAll(tempBean.notifications); |
83 |
} |
} |
84 |
|
|
85 |
if (station.getStrain() != null) { |
if (station.getStrain() != null && (type == FetchTrainType.STOG||type == FetchTrainType.BOTH)) { |
86 |
DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival); |
DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival); |
87 |
departureBean.departureEntries.addAll( tempBean.departureEntries ); |
departureBean.entries.addAll( tempBean.entries ); |
88 |
departureBean.notifications.addAll(tempBean.notifications); |
departureBean.notifications.addAll(tempBean.notifications); |
89 |
} |
} |
90 |
|
|
91 |
if (departureBean.departureEntries.size() == 0) { |
if (departureBean.entries.size() == 0) { |
92 |
logger.info("No departures found for station " + stationID); |
logger.info("No departures found for station " + stationID); |
93 |
} |
} |
94 |
|
|
95 |
Collections.sort( departureBean.departureEntries ); |
if (type == FetchTrainType.BOTH) { //if we have both S-tog and regional order by departure/arrival time |
96 |
|
Collections.sort( departureBean.entries ); |
97 |
|
} |
98 |
|
|
99 |
|
|
100 |
return departureBean; |
return departureBean; |
101 |
} |
} |
102 |
|
|
103 |
public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception { |
public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception { |
104 |
if (useAzureSite == true) { |
if ( settings.getUseAzureSite() == true) { |
105 |
return lookupDeparturesAzureSite(stationcode, type, arrival); |
return lookupDeparturesAzureSite(stationcode, type, arrival); |
106 |
} else { |
} else { |
107 |
return lookupDeparturesWwwSite(stationcode, type, arrival); |
return lookupDeparturesWwwSite(stationcode, type, arrival); |
142 |
|
|
143 |
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
144 |
|
|
145 |
//logger.info("URI: " + uri); |
logger.fine("URI: " + uri); |
146 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() ); |
147 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
148 |
|
|
149 |
Document page = (Document) breaker.invoke(wrapper); |
Document page = (Document) breaker.invoke(wrapper); |
154 |
if (table != null) { |
if (table != null) { |
155 |
Elements tableRows = table.getElementsByTag("tr"); |
Elements tableRows = table.getElementsByTag("tr"); |
156 |
|
|
157 |
boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0); |
//boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0); |
158 |
boolean passedTidsstreg = false; |
//boolean passedTidsstreg = false; |
159 |
|
|
160 |
for (Element currentRow : tableRows) { |
for (Element currentRow : tableRows) { |
161 |
String rowClass = currentRow.attr("class"); |
String rowClass = currentRow.attr("class"); |
162 |
|
/* |
163 |
if (tidsstregExists == true && passedTidsstreg == false) { |
if (tidsstregExists == true && passedTidsstreg == false) { |
164 |
if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) { |
if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) { |
165 |
passedTidsstreg = true; |
passedTidsstreg = true; |
166 |
} else { |
} else { |
167 |
continue; |
continue; |
168 |
} |
} |
169 |
} |
}*/ |
170 |
|
|
171 |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
172 |
|
|
204 |
|
|
205 |
departure.setType(typeString); |
departure.setType(typeString); |
206 |
|
|
207 |
departureBean.departureEntries.add( departure ); |
departureBean.entries.add( departure ); |
208 |
} |
} |
209 |
} |
} |
210 |
} else { |
} else { |
242 |
|
|
243 |
String type = getTypeStringWww(trainType); |
String type = getTypeStringWww(trainType); |
244 |
|
|
245 |
|
stationcode = URLEncoder.encode(stationcode, "ISO-8859-1"); |
246 |
|
|
247 |
|
|
248 |
String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode; |
String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode; |
249 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
logger.fine("URI:" + uri); |
250 |
|
|
251 |
|
|
252 |
|
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() ); |
253 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
254 |
|
|
255 |
Element page = (Element) breaker.invoke(wrapper); |
Element page = (Element) breaker.invoke(wrapper); |
257 |
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel"; |
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel"; |
258 |
Element table = page.getElementById(tableName); |
Element table = page.getElementById(tableName); |
259 |
|
|
260 |
|
|
261 |
|
|
262 |
if (table != null) { |
if (table != null) { |
263 |
Elements tableRows = table.getElementsByTag("tr"); |
Elements tableRows = table.getElementsByTag("tr"); |
264 |
|
|
265 |
|
//boolean passedTidsstreg = false; |
266 |
|
//boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0); |
267 |
|
|
268 |
for (Element currentRow : tableRows) { |
for (Element currentRow : tableRows) { |
269 |
String rowClass = currentRow.attr("class"); |
String rowClass = currentRow.attr("class"); |
270 |
|
/* |
271 |
|
if (tidsstregExists == true && passedTidsstreg == false) { |
272 |
|
if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) { |
273 |
|
passedTidsstreg = true; |
274 |
|
} else { |
275 |
|
continue; |
276 |
|
} |
277 |
|
}*/ |
278 |
|
|
279 |
|
|
280 |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
281 |
Elements fields = currentRow.getElementsByTag("td"); |
Elements fields = currentRow.getElementsByTag("td"); |
282 |
|
|
314 |
|
|
315 |
departure.setType(type); |
departure.setType(type); |
316 |
|
|
317 |
departureBean.departureEntries.add(departure); |
departureBean.entries.add(departure); |
318 |
|
|
319 |
|
|
320 |
} |
} |