4 |
|
|
5 |
import java.net.URL; |
import java.net.URL; |
6 |
import java.sql.SQLException; |
import java.sql.SQLException; |
|
import java.util.ArrayList; |
|
|
import java.util.List; |
|
7 |
import java.util.Map; |
import java.util.Map; |
8 |
import java.util.logging.Level; |
import java.util.logging.Level; |
9 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
12 |
import org.jsoup.nodes.Element; |
import org.jsoup.nodes.Element; |
13 |
import org.jsoup.select.Elements; |
import org.jsoup.select.Elements; |
14 |
|
|
15 |
|
import dk.thoerup.android.traininfo.common.TimetableBean; |
16 |
|
import dk.thoerup.android.traininfo.common.TimetableEntry; |
17 |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
18 |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
19 |
import dk.thoerup.traininfoservice.StationDAO; |
import dk.thoerup.genericjavautils.TimeoutMap; |
20 |
import dk.thoerup.traininfoservice.Statistics; |
import dk.thoerup.traininfoservice.Statistics; |
21 |
|
import dk.thoerup.traininfoservice.TraininfoSettings; |
22 |
|
import dk.thoerup.traininfoservice.db.StationDAO; |
23 |
|
|
24 |
public class TimetableFetcher { |
public class TimetableFetcher { |
25 |
|
|
26 |
|
|
27 |
Map<String, List<TimetableBean>> cache; |
Map<String, TimetableBean> cache; |
28 |
Map<String, Integer> stationCache; |
Map<String, Integer> stationCache; |
29 |
|
|
30 |
StationDAO stationDao = new StationDAO(); |
StationDAO stationDao = new StationDAO(); |
31 |
|
|
32 |
|
|
33 |
Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); |
Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); |
34 |
|
|
35 |
|
TraininfoSettings settings; |
36 |
|
|
37 |
private boolean useAzureSite; |
public TimetableFetcher(TraininfoSettings settings) { |
38 |
private int replyTimeout; |
this.settings = settings; |
|
|
|
|
public TimetableFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) { |
|
|
useAzureSite = azureSite; |
|
|
this.replyTimeout = replyTimeout; |
|
39 |
|
|
40 |
cache = new TimeoutMap<String,List<TimetableBean>>(cacheTimeout); |
cache = new TimeoutMap<String,TimetableBean>( settings.getCacheTimeout() ); |
41 |
stationCache = new TimeoutMap<String,Integer>( 3*60*60*1000 ); |
stationCache = new TimeoutMap<String,Integer>( 3*60*60*1000 ); |
42 |
} |
} |
43 |
|
|
44 |
|
|
45 |
List<TimetableBean> cachedLookupTimetable(String trainID, String type) throws Exception { |
TimetableBean cachedLookupTimetable(String trainID, String type) throws Exception { |
46 |
String key = trainID+type; |
String key = trainID+type; |
47 |
List<TimetableBean> list = cache.get(key); |
TimetableBean list = cache.get(key); |
48 |
|
list = null; //TODO: DEBUG |
49 |
|
|
50 |
if (list == null) { |
if (list == null) { |
51 |
list = lookupTimetable(trainID,type); |
list = lookupTimetable(trainID,type); |
57 |
return list; |
return list; |
58 |
} |
} |
59 |
|
|
60 |
List<TimetableBean> lookupTimetable(String trainID, String type) throws Exception { |
TimetableBean lookupTimetable(String trainID, String type) throws Exception { |
61 |
if (useAzureSite == true ){ |
if (settings.getBackend() == TraininfoSettings.Backend.Azure ){ |
62 |
return lookupTimetableAzureSite(trainID, type); |
return lookupTimetableAzureSite(trainID, type); |
63 |
|
|
64 |
} else { |
} else { |
65 |
return lookupTimetableWwwSite(trainID, type); |
return lookupTimetableMobileSite(trainID, type); |
66 |
} |
} |
67 |
} |
} |
68 |
|
|
82 |
return id; |
return id; |
83 |
} |
} |
84 |
|
|
85 |
List<TimetableBean> lookupTimetableAzureSite(String trainID, String type) throws Exception { |
TimetableBean lookupTimetableAzureSite(String trainID, String type) throws Exception { |
86 |
List<TimetableBean> timetableList = new ArrayList<TimetableBean>(); |
TimetableBean timetableBean = new TimetableBean(); |
87 |
|
|
|
//String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; |
|
|
String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID; |
|
88 |
|
|
89 |
|
String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID; |
90 |
|
logger.fine("URL:" + url); |
91 |
|
|
92 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout); |
JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() ); |
93 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
94 |
|
|
95 |
Document doc = (Document) breaker.invoke(wrapper); |
Document doc = (Document) breaker.invoke(wrapper); |
118 |
continue; |
continue; |
119 |
} |
} |
120 |
|
|
121 |
TimetableBean bean = new TimetableBean(); |
TimetableEntry entry = new TimetableEntry(); |
122 |
|
|
123 |
String station = fields.get(0).text() ; |
String station = fields.get(0).text() ; |
124 |
if (station.equals("København")) |
if (station.equals("København")) |
125 |
station = "København H"; //correct inconsistency in naming |
station = "København H"; //correct inconsistency in naming |
126 |
|
|
127 |
bean.setStation( station ); |
entry.setStation( station ); |
128 |
bean.setArrival( fields.get(1).text() ); |
entry.setArrival( fields.get(1).text() ); |
129 |
bean.setDeparture( fields.get(2).text() ); |
entry.setDeparture( fields.get(2).text() ); |
130 |
|
|
131 |
boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst"); |
boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst"); |
132 |
bean.setCancelled(cancelled); |
entry.setCancelled(cancelled); |
133 |
|
|
134 |
if (currentStation == true && currentStationSaved == false ) { |
if (currentStation == true && currentStationSaved == false ) { |
135 |
bean.setCurrent(currentStation); |
entry.setCurrent(currentStation); |
136 |
currentStationSaved = true; |
currentStationSaved = true; |
137 |
} |
} |
138 |
|
|
139 |
bean.setStationId( getStationId( station )); |
entry.setStationId( getStationId( station )); |
140 |
|
|
141 |
timetableList.add(bean); |
timetableBean.entries.add(entry); |
142 |
} |
} |
143 |
|
|
144 |
//TODO: There is an off-by-one error in this cancelled parser thingie |
//TODO: There is an off-by-one error in this cancelled parser thingie |
145 |
final String cancelledString = "Aflyst"; |
final String cancelledString = "Aflyst"; |
146 |
for (int i=0;i<timetableList.size(); i++) { //handle cancelled labels |
for (int i=0;i<timetableBean.entries.size(); i++) { //handle cancelled labels |
147 |
final int lastIdx = (timetableList.size() - 1); |
final int lastIdx = (timetableBean.entries.size() - 1); |
148 |
|
|
149 |
TimetableBean current = timetableList.get(i); |
TimetableEntry current = timetableBean.entries.get(i); |
150 |
if (current.isCancelled()) { |
if (current.isCancelled()) { |
151 |
if (i == 0) { |
if (i == 0) { |
152 |
current.setDeparture(cancelledString); |
current.setDeparture(cancelledString); |
153 |
} else if (i == lastIdx) { |
} else if (i == lastIdx) { |
154 |
current.setArrival(cancelledString); |
current.setArrival(cancelledString); |
155 |
} else if (i>0 && i<lastIdx) { |
} else if (i>0 && i<lastIdx) { |
156 |
TimetableBean next = timetableList.get(i+1); |
TimetableEntry next = timetableBean.entries.get(i+1); |
157 |
TimetableBean prev = timetableList.get(i-1); |
TimetableEntry prev = timetableBean.entries.get(i-1); |
158 |
|
|
159 |
if (next.isCancelled()) |
if (next.isCancelled()) |
160 |
current.setDeparture(cancelledString); |
current.setDeparture(cancelledString); |
169 |
} |
} |
170 |
|
|
171 |
|
|
172 |
return timetableList; |
return timetableBean; |
173 |
} |
} |
174 |
|
|
175 |
List<TimetableBean> lookupTimetableWwwSite(String trainID, String type) throws Exception { |
TimetableBean lookupTimetableMobileSite(String trainID, String type) throws Exception { |
176 |
List<TimetableBean> timetableList = new ArrayList<TimetableBean>(); |
TimetableBean timetableBean = new TimetableBean(); |
177 |
|
|
178 |
String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; |
String url = "http://mobil.bane.dk/mobilStation.asp?artikelID=5332&tognummer=" + trainID + "&webprofil=" + type + "&mode=rute"; |
179 |
|
logger.fine("URL:" + url); |
180 |
|
|
181 |
|
|
182 |
|
JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() ); |
183 |
|
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
184 |
|
|
185 |
|
Document doc = (Document) breaker.invoke(wrapper); |
186 |
|
|
187 |
|
Element content = doc.getElementsByClass("contentDiv").get(1); |
188 |
|
Element dlist = content.child(0); |
189 |
|
|
190 |
|
|
191 |
|
Elements rows = dlist.getElementsByTag("dt"); |
192 |
|
|
193 |
|
for (int i=0; i<rows.size(); i++) { |
194 |
|
|
195 |
|
Element row = rows.get(i); |
196 |
|
|
197 |
|
logger.fine( row.text() ); |
198 |
|
|
199 |
|
String parts[] = row.text().split(","); |
200 |
|
|
201 |
|
TimetableEntry entry = new TimetableEntry(); |
202 |
|
|
203 |
|
String station = DepartureFetcher.cleanText( parts[0] ) ; |
204 |
|
if (station.equals("København")) |
205 |
|
station = "København H"; //correct inconsistency in naming |
206 |
|
|
207 |
|
String arrival = DepartureFetcher.cleanText( parts[1] ); |
208 |
|
String departure = DepartureFetcher.cleanText( "" ); |
209 |
|
|
210 |
|
entry.setStation( station ); |
211 |
|
entry.setArrival( arrival ); |
212 |
|
entry.setDeparture( departure ); |
213 |
|
|
214 |
|
|
215 |
|
entry.setStationId( getStationId( station )); |
216 |
|
|
217 |
|
timetableBean.entries.add(entry); |
218 |
|
} |
219 |
|
|
220 |
|
|
221 |
|
return timetableBean; |
222 |
|
|
223 |
|
} |
224 |
|
|
225 |
|
@Deprecated |
226 |
|
TimetableBean lookupTimetableWwwSite(String trainID, String type) throws Exception { |
227 |
|
TimetableBean timetableBean = new TimetableBean(); |
228 |
|
|
229 |
|
String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; |
230 |
|
logger.fine("URL:" + url); |
231 |
|
|
232 |
|
|
233 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout); |
JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() ); |
234 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
235 |
|
|
236 |
Document doc = (Document) breaker.invoke(wrapper); |
Document doc = (Document) breaker.invoke(wrapper); |
259 |
continue; |
continue; |
260 |
} |
} |
261 |
|
|
262 |
TimetableBean bean = new TimetableBean(); |
TimetableEntry entry = new TimetableEntry(); |
263 |
|
|
264 |
String station = fields.get(0).text() ; |
String station = DepartureFetcher.cleanText( fields.get(0).text() ) ; |
265 |
if (station.equals("København")) |
if (station.equals("København")) |
266 |
station = "København H"; //correct inconsistency in naming |
station = "København H"; //correct inconsistency in naming |
267 |
|
|
268 |
bean.setStation( station ); |
String arrival = DepartureFetcher.cleanText( fields.get(1).text() ); |
269 |
bean.setArrival( fields.get(1).text() ); |
String departure = DepartureFetcher.cleanText( fields.get(2).text() ); |
270 |
bean.setDeparture( fields.get(2).text() ); |
|
271 |
|
entry.setStation( station ); |
272 |
|
entry.setArrival( arrival ); |
273 |
|
entry.setDeparture( departure ); |
274 |
|
|
275 |
|
|
276 |
if (currentStation == true && currentStationSaved == false ) { |
if (currentStation == true && currentStationSaved == false ) { |
277 |
bean.setCurrent(currentStation); |
entry.setCurrent(currentStation); |
278 |
currentStationSaved = true; |
currentStationSaved = true; |
279 |
} |
} |
280 |
|
|
281 |
bean.setStationId( getStationId( station )); |
entry.setStationId( getStationId( station )); |
282 |
|
|
283 |
timetableList.add(bean); |
timetableBean.entries.add(entry); |
284 |
} |
} |
285 |
|
|
286 |
} else { |
} else { |
288 |
} |
} |
289 |
|
|
290 |
|
|
291 |
return timetableList; |
return timetableBean; |
292 |
} |
} |
293 |
|
|
294 |
} |
} |