1 package net.jaekl.frank;
3 import java.io.PrintWriter;
4 import java.text.DateFormat;
5 import java.text.MessageFormat;
6 import java.text.SimpleDateFormat;
8 import java.util.Locale;
9 import java.util.TimeZone;
11 import net.jaekl.frank.octranspo.Route;
12 import net.jaekl.frank.octranspo.StopInfo;
13 import net.jaekl.frank.octranspo.Trip;
15 public class Schedule {
16 DateFormat m_hourMinFmt;
17 DateFormat m_hourMinSecFmt;
22 public Schedule(Locale locale) {
24 m_style = new Style();
25 m_bundle = FrankBundle.getInst(locale);
26 m_hourMinFmt = new SimpleDateFormat("hh:mma", locale);
27 m_hourMinSecFmt = new SimpleDateFormat("hh:mm:ssa", locale);
29 // We always display times in Ottawa (local) time, because that's the only
30 // time that makes sense for a user who wants to know when to catch a bus.
31 // In the (unlikely?) event that our server is in a different timezone,
32 // we need to explicitly specify the time zone that we want to use.
33 TimeZone tz = TimeZone.getTimeZone("America/Toronto");
34 m_hourMinFmt.setTimeZone(tz);
35 m_hourMinSecFmt.setTimeZone(tz);
38 String trans(String key) {
39 return m_bundle.get(key);
42 String mapUrl(double latitude, double longitude) {
43 return "http://www.openstreetmap.org/?mlat=" + latitude + "&mlon=" + longitude + "&zoom=15";
46 // Countdown timer that updates time remaining until each bus is expected.
47 void writeScript(PrintWriter pw, String remainArray, int remainCount) {
48 String min = trans(FrankBundle.MINUTES);
49 String sec = trans(FrankBundle.SECONDS);
51 pw.println("<SCRIPT>");
52 pw.println("var start; var interval; var remain;");
53 pw.println("window.onload = function() { start = new Date(); interval = setInterval(\"Tick()\", 1000); remain = " + remainArray + "; }");
54 pw.println("function Tick() { ");
55 pw.println(" var now = new Date(); var delta = Math.floor((now.getTime() - start.getTime()) / 1000); ");
56 pw.println(" var secs = delta % 60; var mins = Math.floor(delta / 60);");
57 pw.println(" document.getElementById(\"elapsed\").innerHTML = mins + \"" + min + " \" + secs + \"" + sec + "\";");
58 pw.println(" for (var i = 0; i < " + remainCount + "; ++i) {");
59 pw.println(" var id = \"in_\" + i;");
60 pw.println(" var element = document.getElementById(id);");
61 pw.println(" var remain_delta = (remain[i] * 60) + 30 - delta;");
62 pw.println(" if (remain_delta <= 0) {");
63 pw.println(" element.innerHTML = \"----\";");
64 pw.println(" } else {");
65 pw.println(" element.innerHTML = (Math.floor(remain_delta / 60)) + \"" + min + "\";");
69 pw.println("</SCRIPT>");
73 void writeHeader(PrintWriter pw, String title) {
76 pw.println(" <META NAME=\"viewport\" content=\"width=device-width\"/>");
77 pw.println(" <TITLE>" + title + "</TITLE>");
78 new Style().writeStyle(pw);
79 pw.println("</HEAD>");
82 void writePage(PrintWriter pw, StopInfo stopInfo)
84 StringBuilder remainArray = new StringBuilder("[ ");
87 String title = stopInfo.getDescr() + " (" + stopInfo.getStopNo() + ")";
88 String min = trans(FrankBundle.MINUTES);
90 writeHeader(pw, trans(FrankBundle.FRANK) + ": " + title);
93 pw.println(" <TABLE ID=\"trips\">");
94 pw.println(" <TR><TH COLSPAN=\"5\">" + title + "</TH></TR>");
95 pw.println(" <TR><TH>" +
96 trans(FrankBundle.ROUTE) +
98 trans(FrankBundle.DESTINATION) +
100 trans(FrankBundle.ETA) +
102 trans(FrankBundle.REMAIN) +
104 trans(FrankBundle.GPS_READ) +
107 for (int routeIdx = 0; routeIdx < stopInfo.getNumRoutes(); ++routeIdx) {
108 Route route = stopInfo.getRoute(routeIdx);
109 for (int tripIdx = 0; tripIdx < route.getNumTrips(); ++tripIdx) {
110 Trip trip = route.getTrip(tripIdx);
111 boolean isGhost = (trip.getAdjAge() < 0);
113 // GPS is off. This bus may not exist.
114 pw.println(" <TR CLASS=\"ghost\">");
118 pw.println(" <TD>" + route.getRouteNo() + "</TD>");
119 pw.println(" <TD>" + trip.getDest() + "</TD>");
120 pw.println(" <TD>" + m_hourMinFmt.format(trip.getETA()) + "</TD>");
122 pw.println(" <TD><SPAN ID=\"in_" + remainCount + "\">"+ trip.getAdjTime()+min+"</SPAN></TD>");
123 if (remainCount > 0) {
124 remainArray.append(", ");
126 remainArray.append(trip.getAdjTime());
130 pw.println(" <TD>" + trans(FrankBundle.GPS_OFF) + "</TD>");
133 pw.println(" <TD><A HREF=\"" + mapUrl(trip.getLatitude(), trip.getLongitude()) +
134 "\">" + m_hourMinSecFmt.format(trip.getGPSTime()) + "</A></TD>");
136 pw.println(" </TR>");
140 pw.println(" </TABLE>");
142 String dataCollectedFormat = trans(FrankBundle.DATA_COLLECTED);
143 String dataCollected = MessageFormat.format(dataCollectedFormat, m_hourMinSecFmt.format(new Date()));
144 pw.println(" <P>" + dataCollected + "</P>");
146 remainArray.append(" ]");
147 writeScript(pw, remainArray.toString(), remainCount);
148 pw.println("</BODY></HTML>");