Skip to content
This repository has been archived by the owner on Feb 16, 2022. It is now read-only.

Commit

Permalink
Move info window message creation to Go instead of Javascript
Browse files Browse the repository at this point in the history
  • Loading branch information
jonesc10 committed May 6, 2016
1 parent 56d02d1 commit 9db5545
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 21 deletions.
1 change: 1 addition & 0 deletions main.go
Expand Up @@ -40,6 +40,7 @@ func main() {
r.HandleFunc("/vehicles", App.VehiclesHandler).Methods("GET")
r.HandleFunc("/vehicles/create", App.VehiclesCreateHandler).Methods("POST")
r.HandleFunc("/updates", App.UpdatesHandler).Methods("GET")
r.HandleFunc("/updates/message", App.UpdateMessageHandler).Methods("GET")
r.HandleFunc("/routes", App.RoutesHandler).Methods("GET")
r.HandleFunc("/routes/create", App.RoutesCreateHandler).Methods("POST")
r.HandleFunc("/stops", App.StopsHandler).Methods("GET")
Expand Down
35 changes: 14 additions & 21 deletions static/components/shuttles-map.html
Expand Up @@ -66,6 +66,12 @@
handle-as="json"
on-response="placeStops">
</iron-ajax>
<iron-ajax
id="fetchMessages"
url="/updates/message"
handle-as="json"
last-response="{{messages}}">
</iron-ajax>

<google-map id="shuttleMap" class="shuttle-map" zoom="{{zoom}}" latitude="{{lat}}" longitude="{{lng}}"></google-map>
<div id="logo">
Expand Down Expand Up @@ -138,6 +144,10 @@
stops: {
type: Array,
value: []
},
messages: {
type: Array,
value: []
}
},

Expand All @@ -150,6 +160,7 @@
},

initializeTracking: function() {
this.$.fetchMessages.generateRequest();
this.$.fetchRoutes.generateRequest();
this.$.fetchUpdates.generateRequest();
this.$.fetchStops.generateRequest();
Expand All @@ -158,6 +169,7 @@

periodicUpdates: function() {
// periodically check for vehicle & route updates
setInterval(this.checkForUpdates, 15000, this.$.fetchMessages);
setInterval(this.checkForUpdates, 15000, this.$.fetchUpdates);
setInterval(this.checkForUpdates, 15000, this.$.fetchRoutes);
setInterval(this.checkForUpdates, 15000, this.$.fetchStops);
Expand All @@ -167,20 +179,6 @@
ajax.generateRequest();
},

// Translate heading (0-360 degrees) to cardinal direction
cardinalDirection: function(heading) {
switch (true) {
case (heading >= 22.5 && heading < 67.5): return "North-East";
case (heading >= 67.5 && heading < 112.5): return "East";
case (heading >= 112.5 && heading < 157.5): return "South-East";
case (heading >= 157.5 && heading < 202.5): return "South";
case (heading >= 202.5 && heading < 247.5): return "South-West";
case (heading >= 247.5 && heading < 292.5): return "West";
case (heading >= 292.5 && heading < 337.5): return "North-West";
default: return "North";
}
},

placeVehicles: function(event, response) {
var updates = response.response;
if (updates) {
Expand All @@ -200,15 +198,10 @@
title: updates[i]["vehicleID"]
});

// Get cardinal direction for info window
var cardinal_dir = this.cardinalDirection(updates[i]["heading"]);
// Get update message for the info window
var contentString = this.messages[i];
// Create info window and open it when shuttle clicked
(function(marker, i) {
/* Include ID, heading, speed, time in the infowindow */
var contentString = '<b>Shuttle ID: ' + updates[i]["vehicleID"] + '</b><br />' +
'Traveling ' + cardinal_dir + ' at ' + updates[i]["speed"].substring(0,4) + 'mph<br />' +
'as of ' + updates[i]["time"].substring(0,2) + ':' + updates[i]["time"].substring(2,4) + ':' + updates[i]["time"].substring(4,6);

var infowindow = new google.maps.InfoWindow({
content: contentString
});
Expand Down
51 changes: 51 additions & 0 deletions tracking/vehicles.go
Expand Up @@ -7,6 +7,8 @@ import (
"regexp"
"strings"
"time"
"fmt"
"strconv"

log "github.com/Sirupsen/logrus"
"gopkg.in/mgo.v2/bson"
Expand Down Expand Up @@ -175,3 +177,52 @@ func (App *App) UpdatesHandler(w http.ResponseWriter, r *http.Request) {
// Convert updates to JSON
WriteJSON(w, updates)
}

// UpdateMessageHandler generates a message about an update for a vehicle
func (App *App) UpdateMessageHandler(w http.ResponseWriter, r *http.Request) {
// For each vehicle/update, store message as a string
var messages []string
var message string
var vehicles []Vehicle
var update VehicleUpdate
// Query all Vehicles
err := App.Vehicles.Find(bson.M{}).All(&vehicles)
// Handle errors
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
// Find recent updates and generate message
for _, vehicle := range vehicles {
err := App.Updates.Find(bson.M{"vehicleID": vehicle.VehicleID}).Sort("-created").Limit(1).One(&update)
if (err == nil) {
// Use first 4 char substring of update.Speed
speed := update.Speed
if (len(speed) > 4) {
speed = speed[0:4]
}
message = fmt.Sprintf("<b>%s</b><br/>Traveling %s at<br/> %s mph as of %s", vehicle.VehicleName, CardinalDirection(&update.Heading), speed, update.Created.Format("3:04PM"))
messages = append(messages, message)
}
}
// Convert to JSON
WriteJSON(w, messages)
}

// CardinalDirection figures out the cardinal direction of a vehicle's heading
func CardinalDirection(h *string) string {
heading, err := strconv.ParseFloat(*h,64)
if (err != nil) {
fmt.Println("ERROR",err.Error())
return "North"
}
switch {
case (heading >= 22.5 && heading < 67.5): return "North-East"
case (heading >= 67.5 && heading < 112.5): return "East"
case (heading >= 112.5 && heading < 157.5): return "South-East"
case (heading >= 157.5 && heading < 202.5): return "South"
case (heading >= 202.5 && heading < 247.5): return "South-West"
case (heading >= 247.5 && heading < 292.5): return "West"
case (heading >= 292.5 && heading < 337.5): return "North-West"
default: return "North"
}
}

0 comments on commit 9db5545

Please sign in to comment.