2020-07-17 14:40:09 +00:00
|
|
|
const tam = require('./sources/tam');
|
|
|
|
const util = require('../util');
|
|
|
|
|
2020-07-18 17:00:46 +00:00
|
|
|
let nextUpdate = null;
|
|
|
|
let currentCourses = null;
|
|
|
|
|
2020-07-17 14:40:09 +00:00
|
|
|
/**
|
2020-07-18 17:00:46 +00:00
|
|
|
* Fetch real-time information about active courses in the TaM network.
|
|
|
|
*
|
|
|
|
* New data will only be fetched from the TaM server once every minute,
|
|
|
|
* otherwise pulling from the in-memory cache.
|
2020-07-17 14:40:09 +00:00
|
|
|
*
|
2020-07-18 17:00:46 +00:00
|
|
|
* The following information is provided for each active course:
|
2020-07-17 14:40:09 +00:00
|
|
|
*
|
2020-07-18 17:00:46 +00:00
|
|
|
* - `id`: Unique identifier for the course.
|
|
|
|
* - `line`: Line number.
|
|
|
|
* - `nextStop`: Identifier of the next stop of the course.
|
|
|
|
* - `arrivalTime`: Timestamp at which the vehicle is predicted to arrive
|
|
|
|
* to the next stop.
|
|
|
|
* - `finalStop`: The final stop to which the course is headed.
|
|
|
|
*
|
|
|
|
* @return Mapping from active course IDs to information about each course.
|
2020-07-17 14:40:09 +00:00
|
|
|
*/
|
|
|
|
const getCourses = () => new Promise((res, rej) =>
|
2020-01-14 13:08:08 +00:00
|
|
|
{
|
2020-07-17 14:40:09 +00:00
|
|
|
if (nextUpdate !== null && Date.now() < nextUpdate)
|
2020-01-14 13:08:08 +00:00
|
|
|
{
|
2020-07-17 14:40:09 +00:00
|
|
|
res(currentCourses);
|
|
|
|
return;
|
|
|
|
}
|
2020-01-14 13:08:08 +00:00
|
|
|
|
2020-07-17 14:40:09 +00:00
|
|
|
const courses = {};
|
|
|
|
let lastUpdate = null;
|
2020-01-14 13:08:08 +00:00
|
|
|
|
2020-07-17 14:40:09 +00:00
|
|
|
tam.fetchRealtime((err, entry) =>
|
2020-01-14 13:08:08 +00:00
|
|
|
{
|
2020-07-17 14:40:09 +00:00
|
|
|
if (err)
|
2020-01-14 13:08:08 +00:00
|
|
|
{
|
2020-07-17 14:40:09 +00:00
|
|
|
rej(err);
|
|
|
|
return;
|
|
|
|
}
|
2020-01-14 13:08:08 +00:00
|
|
|
|
2020-07-17 14:40:09 +00:00
|
|
|
if (!util.isObject(entry))
|
|
|
|
{
|
|
|
|
currentCourses = courses;
|
|
|
|
res(currentCourses);
|
|
|
|
return;
|
|
|
|
}
|
2020-01-14 13:08:08 +00:00
|
|
|
|
2020-07-17 14:40:09 +00:00
|
|
|
if ('lastUpdate' in entry)
|
|
|
|
{
|
|
|
|
lastUpdate = entry.lastUpdate;
|
|
|
|
nextUpdate = entry.nextUpdate;
|
|
|
|
return;
|
2020-01-14 13:08:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-17 14:40:09 +00:00
|
|
|
const {
|
|
|
|
course: id,
|
|
|
|
routeShortName: line,
|
|
|
|
stopId: nextStop,
|
|
|
|
destArCode: finalStop,
|
|
|
|
} = entry;
|
2020-01-14 13:08:08 +00:00
|
|
|
|
2020-07-17 14:40:09 +00:00
|
|
|
const arrivalTime = lastUpdate + parseInt(entry.delaySec, 10) * 1000;
|
2020-01-14 13:08:08 +00:00
|
|
|
|
2020-07-17 14:40:09 +00:00
|
|
|
if (!(id in courses))
|
|
|
|
{
|
|
|
|
courses[id] = {id, line, nextStop, arrivalTime, finalStop};
|
|
|
|
}
|
|
|
|
else if (arrivalTime < courses[id].arrivalTime)
|
|
|
|
{
|
|
|
|
// The stop where the next passing is soonest is assumed
|
|
|
|
// to be the next stop
|
|
|
|
courses[id].nextStop = nextStop;
|
|
|
|
courses[id].arrivalTime = arrivalTime;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2020-01-14 13:08:08 +00:00
|
|
|
|
2020-07-17 14:40:09 +00:00
|
|
|
exports.getCourses = getCourses;
|