Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implemenation of a small KV55 alike demo.

  • Loading branch information...
commit 74f5f2db08aede31bfc2ece4cfeb7eaa4a7814a9 1 parent 3e716f1
@skinkie skinkie authored
View
2  kv78turbo/const.py
@@ -1,3 +1,5 @@
ZMQ_KV7KALENDER = "tcp://127.0.0.1:6070"
ZMQ_KV7PLANNING = "tcp://127.0.0.1:6071"
ZMQ_KV8 = "tcp://127.0.0.1:6080"
+
+ZMQ_KV78DEMO = "tcp://127.0.0.1:6090"
View
26 kv78turbo/kv78turbo-demo-client.py
@@ -0,0 +1,26 @@
+import uwsgi
+import zmq
+
+from const import ZMQ_KV78DEMO
+
+def KV78Demo(environ, start_response):
+ try:
+ timingpointcode = environ['PATH_INFO'][1:]
+ if timingpointcode != 'tpc':
+ int(timingpointcode)
+ except ValueError:
+ reply = '[]'
+ start_response('404 File Not Found', [('Content-Type', 'application/json'), ('Content-length', str(len(reply)))])
+ yield reply
+ return
+
+ context = zmq.Context()
+ client = context.socket(zmq.REQ)
+ client.connect(ZMQ_KV78DEMO)
+ client.send(timingpointcode)
+ reply = client.recv()
+ start_response('200 OK', [('Content-Type', 'application/json'), ('Content-length', str(len(reply)))])
+ yield reply
+ return
+
+uwsgi.applications = {'': KV78Demo}
View
83 kv78turbo/kv78turbo-demo.py
@@ -0,0 +1,83 @@
+import sys
+import time
+import zmq
+from const import ZMQ_KV8, ZMQ_KV78DEMO
+from ctx import ctx
+from datetime import datetime, timedelta
+
+store = {}
+
+def toisotime(operationdate, timestamp):
+ hours, minutes, seconds = timestamp.split(':')
+ hours = int(hours)
+ if hours >= 24:
+ hours -= 24
+ years, months, days = operationdate.split('-')
+ return (datetime(int(years), int(months), int(days), hours, int(minutes), int(seconds)) + timedelta(days = 1)).isoformat()
+ else:
+ return operationdate+'T'+timestamp
+
+def cleanup():
+ now = datetime.today()
+ for timingpointcode, values in store.items():
+ for journey, row in values.items():
+ if now > datetime.strptime(row['ExpectedDepartureTime'], "%Y-%m-%dT%H:%M:%S"):
+ del(store[timingpointcode][journey])
+
+
+def storecurrect(row):
+ id = '_'.join([row['DataOwnerCode'], row['OperationDate'], row['LinePlanningNumber'], row['JourneyNumber']])
+
+ row['ExpectedArrivalTime'] = toisotime(row['OperationDate'], row['ExpectedArrivalTime'])
+ row['ExpectedDepartureTime'] = toisotime(row['OperationDate'], row['ExpectedDepartureTime'])
+
+ if row['TimingPointCode'] not in store:
+ store[row['TimingPointCode']] = {id: row}
+ else:
+ store[row['TimingPointCode']][id] = row
+
+context = zmq.Context()
+
+client = context.socket(zmq.REP)
+client.bind(ZMQ_KV78DEMO)
+
+kv8 = context.socket(zmq.SUB)
+kv8.connect(ZMQ_KV8)
+kv8.setsockopt(zmq.SUBSCRIBE, '')
+
+poller = zmq.Poller()
+poller.register(client, zmq.POLLIN)
+poller.register(kv8, zmq.POLLIN)
+
+
+while True:
+ socks = dict(poller.poll(120))
+
+ if socks.get(kv8) == zmq.POLLIN:
+ content = kv8.recv()
+ c = ctx(content)
+ for row in c.ctx['DATEDPASSTIME'].rows():
+ storecurrect(row)
+ sys.stdout.write('8')
+ sys.stdout.flush()
+
+ elif socks.get(client) == zmq.POLLIN:
+ tpc = client.recv()
+ if tpc == 'tpc':
+ reply = {}
+ for tpc, values in store.items():
+ reply[tpc] = len(values)
+ client.send_json(reply)
+ sys.stdout.write('t')
+ sys.stdout.flush()
+ else:
+ if tpc in store:
+ client.send_json(store[tpc])
+ sys.stdout.write('s')
+ sys.stdout.flush()
+ else:
+ client.send_json([])
+ sys.stdout.write('_')
+ sys.stdout.flush()
+ else:
+ cleanup()
Please sign in to comment.
Something went wrong with that request. Please try again.