]> jaekl.net Git - frank.git/blob - prod/net/jaekl/frank/Schedule.java
c7fd70bfcd3a7b5ed991bdaf537b9bf762af6971
[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
10 import net.jaekl.frank.octranspo.Route;
11 import net.jaekl.frank.octranspo.StopInfo;
12 import net.jaekl.frank.octranspo.Trip;
13
14 public class Schedule {
15         Locale m_locale;
16         FrankBundle m_bundle;
17         DateFormat m_hourMinFmt;
18         DateFormat m_hourMinSecFmt;
19         
20         public Schedule(Locale locale) {
21                 m_locale = locale;
22                 m_bundle = FrankBundle.getInst(locale);
23                 m_hourMinFmt = new SimpleDateFormat("hh:mma", locale);
24                 m_hourMinSecFmt = new SimpleDateFormat("hh:mm:ssa", locale);
25         }
26         
27         String trans(String key) {
28                 return m_bundle.get(key);
29         }
30         
31         String mapUrl(double latitude, double longitude) {
32                 return "http://www.openstreetmap.org/?mlat=" + latitude + "&mlon=" + longitude + "&zoom=15";
33         }
34         
35         void writeStyle(PrintWriter pw) {
36                 pw.println("<STYLE>");
37                 pw.println("  body {background-color: #F0F0C0; font-size: 1.5em; }");
38                 pw.println("  #trips {border-collapse: collapse; font-size: 1.5em; }");
39                 pw.println("  #trips td, #trips th {border: 1px solid #600000; padding: 3px 3px 3px 3px; text-align: center;}");
40                 pw.println("  #trips th {background-color: #800000; color: #FFFFFF; }");
41                 pw.println("  #trips tr.ghost td {background-color: #C0C0C0;}");
42                 pw.println("</STYLE>");
43         }
44         
45         // Countdown timer that updates time remaining until each bus is expected.
46         void writeScript(PrintWriter pw, String remainArray, int remainCount) {
47                 String min = trans(FrankBundle.MINUTES);
48                 String sec = trans(FrankBundle.SECONDS);
49                 
50                 pw.println("<SCRIPT>");
51             pw.println("var start; var interval; var remain;");
52             pw.println("window.onload = function() { start = new Date(); interval = setInterval(\"Tick()\", 1000); remain = " + remainArray + "; }");
53             pw.println("function Tick() { ");
54             pw.println("  var now = new Date(); var delta = Math.floor((now.getTime() - start.getTime()) / 1000); ");
55             pw.println("  var secs = delta % 60; var mins = Math.floor(delta / 60);");
56             pw.println("  document.getElementById(\"elapsed\").innerHTML = mins + \"" + min + " \" + secs + \"" + sec + "\";");
57             pw.println("  for (var i = 0; i < " + remainCount + "; ++i) {");
58             pw.println("    var id = \"in_\" + i;");
59             pw.println("    var element = document.getElementById(id);");
60             pw.println("    var remain_delta = (remain[i] * 60) + 30 - delta;");
61             pw.println("    if (remain_delta <= 0) {");
62             pw.println("      element.innerHTML = \"----\";");
63             pw.println("    } else {");
64             pw.println("      element.innerHTML = (Math.floor(remain_delta / 60)) + \"" + trans(min) + "\";");
65             pw.println("    }");
66             pw.println("  }");
67             pw.println("}");
68             pw.println("</SCRIPT>");
69
70         }
71         
72         void writeHeader(PrintWriter pw, String title) {
73                 pw.println("<HTML>");
74                 pw.println("<HEAD>");
75                 pw.println("<TITLE>" + title + "</TITLE>");
76                 writeStyle(pw);
77                 pw.println("</HEAD>");
78         }
79         
80         void writePage(PrintWriter pw, StopInfo stopInfo)
81         {
82                 StringBuilder remainArray = new StringBuilder("[ ");
83                 int           remainCount = 0;
84                 
85                 String title = stopInfo.getDescr() + " (" + stopInfo.getStopNo() + ")";
86                 String min = trans(FrankBundle.MINUTES);
87                 
88                 writeHeader(pw, trans(FrankBundle.FRANK) + ": " + title);
89                 
90                 pw.println("<BODY>");
91                 pw.println("  <TABLE ID=\"trips\">");
92                 pw.println("    <TR><TH COLSPAN=\"5\">" + title + "</TH></TR>");
93                 pw.println("    <TR><TH>" +
94                         trans(FrankBundle.ROUTE) + 
95                                 "</TH><TH>" +
96                                 trans(FrankBundle.DESTINATION) +
97                                 "</TH><TH>" +
98                                 trans(FrankBundle.ETA) +
99                                 "</TH><TH>" +
100                                 trans(FrankBundle.REMAIN) +  
101                                 "</TH><TH>" +
102                                 trans(FrankBundle.GPS_READ) +
103                                 "</TH>");
104                 
105                 for (int routeIdx = 0; routeIdx < stopInfo.getNumRoutes(); ++routeIdx) {
106                         Route route = stopInfo.getRoute(routeIdx);
107                         for (int tripIdx = 0; tripIdx < route.getNumTrips(); ++tripIdx) {
108                                 Trip trip = route.getTrip(tripIdx);
109                                 boolean isGhost = ((-1) == trip.getAdjAge());
110                                 if (isGhost) {
111                                         // GPS is off.  This bus may not exist.
112                                         pw.println("    <TR CLASS=\"ghost\">");
113                                 } else {
114                                         pw.println("    <TR>");
115                                 }
116                                 pw.println("      <TD>" + route.getRouteNo() + "</TD>");
117                                 pw.println("      <TD>" + trip.getDest() + "</TD>");
118                                 pw.println("      <TD>" + m_hourMinFmt.format(trip.getETA()) + "</TD>");
119                                 
120                                 pw.println("      <TD><SPAN ID=\"in_" + remainCount + "\">"+ trip.getAdjTime()+min+"</SPAN></TD>");
121                                 if (remainCount > 0) {
122                                         remainArray.append(", ");
123                                 }
124                                 remainArray.append(trip.getAdjTime());
125                                 remainCount++;
126                                 
127                                 if (trip.getAdjAge() < 0) {
128                                         pw.println("      <TD>" + trans(FrankBundle.GPS_OFF) + "</TD>");
129                                 }
130                                 else {
131                                         pw.println("      <TD><A HREF=\"" + mapUrl(trip.getLatitude(), trip.getLongitude()) + 
132                                                            "\">" + m_hourMinSecFmt.format(trip.getGPSTime()) + "</A></TD>");
133                                 }
134                                 pw.println("    </TR>");
135                         }
136                 }
137                 
138                 pw.println("  </TABLE>");
139                 
140                 String dataCollectedFormat = trans(FrankBundle.DATA_COLLECTED);
141                 String dataCollected = MessageFormat.format(dataCollectedFormat, m_hourMinSecFmt.format(new Date()));
142                 pw.println("  <P>" + dataCollected + "</P>");
143                 
144                 remainArray.append(" ]");
145                 writeScript(pw, remainArray.toString(), remainCount);
146                 pw.println("</BODY></HTML>");
147         }
148 }