Backend work for custom station/platform

This commit is contained in:
RunasSudo 2019-01-01 02:32:17 +11:00
parent 23111668aa
commit 0d73a00a3a
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
2 changed files with 52 additions and 15 deletions

View File

@ -34,8 +34,6 @@ def dest_to_service_name(dest):
def parse_date(dtstring):
return pytz.utc.localize(datetime.strptime(dtstring, '%Y-%m-%dT%H:%M:%SZ')).astimezone(timezone)
STOP_ID = 1099 # Huntingdale Station
PLAT_ID = 1
ROUTE_TYPE = 0
timezone = pytz.timezone('Australia/Melbourne')
@ -49,9 +47,11 @@ def latest():
timenow = pytz.utc.localize(datetime.utcnow()).astimezone(timezone)
result = {}
departures = do_request('/v3/departures/route_type/{}/stop/{}'.format(ROUTE_TYPE, STOP_ID), {'platform_numbers': str(PLAT_ID), 'max_results': '5', 'expand': 'all'})
departures = do_request('/v3/departures/route_type/{}/stop/{}'.format(ROUTE_TYPE, flask.request.args['stop_id']), {'platform_numbers': flask.request.args['plat_id'], 'max_results': '5', 'expand': 'all'})
departures['departures'].sort(key=lambda x: x['scheduled_departure_utc'])
result['stop_name'] = departures['stops'][flask.request.args['stop_id']]['stop_name'].replace(' Station', '')
# Next train
for i, departure in enumerate(departures['departures']):
@ -79,27 +79,26 @@ def latest():
# Calculate stopping pattern until city loop
num_express = 0 # express_stop_count is unreliable for the city loop
for j, stop in enumerate(stops['stops']):
if stop['stop_id'] == STOP_ID:
if stop['stop_id'] == int(flask.request.args['stop_id']):
break
for stop in stops['stops'][j+1:]:
if stop['stop_id'] == 1155 or stop['stop_id'] == 1120 or stop['stop_id'] == 1068 or stop['stop_id'] == 1181 or stop['stop_id'] == 1071: # Parliament, MCS, Flagstaff, SXS, Flinders St
# Calculate stopping pattern in city loop
pattern['departures'].sort(key=lambda x: x['scheduled_departure_utc'])
for k, stop2 in enumerate(pattern['departures']):
if stop2['stop_id'] == stop['stop_id']:
break
for stop in pattern['departures'][k:]:
result['stops'].append(pattern['stops'][str(stop['stop_id'])]['stop_name'].replace(' Station', ''))
break
if stop['stop_id'] in pattern_stops:
result['stops'].append(stop['stop_name'].replace('Station', '').strip())
result['stops'].append(stop['stop_name'].replace(' Station', ''))
else:
result['stops'].append('---')
num_express += 1
if stop['stop_id'] == departures['runs'][str(departure['run_id'])]['final_stop_id']:
break
# Calculate stopping pattern in city loop
pattern['departures'].sort(key=lambda x: x['scheduled_departure_utc'])
for k, stop2 in enumerate(pattern['departures']):
if stop2['stop_id'] == stop['stop_id']:
break
for stop in pattern['departures'][k:]:
result['stops'].append(pattern['stops'][str(stop['stop_id'])]['stop_name'].replace('Station', '').strip())
break
# Next trains

View File

@ -10,17 +10,42 @@
max-width: 100vw;
max-height: 100vh;
}
#topbar {
position: absolute;
left: 0;
top: 0;
z-index: 999;
font-size: small;
}
body, html {
color: #ccc;
background-color: black;
margin: 0;
padding: 0;
overflow: hidden;
}
a, a:active, a:visited {
color: #99f;
}
</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/nunjucks/3.0.1/nunjucks.min.js"></script>
<script>
function getQueryVariable(variable, def) {
var vars = window.location.search.substring(1).split("&");
for (var i = 0; i < vars.length; i++) {
var bits = vars[i].split("=");
if (decodeURIComponent(bits[0]) == variable) {
return decodeURIComponent(bits[1]);
}
}
return def;
}
var stop_id = parseInt(getQueryVariable("stop_id", 1099));
var plat_id = parseInt(getQueryVariable("plat_id", 1));
function start() {
var svg = document.getElementById("mainsvg").getSVGDocument().querySelector("svg");
var template = svg.innerHTML;
@ -30,16 +55,29 @@
function tick() {
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", function() {
svg.innerHTML = nunjucks.renderString(template, JSON.parse(xhr.responseText));
var result = JSON.parse(xhr.responseText);
svg.innerHTML = nunjucks.renderString(template, result);
document.getElementById("stopinfo").innerText = stop_id + " (" + result.stop_name + ")";
});
xhr.open("GET", "latest");
xhr.open("GET", "latest?stop_id=" + stop_id + "&plat_id=" + plat_id);
xhr.send();
}
tick();
window.setInterval(tick, 30000);
}
function changePlatform() {
var new_plat = parseInt(window.prompt("Enter new platform number:", plat_id));
window.location = "?stop_id=" + stop_id + "&plat_id=" + new_plat;
return false;
}
</script>
<div id="topbar">Stop <span id="stopinfo">?</span> [change] Platform <span id="platinfo">?</span> [<a href="#" onclick="return changePlatform();">change</a>]</div>
<object id="mainsvg" data="static/template.svg" type="image/svg+xml" onload="start();"></object>
<script>
document.getElementById("stopinfo").innerText = stop_id;
document.getElementById("platinfo").innerText = plat_id;
</script>
</body>
</html>