forked from KaitaiD/py-network-rail-feeder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord_tds.py
128 lines (107 loc) · 4.36 KB
/
record_tds.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
## TODO
## Reset if lost network or stomp connection
## Read only sd card (prob needs USB)
## Remember to wipe the train ids thingy array every midnight
## If train not identified maybe show both IDs (at the moment just shows headcode)
## will that perf counter thing ever run out? Probs not.
## Show weather or whatever other stuff when no train things happening...
## Update script
## Train IDs still not quite there maybe, that Birmingham Service?
import stomp
import json
import time
import creds
import csv
import logging
import datetime
#import mysql.connector as mysql
dev = False
logging.basicConfig(
filename='activations.log', filemode='a',
format='%(asctime)s %(levelname)-8s %(message)s',
level=logging.CRITICAL,
datefmt='%Y-%m-%d %H:%M:%S')
HOSTNAME = "datafeeds.networkrail.co.uk"
USERNAME = creds.USERNAME
PASSWORD = creds.PASSWORD
td_channel = "TD_ALL_SIG_AREA"
mvt_channel = "TRAIN_MVT_ALL_TOC"
start_time = time.perf_counter()
show_trains = False
train_last_seen = [0,0]
train_text = ["", ""]
train_change = False
current_display = "BLANK"
last_td_message = start_time
last_mvt_message = start_time
train_fake = [False, False, False, False]
print("time ", time.process_time())
train_ids = {}
# try:
# filehandler = open("train_ids", 'rb')
# train_ids = pickle.load(filehandler)
# except:
# print ("couldn't load file")
activations = {}
# db = mysql.connect(
# host = "localhost",
# user = "nikhil",
# passwd = "Bd75W*0p1hB",
# database = "trains"
# )
# cursor = db.cursor()
class TDListener(stomp.ConnectionListener):
def on_error(self, headers, message):
print('received an error "%s"' % message)
logging.critical("Error in TDListener "+str(message))
def on_message(self, headers, messages):
for message in json.loads(messages):
if "CA_MSG" in message and message["CA_MSG"]["to"] in [ "1809", "1807", "1802", "1804"]: #2021 south 2018 north
show_trains = True
print(message)
class MVTListener(stomp.ConnectionListener):
def on_error(self, headers, message):
print('received an error "%s"' % message)
logging.critical("Error in MVTListener "+str(message))
def on_message(self, headers, messages):
global last_mvt_message
last_mvt_message = time.perf_counter()
#print(G+'received a message "%s"' % message)
for message in json.loads(messages):
if message['header']['msg_type'] == "0001": # this will look up all train activations and find the exact uid and trust id of our train
msg = message['body']
print(msg['train_id'])
print("uid", msg['train_uid'])
activations[msg['train_id']] = {
"train_uid": msg['train_uid'],
"train_service_code": msg['train_service_code']
}
query = "INSERT INTO activations (train_uid, train_service_code, train_id) VALUES (%s, %s, %s)"
values = (msg['train_uid'], msg['train_service_code'], msg['train_id'])
cursor.execute(query, values)
db.commit()
# filehandler = open("activations", 'w')
# filehandler.write(json.dumps(activations, indent=4))
# filehandler.close()
# #filehandler = open("train_ids", 'wb')
# #pickle.dump(train_ids, filehandler)
# if str(msg['train_service_code']) == "22180008":
# filehandler = open("found_service_code.txt", 'a')
# filehandler.write(json.dumps(msg, indent=4))
# filehandler.close()
def make_connections():
print("reset connections")
logging.critical("resetting connections")
# mvt_conn = stomp.Connection(host_and_ports=[(HOSTNAME, 61618)])
# mvt_conn.set_listener('', MVTListener())
# mvt_conn.start()
# mvt_conn.connect(username=USERNAME, passcode=PASSWORD)
# mvt_conn.subscribe(destination=f"/topic/{mvt_channel}", id=1, ack='auto')
td_conn = stomp.Connection(host_and_ports=[(HOSTNAME, 61618)])
td_conn.set_listener('', TDListener())
td_conn.start()
td_conn.connect(username=USERNAME, passcode=PASSWORD)
td_conn.subscribe(destination=f"/topic/{td_channel}", id=1, ack='auto')
make_connections()
while 1:
time.sleep(1)