tracktracker/front/data.js

33 lines
1.0 KiB
JavaScript

const updateFromTam = async (courses) =>
{
const currentCourses = await getCurrentCourses();
for (let [id, course] of Object.entries(currentCourses))
{
if (!(id in courses))
{
course.arrivalTime = now() + course.eta;
console.log(`${displayNow(now())} - New course ${id} @ ${course.line} departing from stop ${course.stop} at ${displayNow(course.arrivalTime)}`);
courses[id] = course;
}
else
{
course.arrivalTime = now() + course.eta;
console.log(`${displayNow(now())} - Course ${id} @ ${course.line} will arrive to stop ${course.stop} at ${displayNow(course.arrivalTime)} (previously to stop ${courses[id].stop} at ${displayNow(courses[id].arrivalTime)})`);
courses[id] = course;
}
}
};
const sleep = time => new Promise(res => setTimeout(res, time));
const courses = {};
const loop = async (courses = {}) =>
{
await updateFromTam(courses);
await sleep(30000);
return loop(courses);
};
loop();