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 |
|
|
49 |
if (list == null) { |
if (list == null) { |
50 |
list = lookupTimetable(trainID,type); |
list = lookupTimetable(trainID,type); |
56 |
return list; |
return list; |
57 |
} |
} |
58 |
|
|
59 |
List<TimetableBean> lookupTimetable(String trainID, String type) throws Exception { |
TimetableBean lookupTimetable(String trainID, String type) throws Exception { |
60 |
if (useAzureSite == true ){ |
if (settings.getUseAzureSite() == true ){ |
61 |
return lookupTimetableAzureSite(trainID, type); |
return lookupTimetableAzureSite(trainID, type); |
62 |
|
|
63 |
} else { |
} else { |
64 |
return lookupTimetableWwwSite(trainID, type); |
return lookupTimetableMobileSite(trainID, type); |
65 |
} |
} |
66 |
} |
} |
67 |
|
|
81 |
return id; |
return id; |
82 |
} |
} |
83 |
|
|
84 |
List<TimetableBean> lookupTimetableAzureSite(String trainID, String type) throws Exception { |
TimetableBean lookupTimetableAzureSite(String trainID, String type) throws Exception { |
85 |
List<TimetableBean> timetableList = new ArrayList<TimetableBean>(); |
TimetableBean timetableBean = new TimetableBean(); |
86 |
|
|
|
//String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; |
|
|
String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID; |
|
87 |
|
|
88 |
|
String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID; |
89 |
|
logger.fine("URL:" + url); |
90 |
|
|
91 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout); |
JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() ); |
92 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
93 |
|
|
94 |
Document doc = (Document) breaker.invoke(wrapper); |
Document doc = (Document) breaker.invoke(wrapper); |
117 |
continue; |
continue; |
118 |
} |
} |
119 |
|
|
120 |
TimetableBean bean = new TimetableBean(); |
TimetableEntry entry = new TimetableEntry(); |
121 |
|
|
122 |
String station = fields.get(0).text() ; |
String station = fields.get(0).text() ; |
123 |
if (station.equals("København")) |
if (station.equals("København")) |
124 |
station = "København H"; //correct inconsistency in naming |
station = "København H"; //correct inconsistency in naming |
125 |
|
|
126 |
bean.setStation( station ); |
entry.setStation( station ); |
127 |
bean.setArrival( fields.get(1).text() ); |
entry.setArrival( fields.get(1).text() ); |
128 |
bean.setDeparture( fields.get(2).text() ); |
entry.setDeparture( fields.get(2).text() ); |
129 |
|
|
130 |
boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst"); |
boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst"); |
131 |
bean.setCancelled(cancelled); |
entry.setCancelled(cancelled); |
132 |
|
|
133 |
if (currentStation == true && currentStationSaved == false ) { |
if (currentStation == true && currentStationSaved == false ) { |
134 |
bean.setCurrent(currentStation); |
entry.setCurrent(currentStation); |
135 |
currentStationSaved = true; |
currentStationSaved = true; |
136 |
} |
} |
137 |
|
|
138 |
bean.setStationId( getStationId( station )); |
entry.setStationId( getStationId( station )); |
139 |
|
|
140 |
timetableList.add(bean); |
timetableBean.entries.add(entry); |
141 |
} |
} |
142 |
|
|
143 |
//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 |
144 |
final String cancelledString = "Aflyst"; |
final String cancelledString = "Aflyst"; |
145 |
for (int i=0;i<timetableList.size(); i++) { //handle cancelled labels |
for (int i=0;i<timetableBean.entries.size(); i++) { //handle cancelled labels |
146 |
final int lastIdx = (timetableList.size() - 1); |
final int lastIdx = (timetableBean.entries.size() - 1); |
147 |
|
|
148 |
TimetableBean current = timetableList.get(i); |
TimetableEntry current = timetableBean.entries.get(i); |
149 |
if (current.isCancelled()) { |
if (current.isCancelled()) { |
150 |
if (i == 0) { |
if (i == 0) { |
151 |
current.setDeparture(cancelledString); |
current.setDeparture(cancelledString); |
152 |
} else if (i == lastIdx) { |
} else if (i == lastIdx) { |
153 |
current.setArrival(cancelledString); |
current.setArrival(cancelledString); |
154 |
} else if (i>0 && i<lastIdx) { |
} else if (i>0 && i<lastIdx) { |
155 |
TimetableBean next = timetableList.get(i+1); |
TimetableEntry next = timetableBean.entries.get(i+1); |
156 |
TimetableBean prev = timetableList.get(i-1); |
TimetableEntry prev = timetableBean.entries.get(i-1); |
157 |
|
|
158 |
if (next.isCancelled()) |
if (next.isCancelled()) |
159 |
current.setDeparture(cancelledString); |
current.setDeparture(cancelledString); |
168 |
} |
} |
169 |
|
|
170 |
|
|
171 |
return timetableList; |
return timetableBean; |
172 |
} |
} |
173 |
|
|
174 |
List<TimetableBean> lookupTimetableWwwSite(String trainID, String type) throws Exception { |
TimetableBean lookupTimetableMobileSite(String trainID, String type) throws Exception { |
175 |
List<TimetableBean> timetableList = new ArrayList<TimetableBean>(); |
return new TimetableBean(); //dummy skeleton method |
176 |
|
} |
177 |
|
|
178 |
|
@Deprecated |
179 |
|
TimetableBean lookupTimetableWwwSite(String trainID, String type) throws Exception { |
180 |
|
TimetableBean timetableBean = new TimetableBean(); |
181 |
|
|
182 |
String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; |
String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; |
183 |
|
logger.fine("URL:" + url); |
184 |
|
|
185 |
|
|
186 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout); |
JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() ); |
187 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
188 |
|
|
189 |
Document doc = (Document) breaker.invoke(wrapper); |
Document doc = (Document) breaker.invoke(wrapper); |
212 |
continue; |
continue; |
213 |
} |
} |
214 |
|
|
215 |
TimetableBean bean = new TimetableBean(); |
TimetableEntry entry = new TimetableEntry(); |
216 |
|
|
217 |
String station = DepartureFetcher.cleanText( fields.get(0).text() ) ; |
String station = DepartureFetcher.cleanText( fields.get(0).text() ) ; |
218 |
if (station.equals("København")) |
if (station.equals("København")) |
221 |
String arrival = DepartureFetcher.cleanText( fields.get(1).text() ); |
String arrival = DepartureFetcher.cleanText( fields.get(1).text() ); |
222 |
String departure = DepartureFetcher.cleanText( fields.get(2).text() ); |
String departure = DepartureFetcher.cleanText( fields.get(2).text() ); |
223 |
|
|
224 |
bean.setStation( station ); |
entry.setStation( station ); |
225 |
bean.setArrival( arrival ); |
entry.setArrival( arrival ); |
226 |
bean.setDeparture( departure ); |
entry.setDeparture( departure ); |
227 |
|
|
228 |
|
|
229 |
if (currentStation == true && currentStationSaved == false ) { |
if (currentStation == true && currentStationSaved == false ) { |
230 |
bean.setCurrent(currentStation); |
entry.setCurrent(currentStation); |
231 |
currentStationSaved = true; |
currentStationSaved = true; |
232 |
} |
} |
233 |
|
|
234 |
bean.setStationId( getStationId( station )); |
entry.setStationId( getStationId( station )); |
235 |
|
|
236 |
timetableList.add(bean); |
timetableBean.entries.add(entry); |
237 |
} |
} |
238 |
|
|
239 |
} else { |
} else { |
241 |
} |
} |
242 |
|
|
243 |
|
|
244 |
return timetableList; |
return timetableBean; |
245 |
} |
} |
246 |
|
|
247 |
} |
} |