/
watch.py
executable file
·210 lines (170 loc) · 4.77 KB
/
watch.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
#!/usr/bin/python
import json
import logging
import os
import pickle
import pprint
import re
import sys
import tempfile
import time
import traceback
from stompest.config import StompConfig
from stompest.sync import Stomp
import matplotlib
matplotlib.use('Agg')
import networkx as nx
import matplotlib.pyplot as plt
from networkx.algorithms.traversal.depth_first_search import dfs_tree
class networkMapper(object):
def __init__(self):
self._edges = []
self._berths = {}
self._client = None
self._graphFlag = False
self._graphTime = {}
def configureClient(self, _login, _passcode):
CONFIG = StompConfig('tcp://datafeeds.networkrail.co.uk:61618', login=_login, passcode=_passcode)
client = Stomp(CONFIG)
client.connect()
client.subscribe('/topic/TD_KENT_MCC_SIG_AREA')
self._client = client
def loadEdges(self):
try:
self._edges = pickle.load(open('edges', 'r'))
self._graphFlag = True
except:
print >> sys.stderr, 'Could not load edges'
self._edges = []
def updateGraph(self, area, _from, _to):
if area != 'LB':
return
mFrom = re.match('(\d+)', _from)
if mFrom is None:
print _from
return
mTo = re.match('(\d+)', _to)
if mTo is None:
print _to
return
iFrom = int(mFrom.groups()[0])
iTo = int(mTo.groups()[0])
pair = (_from, _to)
if iFrom <= 220:
if pair not in self._edges:
self._edges.append(pair)
self._graphFlag = True
def oddNumber(self, s):
m = re.search('[13579]$', s)
return m is not None
def evenNumber(self, s):
m = re.search('[13579]$', s)
return m is None
def outputFile(self, plt, filename):
f = tempfile.NamedTemporaryFile(delete=False, prefix='/var/www/html/')
plt.savefig(f, bbox_inches='tight', format='png') # save as png
f.close()
os.chmod(f.name, 292) # 444
if os.path.exists(filename):
os.unlink(filename)
os.link(f.name, filename)
os.unlink(f.name)
def outputGraph(self, filename, _filter=None):
if filename not in self._graphTime:
self._graphTime[filename] = 0
if (time.time() - self._graphTime[filename]) > 10:
# print 'pre-Update Graph: %s' % filename
G = nx.DiGraph()
plt.clf()
for edge in self._edges:
if _filter is None:
G.add_edge(edge[0], edge[1])
elif _filter(edge[0]) or _filter(edge[1]):
G.add_edge(edge[0], edge[1])
try:
G1 = dfs_tree(G, u'0015')
G2 = dfs_tree(G, u'0013')
G3 = nx.compose(G1,G2)
G4 = dfs_tree(G, u'0017')
G = nx.compose(G3, G4)
except:
pass
relabel = {}
newToOld = {}
for n in G.nodes():
if n in self._berths and self._berths[n] is not None:
relabel[n] = self._berths[n]
newToOld[self._berths[n]] = n
nx.relabel_nodes(G, relabel, False)
colours = []
for n in G.nodes():
if n in newToOld:
n = newToOld[n]
if n in self._berths and self._berths[n] is not None:
colours.append('r')
else:
colours.append('g')
pos = nx.graphviz_layout(G, prog='dot')
nx.draw_networkx_nodes(G,pos, node_color=colours, node_shape='s', node_size=900)
nx.draw_networkx_edges(G,pos)
nx.draw_networkx_labels(G,pos)
fig = matplotlib.pyplot.gcf()
fig.set_size_inches(16.0, 25.0)
plt.axis('off')
self.outputFile(plt, filename)
self._graphTime[filename] = time.time()
# print 'Graph Updated: %s' % filename
# self._graphFlag = False
def processMessage(self, msg_type, msg):
headcode = msg['descr']
if True or headcode in sys.argv:
area = msg['area_id']
if area == 'LB':
_from = msg['from']
_to = msg['to']
print '%s %s: %s%s --> %s%s' % (msg_type, headcode, area, _from, area, _to)
if (_from in self._berths) and (self._berths[_from] == headcode):
self._berths[_from] = None
self._berths[_to] = headcode
self.updateGraph(area, _from, _to)
def go(self):
while True:
try:
frame = self._client.receiveFrame()
data = json.loads(frame.body)
for msg in data:
for k in msg:
# if k.startswith(('CA', 'CB', 'CC')):
if k.startswith('CA'):
try:
self.processMessage(k, msg[k])
except Exception, e:
pprint.pprint(msg)
raise
self.outputGraph('/var/www/html/down.png', self.oddNumber)
self.outputGraph('/var/www/html/up.png', self.evenNumber)
except KeyboardInterrupt:
break
except Exception, e:
pprint.pprint(data)
raise
def disconnect(self):
self._client.disconnect()
def dumpEdges(self):
pickle.dump(self._edges, open('edges', 'w'))
def main():
logging.basicConfig()
logging.getLogger().setLevel(logging.INFO)
passcode = open('passcode', 'r').read().strip()
userid = open('userid', 'r').read().strip()
m = networkMapper()
m.configureClient(userid, passcode)
m.loadEdges()
try:
m.go()
finally:
print >> sys.stderr, 'Disconnecting...'
m.disconnect()
m.dumpEdges()
if __name__ == '__main__':
main()