Added stops endpoint & updated app
This commit is contained in:
parent
a903e4050d
commit
a05e208348
|
@ -16,21 +16,26 @@ class Server(ServerAdapter):
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self.server.shutdown()
|
self.server.shutdown()
|
||||||
|
|
||||||
@route('/departures/<stop_id>')
|
|
||||||
def index(stop_id: int):
|
|
||||||
return {'departures': Departure.get(stop_id)}
|
|
||||||
|
|
||||||
@get("/")
|
|
||||||
def static():
|
|
||||||
return static_file("index.html", root="static")
|
|
||||||
|
|
||||||
@error(404)
|
|
||||||
def error404(err):
|
|
||||||
return ''
|
|
||||||
|
|
||||||
class API:
|
class API:
|
||||||
|
|
||||||
def __init__(self, main):
|
def __init__(self, main):
|
||||||
|
|
||||||
|
@route('/departures/<stop_id>')
|
||||||
|
def departures(stop_id: int):
|
||||||
|
return {'departures': Departure.get(stop_id)}
|
||||||
|
|
||||||
|
@route('/stops')
|
||||||
|
def stop():
|
||||||
|
return {'stops': main.config["stops"]}
|
||||||
|
|
||||||
|
@get("/")
|
||||||
|
def static():
|
||||||
|
return static_file("index.html", root="static")
|
||||||
|
|
||||||
|
@error(404)
|
||||||
|
def error404(err):
|
||||||
|
return ''
|
||||||
|
|
||||||
self.server = Server(host=main.config["http"]["host"], port=main.config["http"]["port"])
|
self.server = Server(host=main.config["http"]["host"], port=main.config["http"]["port"])
|
||||||
self.thread = threading.Thread(target=self.start)
|
self.thread = threading.Thread(target=self.start)
|
||||||
self.thread.start()
|
self.thread.start()
|
||||||
|
|
|
@ -37,8 +37,8 @@
|
||||||
<body>
|
<body>
|
||||||
<div class="container" id="app">
|
<div class="container" id="app">
|
||||||
<div class="nav" :class="{'top': top == 0}">
|
<div class="nav" :class="{'top': top == 0}">
|
||||||
<div class="inner">
|
<div class="inner" v-if="stop">
|
||||||
<h3>Borský park</h3>
|
<h3>{{ stop.name }}</h3>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="departure-grid">
|
<div class="departure-grid">
|
||||||
|
@ -62,11 +62,38 @@
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
top: 0,
|
top: 0,
|
||||||
|
interval: null,
|
||||||
|
stop: null,
|
||||||
|
stops: [],
|
||||||
departures: []
|
departures: []
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
async update() {
|
||||||
|
if(app.$data.stop) app.$data.departures = (await api("/departures/"+app.$data.stop.id)).departures;
|
||||||
|
},
|
||||||
|
async setup() {
|
||||||
|
app.$data.stops = (await api("/stops")).stops;
|
||||||
|
if(app.$data.stops.length < 1) {
|
||||||
|
alert("Žádné zastávky nejsou k dispozici!");
|
||||||
|
if(app.$data.interval) clearTimeout(app.$data.interval);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(localStorage.getItem("favstop")) {
|
||||||
|
app.$data.stops.forEach(stop => {
|
||||||
|
if(stop.id == localStorage.getItem("favstop")) app.$data.stop = stop;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if(!app.$data.stop) app.$data.stop = app.$data.stops[0];
|
||||||
|
window.addEventListener("scroll", () => {
|
||||||
|
app.$data.top = document.querySelector('html').scrollTop;
|
||||||
|
});
|
||||||
|
app.$data.interval = setInterval(app.update, 5000);
|
||||||
|
app.update();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}).mount('#app');
|
}).mount('#app');
|
||||||
async function api() {
|
async function api(url) {
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
var xhr = new XMLHttpRequest();
|
var xhr = new XMLHttpRequest();
|
||||||
xhr.onreadystatechange = function() {
|
xhr.onreadystatechange = function() {
|
||||||
|
@ -78,18 +105,11 @@
|
||||||
resolve(null);
|
resolve(null);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
xhr.open("GET", "/departures/40", true);
|
xhr.open("GET", url, true);
|
||||||
xhr.send();
|
xhr.send();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
window.addEventListener("scroll", () => {
|
app.setup();
|
||||||
app.$data.top = document.querySelector('html').scrollTop;
|
|
||||||
})
|
|
||||||
async function update() {
|
|
||||||
app.$data.departures = (await api()).departures;
|
|
||||||
}
|
|
||||||
setInterval(update, 5000);
|
|
||||||
update();
|
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
Loading…
Reference in a new issue