1 |
package dk.thoerup.traininfoservice.banedk; |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
|
3 |
import java.sql.Connection; |
|
|
import java.sql.ResultSet; |
|
|
import java.sql.Statement; |
|
|
import java.util.ArrayList; |
|
4 |
import java.util.Collections; |
import java.util.Collections; |
5 |
import java.util.List; |
import java.util.List; |
6 |
import java.util.Map; |
import java.util.Map; |
7 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
8 |
|
|
9 |
|
import com.gargoylesoftware.htmlunit.BrowserVersion; |
10 |
import com.gargoylesoftware.htmlunit.WebClient; |
import com.gargoylesoftware.htmlunit.WebClient; |
11 |
import com.gargoylesoftware.htmlunit.html.DomNodeList; |
import com.gargoylesoftware.htmlunit.html.DomNodeList; |
12 |
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
14 |
|
|
15 |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
16 |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
17 |
import dk.thoerup.traininfoservice.DBConnection; |
import dk.thoerup.traininfoservice.StationBean; |
18 |
|
import dk.thoerup.traininfoservice.StationDAO; |
19 |
|
import dk.thoerup.traininfoservice.Statistics; |
20 |
|
|
21 |
public class DepartureFetcher { |
public class DepartureFetcher { |
22 |
|
|
23 |
|
enum TrainType{ |
24 |
|
STOG, |
25 |
|
REGIONAL |
26 |
|
} |
27 |
|
|
28 |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
29 |
|
|
30 |
Map<Integer, List<DepartureBean>> cache = new TimeoutMap<Integer,List<DepartureBean>>(120 * 1000); |
Map<String, DepartureBean> cache; |
31 |
|
|
32 |
|
StationDAO stationDao = new StationDAO(); |
33 |
|
|
34 |
private boolean useTempSite; |
private boolean useTempSite; |
35 |
|
|
36 |
public DepartureFetcher(boolean tempSite) { |
public DepartureFetcher(boolean tempSite, int cacheTimeout) { |
37 |
useTempSite = tempSite; |
useTempSite = tempSite; |
38 |
|
cache = new TimeoutMap<String,DepartureBean>(cacheTimeout); |
39 |
} |
} |
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
public List<DepartureBean> cachedLookupDepartures(int stationID) throws Exception { |
public DepartureBean cachedLookupDepartures(int stationID, boolean arrival) throws Exception { |
45 |
|
final String key = "" + stationID + ":" + arrival; |
46 |
|
/* |
47 |
|
DepartureBean departureBean = cache.get(key); |
48 |
|
|
|
List<DepartureBean> list = cache.get(stationID); |
|
49 |
|
|
50 |
if (list == null) { |
if (departureBean == null) { |
51 |
list = lookupDepartures(stationID); |
departureBean = lookupDepartures(stationID,arrival); |
52 |
cache.put(stationID, list); |
cache.put(key, departureBean); |
53 |
} else { |
} else { |
54 |
logger.info("Departure: Cache hit " + stationID); //remove before production |
Statistics.getInstance().incrementDepartureCacheHits(); |
55 |
|
logger.info("Departure: Cache hit " + key); //remove before production |
56 |
} |
} |
57 |
return list; |
return departureBean;*/ |
58 |
|
|
59 |
|
return lookupDepartures(stationID,arrival); |
60 |
} |
} |
61 |
|
|
62 |
|
|
63 |
public List<DepartureBean> lookupDepartures(int stationID) throws Exception { |
public DepartureBean lookupDepartures(int stationID, boolean arrival) throws Exception { |
|
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
|
64 |
|
|
65 |
Connection conn = null; |
DepartureBean departureBean = new DepartureBean(); |
66 |
try |
|
67 |
{ |
StationBean station = stationDao.getById(stationID); |
68 |
conn = DBConnection.getConnection(); |
|
69 |
|
if (station.getRegional() != null) { |
70 |
String SQL = "SELECT stationcode_fjrn, stationcode_stog FROM trainstations WHERE id=" + stationID; |
DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival); |
71 |
Statement stmt = conn.createStatement(); |
departureBean.departureEntries.addAll( tempBean.departureEntries ); |
72 |
ResultSet rs = stmt.executeQuery(SQL); |
departureBean.notifications.addAll(tempBean.notifications); |
|
|
|
|
if (rs.next()) { |
|
|
String code = rs.getString( 1 ); |
|
|
if (! rs.wasNull() ) { |
|
|
List<DepartureBean> list = lookupDepartures(code, "FJRN"); |
|
|
departureList.addAll(list); |
|
|
} |
|
|
|
|
|
code = rs.getString(2); |
|
|
if (! rs.wasNull() ) { |
|
|
List<DepartureBean> list = lookupDepartures(code, "S2"); |
|
|
departureList.addAll(list); |
|
|
} |
|
|
Collections.sort( departureList ); |
|
|
|
|
|
} |
|
|
|
|
|
} finally { |
|
|
if (conn != null && !conn.isClosed() ) { |
|
|
conn.close(); |
|
|
} |
|
73 |
} |
} |
74 |
|
|
75 |
return departureList; |
if (station.getStrain() != null) { |
76 |
|
DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival); |
77 |
|
departureBean.departureEntries.addAll( tempBean.departureEntries ); |
78 |
|
departureBean.notifications.addAll(tempBean.notifications); |
79 |
|
} |
80 |
|
|
81 |
|
Collections.sort( departureBean.departureEntries ); |
82 |
|
|
83 |
|
|
84 |
|
return departureBean; |
85 |
} |
} |
86 |
|
|
87 |
public List<DepartureBean> lookupDepartures(String stationcode, String type) throws Exception { |
public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception { |
88 |
if (useTempSite == false) { |
if (useTempSite == false) { |
89 |
return lookupDeparturesNormalSite(stationcode, type); |
return lookupDeparturesNormalSite(stationcode, type, arrival); |
90 |
} else { |
} else { |
91 |
return lookupDeparturesFromTemporarySite(stationcode, type); |
//return lookupDeparturesFromTemporarySite(stationcode, type); |
92 |
|
//TODO: find out what to to if they ever put a temp site up on trafikinfo.bane.dk |
93 |
|
return null; |
94 |
|
} |
95 |
|
} |
96 |
|
|
97 |
|
private String getTypeString(TrainType type) { |
98 |
|
switch (type) { |
99 |
|
case STOG: |
100 |
|
return "S-Tog"; |
101 |
|
case REGIONAL: |
102 |
|
return "Fjerntog"; |
103 |
|
default: |
104 |
|
return ""; //Can not happen |
105 |
} |
} |
106 |
} |
} |
107 |
|
|
108 |
public List<DepartureBean> lookupDeparturesNormalSite(String stationcode, String type) throws Exception { |
public DepartureBean lookupDeparturesNormalSite(String stationcode, TrainType type, boolean arrival) throws Exception { |
109 |
|
|
110 |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
DepartureBean departureBean = new DepartureBean(); |
111 |
|
|
112 |
final WebClient webClient = new WebClient(); |
final WebClient webClient = new WebClient( BrowserVersion.FIREFOX_3 ); |
113 |
webClient.setTimeout(2500); |
webClient.setTimeout(2500); |
114 |
webClient.setJavaScriptEnabled(false); |
webClient.setJavaScriptEnabled(false); |
115 |
|
|
116 |
|
|
117 |
|
String typeString = getTypeString(type); |
118 |
|
String arrivalDeparture = (arrival==false) ? "Afgang" : "Ankomst"; |
119 |
|
|
120 |
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; |
121 |
BanedkInvocation wrapper = new BanedkInvocation(webClient, uri); |
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
122 |
|
|
123 |
|
//logger.info("URI: " + uri); |
124 |
|
HtmlunitInvocation wrapper = new HtmlunitInvocation(webClient, uri); |
125 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
126 |
|
|
127 |
HtmlPage page = (HtmlPage) breaker.invoke(wrapper); |
HtmlPage page = (HtmlPage) breaker.invoke(wrapper); |
128 |
|
|
129 |
HtmlElement table = page.getElementById("afgangtabel"); |
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel"; |
130 |
|
HtmlElement table = page.getElementById(tableName); |
131 |
|
|
132 |
if (table != null) { |
if (table != null) { |
133 |
DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr"); |
DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr"); |
137 |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
138 |
DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td"); |
DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td"); |
139 |
|
|
140 |
DepartureBean departure = new DepartureBean(); |
DepartureEntry departure = new DepartureEntry(); |
141 |
|
|
142 |
String time = fields.get(0).asText(); |
String time = fields.get(0).asText(); |
143 |
if (time.equals("")) |
if (time.equals("")) |
148 |
departure.setUpdated(updated); |
departure.setUpdated(updated); |
149 |
|
|
150 |
String trainNumber = fields.get(2).asText(); |
String trainNumber = fields.get(2).asText(); |
151 |
if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber |
if (type == TrainType.STOG) //If it is S-train we need to extract the trainNumber |
152 |
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
153 |
departure.setTrainNumber(trainNumber); |
departure.setTrainNumber(trainNumber); |
154 |
|
|
161 |
String location = fields.get(5).asText(); |
String location = fields.get(5).asText(); |
162 |
departure.setLocation(location); |
departure.setLocation(location); |
163 |
|
|
164 |
String status = fields.get(6).asText(); |
String status = fields.get(6).asText().trim(); |
165 |
departure.setStatus(status); |
departure.setStatus(status); |
166 |
|
|
167 |
String note = extractNote( fields.get(7) ); |
String note = extractNote( fields.get(7) ); |
168 |
departure.setNote(note); |
departure.setNote(note); |
169 |
|
|
170 |
departureList.add(departure); |
departure.setType(typeString); |
171 |
|
|
172 |
|
departureBean.departureEntries.add( departure ); |
173 |
} |
} |
174 |
} |
} |
175 |
} else { |
} else { |
176 |
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
177 |
} |
} |
178 |
|
|
179 |
return departureList; |
HtmlElement notifDiv = page.getElementById("station_planlagte_text"); |
180 |
|
if (notifDiv != null) { |
181 |
|
|
182 |
|
DomNodeList<HtmlElement> tables = notifDiv.getElementsByTagName("table"); |
183 |
|
for (HtmlElement tab : tables) { |
184 |
|
|
185 |
|
DomNodeList<HtmlElement> anchors = tab.getElementsByTagName("a"); |
186 |
|
if (anchors.size() == 2) { |
187 |
|
departureBean.notifications.add( anchors.get(1).getTextContent() ); |
188 |
|
} |
189 |
|
} |
190 |
|
|
191 |
|
} |
192 |
|
|
193 |
|
|
194 |
|
webClient.closeAllWindows(); |
195 |
|
|
196 |
|
return departureBean; |
197 |
} |
} |
198 |
|
|
199 |
|
/* |
200 |
|
@Deprecated |
201 |
public List<DepartureBean> lookupDeparturesFromTemporarySite(String stationcode, String type) throws Exception { |
public List<DepartureBean> lookupDeparturesFromTemporarySite(String stationcode, String type) throws Exception { |
202 |
|
|
203 |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
204 |
|
|
205 |
final WebClient webClient = new WebClient(); |
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3); |
206 |
webClient.setTimeout(2500); |
webClient.setTimeout(2500); |
207 |
webClient.setJavaScriptEnabled(false); |
webClient.setJavaScriptEnabled(false); |
208 |
|
|
209 |
|
|
210 |
String uri = "http://bane.dk/lite/station.asp?w=" + type + "&s=" + stationcode; |
String uri = "http://bane.dk/lite/station.asp?w=" + type + "&s=" + stationcode; |
211 |
|
|
212 |
BanedkInvocation wrapper = new BanedkInvocation(webClient, uri); |
HtmlunitInvocation wrapper = new HtmlunitInvocation(webClient, uri); |
213 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
214 |
|
|
215 |
HtmlPage page = (HtmlPage) breaker.invoke(wrapper); |
HtmlPage page = (HtmlPage) breaker.invoke(wrapper); |
232 |
DepartureBean departure = new DepartureBean(); |
DepartureBean departure = new DepartureBean(); |
233 |
|
|
234 |
String time = fields.get(0).asText().trim(); |
String time = fields.get(0).asText().trim(); |
235 |
logger.info("time:" + time); |
|
236 |
if (time.equals("")) |
if (time.equals("")) |
237 |
time = "0:00"; //Bane.dk bug work-around |
time = "0:00"; //Bane.dk bug work-around |
238 |
departure.setTime(time); |
departure.setTime(time); |
258 |
} else { |
} else { |
259 |
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
260 |
} |
} |
261 |
|
webClient.closeAllWindows(); |
262 |
|
|
263 |
|
|
264 |
return departureList; |
return departureList; |
265 |
} |
}*/ |
266 |
|
|
267 |
|
|
268 |
private int extractUpdated(HtmlElement updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif" |
private int extractUpdated(HtmlElement updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif" |
294 |
} |
} |
295 |
|
|
296 |
private String extractTrainNumber(HtmlElement trainTd) { |
private String extractTrainNumber(HtmlElement trainTd) { |
|
String number = ""; |
|
297 |
HtmlElement anchorElement = trainTd.getElementsByTagName("a").get(0); |
HtmlElement anchorElement = trainTd.getElementsByTagName("a").get(0); |
298 |
String href = anchorElement.getAttribute("href"); |
String href = anchorElement.getAttribute("href"); |
|
String argstring = href.substring( href.indexOf('?') + 1); |
|
|
|
|
|
String args[] = argstring.split("&"); |
|
|
for (String arg : args) { |
|
|
String pair[] = arg.split("="); // Key=pair[0], Value=pair[1] |
|
|
|
|
|
if (pair[0].equalsIgnoreCase("TogNr")) |
|
|
number = pair[1]; |
|
|
} |
|
|
|
|
299 |
|
|
300 |
|
int pos = href.lastIndexOf('/'); |
301 |
|
String number = href.substring(pos+1); |
302 |
|
|
303 |
return number; |
return number; |
304 |
} |
} |