Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions muniled.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,16 @@ def main(args):
elif args.display == 'console':
led_strip = ConsoleStrip(160)
led_strip.all_off()

try:
while True:
update(led_strip)
update(led_strip)
time.sleep(5.0)
except:
led_strip.all_off()
raise


def get_predictions(route, stop_id):
def get_predictions(route, stop_id, led_strip):
base_url = 'http://webservices.nextbus.com/service/publicXMLFeed'
params = {
'command': 'predictions',
Expand All @@ -52,9 +51,20 @@ def get_predictions(route, stop_id):
's': stop_id,
'useShortTitles': 'true'
}
response = urllib2.urlopen('{url}?{params}'.format(
url=base_url,
params=urllib.urlencode(params)))

# attempt to fetch and retry on error after a pause
while True:
try:
response = urllib2.urlopen('{url}?{params}'.format(
url=base_url,
params=urllib.urlencode(params)))
break
except urllib2.URLError, e:
print e.reason
dim(led_strip)
time.sleep(5.0)
pass

dom = minidom.parse(response)
predictions = dom.getElementsByTagName('prediction')
return [int(p.attributes['minutes'].value) for p in predictions]
Expand All @@ -64,11 +74,11 @@ def update(led_strip=None):
line = [False for i in range(led_strip.leds)]

# inbound
for prediction in get_predictions('N', 3915):
for prediction in get_predictions('N', 3915, led_strip):
line[80 - prediction - 1] = True

# outbound
for prediction in get_predictions('N', 3914):
for prediction in get_predictions('N', 3914, led_strip):
line[80 + prediction] = True

# set train leds
Expand All @@ -85,6 +95,12 @@ def update(led_strip=None):
# Update the strip
led_strip.update()

def dim(led_strip):
# dim all the leds a little
# TODO
pass


if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Muni trains on a LED strip.')
parser.add_argument('-d', '--display', choices=['led', 'console'],
Expand Down