Skip to content

Commit

Permalink
Add support for connections with departureTime equal to arrivalTime (…
Browse files Browse the repository at this point in the history
…De Lijn)
  • Loading branch information
brechtvdv committed Oct 7, 2015
1 parent 0171fe5 commit 84516a2
Showing 1 changed file with 54 additions and 20 deletions.
74 changes: 54 additions & 20 deletions lib/arrdep2connections.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,39 +139,73 @@ ArrDep2Connections.prototype._getNextArrival = function (cb) {
arrival["arrivalTime"] = new Date(self._normalizeISO8601(arrival["date"] + 'T' + arrival["arrivalTime"]));
self._arrivalsQueue.push(arrival);
cb(arrival);
} else if (self._arrivalsQueue.length > 0) {
// Only possible when stopSequence feature is not available
// and there are connections with the same departure- and arrivalTime
self._arrivalsQueueIndex = 0;
cb(null); // No next arrival, so drop departure
} else {
cb();
self.end(null,null, function () {
// console.error("finished");
//console.error("finished");
});
}
});
}
}

ArrDep2Connections.prototype._processArrival = function (departure, arrival, next, done) {
//check if arrival has the same trip id, if it does, we've got a winner: first thing always wins
var departureTime = departure["departureTime"]; // e.g.: Date object 2015-09-09T00:01
var arrivalTime = arrival["arrivalTime"];
var departureDateTime = new Date(departureTime);
var arrivalDateTime = new Date(arrivalTime);
console.error(arrival);
if (arrival) {
//check if arrival has the same trip id, if it does, we've got a winner: first thing always wins
var departureTime = departure["departureTime"]; // e.g.: Date object 2015-09-09T00:01
var arrivalTime = arrival["arrivalTime"];
var departureDateTime = new Date(departureTime);
var arrivalDateTime = new Date(arrivalTime);

// Connections with equal arrival- and departuretimes are possible (e.g. De Lijn)
// Keep those to stay consistent with stops
if (arrivalDateTime < departureDateTime) {
//discart it (as this is only possible for the first X items, we can do shift and bring the arrivalsQueueIndex back to 0
for (var i = 0; i < this._arrivalsQueueIndex; i++) {
this._arrivalsQueue.shift();
}
this._arrivalsQueueIndex = 0;
next();
} else if (departure["trip"] === arrival["trip"]) {
// If feature with stopSequence is available, we can make connections with departureTime equal to arrivalTime
if (departure['stopSequence'] && arrival['stopSequence'] && (departure['stopSequence'] == (arrival['stopSequence'] - 1))) {
var connection = this._createConnection(arrival, departure);
this._arrivalsQueueIndex = 0;
this.push(connection);
done();
} else if (departureTime < arrivalTime) {
// first one to encounter each other: it's a match!
var connection = this._createConnection(arrival, departure);
this._arrivalsQueueIndex = 0;
this.push(connection);
done();
} else {
// arrival is part of previous connection or feature stopSequence is not available
next();
}
} else {
// Set maximum arrivalTime of connection, so no all arrivals have to be checked. (prevent memory issues)
if (!this._maxArrivalTime) {
this._maxArrivalTime = moment(departure["departureTime"]).add(this._arrivalTimeOffsetUnits, this._arrivalTimeOffsetKey).toDate();
}

if (arrivalDateTime <= departureDateTime) {
//discart it (as this is only possible for the first X items, we can do shift and bring the arrivalsQueueIndex back to 0
for (var i = 0; i < this._arrivalsQueueIndex; i++) {
this._arrivalsQueue.shift();
if (arrival["arrivalTime"].getTime() <= this._maxArrivalTime.getTime()) {
//arrival is part of the next one part of another trip.
next();
} else {
done();
}
}
this._arrivalsQueueIndex = 0;
next();
} else if (departure["trip"] === arrival["trip"]) {
// first one to encounter each other: it's a match!
var connection = this._createConnection(arrival, departure);
this._arrivalsQueueIndex = 0;
this.push(connection);
done();
} else {
//arrival is part of the next one part of another trip.
next();
debugger;
// No matching arrival found
done();
}
}

Expand Down

0 comments on commit 84516a2

Please sign in to comment.