mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
Add a method to check whether a shuttle has arrived at a stop
This commit is contained in:
@@ -37,3 +37,15 @@ export interface IOrderedStop extends IEntityWithTimestamp {
|
||||
systemId: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a shuttle has arrived at a stop based on coordinate proximity.
|
||||
* Uses a threshold of 0.001 degrees (~111 meters at the equator).
|
||||
*/
|
||||
export function shuttleHasArrivedAtStop(shuttle: IShuttle, stop: IStop) {
|
||||
const isWithinLatitudeRange = shuttle.coordinates.latitude > stop.coordinates.latitude - 0.001
|
||||
&& shuttle.coordinates.latitude < stop.coordinates.latitude + 0.001;
|
||||
const isWithinLongitudeRange = shuttle.coordinates.longitude > stop.coordinates.longitude - 0.001
|
||||
&& shuttle.coordinates.longitude < stop.coordinates.longitude + 0.001
|
||||
return isWithinLatitudeRange && isWithinLongitudeRange;
|
||||
}
|
||||
|
||||
|
||||
65
src/entities/__tests__/ShuttleRepositoryEntities.test.ts
Normal file
65
src/entities/__tests__/ShuttleRepositoryEntities.test.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { describe, expect, it } from "@jest/globals";
|
||||
import { shuttleHasArrivedAtStop, IShuttle, IStop } from "../ShuttleRepositoryEntities";
|
||||
|
||||
describe("shuttleHasArrivedAtStop", () => {
|
||||
const baseStop: IStop = {
|
||||
id: "stop1",
|
||||
name: "Test Stop",
|
||||
systemId: "263",
|
||||
coordinates: {
|
||||
latitude: 33.7963,
|
||||
longitude: -117.8540,
|
||||
},
|
||||
updatedTime: new Date(),
|
||||
};
|
||||
|
||||
const createShuttle = (latitude: number, longitude: number): IShuttle => ({
|
||||
id: "shuttle1",
|
||||
name: "Test Shuttle",
|
||||
routeId: "route1",
|
||||
systemId: "263",
|
||||
coordinates: { latitude, longitude },
|
||||
orientationInDegrees: 0,
|
||||
updatedTime: new Date(),
|
||||
});
|
||||
|
||||
it("returns false when shuttle is above latitude range", () => {
|
||||
const shuttle = createShuttle(
|
||||
baseStop.coordinates.latitude + 0.0011,
|
||||
baseStop.coordinates.longitude
|
||||
);
|
||||
expect(shuttleHasArrivedAtStop(shuttle, baseStop)).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false when shuttle is below latitude range", () => {
|
||||
const shuttle = createShuttle(
|
||||
baseStop.coordinates.latitude - 0.0011,
|
||||
baseStop.coordinates.longitude
|
||||
);
|
||||
expect(shuttleHasArrivedAtStop(shuttle, baseStop)).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false when shuttle is to left of longitude range", () => {
|
||||
const shuttle = createShuttle(
|
||||
baseStop.coordinates.latitude,
|
||||
baseStop.coordinates.longitude - 0.0011
|
||||
);
|
||||
expect(shuttleHasArrivedAtStop(shuttle, baseStop)).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false when shuttle is to right of longitude range", () => {
|
||||
const shuttle = createShuttle(
|
||||
baseStop.coordinates.latitude,
|
||||
baseStop.coordinates.longitude + 0.0011
|
||||
);
|
||||
expect(shuttleHasArrivedAtStop(shuttle, baseStop)).toBe(false);
|
||||
});
|
||||
|
||||
it("returns true when shuttle is in the range", () => {
|
||||
const shuttle = createShuttle(
|
||||
baseStop.coordinates.latitude + 0.0005,
|
||||
baseStop.coordinates.longitude - 0.0005
|
||||
);
|
||||
expect(shuttleHasArrivedAtStop(shuttle, baseStop)).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user