mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
Add a helper method for setting the "reference time" of the self-updating repo
This commit is contained in:
@@ -16,30 +16,73 @@ export class RedisSelfUpdatingETARepository extends BaseRedisETARepository imple
|
||||
reconnectStrategy: REDIS_RECONNECT_INTERVAL,
|
||||
},
|
||||
}),
|
||||
private referenceTime: Date | null = null,
|
||||
) {
|
||||
super(redisClient);
|
||||
}
|
||||
|
||||
setReferenceTime(referenceTime: Date) {
|
||||
this.referenceTime = referenceTime;
|
||||
}
|
||||
|
||||
getAverageTravelTimeSeconds(identifier: ShuttleTravelTimeDataIdentifier, dateFilter: ShuttleTravelTimeDateFilterArguments): Promise<number | undefined> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
|
||||
startListeningForUpdates() {
|
||||
this.shuttleRepository.addListener(ShuttleRepositoryEvent.SHUTTLE_UPDATED, this.handleShuttleUpdate);
|
||||
this.shuttleRepository.addListener(ShuttleRepositoryEvent.SHUTTLE_WILL_ARRIVE_AT_STOP, this.handleShuttleArriveAtStop)
|
||||
this.shuttleRepository.addListener(ShuttleRepositoryEvent.SHUTTLE_WILL_ARRIVE_AT_STOP, this.handleShuttleWillArriveAtStop)
|
||||
}
|
||||
|
||||
stopListeningForUpdates() {
|
||||
this.shuttleRepository.removeListener(ShuttleRepositoryEvent.SHUTTLE_UPDATED, this.handleShuttleUpdate);
|
||||
this.shuttleRepository.removeListener(ShuttleRepositoryEvent.SHUTTLE_WILL_ARRIVE_AT_STOP, this.handleShuttleArriveAtStop);
|
||||
this.shuttleRepository.removeListener(ShuttleRepositoryEvent.SHUTTLE_WILL_ARRIVE_AT_STOP, this.handleShuttleWillArriveAtStop);
|
||||
}
|
||||
|
||||
private handleShuttleUpdate(shuttle: IShuttle) {
|
||||
// TODO: handle shuttle update
|
||||
}
|
||||
|
||||
private handleShuttleArriveAtStop(shuttleArrival: ShuttleStopArrival) {
|
||||
// TODO: handle shuttle arrive at stop
|
||||
|
||||
private async handleShuttleWillArriveAtStop(
|
||||
shuttleArrival: ShuttleStopArrival,
|
||||
|
||||
) {
|
||||
const lastStop = await this.shuttleRepository.getShuttleLastStopArrival(shuttleArrival.shuttleId);
|
||||
if (lastStop == undefined) return;
|
||||
|
||||
const shuttle = await this.shuttleRepository.getShuttleById(shuttleArrival.shuttleId);
|
||||
if (shuttle == null) return;
|
||||
|
||||
let referenceCurrentTime = new Date();
|
||||
if (this.referenceTime != null) {
|
||||
referenceCurrentTime = this.referenceTime;
|
||||
}
|
||||
|
||||
const oneWeekAgo = new Date(referenceCurrentTime.getTime() - (60 * 60 * 24 * 7 * 1000));
|
||||
|
||||
const lastOrderedStop = await this.shuttleRepository.getOrderedStopByRouteAndStopId(shuttle.routeId, lastStop.stopId);
|
||||
const nextStop = lastOrderedStop?.nextStop;
|
||||
if (nextStop == null) return;
|
||||
|
||||
const travelTimeSeconds = await this.getAverageTravelTimeSeconds({
|
||||
routeId: shuttle.routeId,
|
||||
fromStopId: lastStop.stopId,
|
||||
toStopId: nextStop.stopId,
|
||||
}, {
|
||||
from: oneWeekAgo,
|
||||
to: new Date(oneWeekAgo.getTime() + (60 * 60 * 1000))
|
||||
});
|
||||
if (travelTimeSeconds == undefined) return;
|
||||
|
||||
const elapsedTimeMs = referenceCurrentTime.getTime() - lastStop.timestamp.getTime();
|
||||
const predictedTimeSeconds = travelTimeSeconds - (elapsedTimeMs / 1000);
|
||||
|
||||
await this.addOrUpdateEta({
|
||||
secondsRemaining: predictedTimeSeconds,
|
||||
shuttleId: shuttle.id,
|
||||
stopId: nextStop.stopId,
|
||||
systemId: nextStop.systemId,
|
||||
updatedTime: new Date(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user