Added stops endpoint & updated app

This commit is contained in:
Filip Znachor 2022-12-12 14:54:23 +01:00
parent a903e4050d
commit a05e208348
2 changed files with 49 additions and 24 deletions

View file

@ -16,21 +16,26 @@ class Server(ServerAdapter):
def stop(self):
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:
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.thread = threading.Thread(target=self.start)
self.thread.start()

View file

@ -37,8 +37,8 @@
<body>
<div class="container" id="app">
<div class="nav" :class="{'top': top == 0}">
<div class="inner">
<h3>Borský park</h3>
<div class="inner" v-if="stop">
<h3>{{ stop.name }}</h3>
</div>
</div>
<div class="departure-grid">
@ -62,11 +62,38 @@
data() {
return {
top: 0,
interval: null,
stop: null,
stops: [],
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');
async function api() {
async function api(url) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
@ -78,18 +105,11 @@
resolve(null);
}
};
xhr.open("GET", "/departures/40", true);
xhr.open("GET", url, true);
xhr.send();
});
}
window.addEventListener("scroll", () => {
app.$data.top = document.querySelector('html').scrollTop;
})
async function update() {
app.$data.departures = (await api()).departures;
}
setInterval(update, 5000);
update();
app.setup();
</script>
</body>
</html>