Search in sources :

Example 16 with ArrivalAndDepartureInstance

use of org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance in project onebusaway-application-modules by camsys.

the class ArrivalAndDepartureAlarmServiceImpl method registerAlarmForArrivalAndDepartureAtStop.

/**
 **
 * {@link ArrivalAndDepartureAlarmService} Interface
 ***
 */
@Override
public AgencyAndId registerAlarmForArrivalAndDepartureAtStop(ArrivalAndDepartureQuery query, RegisterAlarmQueryBean alarmBean) {
    ArrivalAndDepartureInstance instance = _arrivalAndDepartureService.getArrivalAndDepartureForStop(query);
    if (instance == null)
        throw new ServiceException("no arrival-departure found");
    /**
     * We group alarms by block instance
     */
    BlockInstance blockInstance = instance.getBlockInstance();
    /**
     * Retrieve the alarms for the block instance
     */
    AlarmsForBlockInstance alarms = getAlarmsForBlockInstance(blockInstance);
    /**
     * The effective schedule time is the point in the transit vehicle's
     * schedule run time when the alarm should be fired
     */
    int effectiveScheduleTime = computeEffectiveScheduleTimeForAlarm(alarmBean, instance);
    /**
     * Create and register the alarm
     */
    AlarmAction action = new AlarmAction();
    action.setUrl(alarmBean.getUrl());
    AlarmForBlockInstance alarm = alarms.registerAlarm(action, effectiveScheduleTime, instance);
    _alarmsById.put(alarm.getId(), alarm);
    _log.debug("alarm created: {}", alarm.getId());
    return alarm.getId();
}
Also used : ServiceException(org.onebusaway.exceptions.ServiceException) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) ArrivalAndDepartureInstance(org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance) AlarmAction(org.onebusaway.transit_data_federation.services.AlarmAction)

Example 17 with ArrivalAndDepartureInstance

use of org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance in project onebusaway-application-modules by camsys.

the class ArrivalAndDepartureServiceImpl method getArrivalAndDepartureForStop.

@Override
public ArrivalAndDepartureInstance getArrivalAndDepartureForStop(ArrivalAndDepartureQuery query) {
    StopEntry stop = query.getStop();
    int stopSequence = query.getStopSequence();
    TripEntry trip = query.getTrip();
    long serviceDate = query.getServiceDate();
    AgencyAndId vehicleId = query.getVehicleId();
    long time = query.getTime();
    Map<BlockInstance, List<BlockLocation>> locationsByInstance = _blockStatusService.getBlocks(trip.getBlock().getId(), serviceDate, vehicleId, time);
    if (locationsByInstance.isEmpty())
        return null;
    Map.Entry<BlockInstance, List<BlockLocation>> entry = locationsByInstance.entrySet().iterator().next();
    BlockInstance blockInstance = entry.getKey();
    List<BlockLocation> locations = entry.getValue();
    int timeOfServiceDate = (int) ((time - serviceDate) / 1000);
    ArrivalAndDepartureInstance instance = createArrivalAndDeparture(blockInstance, trip.getId(), stop.getId(), stopSequence, serviceDate, timeOfServiceDate, time);
    if (!locations.isEmpty()) {
        /**
         * What if there are multiple locations? Pick the first?
         */
        BlockLocation location = locations.get(0);
        applyBlockLocationToInstance(instance, location, time);
    }
    return instance;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) TripEntry(org.onebusaway.transit_data_federation.services.transit_graph.TripEntry) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) BlockLocation(org.onebusaway.transit_data_federation.services.realtime.BlockLocation) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) StopEntry(org.onebusaway.transit_data_federation.services.transit_graph.StopEntry) ArrayList(java.util.ArrayList) List(java.util.List) ArrivalAndDepartureInstance(org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance) HashMap(java.util.HashMap) Map(java.util.Map) FactoryMap(org.onebusaway.collections.FactoryMap)

Example 18 with ArrivalAndDepartureInstance

use of org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance in project onebusaway-application-modules by camsys.

the class ArrivalAndDepartureServiceImpl method applyPostInterpolateForFrequencyNoSchedule.

private void applyPostInterpolateForFrequencyNoSchedule(StopTimeInstance sti, long fromTime, long toTime, long frequencyOffsetTime, BlockInstance blockInstance, List<ArrivalAndDepartureInstance> results) {
    if (results == null || results.size() == 0)
        return;
    // Find latest instance. Prefer realtime.
    ArrivalAndDepartureInstance instance = findBestArrivalAndDepartureInstance(results);
    // If no realtime data, don't make extrapolations.
    if (instance.getBlockLocation() == null || !instance.getBlockLocation().isPredicted())
        return;
    BlockStopTimeEntry bst = sti.getStopTime();
    // See similar calculation in
    // FrequencyBlockStopTimeEntry.getStopTimeOffset()
    int d0 = bst.getTrip().getDepartureTimeForIndex(0);
    int d1 = bst.getStopTime().getDepartureTime();
    int stopDelta = d1 - d0;
    int stopEndTime = sti.getFrequency().getEndTime() + stopDelta;
    long stopEndTimeExact = sti.getServiceDate() + stopEndTime * 1000;
    int headwayMs = sti.getFrequency().getHeadwaySecs() * 1000;
    /*
     * TODO: if start_time is available from feed we should use it over this calculation
     */
    long time = instance.getBestDepartureTime();
    if (time == 0)
        time = instance.getBestArrivalTime();
    // Do not extrapolate trips starting at the headway change:
    stopEndTimeExact -= headwayMs;
    // Extrapolate future stop times.
    while ((time += headwayMs) < Math.min(toTime, stopEndTimeExact)) {
        ArrivalAndDepartureInstance newInstance = createArrivalAndDepartureForStopTimeInstanceWithTime(sti, time);
        results.add(newInstance);
    }
}
Also used : ArrivalAndDepartureInstance(org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance) BlockStopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry)

Example 19 with ArrivalAndDepartureInstance

use of org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance in project onebusaway-application-modules by camsys.

the class ArrivalAndDepartureServiceImpl method applyRealTimeToStopTimeInstance.

/*
   * here we map realtime on top of schedule and also filter
   * out canceled trips.
   */
private void applyRealTimeToStopTimeInstance(StopTimeInstance sti, TargetTime targetTime, long fromTime, long toTime, long frequencyOffsetTime, BlockInstance blockInstance, List<BlockLocation> locations, List<ArrivalAndDepartureInstance> results) {
    for (BlockLocation location : locations) {
        if (sti.isFrequencyOffsetSpecified() && ((blockInstance.getBlock().getDepartureTimeForIndex(0) + sti.getFrequencyOffset()) != location.getBlockStartTime())) {
            continue;
        }
        if (TransitDataConstants.STATUS_CANCELED.equals(location.getStatus())) {
            continue;
        }
        ArrivalAndDepartureInstance instance = createArrivalAndDepartureForStopTimeInstance(sti, frequencyOffsetTime);
        applyBlockLocationToInstance(instance, location, targetTime.getTargetTime());
        if (isArrivalAndDepartureBeanInRange(instance, fromTime, toTime))
            results.add(instance);
    }
    if (locations.isEmpty()) {
        ArrivalAndDepartureInstance instance = createArrivalAndDepartureForStopTimeInstance(sti, frequencyOffsetTime);
        if (sti.getFrequency() == null) {
            /**
             * We don't need to get the scheduled location of a vehicle unless its
             * in our arrival window
             */
            if (isArrivalAndDepartureBeanInRange(instance, fromTime, toTime)) {
                BlockLocation scheduledLocation = _blockLocationService.getScheduledLocationForBlockInstance(blockInstance, targetTime.getTargetTime());
                if (scheduledLocation != null)
                    applyBlockLocationToInstance(instance, scheduledLocation, targetTime.getTargetTime());
                results.add(instance);
            }
        } else {
            if (isFrequencyBasedArrivalInRange(blockInstance, sti.getFrequency(), fromTime, toTime)) {
                results.add(instance);
            }
        }
    }
}
Also used : ArrivalAndDepartureInstance(org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance) BlockLocation(org.onebusaway.transit_data_federation.services.realtime.BlockLocation)

Example 20 with ArrivalAndDepartureInstance

use of org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance in project onebusaway-application-modules by camsys.

the class ArrivalAndDepartureServiceImpl method getArrivalsAndDeparturesForStopInTimeRange.

@Override
public List<ArrivalAndDepartureInstance> getArrivalsAndDeparturesForStopInTimeRange(StopEntry stop, TargetTime targetTime, long fromTime, long toTime) {
    // We add a buffer before and after to catch late and early buses
    Date fromTimeBuffered = new Date(fromTime - _blockStatusService.getRunningLateWindow() * 1000);
    Date toTimeBuffered = new Date(toTime + _blockStatusService.getRunningEarlyWindow() * 1000);
    List<StopTimeInstance> stis = _stopTimeService.getStopTimeInstancesInTimeRange(stop, fromTimeBuffered, toTimeBuffered, EFrequencyStopTimeBehavior.INCLUDE_UNSPECIFIED);
    long frequencyOffsetTime = Math.max(targetTime.getTargetTime(), fromTime);
    Map<BlockInstance, List<StopTimeInstance>> stisByBlockId = getStopTimeInstancesByBlockInstance(stis);
    List<ArrivalAndDepartureInstance> instances = new ArrayList<ArrivalAndDepartureInstance>();
    for (Map.Entry<BlockInstance, List<StopTimeInstance>> entry : stisByBlockId.entrySet()) {
        BlockInstance blockInstance = entry.getKey();
        List<BlockLocation> locations = _blockLocationService.getLocationsForBlockInstance(blockInstance, targetTime);
        List<StopTimeInstance> stisForBlock = entry.getValue();
        for (StopTimeInstance sti : stisForBlock) {
            applyRealTimeToStopTimeInstance(sti, targetTime, fromTime, toTime, frequencyOffsetTime, blockInstance, locations, instances);
            if (sti.getFrequency() != null && sti.getFrequency().getExactTimes() == 0) {
                /*
           * adjust following schedule times relative to current realtime data
           */
                applyPostInterpolateForFrequencyNoSchedule(sti, fromTime, toTime, frequencyOffsetTime, blockInstance, instances);
            }
        }
    }
    if (removeFuturePredictionsWithoutRealtime) {
        List<ArrivalAndDepartureInstance> filteredInstances = new ArrayList<ArrivalAndDepartureInstance>();
        for (ArrivalAndDepartureInstance instance : instances) {
            FrequencyEntry entry = instance.getFrequency();
            boolean toAdd = // not a frequency-based instance
            (entry == null) || // frequency interval has started
            (instance.getServiceDate() + (entry.getStartTime() * 1000) < targetTime.getTargetTime()) || // instance has realtime data
            (instance.getBlockLocation() != null && instance.getBlockLocation().isPredicted());
            if (toAdd)
                filteredInstances.add(instance);
        }
        return filteredInstances;
    }
    return instances;
}
Also used : StopTimeInstance(org.onebusaway.transit_data_federation.model.StopTimeInstance) ArrayList(java.util.ArrayList) FrequencyEntry(org.onebusaway.transit_data_federation.services.transit_graph.FrequencyEntry) BlockLocation(org.onebusaway.transit_data_federation.services.realtime.BlockLocation) Date(java.util.Date) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) ArrayList(java.util.ArrayList) List(java.util.List) ArrivalAndDepartureInstance(org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance) HashMap(java.util.HashMap) Map(java.util.Map) FactoryMap(org.onebusaway.collections.FactoryMap)

Aggregations

ArrivalAndDepartureInstance (org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance)37 Test (org.junit.Test)18 TimepointPredictionRecord (org.onebusaway.realtime.api.TimepointPredictionRecord)17 StopTimeInstance (org.onebusaway.transit_data_federation.model.StopTimeInstance)12 BlockInstance (org.onebusaway.transit_data_federation.services.blocks.BlockInstance)10 BlockLocation (org.onebusaway.transit_data_federation.services.realtime.BlockLocation)10 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)9 BlockStopTimeEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry)9 BlockConfigurationEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry)8 Date (java.util.Date)7 TargetTime (org.onebusaway.transit_data_federation.model.TargetTime)7 BlockEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.BlockEntryImpl)6 ArrayList (java.util.ArrayList)5 VehicleLocationRecord (org.onebusaway.realtime.api.VehicleLocationRecord)5 ScheduledBlockLocationServiceImpl (org.onebusaway.transit_data_federation.impl.blocks.ScheduledBlockLocationServiceImpl)5 VehicleLocationRecordCacheImpl (org.onebusaway.transit_data_federation.impl.realtime.VehicleLocationRecordCacheImpl)5 ScheduledBlockLocation (org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation)5 HashMap (java.util.HashMap)3 ArrivalAndDepartureBean (org.onebusaway.transit_data.model.ArrivalAndDepartureBean)3 StopBean (org.onebusaway.transit_data.model.StopBean)3