-
Notifications
You must be signed in to change notification settings - Fork 17
/
London_Underground_Status.py
101 lines (86 loc) · 3.25 KB
/
London_Underground_Status.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""
London Underground Status for Blinky Tape
To run on default (Raspberry Pi) USB serial port: python London_Underground_Status.py &
(C) 2015 James Singleton (https://unop.uk)
MIT Licensed
"""
import tempfile
import json
from blinkytape import BlinkyTape
from time import sleep
import optparse
import sys
if sys.version_info > (3, 0):
import urllib.request as requestlib
else:
import urllib2 as requestlib
# Default Blinky Tape port on Raspberry Pi is /dev/ttyACM0
parser = optparse.OptionParser()
parser.add_option("-p", "--port", dest="portname",
help="serial port (ex: /dev/ttyACM0)", default="/dev/ttyACM0")
(options, args) = parser.parse_args()
if options.portname is not None:
port = options.portname
else:
print("Usage: python London_Underground_Status.py -p <port name>")
print("(ex.: python London_Underground_Status.py -p /dev/ttyACM0)")
exit()
url = "https://api.tfl.gov.uk/line/mode/tube,overground,dlr,tflrail/status"
colours = {"bakerloo": (137, 78, 36),
"central": (220, 36, 31),
"circle": (255, 206, 0),
"district": (0, 114, 41),
"dlr": (0, 175, 173),
"hammersmith-city": (215, 153, 175),
"jubilee": (106, 114, 120),
"london-overground": (232, 106, 16),
"metropolitan": (117, 16, 86),
"northern": (0, 0, 0),
"piccadilly": (0, 25, 168),
"victoria": (0, 160, 226),
"waterloo-city": (118, 208, 189),
"tfl-rail": (0, 25, 168)}
bt = BlinkyTape(port)
# Some visual indication that it works for headless setups (green tape)
bt.displayColor(0, 100, 0)
sleep(2)
# Tape resets to stored pattern after a couple of seconds of inactivity
while True:
try:
print("GET %s" % (url))
rawHttpResponse = requestlib.urlopen(url)
lines = json.load(rawHttpResponse)
if not len(lines) or lines is None:
raise Exception("Error parsing data")
# Sort the lines
lines.sort(key=lambda l: l['modeName'], reverse=True)
# Takes at least 2 min
for cycles in range(240):
for pixel in range(2):
bt.sendPixel(0, 0, 0)
for line in lines:
alert = False
for status in line['lineStatuses']:
# https://api.tfl.gov.uk/line/meta/severity
if status['statusSeverity'] != 10:
alert = True
r, g, b = colours[line['id']]
for pixel in range(4):
if alert:
# Flash for delays
l = (cycles - pixel) % 2
if line['id'] == "northern":
# Because black on black doesn't show up
r, g, b = 100, 100, 100
bt.sendPixel(l * r, l * g, l * b)
else:
bt.sendPixel(r, g, b)
for pixel in range(2):
bt.sendPixel(0, 0, 0)
bt.show()
sleep(0.5)
except:
# Blue indicates an error
bt.displayColor(0, 0, 100)
sleep(120) # wait 2 min
pass