Skip to content

Instantly share code, notes, and snippets.

@cannuk
Last active October 24, 2015 08:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cannuk/589d7d4dcbefc3af024e to your computer and use it in GitHub Desktop.
Save cannuk/589d7d4dcbefc3af024e to your computer and use it in GitHub Desktop.
Dashing BART

Dashing BART

A simple Dashing widget to show upcoming departures for a specific BART station. You can filter the destinations by platform, direction and specific destination trains.

ScreenShot

Installation

To install, use the Dashing gist install command with the GIST_ID, like so

dashing install 589d7d4dcbefc3af024e

You will also need to install the Bart API gem from Jish. Note: this widget makes use of a feature I have added to a fork of the bart gem. Once my pull request is merged I will update this gist. Until then, use my branch by placing the following in your GEMFILE:

gem 'bart', github: 'cannuk/bart', branch: 's-update-destination-query'

Usage

The Dashing BART widget can be added to your dashing dashboard easily like this:

<li data-row="1" data-col="2" data-sizex="1" data-sizey="1">
   <div data-id="bart" data-view="Bart" data-title="Bart"></div>
</li>

In the bart.rb file you need to specify the departure station:

DEPART_STATION = {abbr: "embr"}

You can add train direction and or train platform to filter the results:

DEPART_STATION = {abbr: "embr", dir: "s", plat: 1}

You can also filter to specific destination trains:

DESTINATION_STATIONS = ["mlbr", "sfia"]

Further station abbreviations can be found at the BART API

License

Dashing BART is released under the MIT license.

Thanks

Thanks to the Dashing MTA and the RSS Feed widgets for providing insparation and examples.

class Dashing.Bart extends Dashing.Widget
ready: ->
@stationElem = $(@node).find('.station')
@estimatesElem = $(@node).find('.estimates')
@wrapElem = $(@node).find('.wrap')
@updateDestination()
onData: (data) ->
@updateDestination()
updateDestination: =>
@destination = @get("destination")
@destination.formatted_estimates = ""
@destination.formatted_estimates += @formatEstimate estimate for estimate in @destination.estimates
@set 'curr_dest', @destination
formatEstimate: (est) =>
"<div class='estimate'><div class='time'>#{est.minutes.toString().lpad("&nbsp;", 2)} min</div><div class='cars'>#{est.train_length.toString().lpad("&nbsp;", 2)} cars</div></div>"
#stding padding function
if (typeof String::lpad != 'function')
String::lpad = (padString, length) ->
str = this
while str.length < length
str = padString + str
return str
<div class="wrap">
<h3 class="station" data-bind="curr_dest.station_name | raw"></h3>
<div class="estimates" data-bind="curr_dest.formatted_estimates | raw"></div>
</div>
require 'bart/station'
#station abbreviations you want departure estimates for.
#You can also specify direction and platform according to the BART api
#(http://api.bart.gov/docs/etd/etd.aspx)
#ex: {abbr: "mlbr", plat: 1}
# {abbr: "embr", dir: "s"}
DEPART_STATION = {abbr: "embr", dir: "s"}
#Filter the list of destination estimates from the depart stations to the following stations
DESTINATION_STATIONS = ["mlbr", "sfia"]
UPDATE = '5m' #This is how often we query the BART API. Don't be a jerk and do it too often...
UPDATE_DISPLAY = '10s' #This is how often we update the display on the dashboard.
class BartEstimate
def initialize (options)
@depart_station = options[:depart_station]
@destination_stations = options[:destination_stations]
@destination_index = 0
@dest_estimates = Array.new
@last_updated = Time.now
end
#Handles fetching from the Bart API
def fetch_destination_estimates
#create the station object
station = Bart::Station.new(abbr:@depart_station[:abbr])
#load the departures passing any extra parameters
params = @depart_station.select {|k| [:plat, :dir].include?(k)}
departures = station.load_departures(params)
#filter the departures to our destinations
if @destination_stations.length > 0
@dest_estimates = departures.select { |d| @destination_stations.include?(d.destination.abbr) }
else
@dest_estimates = departures
end
@last_updated = Time.now
end
#Formats and returns a single destination ready for the front end.
def get_destination
if @dest_estimates.length > 0
@destination_index = 0 if @destination_index >= @dest_estimates.length
destination = Hash.new
dest = @dest_estimates[@destination_index]
destination[:abbr] = dest.destination.abbr
destination[:station_name] = dest.destination.name
destination[:estimates] = Array.new
dest.estimates.each do |e|
#get the difference since the last update in seconds
minutes_since = ((Time.now - @last_updated)/60).round
#only add to the estimates if the train hasn't departed yet
if minutes_since <= e.minutes
destination[:estimates] << {:minutes => (e.minutes - minutes_since), :train_length => e.length}
end
end
@destination_index += 1
destination
end
end
end
#Create our BartEstimate with the values and call bart
@BartEst = BartEstimate.new({depart_station: DEPART_STATION, destination_stations: DESTINATION_STATIONS})
@BartEst.fetch_destination_estimates
#Send the first event
send_event('bart', destination: @BartEst.get_destination)
#This queries the BART API
SCHEDULER.every UPDATE, :first_in => 0 do |job|
@BartEst.fetch_destination_estimates
end
#This sends the data to the front end. It accounts for time since update
SCHEDULER.every UPDATE_DISPLAY, :first_in => 1 do |job|
send_event('bart', destination: @BartEst.get_destination)
end
// ----------------------------------------------------------------------------
// Sass declarations
// ----------------------------------------------------------------------------
$borderColor: #000;
$text-color: #FF0000;
// ----------------------------------------------------------------------------
// Widget-text styles
// ----------------------------------------------------------------------------
.bart {
font-family:'Conv_modern_led_board-7',Sans-Serif;
background: -moz-radial-gradient(center, ellipse cover, #222222 0%, #000000 100%); /* FF3.6+ */
background: -webkit-gradient(radial, center center, 0px, center center, 100%, color-stop(0%,#222222), color-stop(100%,#000000)); /* Chrome,Safari4+ */
background: -webkit-radial-gradient(center, ellipse cover, #222222 0%,#000000 100%); /* Chrome10+,Safari5.1+ */
background: -o-radial-gradient(center, ellipse cover, #222222 0%,#000000 100%); /* Opera 12+ */
background: -ms-radial-gradient(center, ellipse cover, #222222 0%,#000000 100%); /* IE10+ */
background: radial-gradient(ellipse at center, #222222 0%,#000000 100%); /* W3C */
border: 2px solid $borderColor;
.estimates {
.estimate{
clear: both;
padding-bottom: 10px;
font-size: 18px;
line-height: 35px;
color: $text-color;
.time{
float: left;
}
.cars{
float: right;
}
}
}
.station {
font-size: 24px;
line-height: 45px;
color: $text-color;
}
}
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment