Search in sources :

Example 6 with VehicleStatusBean

use of org.onebusaway.transit_data.model.VehicleStatusBean in project onebusaway-application-modules by camsys.

the class TripUpdatesForAgencyActionTest method test.

@Test
public void test() {
    long now = System.currentTimeMillis();
    List<VehicleStatusBean> vehicles = new ArrayList<VehicleStatusBean>();
    RouteBean.Builder routeBuilder = RouteBean.builder();
    routeBuilder.setId("1_r1");
    RouteBean route = routeBuilder.create();
    {
        VehicleStatusBean vehicle = new VehicleStatusBean();
        vehicles.add(vehicle);
        vehicle.setLastUpdateTime(1234 * 1000);
        vehicle.setVehicleId("1_v1");
        TripStatusBean tripStatus = new TripStatusBean();
        vehicle.setTripStatus(tripStatus);
        tripStatus.setScheduleDeviation(2 * 60);
        TripBean trip = new TripBean();
        trip.setId("1_t0");
        trip.setRoute(route);
        tripStatus.setActiveTrip(trip);
        StopBean stop = new StopBean();
        stop.setId("1_s2");
        tripStatus.setNextStop(stop);
        tripStatus.setNextStopTimeOffset(5 * 60);
    }
    {
        VehicleStatusBean vehicle = new VehicleStatusBean();
        vehicles.add(vehicle);
        vehicle.setLastUpdateTime(5678 * 1000);
        vehicle.setVehicleId("1_v2");
        TripStatusBean tripStatus = new TripStatusBean();
        vehicle.setTripStatus(tripStatus);
        tripStatus.setScheduleDeviation(3 * 60);
        TripBean trip = new TripBean();
        trip.setId("1_t1");
        trip.setRoute(route);
        tripStatus.setActiveTrip(trip);
        StopBean stop = new StopBean();
        stop.setId("1_s3");
        tripStatus.setNextStop(stop);
        tripStatus.setNextStopTimeOffset(10 * 60);
    }
    ListBean<VehicleStatusBean> bean = new ListBean<VehicleStatusBean>();
    bean.setList(vehicles);
    Mockito.when(_service.getAllVehiclesForAgency("1", now)).thenReturn(bean);
    _action.setId("1");
    _action.setTime(new Date(now));
    _action.show();
    ResponseBean model = _action.getModel();
    FeedMessage feed = (FeedMessage) model.getData();
    assertEquals(now / 1000, feed.getHeader().getTimestamp());
    assertEquals(2, feed.getEntityCount());
    {
        FeedEntity entity = feed.getEntity(0);
        assertEquals("1", entity.getId());
        TripUpdate tripUpdate = entity.getTripUpdate();
        assertEquals("t0", tripUpdate.getTrip().getTripId());
        assertEquals("r1", tripUpdate.getTrip().getRouteId());
        assertEquals("v1", tripUpdate.getVehicle().getId());
        assertEquals(1234, tripUpdate.getTimestamp());
        assertEquals(120, tripUpdate.getDelay());
        assertEquals(1, tripUpdate.getStopTimeUpdateCount());
        StopTimeUpdate stopTimeUpdate = tripUpdate.getStopTimeUpdate(0);
        assertEquals("s2", stopTimeUpdate.getStopId());
        assertEquals(now / 1000 + 5 * 60, stopTimeUpdate.getDeparture().getTime());
    }
    {
        FeedEntity entity = feed.getEntity(1);
        assertEquals("2", entity.getId());
        TripUpdate tripUpdate = entity.getTripUpdate();
        assertEquals("t1", tripUpdate.getTrip().getTripId());
        assertEquals("r1", tripUpdate.getTrip().getRouteId());
        assertEquals("v2", tripUpdate.getVehicle().getId());
        assertEquals(5678, tripUpdate.getTimestamp());
        assertEquals(180, tripUpdate.getDelay());
        assertEquals(1, tripUpdate.getStopTimeUpdateCount());
        StopTimeUpdate stopTimeUpdate = tripUpdate.getStopTimeUpdate(0);
        assertEquals("s3", stopTimeUpdate.getStopId());
        assertEquals(now / 1000 + 10 * 60, stopTimeUpdate.getDeparture().getTime());
    }
}
Also used : TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) ArrayList(java.util.ArrayList) ListBean(org.onebusaway.transit_data.model.ListBean) TripBean(org.onebusaway.transit_data.model.trips.TripBean) Date(java.util.Date) VehicleStatusBean(org.onebusaway.transit_data.model.VehicleStatusBean) RouteBean(org.onebusaway.transit_data.model.RouteBean) FeedMessage(com.google.transit.realtime.GtfsRealtime.FeedMessage) StopTimeUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate) StopBean(org.onebusaway.transit_data.model.StopBean) ResponseBean(org.onebusaway.api.model.ResponseBean) TripStatusBean(org.onebusaway.transit_data.model.trips.TripStatusBean) FeedEntity(com.google.transit.realtime.GtfsRealtime.FeedEntity) Test(org.junit.Test)

Example 7 with VehicleStatusBean

use of org.onebusaway.transit_data.model.VehicleStatusBean in project onebusaway-application-modules by camsys.

the class TripUpdatesForAgencyAction method fillFeedMessage.

@Override
protected void fillFeedMessage(FeedMessage.Builder feed, String agencyId, long timestamp) {
    ListBean<VehicleStatusBean> vehicles = _service.getAllVehiclesForAgency(agencyId, timestamp);
    for (VehicleStatusBean vehicle : vehicles.getList()) {
        TripStatusBean tripStatus = vehicle.getTripStatus();
        if (tripStatus == null) {
            continue;
        }
        TripBean activeTrip = tripStatus.getActiveTrip();
        RouteBean route = activeTrip.getRoute();
        FeedEntity.Builder entity = feed.addEntityBuilder();
        entity.setId(Integer.toString(feed.getEntityCount()));
        TripUpdate.Builder tripUpdate = entity.getTripUpdateBuilder();
        TripDescriptor.Builder tripDesc = tripUpdate.getTripBuilder();
        tripDesc.setTripId(normalizeId(activeTrip.getId()));
        tripDesc.setRouteId(normalizeId(route.getId()));
        VehicleDescriptor.Builder vehicleDesc = tripUpdate.getVehicleBuilder();
        vehicleDesc.setId(normalizeId(vehicle.getVehicleId()));
        if (tripStatus.getTimepointPredictions() != null && tripStatus.getTimepointPredictions().size() > 0) {
            for (TimepointPredictionBean timepointPrediction : tripStatus.getTimepointPredictions()) {
                AgencyAndId stopId = modifiedStopId(agencyId, timepointPrediction.getTimepointId());
                if (!stopId.getAgencyId().equals(agencyId))
                    continue;
                TripUpdate.StopTimeUpdate.Builder stopTimeUpdate = tripUpdate.addStopTimeUpdateBuilder();
                stopTimeUpdate.setStopId(normalizeId(stopId.toString()));
                TripUpdate.StopTimeEvent.Builder arrival = stopTimeUpdate.getArrivalBuilder();
                if (timepointPrediction.getTimepointPredictedArrivalTime() != -1) {
                    arrival.setTime(timepointPrediction.getTimepointPredictedArrivalTime() / 1000L);
                }
                TripUpdate.StopTimeEvent.Builder departure = stopTimeUpdate.getDepartureBuilder();
                if (timepointPrediction.getTimepointPredictedDepartureTime() != -1) {
                    departure.setTime(timepointPrediction.getTimepointPredictedDepartureTime() / 1000L);
                }
            }
            tripUpdate.setTimestamp(vehicle.getLastUpdateTime() / 1000);
        } else {
            StopBean nextStop = tripStatus.getNextStop();
            if (nextStop != null) {
                AgencyAndId stopId = modifiedStopId(agencyId, nextStop.getId());
                if (stopId.getAgencyId().equals(agencyId)) {
                    TripUpdate.StopTimeUpdate.Builder stopTimeUpdate = tripUpdate.addStopTimeUpdateBuilder();
                    stopTimeUpdate.setStopId(normalizeId(stopId.toString()));
                    TripUpdate.StopTimeEvent.Builder departure = stopTimeUpdate.getDepartureBuilder();
                    departure.setTime(timestamp / 1000 + tripStatus.getNextStopTimeOffset());
                }
            }
        }
        tripUpdate.setDelay((int) tripStatus.getScheduleDeviation());
        tripUpdate.setTimestamp(vehicle.getLastUpdateTime() / 1000);
    }
}
Also used : TimepointPredictionBean(org.onebusaway.transit_data.model.trips.TimepointPredictionBean) TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) TripBean(org.onebusaway.transit_data.model.trips.TripBean) VehicleStatusBean(org.onebusaway.transit_data.model.VehicleStatusBean) RouteBean(org.onebusaway.transit_data.model.RouteBean) TripDescriptor(com.google.transit.realtime.GtfsRealtime.TripDescriptor) StopBean(org.onebusaway.transit_data.model.StopBean) TripStatusBean(org.onebusaway.transit_data.model.trips.TripStatusBean) FeedEntity(com.google.transit.realtime.GtfsRealtime.FeedEntity) VehicleDescriptor(com.google.transit.realtime.GtfsRealtime.VehicleDescriptor)

Example 8 with VehicleStatusBean

use of org.onebusaway.transit_data.model.VehicleStatusBean in project onebusaway-application-modules by camsys.

the class VehiclePositionsForAgencyAction method fillFeedMessage.

@Override
protected void fillFeedMessage(FeedMessage.Builder feed, String agencyId, long timestamp) {
    ListBean<VehicleStatusBean> vehicles = _service.getAllVehiclesForAgency(agencyId, timestamp);
    for (VehicleStatusBean vehicle : vehicles.getList()) {
        FeedEntity.Builder entity = feed.addEntityBuilder();
        entity.setId(Integer.toString(feed.getEntityCount()));
        VehiclePosition.Builder vehiclePosition = entity.getVehicleBuilder();
        TripStatusBean tripStatus = vehicle.getTripStatus();
        if (tripStatus != null) {
            TripBean activeTrip = tripStatus.getActiveTrip();
            RouteBean route = activeTrip.getRoute();
            TripDescriptor.Builder tripDesc = vehiclePosition.getTripBuilder();
            tripDesc.setTripId(normalizeId(activeTrip.getId()));
            tripDesc.setRouteId(normalizeId(route.getId()));
        }
        VehicleDescriptor.Builder vehicleDesc = vehiclePosition.getVehicleBuilder();
        vehicleDesc.setId(normalizeId(vehicle.getVehicleId()));
        CoordinatePoint location = vehicle.getLocation();
        if (location != null) {
            Position.Builder position = vehiclePosition.getPositionBuilder();
            position.setLatitude((float) location.getLat());
            position.setLongitude((float) location.getLon());
        }
        vehiclePosition.setTimestamp(vehicle.getLastUpdateTime() / 1000);
    }
}
Also used : CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) VehiclePosition(com.google.transit.realtime.GtfsRealtime.VehiclePosition) Position(com.google.transit.realtime.GtfsRealtime.Position) TripBean(org.onebusaway.transit_data.model.trips.TripBean) VehicleStatusBean(org.onebusaway.transit_data.model.VehicleStatusBean) RouteBean(org.onebusaway.transit_data.model.RouteBean) VehiclePosition(com.google.transit.realtime.GtfsRealtime.VehiclePosition) TripDescriptor(com.google.transit.realtime.GtfsRealtime.TripDescriptor) TripStatusBean(org.onebusaway.transit_data.model.trips.TripStatusBean) FeedEntity(com.google.transit.realtime.GtfsRealtime.FeedEntity) VehicleDescriptor(com.google.transit.realtime.GtfsRealtime.VehicleDescriptor)

Example 9 with VehicleStatusBean

use of org.onebusaway.transit_data.model.VehicleStatusBean in project onebusaway-application-modules by camsys.

the class VehiclesForAgencyAction method show.

public DefaultHttpHeaders show() throws IOException, ServiceException {
    if (!isVersion(V2))
        return setUnknownVersionResponse();
    if (hasErrors())
        return setValidationErrorsResponse();
    long time = SystemTime.currentTimeMillis();
    if (_time != 0)
        time = _time;
    BeanFactoryV2 factory = getBeanFactoryV2();
    try {
        ListBean<VehicleStatusBean> vehicles = _service.getAllVehiclesForAgency(_id, time);
        return setOkResponse(factory.getVehicleStatusResponse(vehicles));
    } catch (OutOfServiceAreaServiceException ex) {
        return setOkResponse(factory.getEmptyList(VehicleStatusV2Bean.class, true));
    }
}
Also used : OutOfServiceAreaServiceException(org.onebusaway.exceptions.OutOfServiceAreaServiceException) BeanFactoryV2(org.onebusaway.api.model.transit.BeanFactoryV2) VehicleStatusBean(org.onebusaway.transit_data.model.VehicleStatusBean)

Example 10 with VehicleStatusBean

use of org.onebusaway.transit_data.model.VehicleStatusBean in project onebusaway-application-modules by camsys.

the class VehicleStatusBeanServiceImpl method getStatusAsBean.

/**
 **
 *
 ***
 */
private VehicleStatusBean getStatusAsBean(VehicleStatus status, long time) {
    VehicleLocationRecord record = status.getRecord();
    VehicleStatusBean bean = new VehicleStatusBean();
    bean.setLastUpdateTime(record.getTimeOfRecord());
    bean.setLastLocationUpdateTime(record.getTimeOfLocationUpdate());
    EVehiclePhase phase = record.getPhase();
    if (phase != null)
        bean.setPhase(phase.toLabel());
    bean.setStatus(record.getStatus());
    if (record.isCurrentLocationSet())
        bean.setLocation(new CoordinatePoint(record.getCurrentLocationLat(), record.getCurrentLocationLon()));
    bean.setVehicleId(AgencyAndIdLibrary.convertToString(record.getVehicleId()));
    TripDetailsBean details = _tripDetailsBeanService.getTripForVehicle(record.getVehicleId(), time, new TripDetailsInclusionBean(true, false, true));
    if (details != null && details.getStatus() != null) {
        bean.setTrip(details.getTrip());
        bean.setTripStatus(details.getStatus());
    }
    List<VehicleLocationRecord> allRecords = status.getAllRecords();
    if (!CollectionsLibrary.isEmpty(allRecords)) {
        List<VehicleLocationRecordBean> allRecordBeans = new ArrayList<VehicleLocationRecordBean>();
        bean.setAllRecords(allRecordBeans);
        for (VehicleLocationRecord r : allRecords) {
            VehicleLocationRecordBean rBean = getVehicleLocationRecordAsBean(r);
            allRecordBeans.add(rBean);
        }
    }
    return bean;
}
Also used : CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint) EVehiclePhase(org.onebusaway.realtime.api.EVehiclePhase) ArrayList(java.util.ArrayList) VehicleLocationRecord(org.onebusaway.realtime.api.VehicleLocationRecord) TripDetailsInclusionBean(org.onebusaway.transit_data.model.trips.TripDetailsInclusionBean) VehicleLocationRecordBean(org.onebusaway.transit_data.model.realtime.VehicleLocationRecordBean) TripDetailsBean(org.onebusaway.transit_data.model.trips.TripDetailsBean) VehicleStatusBean(org.onebusaway.transit_data.model.VehicleStatusBean)

Aggregations

VehicleStatusBean (org.onebusaway.transit_data.model.VehicleStatusBean)10 ArrayList (java.util.ArrayList)6 ListBean (org.onebusaway.transit_data.model.ListBean)5 FeedEntity (com.google.transit.realtime.GtfsRealtime.FeedEntity)4 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)4 RouteBean (org.onebusaway.transit_data.model.RouteBean)4 TripBean (org.onebusaway.transit_data.model.trips.TripBean)4 TripStatusBean (org.onebusaway.transit_data.model.trips.TripStatusBean)4 CoordinatePoint (org.onebusaway.geospatial.model.CoordinatePoint)3 FeedMessage (com.google.transit.realtime.GtfsRealtime.FeedMessage)2 TripDescriptor (com.google.transit.realtime.GtfsRealtime.TripDescriptor)2 TripUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate)2 VehicleDescriptor (com.google.transit.realtime.GtfsRealtime.VehicleDescriptor)2 VehiclePosition (com.google.transit.realtime.GtfsRealtime.VehiclePosition)2 IOException (java.io.IOException)2 Date (java.util.Date)2 List (java.util.List)2 Test (org.junit.Test)2 ResponseBean (org.onebusaway.api.model.ResponseBean)2 BeanFactoryV2 (org.onebusaway.api.model.transit.BeanFactoryV2)2