Backend work for custom station/platform
This commit is contained in:
parent
23111668aa
commit
0d73a00a3a
@ -34,8 +34,6 @@ def dest_to_service_name(dest):
|
|||||||
def parse_date(dtstring):
|
def parse_date(dtstring):
|
||||||
return pytz.utc.localize(datetime.strptime(dtstring, '%Y-%m-%dT%H:%M:%SZ')).astimezone(timezone)
|
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
|
ROUTE_TYPE = 0
|
||||||
|
|
||||||
timezone = pytz.timezone('Australia/Melbourne')
|
timezone = pytz.timezone('Australia/Melbourne')
|
||||||
@ -49,9 +47,11 @@ def latest():
|
|||||||
timenow = pytz.utc.localize(datetime.utcnow()).astimezone(timezone)
|
timenow = pytz.utc.localize(datetime.utcnow()).astimezone(timezone)
|
||||||
result = {}
|
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'])
|
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
|
# Next train
|
||||||
|
|
||||||
for i, departure in enumerate(departures['departures']):
|
for i, departure in enumerate(departures['departures']):
|
||||||
@ -79,27 +79,26 @@ def latest():
|
|||||||
# Calculate stopping pattern until city loop
|
# Calculate stopping pattern until city loop
|
||||||
num_express = 0 # express_stop_count is unreliable for the city loop
|
num_express = 0 # express_stop_count is unreliable for the city loop
|
||||||
for j, stop in enumerate(stops['stops']):
|
for j, stop in enumerate(stops['stops']):
|
||||||
if stop['stop_id'] == STOP_ID:
|
if stop['stop_id'] == int(flask.request.args['stop_id']):
|
||||||
break
|
break
|
||||||
for stop in stops['stops'][j+1:]:
|
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
|
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
|
break
|
||||||
if stop['stop_id'] in pattern_stops:
|
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:
|
else:
|
||||||
result['stops'].append('---')
|
result['stops'].append('---')
|
||||||
num_express += 1
|
num_express += 1
|
||||||
if stop['stop_id'] == departures['runs'][str(departure['run_id'])]['final_stop_id']:
|
if stop['stop_id'] == departures['runs'][str(departure['run_id'])]['final_stop_id']:
|
||||||
break
|
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
|
break
|
||||||
|
|
||||||
# Next trains
|
# Next trains
|
||||||
|
@ -10,17 +10,42 @@
|
|||||||
max-width: 100vw;
|
max-width: 100vw;
|
||||||
max-height: 100vh;
|
max-height: 100vh;
|
||||||
}
|
}
|
||||||
|
#topbar {
|
||||||
|
position: absolute;
|
||||||
|
left: 0;
|
||||||
|
top: 0;
|
||||||
|
z-index: 999;
|
||||||
|
font-size: small;
|
||||||
|
}
|
||||||
body, html {
|
body, html {
|
||||||
|
color: #ccc;
|
||||||
background-color: black;
|
background-color: black;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
}
|
}
|
||||||
|
a, a:active, a:visited {
|
||||||
|
color: #99f;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/nunjucks/3.0.1/nunjucks.min.js"></script>
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/nunjucks/3.0.1/nunjucks.min.js"></script>
|
||||||
<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() {
|
function start() {
|
||||||
var svg = document.getElementById("mainsvg").getSVGDocument().querySelector("svg");
|
var svg = document.getElementById("mainsvg").getSVGDocument().querySelector("svg");
|
||||||
var template = svg.innerHTML;
|
var template = svg.innerHTML;
|
||||||
@ -30,16 +55,29 @@
|
|||||||
function tick() {
|
function tick() {
|
||||||
var xhr = new XMLHttpRequest();
|
var xhr = new XMLHttpRequest();
|
||||||
xhr.addEventListener("load", function() {
|
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();
|
xhr.send();
|
||||||
}
|
}
|
||||||
|
|
||||||
tick();
|
tick();
|
||||||
window.setInterval(tick, 30000);
|
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>
|
</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>
|
<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>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
Reference in New Issue
Block a user