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; |
|
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 TimetableFetcher { |
public class TimetableFetcher { |
24 |
|
|
25 |
|
|
26 |
Map<String, List<TimetableBean>> cache; |
Map<String, TimetableBean> cache; |
27 |
Map<String, Integer> stationCache; |
Map<String, Integer> stationCache; |
28 |
|
|
29 |
StationDAO stationDao = new StationDAO(); |
StationDAO stationDao = new StationDAO(); |
30 |
|
|
31 |
|
|
32 |
Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); |
Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); |
33 |
|
|
34 |
|
TraininfoSettings settings; |
35 |
|
|
36 |
private boolean useAzureSite; |
public TimetableFetcher(TraininfoSettings settings) { |
37 |
private int replyTimeout; |
this.settings = settings; |
|
|
|
|
public TimetableFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) { |
|
|
useAzureSite = azureSite; |
|
|
this.replyTimeout = replyTimeout; |
|
38 |
|
|
39 |
cache = new TimeoutMap<String,List<TimetableBean>>(cacheTimeout); |
cache = new TimeoutMap<String,TimetableBean>( settings.getCacheTimeout() ); |
40 |
stationCache = new TimeoutMap<String,Integer>( 3*60*60*1000 ); |
stationCache = new TimeoutMap<String,Integer>( 3*60*60*1000 ); |
41 |
} |
} |
42 |
|
|
43 |
|
|
44 |
List<TimetableBean> cachedLookupTimetable(String trainID, String type) throws Exception { |
TimetableBean cachedLookupTimetable(String trainID, String type) throws Exception { |
45 |
String key = trainID+type; |
String key = trainID+type; |
46 |
List<TimetableBean> list = cache.get(key); |
TimetableBean list = cache.get(key); |
47 |
|
|
48 |
if (list == null) { |
if (list == null) { |
49 |
list = lookupTimetable(trainID,type); |
list = lookupTimetable(trainID,type); |
55 |
return list; |
return list; |
56 |
} |
} |
57 |
|
|
58 |
List<TimetableBean> lookupTimetable(String trainID, String type) throws Exception { |
TimetableBean lookupTimetable(String trainID, String type) throws Exception { |
59 |
if (useAzureSite == true ){ |
if (settings.getUseAzureSite() == true ){ |
60 |
return lookupTimetableAzureSite(trainID, type); |
return lookupTimetableAzureSite(trainID, type); |
61 |
|
|
62 |
} else { |
} else { |
63 |
return lookupTimetableWwwSite(trainID, type); |
return lookupTimetableMobileSite(trainID, type); |
64 |
} |
} |
65 |
} |
} |
66 |
|
|
80 |
return id; |
return id; |
81 |
} |
} |
82 |
|
|
83 |
List<TimetableBean> lookupTimetableAzureSite(String trainID, String type) throws Exception { |
TimetableBean lookupTimetableAzureSite(String trainID, String type) throws Exception { |
84 |
List<TimetableBean> timetableList = new ArrayList<TimetableBean>(); |
TimetableBean timetableBean = new TimetableBean(); |
85 |
|
|
86 |
|
|
87 |
String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID; |
String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID; |
88 |
logger.fine("URL:" + url); |
logger.fine("URL:" + url); |
89 |
|
|
90 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout); |
JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() ); |
91 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
92 |
|
|
93 |
Document doc = (Document) breaker.invoke(wrapper); |
Document doc = (Document) breaker.invoke(wrapper); |
116 |
continue; |
continue; |
117 |
} |
} |
118 |
|
|
119 |
TimetableBean bean = new TimetableBean(); |
TimetableEntry entry = new TimetableEntry(); |
120 |
|
|
121 |
String station = fields.get(0).text() ; |
String station = fields.get(0).text() ; |
122 |
if (station.equals("København")) |
if (station.equals("København")) |
123 |
station = "København H"; //correct inconsistency in naming |
station = "København H"; //correct inconsistency in naming |
124 |
|
|
125 |
bean.setStation( station ); |
entry.setStation( station ); |
126 |
bean.setArrival( fields.get(1).text() ); |
entry.setArrival( fields.get(1).text() ); |
127 |
bean.setDeparture( fields.get(2).text() ); |
entry.setDeparture( fields.get(2).text() ); |
128 |
|
|
129 |
boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst"); |
boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst"); |
130 |
bean.setCancelled(cancelled); |
entry.setCancelled(cancelled); |
131 |
|
|
132 |
if (currentStation == true && currentStationSaved == false ) { |
if (currentStation == true && currentStationSaved == false ) { |
133 |
bean.setCurrent(currentStation); |
entry.setCurrent(currentStation); |
134 |
currentStationSaved = true; |
currentStationSaved = true; |
135 |
} |
} |
136 |
|
|
137 |
bean.setStationId( getStationId( station )); |
entry.setStationId( getStationId( station )); |
138 |
|
|
139 |
timetableList.add(bean); |
timetableBean.entries.add(entry); |
140 |
} |
} |
141 |
|
|
142 |
//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 |
143 |
final String cancelledString = "Aflyst"; |
final String cancelledString = "Aflyst"; |
144 |
for (int i=0;i<timetableList.size(); i++) { //handle cancelled labels |
for (int i=0;i<timetableBean.entries.size(); i++) { //handle cancelled labels |
145 |
final int lastIdx = (timetableList.size() - 1); |
final int lastIdx = (timetableBean.entries.size() - 1); |
146 |
|
|
147 |
TimetableBean current = timetableList.get(i); |
TimetableEntry current = timetableBean.entries.get(i); |
148 |
if (current.isCancelled()) { |
if (current.isCancelled()) { |
149 |
if (i == 0) { |
if (i == 0) { |
150 |
current.setDeparture(cancelledString); |
current.setDeparture(cancelledString); |
151 |
} else if (i == lastIdx) { |
} else if (i == lastIdx) { |
152 |
current.setArrival(cancelledString); |
current.setArrival(cancelledString); |
153 |
} else if (i>0 && i<lastIdx) { |
} else if (i>0 && i<lastIdx) { |
154 |
TimetableBean next = timetableList.get(i+1); |
TimetableEntry next = timetableBean.entries.get(i+1); |
155 |
TimetableBean prev = timetableList.get(i-1); |
TimetableEntry prev = timetableBean.entries.get(i-1); |
156 |
|
|
157 |
if (next.isCancelled()) |
if (next.isCancelled()) |
158 |
current.setDeparture(cancelledString); |
current.setDeparture(cancelledString); |
167 |
} |
} |
168 |
|
|
169 |
|
|
170 |
return timetableList; |
return timetableBean; |
171 |
} |
} |
172 |
|
|
173 |
List<TimetableBean> lookupTimetableWwwSite(String trainID, String type) throws Exception { |
TimetableBean lookupTimetableMobileSite(String trainID, String type) throws Exception { |
174 |
List<TimetableBean> timetableList = new ArrayList<TimetableBean>(); |
return new TimetableBean(); //dummy skeleton method |
175 |
|
} |
176 |
|
|
177 |
|
@Deprecated |
178 |
|
TimetableBean lookupTimetableWwwSite(String trainID, String type) throws Exception { |
179 |
|
TimetableBean timetableBean = new TimetableBean(); |
180 |
|
|
181 |
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"; |
182 |
logger.fine("URL:" + url); |
logger.fine("URL:" + url); |
183 |
|
|
184 |
|
|
185 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout); |
JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() ); |
186 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
187 |
|
|
188 |
Document doc = (Document) breaker.invoke(wrapper); |
Document doc = (Document) breaker.invoke(wrapper); |
211 |
continue; |
continue; |
212 |
} |
} |
213 |
|
|
214 |
TimetableBean bean = new TimetableBean(); |
TimetableEntry entry = new TimetableEntry(); |
215 |
|
|
216 |
String station = DepartureFetcher.cleanText( fields.get(0).text() ) ; |
String station = DepartureFetcher.cleanText( fields.get(0).text() ) ; |
217 |
if (station.equals("København")) |
if (station.equals("København")) |
220 |
String arrival = DepartureFetcher.cleanText( fields.get(1).text() ); |
String arrival = DepartureFetcher.cleanText( fields.get(1).text() ); |
221 |
String departure = DepartureFetcher.cleanText( fields.get(2).text() ); |
String departure = DepartureFetcher.cleanText( fields.get(2).text() ); |
222 |
|
|
223 |
bean.setStation( station ); |
entry.setStation( station ); |
224 |
bean.setArrival( arrival ); |
entry.setArrival( arrival ); |
225 |
bean.setDeparture( departure ); |
entry.setDeparture( departure ); |
226 |
|
|
227 |
|
|
228 |
if (currentStation == true && currentStationSaved == false ) { |
if (currentStation == true && currentStationSaved == false ) { |
229 |
bean.setCurrent(currentStation); |
entry.setCurrent(currentStation); |
230 |
currentStationSaved = true; |
currentStationSaved = true; |
231 |
} |
} |
232 |
|
|
233 |
bean.setStationId( getStationId( station )); |
entry.setStationId( getStationId( station )); |
234 |
|
|
235 |
timetableList.add(bean); |
timetableBean.entries.add(entry); |
236 |
} |
} |
237 |
|
|
238 |
} else { |
} else { |
240 |
} |
} |
241 |
|
|
242 |
|
|
243 |
return timetableList; |
return timetableBean; |
244 |
} |
} |
245 |
|
|
246 |
} |
} |