Search in sources :

Example 1 with BlockTripInstance

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

the class TripStatusBeanServiceImpl method getTripsForId.

@Override
public ListBean<TripDetailsBean> getTripsForId(TripDetailsQueryBean query) {
    AgencyAndId tripId = AgencyAndIdLibrary.convertFromString(query.getTripId());
    long serviceDate = query.getServiceDate();
    AgencyAndId vehicleId = AgencyAndIdLibrary.convertFromString(query.getVehicleId());
    long time = query.getTime();
    TripEntry tripEntry = _transitGraphDao.getTripEntryForId(tripId);
    if (tripEntry == null)
        return new ListBean<TripDetailsBean>();
    Map<BlockInstance, List<BlockLocation>> locationsByInstance = _blockStatusService.getBlocks(tripEntry.getBlock().getId(), serviceDate, vehicleId, time);
    List<TripDetailsBean> tripDetails = new ArrayList<TripDetailsBean>();
    for (Map.Entry<BlockInstance, List<BlockLocation>> entry : locationsByInstance.entrySet()) {
        BlockInstance blockInstance = entry.getKey();
        List<BlockLocation> locations = entry.getValue();
        BlockTripInstance blockTripInstance = BlockTripInstanceLibrary.getBlockTripInstance(blockInstance, tripId);
        if (blockTripInstance == null)
            throw new IllegalStateException("expected blockTrip for trip=" + tripEntry + " and block=" + blockInstance);
        /**
         * If we have no locations for the specified block instance, it means the
         * block is not currently active. But we can still attempt to construct a
         * trip details
         */
        if (locations.isEmpty()) {
            TripDetailsBean details = getTripEntryAndBlockLocationAsTripDetails(blockTripInstance, null, query.getInclusion(), time);
            tripDetails.add(details);
        } else {
            for (BlockLocation location : locations) {
                TripDetailsBean details = getBlockLocationAsTripDetails(blockTripInstance, location, query.getInclusion(), time);
                tripDetails.add(details);
            }
        }
    }
    return new ListBean<TripDetailsBean>(tripDetails, false);
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) BlockTripInstance(org.onebusaway.transit_data_federation.services.blocks.BlockTripInstance) ArrayList(java.util.ArrayList) ListBean(org.onebusaway.transit_data.model.ListBean) 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) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) TripDetailsBean(org.onebusaway.transit_data.model.trips.TripDetailsBean)

Example 2 with BlockTripInstance

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

the class ServiceAlertsServiceImplTest method testGetServiceAlertsForVehicleJourney.

@Test
public void testGetServiceAlertsForVehicleJourney() {
    /**
     * These alerts should match
     */
    ServiceAlertRecord alert2 = new ServiceAlertRecord();
    alert2.setAgencyId("1");
    alert2.setServiceAlertId("B");
    ServiceAlertsSituationAffectsClause affectsClause2 = new ServiceAlertsSituationAffectsClause();
    affectsClause2.setAgencyId("1");
    affectsClause2.setTripId("TripA");
    alert2.getAllAffects().add(affectsClause2);
    alert2 = _service.createOrUpdateServiceAlert(alert2);
    ServiceAlertRecord alert3 = new ServiceAlertRecord();
    alert3.setAgencyId("1");
    alert3.setServiceAlertId("C");
    ServiceAlertsSituationAffectsClause affectsClause3 = new ServiceAlertsSituationAffectsClause();
    affectsClause3.setAgencyId("1");
    affectsClause3.setRouteId("RouteX");
    alert3.getAllAffects().add(affectsClause3);
    alert3 = _service.createOrUpdateServiceAlert(alert3);
    ServiceAlertRecord alert4 = new ServiceAlertRecord();
    alert4.setAgencyId("1");
    alert4.setServiceAlertId("D");
    ServiceAlertsSituationAffectsClause affectsClause4 = new ServiceAlertsSituationAffectsClause();
    affectsClause4.setAgencyId("1");
    affectsClause4.setRouteId("RouteX");
    affectsClause4.setDirectionId("1");
    alert4.getAllAffects().add(affectsClause4);
    alert4 = _service.createOrUpdateServiceAlert(alert4);
    /**
     * These alerts shouldn't match
     */
    ServiceAlertRecord alert5 = new ServiceAlertRecord();
    alert5.setAgencyId("1");
    alert5.setServiceAlertId("E");
    ServiceAlertsSituationAffectsClause affectsClause5 = new ServiceAlertsSituationAffectsClause();
    affectsClause5.setAgencyId("1");
    affectsClause5.setStopId("10021");
    affectsClause5.setTripId("TripA");
    alert5.getAllAffects().add(affectsClause5);
    alert5 = _service.createOrUpdateServiceAlert(alert5);
    ServiceAlertRecord alert6 = new ServiceAlertRecord();
    alert6.setAgencyId("1");
    alert6.setServiceAlertId("F");
    ServiceAlertsSituationAffectsClause affectsClause6 = new ServiceAlertsSituationAffectsClause();
    affectsClause6.setAgencyId("1");
    affectsClause6.setStopId("10020");
    affectsClause6.setTripId("TripB");
    alert6.getAllAffects().add(affectsClause6);
    alert6 = _service.createOrUpdateServiceAlert(alert6);
    ServiceAlertRecord alert7 = new ServiceAlertRecord();
    alert7.setAgencyId("1");
    alert7.setServiceAlertId("G");
    ServiceAlertsSituationAffectsClause affectsClause7 = new ServiceAlertsSituationAffectsClause();
    affectsClause7.setAgencyId("1");
    affectsClause7.setTripId("TripB");
    alert7.getAllAffects().add(affectsClause7);
    alert7 = _service.createOrUpdateServiceAlert(alert7);
    ServiceAlertRecord alert8 = new ServiceAlertRecord();
    alert8.setAgencyId("1");
    alert8.setServiceAlertId("H");
    ServiceAlertsSituationAffectsClause affectsClause8 = new ServiceAlertsSituationAffectsClause();
    affectsClause8.setAgencyId("1");
    affectsClause8.setRouteId("RouteY");
    alert8.getAllAffects().add(affectsClause8);
    alert8 = _service.createOrUpdateServiceAlert(alert8);
    ServiceAlertRecord alert9 = new ServiceAlertRecord();
    alert9.setAgencyId("1");
    alert9.setServiceAlertId("I");
    ServiceAlertsSituationAffectsClause affectsClause9 = new ServiceAlertsSituationAffectsClause();
    affectsClause9.setAgencyId("1");
    affectsClause9.setRouteId("RouteX");
    affectsClause9.setDirectionId("0");
    alert9.getAllAffects().add(affectsClause9);
    alert9 = _service.createOrUpdateServiceAlert(alert9);
    RouteEntryImpl route = route("RouteX");
    routeCollection("RouteX", route);
    StopEntryImpl stop = stop("10020", 47.0, -122.0);
    TripEntryImpl trip = trip("TripA");
    trip.setRoute(route);
    trip.setDirectionId("1");
    stopTime(0, stop, trip, time(8, 53), 0);
    BlockEntryImpl block = block("block");
    BlockConfigurationEntry blockConfig = blockConfiguration(block, serviceIds(lsids("a"), lsids()), trip);
    BlockTripInstance blockTripInstance = new BlockTripInstance(blockConfig.getTrips().get(0), new InstanceState(System.currentTimeMillis()));
    List<ServiceAlertRecord> alerts = _service.getServiceAlertsForVehicleJourney(System.currentTimeMillis(), blockTripInstance, new AgencyAndId("1", "1111"));
    assertEquals(3, alerts.size());
    assertTrue(alerts.contains(alert2));
    assertTrue(alerts.contains(alert3));
    assertTrue(alerts.contains(alert4));
}
Also used : InstanceState(org.onebusaway.transit_data_federation.services.blocks.InstanceState) BlockTripInstance(org.onebusaway.transit_data_federation.services.blocks.BlockTripInstance) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) RouteEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.RouteEntryImpl) BlockEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.BlockEntryImpl) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry) TripEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl) StopEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.StopEntryImpl) Test(org.junit.Test)

Example 3 with BlockTripInstance

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

the class ArrivalAndDepartureServiceImpl method createArrivalAndDeparture.

private ArrivalAndDepartureInstance createArrivalAndDeparture(BlockInstance blockInstance, AgencyAndId tripId, AgencyAndId stopId, int stopSequence, long serviceDate, int timeOfServiceDate, long prevFrequencyTime) {
    BlockTripInstance blockTripInstance = BlockTripInstanceLibrary.getBlockTripInstance(blockInstance, tripId);
    if (blockTripInstance == null)
        return null;
    BlockStopTimeEntry blockStopTime = getBlockStopTime(blockTripInstance, stopId, stopSequence, timeOfServiceDate);
    if (blockStopTime == null) {
        _log.error("block stop time is null for stopid=" + stopId + " and blockTripInstance=" + blockTripInstance + " and timeOfServiceDate=" + timeOfServiceDate);
        return null;
    }
    StopTimeInstance stopTimeInstance = new StopTimeInstance(blockStopTime, blockTripInstance.getState());
    return createArrivalAndDeparture(stopTimeInstance, prevFrequencyTime, StopTimeInstance.UNSPECIFIED_FREQUENCY_OFFSET);
}
Also used : BlockTripInstance(org.onebusaway.transit_data_federation.services.blocks.BlockTripInstance) StopTimeInstance(org.onebusaway.transit_data_federation.model.StopTimeInstance) BlockStopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry)

Example 4 with BlockTripInstance

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

the class TripStatusBeanServiceImpl method getBlockLocationAsStatusBean.

@Override
public TripStatusBean getBlockLocationAsStatusBean(BlockLocation blockLocation, long time) {
    TripStatusBean bean = new TripStatusBean();
    bean.setStatus("default");
    BlockInstance blockInstance = blockLocation.getBlockInstance();
    long serviceDate = blockInstance.getServiceDate();
    bean.setServiceDate(serviceDate);
    bean.setLastUpdateTime(blockLocation.getLastUpdateTime());
    bean.setLastLocationUpdateTime(blockLocation.getLastLocationUpdateTime());
    bean.setLastKnownLocation(blockLocation.getLastKnownLocation());
    bean.setLastKnownOrientation(blockLocation.getLastKnownOrientation());
    bean.setLocation(blockLocation.getLocation());
    bean.setOrientation(blockLocation.getOrientation());
    bean.setLastKnownLocation(blockLocation.getLastKnownLocation());
    if (blockLocation.isLastKnownOrientationSet())
        bean.setLastKnownOrientation(blockLocation.getLastKnownOrientation());
    bean.setScheduleDeviation(blockLocation.getScheduleDeviation());
    BlockTripInstance activeTripInstance = blockLocation.getActiveTripInstance();
    if (activeTripInstance != null) {
        BlockTripEntry activeBlockTrip = activeTripInstance.getBlockTrip();
        bean.setScheduledDistanceAlongTrip(blockLocation.getScheduledDistanceAlongBlock() - activeBlockTrip.getDistanceAlongBlock());
        bean.setDistanceAlongTrip(blockLocation.getDistanceAlongBlock() - activeBlockTrip.getDistanceAlongBlock());
        TripEntry activeTrip = activeBlockTrip.getTrip();
        bean.setTotalDistanceAlongTrip(activeTrip.getTotalTripDistance());
        TripBean activeTripBean = _tripBeanService.getTripForId(activeTrip.getId());
        bean.setActiveTrip(activeTripBean);
        bean.setBlockTripSequence(activeBlockTrip.getSequence());
        if (blockLocation.isLastKnownDistanceAlongBlockSet()) {
            bean.setLastKnownDistanceAlongTrip(blockLocation.getLastKnownDistanceAlongBlock() - activeBlockTrip.getDistanceAlongBlock());
        }
        FrequencyEntry frequencyLabel = activeTripInstance.getFrequencyLabel();
        if (frequencyLabel != null) {
            FrequencyBean fb = FrequencyBeanLibrary.getBeanForFrequency(serviceDate, frequencyLabel);
            bean.setFrequency(fb);
        }
    } else {
        _log.warn("no active block trip for block location: blockInstance=" + blockLocation.getBlockInstance() + " time=" + time);
    }
    BlockStopTimeEntry closestStop = blockLocation.getClosestStop();
    if (closestStop != null) {
        StopTimeEntry stopTime = closestStop.getStopTime();
        StopBean stopBean = _stopBeanService.getStopForId(stopTime.getStop().getId());
        bean.setClosestStop(stopBean);
        bean.setClosestStopTimeOffset(blockLocation.getClosestStopTimeOffset());
    }
    BlockStopTimeEntry nextStop = blockLocation.getNextStop();
    if (nextStop != null) {
        StopTimeEntry stopTime = nextStop.getStopTime();
        StopBean stopBean = _stopBeanService.getStopForId(stopTime.getStop().getId());
        bean.setNextStop(stopBean);
        bean.setNextStopTimeOffset(blockLocation.getNextStopTimeOffset());
        bean.setNextStopDistanceFromVehicle(blockLocation.getNextStop().getDistanceAlongBlock() - blockLocation.getDistanceAlongBlock());
    }
    BlockStopTimeEntry previousStop = blockLocation.getPreviousStop();
    if (previousStop != null) {
        StopTimeEntry stopTime = previousStop.getStopTime();
        StopBean stopBean = _stopBeanService.getStopForId(stopTime.getStop().getId());
        bean.setPreviousStop(stopBean);
        bean.setPreviousStopTimeOffset(blockLocation.getPreviousStopTimeOffset());
        bean.setPreviousStopDistanceFromVehicle(blockLocation.getPreviousStop().getDistanceAlongBlock() - blockLocation.getDistanceAlongBlock());
    }
    EVehiclePhase phase = blockLocation.getPhase();
    if (phase != null)
        bean.setPhase(phase.toLabel());
    String status = blockLocation.getStatus();
    if (status != null)
        bean.setStatus(status);
    if (blockLocation.getVehicleType() != null)
        bean.setVehicleType(blockLocation.getVehicleType().toLabel());
    bean.setPredicted(blockLocation.isPredicted());
    AgencyAndId vid = blockLocation.getVehicleId();
    if (vid != null)
        bean.setVehicleId(ApplicationBeanLibrary.getId(vid));
    if (activeTripInstance != null) {
        List<ServiceAlertBean> situations = _serviceAlertBeanService.getServiceAlertsForVehicleJourney(time, activeTripInstance, blockLocation.getVehicleId());
        if (!situations.isEmpty())
            bean.setSituations(situations);
    }
    if (blockLocation.getTimepointPredictions() != null && blockLocation.getTimepointPredictions().size() > 0) {
        List<TimepointPredictionBean> timepointPredictions = new ArrayList<TimepointPredictionBean>();
        for (TimepointPredictionRecord tpr : blockLocation.getTimepointPredictions()) {
            TimepointPredictionBean tpb = new TimepointPredictionBean();
            tpb.setTimepointId(tpr.getTimepointId().toString());
            tpb.setTripId(tpr.getTripId().toString());
            tpb.setStopSequence(tpr.getStopSequence());
            tpb.setTimepointPredictedArrivalTime(tpr.getTimepointPredictedArrivalTime());
            tpb.setTimepointPredictedDepartureTime(tpr.getTimepointPredictedDepartureTime());
            timepointPredictions.add(tpb);
        }
        bean.setTimepointPredictions(timepointPredictions);
    }
    return bean;
}
Also used : TimepointPredictionBean(org.onebusaway.transit_data.model.trips.TimepointPredictionBean) BlockTripInstance(org.onebusaway.transit_data_federation.services.blocks.BlockTripInstance) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) ArrayList(java.util.ArrayList) TripEntry(org.onebusaway.transit_data_federation.services.transit_graph.TripEntry) BlockTripEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry) TripBean(org.onebusaway.transit_data.model.trips.TripBean) FrequencyEntry(org.onebusaway.transit_data_federation.services.transit_graph.FrequencyEntry) EVehiclePhase(org.onebusaway.realtime.api.EVehiclePhase) FrequencyBean(org.onebusaway.transit_data.model.schedule.FrequencyBean) StopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.StopTimeEntry) BlockStopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) TimepointPredictionRecord(org.onebusaway.realtime.api.TimepointPredictionRecord) StopBean(org.onebusaway.transit_data.model.StopBean) TripStatusBean(org.onebusaway.transit_data.model.trips.TripStatusBean) ServiceAlertBean(org.onebusaway.transit_data.model.service_alerts.ServiceAlertBean) BlockStopTimeEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry)

Aggregations

BlockTripInstance (org.onebusaway.transit_data_federation.services.blocks.BlockTripInstance)4 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)3 ArrayList (java.util.ArrayList)2 BlockInstance (org.onebusaway.transit_data_federation.services.blocks.BlockInstance)2 BlockStopTimeEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry)2 BlockTripEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockTripEntry)2 TripEntry (org.onebusaway.transit_data_federation.services.transit_graph.TripEntry)2 List (java.util.List)1 Map (java.util.Map)1 Test (org.junit.Test)1 EVehiclePhase (org.onebusaway.realtime.api.EVehiclePhase)1 TimepointPredictionRecord (org.onebusaway.realtime.api.TimepointPredictionRecord)1 ListBean (org.onebusaway.transit_data.model.ListBean)1 StopBean (org.onebusaway.transit_data.model.StopBean)1 FrequencyBean (org.onebusaway.transit_data.model.schedule.FrequencyBean)1 ServiceAlertBean (org.onebusaway.transit_data.model.service_alerts.ServiceAlertBean)1 TimepointPredictionBean (org.onebusaway.transit_data.model.trips.TimepointPredictionBean)1 TripBean (org.onebusaway.transit_data.model.trips.TripBean)1 TripDetailsBean (org.onebusaway.transit_data.model.trips.TripDetailsBean)1 TripStatusBean (org.onebusaway.transit_data.model.trips.TripStatusBean)1