private ScheduledBlockLocation getScheduledBlockLocationForVehicleLocationCacheRecord(
BlockInstance blockInstance, VehicleLocationCacheElement cacheElement,
long targetTime) {
VehicleLocationRecord record = cacheElement.getRecord();
ScheduledBlockLocation scheduledBlockLocation = cacheElement.getScheduledBlockLocation();
BlockConfigurationEntry blockConfig = blockInstance.getBlock();
long serviceDate = blockInstance.getServiceDate();
int scheduledTime = (int) ((targetTime - serviceDate) / 1000);
/**
* If location interpolation has been turned off, then we assume the vehicle
* is at its last known location, so we return that if it's been stored with
* the cache element.
*/
if (!_locationInterpolation && scheduledBlockLocation != null) {
return scheduledBlockLocation;
}
if (record.isScheduleDeviationSet()) {
/**
* Effective scheduled time is the point that a transit vehicle is at on
* its schedule, with schedule deviation taken into account. So if it's
* 100 minutes into the current service date and the bus is running 10
* minutes late, it's actually at the 90 minute point in its scheduled
* operation.
*/
int effectiveScheduledTime = (int) (scheduledTime - record.getScheduleDeviation());
if (scheduledBlockLocation != null
&& scheduledBlockLocation.getScheduledTime() <= effectiveScheduledTime) {
return _scheduledBlockLocationService.getScheduledBlockLocationFromScheduledTime(
scheduledBlockLocation, effectiveScheduledTime);
}
return _scheduledBlockLocationService.getScheduledBlockLocationFromScheduledTime(
blockConfig, effectiveScheduledTime);
}
if (record.isDistanceAlongBlockSet()) {
if ((_locationInterpolation || _distanceAlongBlockLocationInterpolation)
&& scheduledBlockLocation != null
&& scheduledBlockLocation.getDistanceAlongBlock() <= record.getDistanceAlongBlock()) {
int ellapsedTime = (int) ((targetTime - record.getTimeOfRecord()) / 1000);
if (ellapsedTime >= 0) {
int effectiveScheduledTime = scheduledBlockLocation.getScheduledTime()
+ ellapsedTime;
return _scheduledBlockLocationService.getScheduledBlockLocationFromScheduledTime(
blockConfig, effectiveScheduledTime);
}
return _scheduledBlockLocationService.getScheduledBlockLocationFromDistanceAlongBlock(
scheduledBlockLocation, record.getDistanceAlongBlock());
}
return _scheduledBlockLocationService.getScheduledBlockLocationFromDistanceAlongBlock(
blockConfig, record.getDistanceAlongBlock());
}
return _scheduledBlockLocationService.getScheduledBlockLocationFromScheduledTime(
blockConfig, scheduledTime);
}