From e03650fb65dc7104cf6804a80c8a042f34b5f48a Mon Sep 17 00:00:00 2001 From: Dustin Selman Date: Tue, 5 Mar 2013 00:44:35 +0000 Subject: [PATCH 1/2] Continue to retry after network exception --- muniled.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/muniled.py b/muniled.py index 80db42f..637409e 100644 --- a/muniled.py +++ b/muniled.py @@ -33,11 +33,17 @@ def main(args): elif args.display == 'console': led_strip = ConsoleStrip(160) led_strip.all_off() - try: while True: - update(led_strip) - time.sleep(5.0) + try: + update(led_strip) + except urllib2.URLError, e: + print e.reason + pass + except: + raise + finally: + time.sleep(5.0) except: led_strip.all_off() raise From cbc18a00ccb6f9d899322ea090a6abefce8092c1 Mon Sep 17 00:00:00 2001 From: Dustin Selman Date: Mon, 4 Mar 2013 18:40:34 -0800 Subject: [PATCH 2/2] Moved network retry handling into get_prediction --- muniled.py | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/muniled.py b/muniled.py index 637409e..aa3079c 100644 --- a/muniled.py +++ b/muniled.py @@ -35,21 +35,14 @@ def main(args): led_strip.all_off() try: while True: - try: - update(led_strip) - except urllib2.URLError, e: - print e.reason - pass - except: - raise - finally: - time.sleep(5.0) + 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', @@ -58,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] @@ -70,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 @@ -91,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'],