/**
* TODO: Eventually, use startDate and startTime to distinguish between
* different instances
*/
BlockInstance instance = instances.get(0);
BlockConfigurationEntry blockConfiguration = instance.getBlock();
List<BlockTripEntry> blockTrips = blockConfiguration.getTrips();
Map<String, List<TripUpdate>> tripUpdatesByTripId = MappingLibrary.mapToValueList(
tripUpdates, "trip.tripId");
int currentTime = (int) ((t - instance.getServiceDate()) / 1000);
BestScheduleDeviation best = new BestScheduleDeviation();
for (BlockTripEntry blockTrip : blockTrips) {
TripEntry trip = blockTrip.getTrip();
AgencyAndId tripId = trip.getId();
List<TripUpdate> updatesForTrip = tripUpdatesByTripId.get(tripId.getId());
if (updatesForTrip != null) {
for (TripUpdate tripUpdate : updatesForTrip) {
if (tripUpdate.hasExtension(GtfsRealtimeOneBusAway.obaTripUpdate)) {
OneBusAwayTripUpdate obaTripUpdate = tripUpdate.getExtension(GtfsRealtimeOneBusAway.obaTripUpdate);
if (obaTripUpdate.hasDelay()) {
/**
* TODO: Improved logic around picking the "best" schedule deviation
*/
int delay = obaTripUpdate.getDelay();
best.delta = 0;
best.isInPast = false;
best.scheduleDeviation = delay;
}
if (obaTripUpdate.hasTimestamp()) {
best.timestamp = obaTripUpdate.getTimestamp() * 1000;
}
}
for (StopTimeUpdate stopTimeUpdate : tripUpdate.getStopTimeUpdateList()) {
BlockStopTimeEntry blockStopTime = getBlockStopTimeForStopTimeUpdate(
tripUpdate, stopTimeUpdate, blockTrip.getStopTimes(),
instance.getServiceDate());
if (blockStopTime == null)
continue;
StopTimeEntry stopTime = blockStopTime.getStopTime();
int currentArrivalTime = computeArrivalTime(stopTime,
stopTimeUpdate, instance.getServiceDate());
if (currentArrivalTime >= 0) {
updateBestScheduleDeviation(currentTime,
stopTime.getArrivalTime(), currentArrivalTime, best);
}
int currentDepartureTime = computeDepartureTime(stopTime,
stopTimeUpdate, instance.getServiceDate());
if (currentDepartureTime >= 0) {
updateBestScheduleDeviation(currentTime,
stopTime.getDepartureTime(), currentDepartureTime, best);
}
}
}
}
}
record.setServiceDate(instance.getServiceDate());
record.setScheduleDeviation(best.scheduleDeviation);
if (best.timestamp != 0) {
record.setTimeOfRecord(best.timestamp);
}
}