-
Notifications
You must be signed in to change notification settings - Fork 2
/
blinkt-tube-status.py
213 lines (188 loc) · 5.72 KB
/
blinkt-tube-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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python
import signal
import sys
import tubestatus
import datetime
from time import sleep, localtime, strftime
from blinkt import set_pixel, show, set_brightness
RED = (255, 0, 0, 0.4)
YELLOW = (255, 255, 0, 0.4)
GOOD_SERVICE = "Good Service"
# the config for the application, stores the colours
# used for each line and when those lines operate
# (night tube is coming soon!)
CONFIG = {
"Bakerloo": {
"led_no": 7,
"colour": {
"Good Service": (132, 40, 3),
"Severe Delays": RED,
"Minor Delays": YELLOW
},
"operating_hours": {
"start": datetime.time(5, 0, 0),
"end": datetime.time(1, 0, 0),
}
},
"Central": {
"led_no": 6,
"colour": {
"Good Service": (90, 0, 0),
"Severe Delays": RED,
"Minor Delays": YELLOW
},
"operating_hours": {
"start": datetime.time(5, 0, 0),
"end": datetime.time(1, 0, 0),
}
},
"Circle": {
"led_no": 5,
"colour": {
"Good Service": (120, 120, 0),
"Severe Delays": RED,
"Minor Delays": YELLOW
},
"operating_hours": {
"start": datetime.time(5, 0, 0),
"end": datetime.time(1, 0, 0),
}
},
"District": {
"led_no": 4,
"colour": {
"Good Service": (0, 50, 0),
"Severe Delays": RED,
"Minor Delays": YELLOW
},
"operating_hours": {
"start": datetime.time(5, 0, 0),
"end": datetime.time(1, 0, 0),
}
},
"Hammersmith and City": {
"led_no": 3,
"colour": {
"Good Service": (255, 50, 50),
"Severe Delays": RED,
"Minor Delays": YELLOW
},
"operating_hours": {
"start": datetime.time(5, 0, 0),
"end": datetime.time(1, 0, 0),
}
},
"Jubilee": {
"led_no": 2,
"colour": {
"Good Service": (6, 6, 6),
"Severe Delays": RED,
"Minor Delays": YELLOW
},
"operating_hours": {
"start": datetime.time(5, 0, 0),
"end": datetime.time(1, 0, 0),
}
},
"Metropolitan": {
"led_no": 1,
"colour": {
"Good Service": (30, 0, 10),
"Severe Delays": RED,
"Minor Delays": YELLOW
},
"operating_hours": {
"start": datetime.time(5, 0, 0),
"end": datetime.time(1, 0, 0),
}
},
"Northern": {
"led_no": 0,
"colour": {
"Good Service": (100, 100, 100),
"Severe Delays": RED,
"Minor Delays": YELLOW
},
"operating_hours": {
"start": datetime.time(5, 0, 0),
"end": datetime.time(1, 0, 0),
}
}
}
def time_in_range(start, end, x):
today = datetime.date.today()
start = datetime.datetime.combine(today, start)
end = datetime.datetime.combine(today, end)
x = datetime.datetime.combine(today, x)
if end <= start:
end += datetime.timedelta(1) # tomorrow!
if x <= start:
x += datetime.timedelta(1) # tomorrow!
return start <= x <= end
def turn_led_off_for(line):
set_pixel(CONFIG[line]["led_no"], 0, 0, 0)
def clear(*args):
for line, line_desc in CONFIG.iteritems():
turn_led_off_for(line)
show()
sys.exit(0)
def set_led_status(line, line_ok):
colours = line["colour"]
led_no = line["led_no"]
if line_ok:
set_pixel(led_no, *colours[GOOD_SERVICE])
else:
# colour should be set to the colour of the status (if available)
# otherwise change to RED
status = line["status"]
if status in colours:
set_pixel(led_no, *colours[status])
else:
set_pixel(led_no, *RED)
def log(message):
t = strftime("%Y-%m-%d %H:%M:%S", localtime())
print t + " - " + message
sys.stdout.flush()
def line_is_open(line, current_time):
return time_in_range(line["operating_hours"]["start"],
line["operating_hours"]["end"],
current_time)
# Update the status of each line
def update_line_statuses(tfl_status_obj):
now = datetime.datetime.now().time()
log("Updating statuses...")
for line, line_desc in CONFIG.iteritems():
if line_is_open(line_desc, now):
line_desc["open"] = True
try:
line_desc["status"] = tfl_status_obj.get_status(line).description
log(line + " line status = " + line_desc["status"])
except:
line_desc["status"] = "error"
log(line + " [error] line status = " + line_desc["status"])
sys.stdout.flush()
else:
log(line + " line is CLOSED")
line_desc["open"] = False
turn_led_off_for(line)
def main(brightness, update_interval, blink_rate=0.1):
tfl_status_obj = tubestatus.Status()
should_blink = True
while True:
update_line_statuses(tfl_status_obj)
# update LEDS
for x in range(0, int(update_interval/blink_rate), 1):
set_brightness(brightness)
for line, line_desc in CONFIG.iteritems():
if line_desc["open"]:
if line_desc["status"] != GOOD_SERVICE:
set_led_status(line_desc, should_blink)
else:
set_led_status(line_desc, True)
show()
sleep(blink_rate)
should_blink = not should_blink
if __name__ == '__main__':
signal.signal(signal.SIGTERM, clear)
signal.signal(signal.SIGINT, clear)
main(0.3, 120, 0.7)