Compare commits
No commits in common. "51ec5f5d408dc1a1b88d38ad75db8d711818c616" and "5c4f219f7ae243de06ed4140804cfe3d042948ad" have entirely different histories.
51ec5f5d40
...
5c4f219f7a
|
@ -5,8 +5,7 @@
|
|||
"main": "index.js",
|
||||
"scripts": {
|
||||
"back": "node src/back",
|
||||
"front:debug": "parcel serve src/front/index.html",
|
||||
"front:prod": "npx parcel build src/front/index.html --no-source-maps --no-autoinstall",
|
||||
"front": "parcel serve src/front/index.html",
|
||||
"lint": "eslint ."
|
||||
},
|
||||
"keywords": [],
|
||||
|
|
|
@ -239,6 +239,7 @@ const createMap = target =>
|
|||
|
||||
// Course on which the view is currently focused
|
||||
let focusedCourse = null;
|
||||
const focusZoom = 17;
|
||||
|
||||
const startFocus = courseId =>
|
||||
{
|
||||
|
@ -247,6 +248,7 @@ const createMap = target =>
|
|||
const course = simulInstance.courses[courseId];
|
||||
view.animate({
|
||||
center: course.position,
|
||||
zoom: focusZoom,
|
||||
duration: 500,
|
||||
}, () => focusedCourse = courseId);
|
||||
}
|
||||
|
@ -292,6 +294,7 @@ const createMap = target =>
|
|||
if (course.id === focusedCourse)
|
||||
{
|
||||
view.setCenter(course.position);
|
||||
// view.setZoom(focus);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,18 @@ const tam = require('./sources/tam');
|
|||
const util = require('../util');
|
||||
const network = require('./network.json');
|
||||
|
||||
/**
|
||||
* Comparison function between two stop passings.
|
||||
*
|
||||
* @param passing1 First stop passing.
|
||||
* @param passing2 Second stop passing.
|
||||
* @return Negative value if passing1 is sooner than passing2, positive
|
||||
* otherwise, zero if they occur at the same time.
|
||||
*/
|
||||
const passingCompare = ({arrivalTime: time1}, {arrivalTime: time2}) => (
|
||||
time1 - time2
|
||||
);
|
||||
|
||||
// Time at which the course data needs to be updated next
|
||||
let nextUpdate = null;
|
||||
|
||||
|
@ -19,8 +31,9 @@ let currentCourses = null;
|
|||
* - `id`: Unique identifier for the course.
|
||||
* - `line`: Line number.
|
||||
* - `finalStop`: The final stop to which the course is headed.
|
||||
* - `nextPassings`: Next passings of the vehicle, as a dictionary associating
|
||||
* each next stop to the passing timestamp.
|
||||
* - `nextPassings`: Next passings of the vehicle, sorted by increasing
|
||||
* arrival time, containing both the stop identifier (`stopId`) and the
|
||||
* expected arrival timestamp (`arrivalTime`).
|
||||
*
|
||||
* @return Mapping from active course IDs to information about each course.
|
||||
*/
|
||||
|
@ -56,9 +69,9 @@ const getCourses = () => new Promise((res, rej) =>
|
|||
}
|
||||
else
|
||||
{
|
||||
for (let stopId of Object.keys(course.nextPassings))
|
||||
for (let passing of course.nextPassings)
|
||||
{
|
||||
if (!(stopId in network.stops))
|
||||
if (!(passing.stopId in network.stops))
|
||||
{
|
||||
delete courses[courseId];
|
||||
break;
|
||||
|
@ -67,6 +80,13 @@ const getCourses = () => new Promise((res, rej) =>
|
|||
}
|
||||
}
|
||||
|
||||
// End of courses information stream. Sort next stops by increasing
|
||||
// arrival time in each course then save result in memory cache
|
||||
for (let course of Object.values(courses))
|
||||
{
|
||||
course.nextPassings.sort(passingCompare);
|
||||
}
|
||||
|
||||
currentCourses = courses;
|
||||
res(currentCourses);
|
||||
return;
|
||||
|
@ -93,12 +113,12 @@ const getCourses = () => new Promise((res, rej) =>
|
|||
{
|
||||
courses[id] = {
|
||||
id, line, finalStop,
|
||||
nextPassings: {[stopId]: arrivalTime},
|
||||
nextPassings: [{stopId, arrivalTime}],
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
courses[id].nextPassings[stopId] = arrivalTime;
|
||||
courses[id].nextPassings.push({stopId, arrivalTime});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
@ -4,170 +4,188 @@ const network = require('./network.json');
|
|||
|
||||
const server = 'http://localhost:4321';
|
||||
|
||||
class Course
|
||||
const arriveAtStop = (course, stop) =>
|
||||
{
|
||||
constructor(data)
|
||||
course.state = 'stopped';
|
||||
course.currentStop = stop;
|
||||
delete course.departureStop;
|
||||
delete course.arrivalStop;
|
||||
delete course.arrivalTime;
|
||||
delete course.traveledDistance;
|
||||
delete course.speed;
|
||||
};
|
||||
|
||||
const moveToStop = (course, stop, arrivalTime) =>
|
||||
{
|
||||
course.state = 'moving';
|
||||
course.departureStop = course.currentStop;
|
||||
course.arrivalStop = stop;
|
||||
course.arrivalTime = arrivalTime;
|
||||
course.traveledDistance = 0;
|
||||
course.speed = 0;
|
||||
delete course.currentStop;
|
||||
|
||||
const segment = `${course.departureStop}-${course.arrivalStop}`;
|
||||
|
||||
if (!(segment in network.segments))
|
||||
{
|
||||
this.id = data.id;
|
||||
this.passings = {};
|
||||
this.state = null;
|
||||
|
||||
// Attributes for the `stopped` state
|
||||
this.currentStop = null;
|
||||
|
||||
// Attributes for the `moving` state
|
||||
this.departureStop = null;
|
||||
this.arrivalStop = null;
|
||||
this.arrivalTime = 0;
|
||||
this.traveledDistance = 0;
|
||||
this.speed = 0;
|
||||
|
||||
this.position = [0, 0];
|
||||
this.angle = 0;
|
||||
|
||||
this.history = [];
|
||||
}
|
||||
|
||||
get currentSegment()
|
||||
{
|
||||
if (this.state !== 'moving')
|
||||
{
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return network.segments[`${this.departureStop}-${this.arrivalStop}`];
|
||||
}
|
||||
|
||||
updateData(data)
|
||||
{
|
||||
this.line = data.line;
|
||||
this.finalStop = data.finalStop;
|
||||
Object.assign(this.passings, data.nextPassings);
|
||||
|
||||
const now = Date.now();
|
||||
|
||||
// Make sure we’re on the right `stopped`/`moving` state
|
||||
if (this.state === null)
|
||||
{
|
||||
let previousStop = null;
|
||||
let departureTime = 0;
|
||||
|
||||
let nextStop = null;
|
||||
let arrivalTime = Infinity;
|
||||
|
||||
for (let [stopId, time] of Object.entries(this.passings))
|
||||
{
|
||||
if (time > now && time < arrivalTime)
|
||||
{
|
||||
nextStop = stopId;
|
||||
arrivalTime = time;
|
||||
}
|
||||
|
||||
if (time < now && time > departureTime)
|
||||
{
|
||||
previousStop = stopId;
|
||||
departureTime = time;
|
||||
}
|
||||
}
|
||||
|
||||
if (nextStop === null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (previousStop === null)
|
||||
{
|
||||
// Teleport to the first known stop
|
||||
this.arriveToStop(nextStop);
|
||||
// There is no segment between the two requested stops, jump
|
||||
// directly to the arrival stop
|
||||
arriveAtStop(course, course.arrivalStop);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Teleport to the first known segment
|
||||
this.arriveToStop(previousStop);
|
||||
this.moveToStop(nextStop, arrivalTime);
|
||||
updateSpeed(course);
|
||||
}
|
||||
}
|
||||
else if (this.state === 'moving')
|
||||
{
|
||||
// Should already be at the next stop
|
||||
if (this.passings[this.arrivalStop] <= now)
|
||||
{
|
||||
this.arriveToStop(this.arrivalStop);
|
||||
}
|
||||
// On the right track, update the arrival time
|
||||
else
|
||||
{
|
||||
this.arrivalTime = this.passings[this.arrivalStop];
|
||||
}
|
||||
}
|
||||
else // this.state === 'stopped'
|
||||
{
|
||||
// Try moving to the next stop
|
||||
let nextStop = null;
|
||||
let arrivalTime = Infinity;
|
||||
};
|
||||
|
||||
for (let [stopId, time] of Object.entries(this.passings))
|
||||
const getCurrentSegment = course =>
|
||||
{
|
||||
if (course.state === 'stopped')
|
||||
{
|
||||
if (time > now && time < arrivalTime)
|
||||
{
|
||||
nextStop = stopId;
|
||||
arrivalTime = time;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
if (nextStop === null)
|
||||
{
|
||||
// This course is finished
|
||||
return false;
|
||||
}
|
||||
return network.segments[`${course.departureStop}-${course.arrivalStop}`];
|
||||
};
|
||||
|
||||
if (nextStop !== this.currentStop)
|
||||
{
|
||||
this.moveToStop(nextStop, arrivalTime);
|
||||
}
|
||||
}
|
||||
|
||||
if (this.state === 'moving')
|
||||
{
|
||||
this.speed = this.computeTheoreticalSpeed();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
tick(time)
|
||||
{
|
||||
if (this.state === null)
|
||||
{
|
||||
// Ignore uninitalized courses
|
||||
}
|
||||
else if (this.state === 'moving')
|
||||
{
|
||||
// Integrate current speed in travelled distance
|
||||
const delta = this.speed * time;
|
||||
|
||||
const segment = this.currentSegment;
|
||||
const updateSpeed = course =>
|
||||
{
|
||||
const segment = getCurrentSegment(course);
|
||||
const length = segment.points[segment.points.length - 1].distance;
|
||||
this.traveledDistance += delta;
|
||||
|
||||
if (this.traveledDistance >= length)
|
||||
const remainingTime = course.arrivalTime - Date.now();
|
||||
const remainingDistance = length - course.traveledDistance;
|
||||
|
||||
if (remainingTime <= 0 || remainingDistance <= 0)
|
||||
{
|
||||
this.arriveToStop(this.arrivalStop);
|
||||
arriveAtStop(course, course.arrivalStop);
|
||||
return;
|
||||
}
|
||||
|
||||
course.speed = remainingDistance / remainingTime;
|
||||
};
|
||||
|
||||
const updateFromTam = async (courses) =>
|
||||
{
|
||||
const currentCourses = (await axios.get(`${server}/courses`)).data;
|
||||
|
||||
for (let [id, course] of Object.entries(currentCourses))
|
||||
{
|
||||
// Find out the next stop, ignoring the ones that are in the past
|
||||
let nextStop = null;
|
||||
let arrivalTime = null;
|
||||
|
||||
for (let {stopId, arrivalTime: time} of course.nextPassings)
|
||||
{
|
||||
if (time > Date.now())
|
||||
{
|
||||
nextStop = stopId;
|
||||
arrivalTime = time;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (nextStop === null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Update an existing course
|
||||
if (id in courses)
|
||||
{
|
||||
const prev = courses[id];
|
||||
|
||||
if (prev.state === 'stopped')
|
||||
{
|
||||
if (prev.currentStop !== nextStop)
|
||||
{
|
||||
// Start traveling from the current stop to the next
|
||||
moveToStop(prev, nextStop, arrivalTime);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Update the ETA if we’re still headed to the same stop
|
||||
if (prev.arrivalStop === nextStop)
|
||||
{
|
||||
prev.arrivalTime = arrivalTime;
|
||||
updateSpeed(prev);
|
||||
}
|
||||
// Otherwise, we missed a stop, try to go directly to the
|
||||
// next segment
|
||||
else
|
||||
{
|
||||
arriveAtStop(prev, prev.arrivalStop);
|
||||
moveToStop(prev, nextStop, arrivalTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Create a new course
|
||||
else
|
||||
{
|
||||
courses[id] = {
|
||||
id,
|
||||
line: course.line,
|
||||
finalStop: course.finalStop,
|
||||
position: [0, 0],
|
||||
angle: 0,
|
||||
};
|
||||
|
||||
arriveAtStop(courses[id], nextStop);
|
||||
}
|
||||
}
|
||||
|
||||
// Remove stale courses
|
||||
for (let id of Object.keys(courses))
|
||||
{
|
||||
if (!(id in currentCourses))
|
||||
{
|
||||
delete courses[id];
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const updatePositions = (courses, time) =>
|
||||
{
|
||||
for (let [id, course] of Object.entries(courses))
|
||||
{
|
||||
if (course.state === 'moving')
|
||||
{
|
||||
// Increase the travelled distance respective to the current speed
|
||||
const delta = course.speed * time;
|
||||
|
||||
const segment = getCurrentSegment(course);
|
||||
const length = segment.points[segment.points.length - 1].distance;
|
||||
|
||||
if (course.traveledDistance + delta >= length)
|
||||
{
|
||||
course.traveledDistance = length;
|
||||
}
|
||||
else
|
||||
{
|
||||
course.traveledDistance += delta;
|
||||
}
|
||||
|
||||
// Recompute updated position
|
||||
const departureStop = network.stops[this.departureStop];
|
||||
const arrivalStop = network.stops[this.arrivalStop];
|
||||
const departureStop = network.stops[course.departureStop];
|
||||
const arrivalStop = network.stops[course.arrivalStop];
|
||||
const nextNodeIndex = segment.points.findIndex(
|
||||
({distance}) => distance >= this.traveledDistance);
|
||||
({distance}) => distance >= course.traveledDistance);
|
||||
|
||||
if (nextNodeIndex === 0)
|
||||
{
|
||||
this.position = turf.toMercator([
|
||||
departureStop.lon,
|
||||
departureStop.lat
|
||||
]);
|
||||
course.position = {
|
||||
lat: departureStop.lat,
|
||||
lon: departureStop.lon
|
||||
};
|
||||
}
|
||||
else if (nextNodeIndex === -1)
|
||||
{
|
||||
course.position = {
|
||||
lat: arrivalStop.lat,
|
||||
lon: arrivalStop.lon
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -184,161 +202,33 @@ class Course
|
|||
nextNode.lat
|
||||
]);
|
||||
|
||||
const curLength = this.traveledDistance
|
||||
const curLength = course.traveledDistance
|
||||
- previousNode.distance;
|
||||
const totalLength = nextNode.distance
|
||||
- previousNode.distance;
|
||||
const t = curLength / totalLength;
|
||||
|
||||
this.position = [
|
||||
course.position = [
|
||||
t * nextPoint[0] + (1 - t) * previousPoint[0],
|
||||
t * nextPoint[1] + (1 - t) * previousPoint[1],
|
||||
];
|
||||
|
||||
this.angle = Math.atan2(
|
||||
course.angle = Math.atan2(
|
||||
previousPoint[1] - nextPoint[1],
|
||||
nextPoint[0] - previousPoint[0],
|
||||
);
|
||||
}
|
||||
}
|
||||
else // this.state === 'stopped'
|
||||
else
|
||||
{
|
||||
const currentNode = network.stops[this.currentStop];
|
||||
const currentNode = network.stops[course.currentStop];
|
||||
|
||||
this.position = turf.toMercator([
|
||||
course.position = turf.toMercator([
|
||||
currentNode.lon,
|
||||
currentNode.lat
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Transition this course to a state where it has arrived to a stop.
|
||||
*
|
||||
* @param stop Identifier for the stop to which the course arrives.
|
||||
*/
|
||||
arriveToStop(stop)
|
||||
{
|
||||
this.state = 'stopped';
|
||||
this.currentStop = stop;
|
||||
this.position = turf.toMercator([
|
||||
network.stops[this.currentStop].lon,
|
||||
network.stops[this.currentStop].lat,
|
||||
]);
|
||||
this.history.push(['arriveToStop', stop]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transition this course to a state where it is moving to a stop.
|
||||
*
|
||||
* @param stop Next stop for this course.
|
||||
* @param arrivalTime Planned arrival time to that stop.
|
||||
*/
|
||||
moveToStop(stop, arrivalTime)
|
||||
{
|
||||
if (!(`${this.currentStop}-${stop}` in network.segments))
|
||||
{
|
||||
console.warn(`Course ${this.id} is cannot go from stop
|
||||
${this.currentStop} to stop ${stop}. Teleporting to ${stop}`);
|
||||
this.arriveToStop(stop);
|
||||
return;
|
||||
}
|
||||
|
||||
this.state = 'moving';
|
||||
this.departureStop = this.currentStop;
|
||||
this.arrivalStop = stop;
|
||||
this.arrivalTime = arrivalTime;
|
||||
this.traveledDistance = 0;
|
||||
this.speed = 0;
|
||||
this.position = turf.toMercator([
|
||||
network.stops[this.departureStop].lon,
|
||||
network.stops[this.departureStop].lat,
|
||||
]);
|
||||
this.history.push(['moveToStop', stop, arrivalTime]);
|
||||
|
||||
console.info(`Course ${this.id} leaving stop ${this.currentStop} \
|
||||
with initial speed ${this.computeTheoreticalSpeed() * 3600} km/h`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the speed that needs to be maintained to arrive on time.
|
||||
*/
|
||||
computeTheoreticalSpeed()
|
||||
{
|
||||
if (this.state !== 'moving')
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
const segment = this.currentSegment;
|
||||
const length = segment.points[segment.points.length - 1].distance;
|
||||
|
||||
const remainingTime = this.arrivalTime - Date.now();
|
||||
const remainingDistance = length - this.traveledDistance;
|
||||
|
||||
if (remainingDistance <= 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else if (remainingTime <= 0)
|
||||
{
|
||||
// We’re late, go to maximum speed
|
||||
return 50 / 3600; // 50 km/h
|
||||
}
|
||||
else
|
||||
{
|
||||
return remainingDistance / remainingTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const updateData = async (courses) =>
|
||||
{
|
||||
const dataset = (await axios.get(`${server}/courses`)).data;
|
||||
|
||||
// Update or create new courses
|
||||
for (let [id, data] of Object.entries(dataset))
|
||||
{
|
||||
if (id in courses)
|
||||
{
|
||||
if (!courses[id].updateData(data))
|
||||
{
|
||||
console.info(`Course ${id} is finished.`);
|
||||
delete courses[id];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
const newCourse = new Course(data);
|
||||
|
||||
if (!newCourse.updateData(data))
|
||||
{
|
||||
console.info(`Ignoring course ${id} which is outdated.`);
|
||||
}
|
||||
else
|
||||
{
|
||||
console.info(`Course ${id} starting.`);
|
||||
courses[id] = newCourse;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remove stale courses
|
||||
for (let id of Object.keys(courses))
|
||||
{
|
||||
if (!(id in dataset))
|
||||
{
|
||||
delete courses[id];
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const tick = (courses, time) =>
|
||||
{
|
||||
for (let course of Object.values(courses))
|
||||
{
|
||||
course.tick(time);
|
||||
}
|
||||
};
|
||||
|
||||
const start = () =>
|
||||
|
@ -354,12 +244,12 @@ const start = () =>
|
|||
if (lastUpdate === null || lastUpdate + 5000 <= now)
|
||||
{
|
||||
lastUpdate = now;
|
||||
updateData(courses);
|
||||
updateFromTam(courses);
|
||||
}
|
||||
|
||||
const time = lastFrame === null ? 0 : now - lastFrame;
|
||||
lastFrame = now;
|
||||
tick(courses, time);
|
||||
updatePositions(courses, time);
|
||||
};
|
||||
|
||||
return {courses, update};
|
||||
|
|
Loading…
Reference in New Issue