forked from Blinkinlabs/BlinkyTape_Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Huxley_UK_Rail_Station_Delays.py
82 lines (60 loc) · 2.73 KB
/
Huxley_UK_Rail_Station_Delays.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
"""
Huxley - UK Live Train Status Blinky Tape Example
This example uses the Huxley Live Departure Board delays endpoint:
https://github.com/jpsingleton/Huxley#delays
If there are delays between the configured stations then the Blinky Tape will throb red.
To run on default (Raspberry Pi) USB serial port: python Huxley_UK_Rail_Station_Delays.py &
(C) 2015 James Singleton (https://unop.uk)
MIT Licensed
"""
from BlinkyTape import BlinkyTape
from time import sleep
from itertools import chain
import optparse
import urllib
import json
import tempfile
# This token will only work on the demo server. You may also want to host Huxley yourself.
# Get a token here: https://realtime.nationalrail.co.uk/OpenLDBWSRegistration/Registration
accessToken = "DA1C7740-9DA0-11E4-80E6-A920340000B1"
# CRS codes here: http://www.nationalrail.co.uk/static/documents/content/station_codes.csv
crs = "clj" # Clapham Junction
filterCrs = "wat" # to Waterloo
trainTime = "" # STD of a specific train to look for (24hr format HHmm) blank for none
# 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 Huxley_UK_Rail_Station_Delays.py -p <port name>"
print "(ex.: python Huxley_UK_Rail_Station_Delays.py -p /dev/ttyACM0)"
exit()
url = "https://huxley.apphb.com/delays/{}/to/{}/50/{}?accessToken={}".format(crs, filterCrs, trainTime, accessToken)
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 = urllib.urlopen(url)
stationStatus = json.load(rawHttpResponse)
if not len(stationStatus) or stationStatus is None:
raise Exception("Error parsing data")
alert = stationStatus["delays"] # bool
print "%s to %s - Trains Delayed by over 5 minutes: %s" % (
stationStatus["locationName"], stationStatus["filterLocationName"], stationStatus["delays"])
# Throb red for delays (or black for none) - takes at least 2 min
for x in xrange(60):
for y in chain(xrange(100), xrange(100, 0, -1)):
bt.displayColor(y * alert, 0, 0)
sleep(0.01)
except:
# Blue indicates an error
bt.displayColor(0, 0, 100)
sleep(120) # wait 2 min
pass