Search in sources :

Example 31 with BlockInstance

use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance 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)

Example 32 with BlockInstance

use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.

the class ArrivalAndDepartureServiceImpl method getScheduledArrivalsAndDeparturesForStopInTimeRange.

@Override
public List<ArrivalAndDepartureInstance> getScheduledArrivalsAndDeparturesForStopInTimeRange(StopEntry stop, long currentTime, long fromTime, long toTime) {
    List<StopTimeInstance> stis = _stopTimeService.getStopTimeInstancesInTimeRange(stop, new Date(fromTime), new Date(toTime), EFrequencyStopTimeBehavior.INCLUDE_UNSPECIFIED);
    List<ArrivalAndDepartureInstance> instances = new ArrayList<ArrivalAndDepartureInstance>();
    long prevFrequencyTime = Math.max(currentTime, fromTime);
    for (StopTimeInstance sti : stis) {
        BlockInstance blockInstance = sti.getBlockInstance();
        ArrivalAndDepartureInstance instance = createArrivalAndDepartureForStopTimeInstance(sti, prevFrequencyTime);
        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, currentTime);
                if (scheduledLocation != null)
                    applyBlockLocationToInstance(instance, scheduledLocation, currentTime);
                instances.add(instance);
            }
        } else {
            if (isFrequencyBasedArrivalInRange(blockInstance, sti.getFrequency(), fromTime, toTime)) {
                instances.add(instance);
            }
        }
    }
    return instances;
}
Also used : StopTimeInstance(org.onebusaway.transit_data_federation.model.StopTimeInstance) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) ArrayList(java.util.ArrayList) ArrivalAndDepartureInstance(org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance) BlockLocation(org.onebusaway.transit_data_federation.services.realtime.BlockLocation) Date(java.util.Date)

Example 33 with BlockInstance

use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.

the class ArrivalAndDepartureServiceImpl method getStopTimeInstancesByBlockInstance.

private Map<BlockInstance, List<StopTimeInstance>> getStopTimeInstancesByBlockInstance(List<StopTimeInstance> stopTimes) {
    Map<BlockInstance, List<StopTimeInstance>> r = new FactoryMap<BlockInstance, List<StopTimeInstance>>(new ArrayList<StopTimeInstance>());
    for (StopTimeInstance stopTime : stopTimes) {
        BlockStopTimeEntry blockStopTime = stopTime.getStopTime();
        BlockTripEntry blockTrip = blockStopTime.getTrip();
        BlockConfigurationEntry blockConfiguration = blockTrip.getBlockConfiguration();
        long serviceDate = stopTime.getServiceDate();
        BlockInstance blockInstance = new BlockInstance(blockConfiguration, serviceDate, stopTime.getFrequency());
        r.get(blockInstance).add(stopTime);
    }
    return r;
}
Also used : FactoryMap(org.onebusaway.collections.FactoryMap) StopTimeInstance(org.onebusaway.transit_data_federation.model.StopTimeInstance) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) ArrayList(java.util.ArrayList) List(java.util.List) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry) BlockStopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry)

Example 34 with BlockInstance

use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.

the class SimulateScheduledVehicleLocationsController method index.

@RequestMapping()
public ModelAndView index(@RequestParam() String blockId, @RequestParam() long serviceDate, @RequestParam(defaultValue = "0") int scheduleDeviation, @RequestParam(defaultValue = "0.0") double noise) {
    AgencyAndId bid = AgencyAndIdLibrary.convertFromString(blockId);
    BlockInstance blockInstance = _blockCalendarService.getBlockInstance(bid, serviceDate);
    CurrentVehicleEstimateQueryBean bean = new CurrentVehicleEstimateQueryBean();
    long time = SystemTime.currentTimeMillis();
    List<Record> records = new ArrayList<Record>();
    for (int i = 0; i < 5 * 60; i += 30) {
        int scheduleTime = (int) ((time - blockInstance.getServiceDate()) / 1000 - scheduleDeviation - i);
        ScheduledBlockLocation location = _scheduledBlockLocationService.getScheduledBlockLocationFromScheduledTime(blockInstance.getBlock(), scheduleTime);
        if (location != null) {
            CoordinatePoint p = applyNoiseToLocation(location.getLocation(), noise);
            Record r = new Record();
            r.setLocation(location.getLocation());
            r.setTimestamp(time - i * 1000);
            r.setLocation(p);
            r.setAccuracy(noise);
            records.add(r);
        }
    }
    bean.setRecords(records);
    ModelAndView mv = new ModelAndView("simulate-vehicle-locations.jspx");
    mv.addObject("time", time);
    mv.addObject("query", bean);
    return mv;
}
Also used : ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) CurrentVehicleEstimateQueryBean(org.onebusaway.transit_data.model.realtime.CurrentVehicleEstimateQueryBean) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) ArrayList(java.util.ArrayList) ModelAndView(org.springframework.web.servlet.ModelAndView) Record(org.onebusaway.transit_data.model.realtime.CurrentVehicleEstimateQueryBean.Record) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 35 with BlockInstance

use of org.onebusaway.transit_data_federation.services.blocks.BlockInstance in project onebusaway-application-modules by camsys.

the class SiriService method handleVehicleMonitoring.

/**
 **
 * Private Methods
 ***
 */
private void handleVehicleMonitoring(ServiceDelivery serviceDelivery, VehicleMonitoringDeliveryStructure deliveryForModule, SiriEndpointDetails endpointDetails) {
    List<VehicleLocationRecord> records = new ArrayList<VehicleLocationRecord>();
    Date now = new Date();
    long timeFrom = now.getTime() - _blockInstanceSearchWindow * 60 * 1000;
    long timeTo = now.getTime() + _blockInstanceSearchWindow * 60 * 1000;
    for (VehicleActivityStructure vehicleActivity : deliveryForModule.getVehicleActivity()) {
        Date time = vehicleActivity.getRecordedAtTime();
        if (time == null)
            time = now;
        MonitoredVehicleJourney mvj = vehicleActivity.getMonitoredVehicleJourney();
        Duration delay = mvj.getDelay();
        if (delay == null)
            continue;
        VehicleRefStructure vehicleRef = mvj.getVehicleRef();
        if (vehicleRef == null || vehicleRef.getValue() == null)
            continue;
        BlockEntry block = getBlockForMonitoredVehicleJourney(mvj, endpointDetails);
        if (block == null) {
            TripEntry trip = getTripForMonitoredVehicleJourney(mvj, endpointDetails);
            if (trip != null)
                block = trip.getBlock();
        }
        if (block == null)
            continue;
        List<BlockInstance> instances = _blockCalendarService.getActiveBlocks(block.getId(), timeFrom, timeTo);
        // TODO : We currently assume that a block won't overlap with itself
        if (instances.size() != 1)
            continue;
        BlockInstance instance = instances.get(0);
        VehicleLocationRecord r = new VehicleLocationRecord();
        r.setTimeOfRecord(time.getTime());
        r.setServiceDate(instance.getServiceDate());
        r.setBlockId(block.getId());
        String agencyId = block.getId().getAgencyId();
        r.setVehicleId(new AgencyAndId(agencyId, vehicleRef.getValue()));
        r.setScheduleDeviation(delay.getTimeInMillis(now) / 1000);
        LocationStructure location = mvj.getVehicleLocation();
        if (location != null) {
            r.setCurrentLocationLat(location.getLatitude().doubleValue());
            r.setCurrentLocationLon(location.getLongitude().doubleValue());
        }
        records.add(r);
    }
    if (!records.isEmpty())
        _vehicleLocationListener.handleVehicleLocationRecords(records);
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ArrayList(java.util.ArrayList) Duration(javax.xml.datatype.Duration) TripEntry(org.onebusaway.transit_data_federation.services.transit_graph.TripEntry) Date(java.util.Date) MonitoredVehicleJourney(uk.org.siri.siri.VehicleActivityStructure.MonitoredVehicleJourney) BlockEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockEntry) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) VehicleLocationRecord(org.onebusaway.realtime.api.VehicleLocationRecord)

Aggregations

BlockInstance (org.onebusaway.transit_data_federation.services.blocks.BlockInstance)63 BlockConfigurationEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry)36 BlockEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.BlockEntryImpl)25 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)24 Test (org.junit.Test)20 TripEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl)20 ArrayList (java.util.ArrayList)19 BlockLocation (org.onebusaway.transit_data_federation.services.realtime.BlockLocation)19 VehicleLocationRecord (org.onebusaway.realtime.api.VehicleLocationRecord)16 Date (java.util.Date)15 ScheduledBlockLocation (org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation)12 BlockTripEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry)12 List (java.util.List)11 CoordinatePoint (org.onebusaway.geospatial.model.CoordinatePoint)11 TripUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate)10 ArrivalAndDepartureInstance (org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance)10 BlockStopTimeEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry)10 StopTimeUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate)9 StopTimeInstance (org.onebusaway.transit_data_federation.model.StopTimeInstance)9 TripEntry (org.onebusaway.transit_data_federation.services.transit_graph.TripEntry)9