]> jaekl.net Git - frank.git/blob - prod/net/jaekl/frank/Schedule.java
8db11d5d0fd6a7ab332f74a25e3a2dce377c9c6a
[frank.git] / prod / net / jaekl / frank / Schedule.java
1 package net.jaekl.frank;
2
3 import java.io.PrintWriter;
4 import java.text.DateFormat;
5 import java.text.MessageFormat;
6 import java.text.SimpleDateFormat;
7 import java.util.Date;
8 import java.util.Locale;
9 import java.util.TimeZone;
10
11 import net.jaekl.frank.octranspo.Route;
12 import net.jaekl.frank.octranspo.StopInfo;
13 import net.jaekl.frank.octranspo.Trip;
14
15 public class Schedule {
16         DateFormat m_hourMinFmt;
17         DateFormat m_hourMinSecFmt;
18         FrankBundle m_bundle;
19         Locale m_locale;
20         Style m_style;
21         
22         public Schedule(Locale locale) {
23                 m_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);
28                 
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);
36         }
37         
38         String trans(String key) {
39                 return m_bundle.get(key);
40         }
41         
42         String mapUrl(double latitude, double longitude) {
43                 return "http://www.openstreetmap.org/?mlat=" + latitude + "&mlon=" + longitude + "&zoom=15";
44         }
45
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);
50                 
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 + "\";");
66             pw.println("    }");
67             pw.println("  }");
68             pw.println("}");
69             pw.println("</SCRIPT>");
70
71         }
72         
73         void writeHeader(PrintWriter pw, String title) {
74                 pw.println("<HTML>");
75                 pw.println("<HEAD>");
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>");
80         }
81         
82         void writePage(PrintWriter pw, StopInfo stopInfo)
83         {
84                 StringBuilder remainArray = new StringBuilder("[ ");
85                 int           remainCount = 0;
86                 
87                 String title = stopInfo.getDescr() + " (" + stopInfo.getStopNo() + ")";
88                 String min = trans(FrankBundle.MINUTES);
89                 
90                 writeHeader(pw, trans(FrankBundle.FRANK) + ": " + title);
91                 
92                 pw.println("<BODY>");
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) + 
97                                 "</TH><TH>" +
98                                 trans(FrankBundle.DESTINATION) +
99                                 "</TH><TH>" +
100                                 trans(FrankBundle.ETA) +
101                                 "</TH><TH>" +
102                                 trans(FrankBundle.REMAIN) +  
103                                 "</TH><TH>" +
104                                 trans(FrankBundle.GPS_READ) +
105                                 "</TH>");
106                 
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);
112                                 if (isGhost) {
113                                         // GPS is off.  This bus may not exist.
114                                         pw.println("    <TR CLASS=\"ghost\">");
115                                 } else {
116                                         pw.println("    <TR>");
117                                 }
118                                 pw.println("      <TD>" + route.getRouteNo() + "</TD>");
119                                 pw.println("      <TD>" + trip.getDest() + "</TD>");
120                                 pw.println("      <TD>" + m_hourMinFmt.format(trip.getETA()) + "</TD>");
121                                 
122                                 pw.println("      <TD><SPAN ID=\"in_" + remainCount + "\">"+ trip.getAdjTime()+min+"</SPAN></TD>");
123                                 if (remainCount > 0) {
124                                         remainArray.append(", ");
125                                 }
126                                 remainArray.append(trip.getAdjTime());
127                                 remainCount++;
128                                 
129                                 if (isGhost) {
130                                         pw.println("      <TD>" + trans(FrankBundle.GPS_OFF) + "</TD>");
131                                 }
132                                 else {
133                                         pw.println("      <TD><A HREF=\"" + mapUrl(trip.getLatitude(), trip.getLongitude()) + 
134                                                            "\">" + m_hourMinSecFmt.format(trip.getGPSTime()) + "</A></TD>");
135                                 }
136                                 pw.println("    </TR>");
137                         }
138                 }
139                 
140                 pw.println("  </TABLE>");
141                 
142                 String dataCollectedFormat = trans(FrankBundle.DATA_COLLECTED);
143                 String dataCollected = MessageFormat.format(dataCollectedFormat, m_hourMinSecFmt.format(new Date()));
144                 pw.println("  <P>" + dataCollected + "</P>");
145                 
146                 remainArray.append(" ]");
147                 writeScript(pw, remainArray.toString(), remainCount);
148                 pw.println("</BODY></HTML>");
149         }
150 }