use of org.onebusaway.gtfs.serialization.mappings.InvalidStopTimeException in project onebusaway-application-modules by camsys.
the class GtfsRealtimeTripLibrary method getTripDescriptorAsBlockDescriptor.
private BlockDescriptor getTripDescriptorAsBlockDescriptor(MonitoredResult result, TripDescriptor trip, long currentTime) {
if (!trip.hasTripId()) {
return null;
}
TripEntry tripEntry = _entitySource.getTrip(trip.getTripId());
if (tripEntry == null) {
if (result != null) {
_log.debug("reporting unmatched trip with id=" + trip.getTripId());
result.addUnmatchedTripId(trip.getTripId());
} else {
_log.warn("no trip found with id=" + trip.getTripId());
}
return null;
}
ServiceDate serviceDate = null;
BlockInstance instance;
BlockEntry block = tripEntry.getBlock();
if (trip.hasStartDate() && !"0".equals(trip.getStartDate())) {
try {
serviceDate = ServiceDate.parseString(trip.getStartDate());
} catch (ParseException ex) {
_log.warn("Could not parse service date " + trip.getStartDate(), ex);
}
}
if (serviceDate != null) {
instance = _blockCalendarService.getBlockInstance(block.getId(), serviceDate.getAsDate().getTime());
if (instance == null) {
_log.warn("block " + block.getId() + " does not exist on service date " + serviceDate);
return null;
}
} else {
long timeFrom = currentTime - 30 * 60 * 1000;
long timeTo = currentTime + 30 * 60 * 1000;
List<BlockInstance> instances = _blockCalendarService.getActiveBlocks(block.getId(), timeFrom, timeTo);
if (instances.isEmpty()) {
instances = _blockCalendarService.getClosestActiveBlocks(block.getId(), currentTime);
}
if (instances.isEmpty()) {
_log.warn("could not find any active instances for the specified block=" + block.getId() + " trip=" + trip);
return null;
}
instance = instances.get(0);
}
if (serviceDate == null) {
serviceDate = new ServiceDate(new Date(instance.getServiceDate()));
}
BlockDescriptor blockDescriptor = new BlockDescriptor();
blockDescriptor.setBlockInstance(instance);
blockDescriptor.setStartDate(serviceDate);
if (trip.hasScheduleRelationship()) {
blockDescriptor.setScheduleRelationshipValue(trip.getScheduleRelationship().toString());
}
int tripStartTime = 0;
int blockStartTime = 0;
if (trip.hasStartTime() && !"0".equals(trip.getStartTime())) {
try {
tripStartTime = StopTimeFieldMappingFactory.getStringAsSeconds(trip.getStartTime());
} catch (InvalidStopTimeException iste) {
_log.error("invalid stopTime of " + trip.getStartTime() + " for trip " + trip);
}
blockStartTime = getBlockStartTimeForTripStartTime(instance, tripEntry.getId(), tripStartTime);
blockDescriptor.setStartTime(blockStartTime);
}
return blockDescriptor;
}
Aggregations