diff --git a/protosign.py b/protosign.py index a976abf..d95fea4 100644 --- a/protosign.py +++ b/protosign.py @@ -3,6 +3,9 @@ from vestactrl import setup_digits, board_init_uart import textwrap +import urllib2 +import json +import time def send_sign(text): """ @@ -26,8 +29,31 @@ def send_sign(text): setup_digits(board_lines, left_to_right_swap=False, real_hw=True) +def poll_and_display(): + """ + Polls the Protospace API and displays the sign message. + """ + last_message = "" + url = 'https://api.my.protospace.ca/stats/' + + while True: + try: + response = urllib2.urlopen(url) + data = json.load(response) + message = data.get('sign') + + if message and message != last_message: + print "Updating sign with new message: " + message + send_sign(message) + last_message = message + + except (urllib2.URLError, ValueError, KeyError) as e: + print "Error: {}".format(e) + + time.sleep(5) + + if __name__ == '__main__': # Initialize communication with the board. board_init_uart() - message = "This is a test of the protosign script. This message should wrap across multiple lines and be truncated if it is too long to fit on the display." - send_sign(message) + poll_and_display()