Created
December 9, 2021 15:21
-
-
Save EvelynSubarrow/53a78988371ee512e30063bf51518c3c to your computer and use it in GitHub Desktop.
Network Rail TD demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import logging | |
from time import sleep | |
import json | |
# dependencies are: | |
# stomp.py, termcolor | |
from termcolor import colored | |
import stomp | |
trains = {} | |
NETWORK_RAIL_AUTH = ('email', 'password') | |
DESCRIPTORS = { | |
"WI:F*01" : "London Waterloo [Pl F 1]", | |
"WI:F*03" : "London Waterloo [Pl F 2]", | |
"WI:F*05" : "London Waterloo [Pl F 3]", | |
"WI:F*07" : "London Waterloo [Pl F 4]", | |
"WI:F*09" : "London Waterloo [Pl F 5]", | |
"WI:F*11" : "London Waterloo [Pl F 6]", | |
"WI:F*13" : "London Waterloo [Pl F 7]", | |
"WI:F*15" : "London Waterloo [Pl F 8]", | |
"WI:F*17" : "London Waterloo [Pl F 9]", | |
"WI:F*19" : "London Waterloo [Pl F 10]", | |
"WI:F*21" : "London Waterloo [Pl F 11]", | |
"WI:F*23" : "London Waterloo [Pl F 12]", | |
"WI:F*25" : "London Waterloo [Pl F 13]", | |
"WI:F*27" : "London Waterloo [Pl F 14]", | |
"WI:F*29" : "London Waterloo [Pl F 15]", | |
"WI:F*31" : "London Waterloo [Pl F 16]", | |
"WI:F*33" : "London Waterloo [Pl F 17]", | |
"WI:F*35" : "London Waterloo [Pl F 18]", | |
"WI:F*37" : "London Waterloo [Pl F 19]", | |
"WI:F901" : "London Waterloo [Pl F 20]", | |
"WI:F903" : "London Waterloo [Pl F 21]", | |
"WI:F905" : "London Waterloo [Pl F 22]", | |
"WI:F907" : "London Waterloo [Pl F 23]", | |
"WI:F909" : "London Waterloo [Pl F 24]", | |
"WI:R001" : "London Waterloo [Pl R 1]", | |
"WI:R003" : "London Waterloo [Pl R 2]", | |
"WI:R005" : "London Waterloo [Pl R 3]", | |
"WI:R007" : "London Waterloo [Pl R 4]", | |
"WI:R009" : "London Waterloo [Pl R 5]", | |
"WI:R011" : "London Waterloo [Pl R 6]", | |
"WI:R013" : "London Waterloo [Pl R 7]", | |
"WI:R015" : "London Waterloo [Pl R 8]", | |
"WI:R017" : "London Waterloo [Pl R 9]", | |
"WI:R019" : "London Waterloo [Pl R 10]", | |
"WI:R021" : "London Waterloo [Pl R 11]", | |
"WI:R023" : "London Waterloo [Pl R 12]", | |
"WI:R025" : "London Waterloo [Pl R 13]", | |
"WI:R027" : "London Waterloo [Pl R 14]", | |
"WI:R029" : "London Waterloo [Pl R 15]", | |
"WI:R031" : "London Waterloo [Pl R 16]", | |
"WI:R033" : "London Waterloo [Pl R 17]", | |
"WI:R035" : "London Waterloo [Pl R 18]", | |
"WI:R037" : "London Waterloo [Pl R 19]", | |
"WI:R901" : "London Waterloo [Pl R 20]", | |
"WI:R903" : "London Waterloo [Pl R 21]", | |
"WI:R905" : "London Waterloo [Pl R 22]", | |
"WI:R907" : "London Waterloo [Pl R 23]", | |
"WI:R909" : "London Waterloo [Pl R 24]", | |
"WI:0155" : "Clapham Junction [Plat 11]", | |
"WI:0120" : "Clapham Junction [Plat 10]", | |
"WI:0157" : "Clapham Junction [Plat 9]", | |
"WI:0122" : "Clapham Junction [Plat 8]", | |
"WI:0124" : "Clapham Junction [Plat 7]", | |
"WI:0307" : "Clapham Junction [Plat 6]", | |
"WI:0309" : "Clapham Junction [Plat 5]", | |
"WI:0300" : "Clapham Junction [Plat 4]", | |
"WI:0302" : "Clapham Junction [Plat 3]", | |
"WI:0306" : "Clapham Junction [Plat 2]", | |
} | |
class Listener(object): | |
def __init__(self, mq): | |
self._mq = mq | |
self.colour = [lambda x: x, lambda x: colored(x, "white")] | |
def on_message(self, frame): | |
headers, message = frame.headers, frame.body | |
self._mq.ack(id=headers['message-id'], subscription=headers['subscription']) | |
parsed = json.loads(message) | |
for foo in parsed: | |
for actual in foo.values(): | |
if actual["area_id"]=="ZZ" or actual["msg_type"] in ["CT", "SF", "SG", "SH"]: continue | |
self.colour = self.colour[::-1] | |
print(self.colour[0]("[{0:2}] {1:4} {2:>30} {3:>5}->{4:5} {5:30}".format( | |
actual["area_id"], actual.get("descr", " "), | |
DESCRIPTORS.get(actual["area_id"] + ":" + actual.get("from", ""), ''), | |
actual.get("from", " "), actual.get("to", " "), | |
DESCRIPTORS.get(actual["area_id"] + ":" + actual.get("to", ""), '') | |
))) | |
def on_error(self, frame): | |
message = frame.body | |
print('received an error "%s"' % message) | |
mq = stomp.Connection([('datafeeds.networkrail.co.uk', 61618)], | |
keepalive=True, heartbeats=(10000, 5000)) | |
mq.set_listener('', Listener(mq)) | |
#mq.start() | |
mq.connect(**{ | |
"username": NETWORK_RAIL_AUTH[0], | |
"passcode": NETWORK_RAIL_AUTH[1], | |
"wait": True, | |
"client-id": NETWORK_RAIL_AUTH[0], | |
}) | |
mq.subscribe(**{ | |
"destination": "/topic/TD_ALL_SIG_AREA", | |
"id": 1, | |
"ack": "client-individual", | |
"activemq.subscriptionName": "sekmet-td", | |
}) | |
while mq.is_connected(): | |
sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment