Search in sources :

Example 1 with InvalidStopTimeException

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;
}
Also used : ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) BlockEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockEntry) InvalidStopTimeException(org.onebusaway.gtfs.serialization.mappings.InvalidStopTimeException) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) TripEntry(org.onebusaway.transit_data_federation.services.transit_graph.TripEntry) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) ParseException(java.text.ParseException) Date(java.util.Date) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint)

Aggregations

ParseException (java.text.ParseException)1 Date (java.util.Date)1 CoordinatePoint (org.onebusaway.geospatial.model.CoordinatePoint)1 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)1 InvalidStopTimeException (org.onebusaway.gtfs.serialization.mappings.InvalidStopTimeException)1 BlockInstance (org.onebusaway.transit_data_federation.services.blocks.BlockInstance)1 BlockEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockEntry)1 BlockTripEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry)1 TripEntry (org.onebusaway.transit_data_federation.services.transit_graph.TripEntry)1