Commits

Marco Yuen  committed a2989f6

Display the train schedule for the next 30 min.

  • Participants
  • Parent commits 995b357

Comments (0)

Files changed (6)

File resources/public/js/nextrain.coffee

                 if navigator.geolocation
                     navigator.geolocation.getCurrentPosition(app.locSuccess, app.locError,
                                                              {enableHighAccuracy: true,
-                                                             maximumAge: 60000,
+                                                             maximumAge: 6000,
                                                              timeout: 30000})
         )
         $('#stoppage').live('pagebeforecreate',
             (event) ->
-                console.log "stopage: before create"
                 stop_id   = _.last(event.target.baseURI.split('/'))
-                timestamp = Math.round(new Date().getTime() / 1000)
-                $.post('/stoptimes',
+                timestamp = new Date().getTime()
+                $.post('/traintimes',
                       {timestamp: timestamp, stop_id: stop_id},
                       (data, status, jqxhr) ->
-                          #$('#stops-list-template').autoRender(data)
-                          console.log(data))
+                        $timesList = $('#times-list')
+                        for time in data.times
+                          do (time) ->
+                            $timesList.append("<li>" + time.trip_headsign +
+                                              "<span class='ui-li-aside'>" +
+                                              time.departure_time + "</span></li>")
+                        $timesList.listview('refresh')
+                      )
         )
     app.init = -> app.bindings()
     app.init()) nextrainapp

File resources/public/js/nextrain.js

 // Generated by CoffeeScript 1.3.3
-var nextrainapp;
+(function() {
+  var nextrainapp;
 
-nextrainapp = {};
+  nextrainapp = {};
 
-(function(app) {
-  app.locSuccess = function(position) {
-    console.log(position);
-    return $.post('/findstops', {
-      lat: position.coords.latitude,
-      lon: position.coords.longitude
-    }, function(data, status, jqxhr) {
-      $('#stops-list-template').autoRender(data);
-      return console.log(data);
-    });
-  };
-  app.locError = function(error) {
-    console.log("Cannot acquire location.");
-    switch (error.code) {
-      case error.PERMISSION_DENIED:
-        return alert("User did not share location data.");
-      case error.POSITION_UNAVAILABLE:
-        return alert("Could not detect current position");
-      case error.TIMEOUT:
-        return alert("Retrieving position timed out");
-      default:
-        return alert("Unknown error");
-    }
-  };
-  app.bindings = function() {
-    $('#indexpage').live('pagebeforecreate', function(event) {
-      if (navigator.geolocation) {
-        return navigator.geolocation.getCurrentPosition(app.locSuccess, app.locError, {
-          enableHighAccuracy: true,
-          maximumAge: 60000,
-          timeout: 30000
-        });
-      }
-    });
-    return $('#stoppage').live('pagebeforecreate', function(event) {
-      var stop_id, timestamp;
-      console.log("stopage: before create");
-      stop_id = _.last(event.target.baseURI.split('/'));
-      timestamp = Math.round(new Date().getTime() / 1000);
-      return $.post('/stoptimes', {
-        timestamp: timestamp,
-        stop_id: stop_id
+  (function(app) {
+    app.locSuccess = function(position) {
+      console.log(position);
+      return $.post('/findstops', {
+        lat: position.coords.latitude,
+        lon: position.coords.longitude
       }, function(data, status, jqxhr) {
+        $('#stops-list-template').autoRender(data);
         return console.log(data);
       });
-    });
-  };
-  app.init = function() {
-    return app.bindings();
-  };
-  return app.init();
-})(nextrainapp);
+    };
+    app.locError = function(error) {
+      console.log("Cannot acquire location.");
+      switch (error.code) {
+        case error.PERMISSION_DENIED:
+          return alert("User did not share location data.");
+        case error.POSITION_UNAVAILABLE:
+          return alert("Could not detect current position");
+        case error.TIMEOUT:
+          return alert("Retrieving position timed out");
+        default:
+          return alert("Unknown error");
+      }
+    };
+    app.bindings = function() {
+      $('#indexpage').live('pagebeforecreate', function(event) {
+        if (navigator.geolocation) {
+          return navigator.geolocation.getCurrentPosition(app.locSuccess, app.locError, {
+            enableHighAccuracy: true,
+            maximumAge: 6000,
+            timeout: 30000
+          });
+        }
+      });
+      return $('#stoppage').live('pagebeforecreate', function(event) {
+        var stop_id, timestamp;
+        stop_id = _.last(event.target.baseURI.split('/'));
+        timestamp = new Date().getTime();
+        return $.post('/traintimes', {
+          timestamp: timestamp,
+          stop_id: stop_id
+        }, function(data, status, jqxhr) {
+          var $timesList, time, _fn, _i, _len, _ref;
+          $timesList = $('#times-list');
+          _ref = data.times;
+          _fn = function(time) {
+            return $timesList.append("<li>" + time.trip_headsign + "<span class='ui-li-aside'>" + time.departure_time + "</span></li>");
+          };
+          for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+            time = _ref[_i];
+            _fn(time);
+          }
+          return $timesList.listview('refresh');
+        });
+      });
+    };
+    app.init = function() {
+      return app.bindings();
+    };
+    return app.init();
+  })(nextrainapp);
+
+}).call(this);

File src/nextrain/controller.clj

         nextrain.utils
         hiccup.core
         ring.util.json-response
-        [korma.core :only [select where]]
-        [clojure.data.json :only [json-str]]))
+        [korma.core :only [select where join fields subselect order]]
+        [clojure.data.json :only [json-str]]
+        [clojure.pprint :only [pprint cl-format]])
+  (:import (java.util Calendar)
+           (java.text SimpleDateFormat)))
+
+; 30 minutes in millis
+(defonce time-offset (* 30 60 1000))
+(defonce calendar-date-format (SimpleDateFormat. "yyyyMMdd"))
+(defonce train-time-format    (SimpleDateFormat. "HH:mm:ss"))
 
 (defn nearest-stops [coord]
   (let [[[lat-min lon-min] [lat-max lon-max]] (bounding-box coord 1500)
   (select stops
     (where {:stop_id stop-id})))
 
+(defn next-train [params]
+  (let [timestamp (read-string (:timestamp params))
+        client-time (Calendar/getInstance)
+        client-future (Calendar/getInstance)
+        _ (.setTimeInMillis client-time timestamp)
+        _ (.setTimeInMillis client-future (+ timestamp
+                                             time-offset))
+        time (.format train-time-format (.getTime client-time))
+        future (.format train-time-format (.getTime client-future))
+        date (.format calendar-date-format (.getTime client-time))
+        stop-id (:stop_id params)]
+    (select stop_times
+      (fields :trip_id :departure_time :trips.trip_headsign :trips.route_id)
+      (join trips (= :stop_times.trip_id :trips.trip_id))
+      (where (and {:stop_id stop-id}
+                  {:departure_time [>= time]}
+                  {:departure_time [<  future]}
+                  {:trips.direction_id 1} ; outbound
+                  {date [in (subselect calendar_dates
+                                       (fields :date)
+                                       (where {:service_id :trips.service_id}))]}
+                  ))
+      (order :departure_time)
+      )
+    ))
+
+
 ;;;
 ;;; Handlers
 ;;;
 (defn stop [stop-id]
   (layout :body (stop-mobile-page (first (stop-info stop-id)))))
 
-(defn find-stop-times [params]
-  (println "find-stop-times:" params)
-  (str params))
+(defn next-train-times [params]
+  (println "next-train-times:" params)
+  (let [times (next-train params)]
+    (pprint times)
+    (flush)
+    (json-response {:times times})))
 
 (defn find-stops [params]
   (let [lat (read-string (:lat params))

File src/nextrain/core.clj

   (GET "/" [] (index))
   (GET "/stop/:id" [id] (stop id))
   (POST "/findstops" {params :params} (find-stops params))
-  (POST "/stoptimes" {params :params} (find-stop-times params))
+  (POST "/traintimes" {params :params} (next-train-times params))
   (GET "/ses" {params :params
                session :session} (testsession params session))
   (route/resources "/")

File src/nextrain/data/gtfs.clj

 
 (deftable gtfsdb stop_times
   [:trip_id :integer]
-  [:arrival_time "varchar(128)"]
-  [:departure_time "varchar(128)"]
+  [:arrival_time :datetime]
+  [:departure_time :datetime]
   [:stop_id "varchar(16)"]
   [:stop_sequence :integer]
   [:pickup_type :integer]
 
 (deftable gtfsdb calendar_dates
   [:service_id :integer]
-  [:date "varchar(128)"]
+  [:date :datetime]
   [:exception_type :integer])
 
 (deftable gtfsdb trips
   [:agency_url "varchar(512)"]
   [:agency_timezone "varchar(128)"]
   [:agency_lang"varchar(8)"]
-  [:agency_phone "varchar(24)"])
+  [:agency_phone "varchar(24)"])
+
+;; Query Test
+;; (use 'korma.db)
+;; (use 'korma.core)
+;; (select stop_times
+;;     (fields :trip_id :departure_time :trips.trip_headsign)
+;;     (join trips (= :stop_times.trip_id :trips.trip_id))
+;;     (where (and {:stop_id 105}
+;;                 {:departure_time [>= "21:40:00"]}
+;;                 {:departure_time [<  "22:00:00"]}
+;;                 {:trips.direction_id 1}
+;;                 {"20120620" [in (subselect calendar_dates
+;;                                     (fields :date)
+;;                                     (where {:service_id :trips.service_id}))]}
+;;                 )))

File src/nextrain/snippets.clj

   (println stop)
   (mobile-page :id "stoppage"
                :header  (html [:h1 (:stop_name stop)])
-               :content (html [:h1 (str)])))
+               :content (html [:div#times-list-template
+                               [:ul#times-list {:data-role "listview"}]])))