14 |
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
15 |
import com.gargoylesoftware.htmlunit.html.HtmlPage; |
import com.gargoylesoftware.htmlunit.html.HtmlPage; |
16 |
|
|
17 |
import dk.thoerup.curcuitbreaker.CircuitBreaker; |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
18 |
import dk.thoerup.curcuitbreaker.CircuitBreakerManager; |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
19 |
import dk.thoerup.traininfoservice.DBConnection; |
import dk.thoerup.traininfoservice.DBConnection; |
20 |
|
|
21 |
public class DepartureFetcher { |
public class DepartureFetcher { |
24 |
|
|
25 |
Map<Integer, List<DepartureBean>> cache = new TimeoutMap<Integer,List<DepartureBean>>(120 * 1000); |
Map<Integer, List<DepartureBean>> cache = new TimeoutMap<Integer,List<DepartureBean>>(120 * 1000); |
26 |
|
|
27 |
|
private boolean useTempSite; |
28 |
|
|
29 |
|
public DepartureFetcher(boolean tempSite) { |
30 |
|
useTempSite = tempSite; |
31 |
|
} |
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
public List<DepartureBean> cachedLookupDepartures(int stationID) throws Exception { |
public List<DepartureBean> cachedLookupDepartures(int stationID) throws Exception { |
85 |
} |
} |
86 |
|
|
87 |
public List<DepartureBean> lookupDepartures(String stationcode, String type) throws Exception { |
public List<DepartureBean> lookupDepartures(String stationcode, String type) throws Exception { |
88 |
|
if (useTempSite == false) { |
89 |
|
return lookupDeparturesNormalSite(stationcode, type); |
90 |
|
} else { |
91 |
|
return lookupDeparturesFromTemporarySite(stationcode, type); |
92 |
|
} |
93 |
|
} |
94 |
|
|
95 |
|
public List<DepartureBean> lookupDeparturesNormalSite(String stationcode, String type) throws Exception { |
96 |
|
|
97 |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
98 |
|
|
156 |
return departureList; |
return departureList; |
157 |
} |
} |
158 |
|
|
159 |
|
public List<DepartureBean> lookupDeparturesFromTemporarySite(String stationcode, String type) throws Exception { |
160 |
|
|
161 |
|
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
162 |
|
|
163 |
|
final WebClient webClient = new WebClient(); |
164 |
|
webClient.setTimeout(2500); |
165 |
|
webClient.setJavaScriptEnabled(false); |
166 |
|
|
167 |
|
|
168 |
|
String uri = "http://bane.dk/lite/station.asp?w=" + type + "&s=" + stationcode; |
169 |
|
|
170 |
|
BanedkInvocation wrapper = new BanedkInvocation(webClient, uri); |
171 |
|
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
172 |
|
|
173 |
|
HtmlPage page = (HtmlPage) breaker.invoke(wrapper); |
174 |
|
|
175 |
|
HtmlElement table = page.getElementById("traf_afgang"); |
176 |
|
|
177 |
|
if (table != null) { |
178 |
|
DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr"); |
179 |
|
|
180 |
|
boolean isFirst = true; |
181 |
|
|
182 |
|
for (HtmlElement currentRow : tableRows) { |
183 |
|
if (isFirst == true) { //skip table headers |
184 |
|
isFirst = false; |
185 |
|
continue; |
186 |
|
} |
187 |
|
|
188 |
|
DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td"); |
189 |
|
|
190 |
|
DepartureBean departure = new DepartureBean(); |
191 |
|
|
192 |
|
String time = fields.get(0).asText().trim(); |
193 |
|
|
194 |
|
if (time.equals("")) |
195 |
|
time = "0:00"; //Bane.dk bug work-around |
196 |
|
departure.setTime(time); |
197 |
|
|
198 |
|
|
199 |
|
String trainNumber = fields.get(1).asText(); |
200 |
|
departure.setTrainNumber(trainNumber); |
201 |
|
|
202 |
|
String destination = fields.get(2).asText(); |
203 |
|
departure.setDestination(destination); |
204 |
|
|
205 |
|
String origin = fields.get(3).asText(); |
206 |
|
departure.setOrigin(origin); |
207 |
|
|
208 |
|
String status = fields.get(4).asText(); |
209 |
|
departure.setStatus(status); |
210 |
|
|
211 |
|
String note = fields.get(5).asText(); |
212 |
|
departure.setNote(note); |
213 |
|
|
214 |
|
departureList.add(departure); |
215 |
|
} |
216 |
|
} else { |
217 |
|
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
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" |
private int extractUpdated(HtmlElement updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif" |
225 |
int updated = -1; |
int updated = -1; |
226 |
|
|
269 |
} |
} |
270 |
|
|
271 |
//test |
//test |
272 |
|
/* |
273 |
public static void main(String args[]) throws Exception { |
public static void main(String args[]) throws Exception { |
274 |
DepartureFetcher f = new DepartureFetcher(); |
DepartureFetcher f = new DepartureFetcher(); |
275 |
List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN"); |
List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN"); |
279 |
} |
} |
280 |
|
|
281 |
System.out.println("--------------------------"); |
System.out.println("--------------------------"); |
282 |
} |
}*/ |
283 |
} |
} |