@golemio/pid
Version:
Golemio PID Module
92 lines • 5.97 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StopTimesTripScopeHandler = void 0;
const OgModuleToken_1 = require("../../../../ioc/OgModuleToken");
const PublicVPTripStopTimesTransformation_1 = require("../../../transformations/scopes/PublicVPTripStopTimesTransformation");
const Geo_1 = require("@golemio/core/dist/output-gateway/Geo");
const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors");
const luxon_1 = require("@golemio/core/dist/shared/luxon");
const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe");
const AbstractDetailedTripScopeHandler_1 = require("./AbstractDetailedTripScopeHandler");
let StopTimesTripScopeHandler = exports.StopTimesTripScopeHandler = class StopTimesTripScopeHandler extends AbstractDetailedTripScopeHandler_1.AbstractDetailedTripScopeHandler {
constructor(delayComputationRepository, stopTimeRepository) {
super();
this.delayComputationRepository = delayComputationRepository;
this.stopTimeRepository = stopTimeRepository;
this.tripStopTimesTransformation = new PublicVPTripStopTimesTransformation_1.PublicVPTripStopTimesTransformation();
}
async handle(output, vehiclePosition) {
const delayComputationCache = await this.delayComputationRepository.getDelayComputationCache(vehiclePosition.gtfs_trip_id);
if (!delayComputationCache) {
throw new golemio_errors_1.GeneralError("delay_computation_cache_not_found", this.constructor.name, undefined, 500);
}
const filteredStopTimes = this.filterNoStopWaypoints(delayComputationCache.stop_times);
const vpAndFilteredStopTimes = filteredStopTimes.map((stopTime) => ({ stopTime, vehiclePosition }));
const stopTimes = this.tripStopTimesTransformation.transformArray(vpAndFilteredStopTimes);
const stopTimesWithDelay = await this.stopTimeRepository.getPublicStopTimeCache(vehiclePosition.vehicle_id, vehiclePosition.gtfs_trip_id);
if (stopTimesWithDelay.length > 0) {
let prevStopArrivalTime = null;
let prevStopDepartureTime = null;
for (const stopTimeWithDelay of stopTimesWithDelay) {
// note: We can improve performance here if needed
const stopTime = stopTimes.find((currentStopTime) => currentStopTime.properties.stop_sequence === stopTimeWithDelay.sequence);
if (stopTime) {
// note: optimization point here in case of worsening performance on output API (additional cache)
const rtArrivalTime = stopTimeWithDelay.arr_delay === null
? null
: luxon_1.DateTime.fromFormat(stopTime.properties.arrival_time, "HH:mm:ss").plus({
seconds: stopTimeWithDelay.arr_delay,
});
let rtDepartureTime = stopTimeWithDelay.dep_delay === null
? null
: luxon_1.DateTime.fromFormat(stopTime.properties.departure_time, "HH:mm:ss").plus({
seconds: stopTimeWithDelay.dep_delay,
});
// note: for better performance, we should address this on the DB side in the future
const { currentArrival, currentDeparture } = this.checkStopTimesSequence(rtArrivalTime, prevStopArrivalTime, rtDepartureTime, prevStopDepartureTime);
prevStopArrivalTime = currentArrival;
prevStopDepartureTime = currentDeparture;
stopTime.properties.realtime_arrival_time = currentArrival ? currentArrival.toFormat("HH:mm:ss") : null;
stopTime.properties.realtime_departure_time = currentDeparture ? currentDeparture.toFormat("HH:mm:ss") : null;
}
}
}
output.stop_times = (0, Geo_1.buildGeojsonFeatureCollection)(stopTimes);
return output;
}
filterNoStopWaypoints(stopTimes) {
return stopTimes.filter((stopTime) => !stopTime.is_no_stop_waypoint);
}
checkStopTimesSequence(rtArrival, prevArrival, rtDeparture, prevDeparture) {
const returnValue = {
currentArrival: rtArrival,
currentDeparture: rtDeparture,
};
if (rtArrival > rtDeparture || prevDeparture > rtDeparture || prevArrival > rtDeparture) {
returnValue.currentDeparture = null;
}
if (prevDeparture > rtArrival || prevArrival > rtArrival) {
returnValue.currentArrival = null;
}
return returnValue;
}
};
exports.StopTimesTripScopeHandler = StopTimesTripScopeHandler = __decorate([
(0, tsyringe_1.injectable)(),
__param(0, (0, tsyringe_1.inject)(OgModuleToken_1.OgModuleToken.DelayComputationRepository)),
__param(1, (0, tsyringe_1.inject)(OgModuleToken_1.OgModuleToken.PublicStopTimeRepository)),
__metadata("design:paramtypes", [Object, Object])
], StopTimesTripScopeHandler);
//# sourceMappingURL=StopTimesTripScopeHandler.js.map