Search in sources :

Example 1 with MonitoredVehicleJourney

use of uk.org.siri.siri.VehicleActivityStructure.MonitoredVehicleJourney in project onebusaway-application-modules by camsys.

the class VehicleMonitoringActionTest method testExecuteByRoute.

@Test
public void testExecuteByRoute() throws Exception {
    when(request.getParameter(eq("LineRef"))).thenReturn("40_100479");
    when(request.getParameter(eq("OperatorRef"))).thenReturn("1");
    PrintWriter nothingPrintWriter = new PrintWriter(new OutputStream() {

        @Override
        public void write(int b) throws IOException {
        // Do nothing
        }
    });
    when(servletResponse.getWriter()).thenReturn(nothingPrintWriter);
    List<VehicleActivityStructure> vehicleActivities = new ArrayList<VehicleActivityStructure>();
    when(realtimeService.getVehicleActivityForRoute(eq("40_100479"), anyString(), eq(0), anyLong(), eq(false))).thenReturn(vehicleActivities);
    VehicleActivityStructure vehicleActivity = new VehicleActivityStructure();
    vehicleActivities.add(vehicleActivity);
    MonitoredVehicleJourney mvJourney = new MonitoredVehicleJourney();
    vehicleActivity.setMonitoredVehicleJourney(mvJourney);
    LocationStructure locationStructure = new LocationStructure();
    mvJourney.setVehicleLocation(locationStructure);
    locationStructure.setLatitude(BigDecimal.valueOf(88.0));
    locationStructure.setLongitude(BigDecimal.valueOf(89.0));
    ServiceAlertBean serviceAlertBean = ServiceAlertsTestSupport.createServiceAlertBean("1_1");
    when(transitDataService.getServiceAlertForId(anyString())).thenReturn(serviceAlertBean);
    RouteBean routeBean = RouteBean.builder().create();
    when(transitDataService.getRouteForId(anyString())).thenReturn(routeBean);
    when(configurationService.getConfigurationValueAsString(eq("display.googleAnalyticsSiteId"), anyString())).thenReturn("foo");
    List<SituationRefStructure> sitRef = mvJourney.getSituationRef();
    SituationRefStructure sitRefStructure = new SituationRefStructure();
    sitRef.add(sitRefStructure);
    SituationSimpleRefStructure sitSimpleRef = new SituationSimpleRefStructure();
    sitRefStructure.setSituationSimpleRef(sitSimpleRef);
    sitSimpleRef.setValue("situation ref");
    SiriXmlSerializer serializer = new SiriXmlSerializer();
    when(realtimeService.getSiriXmlSerializer()).thenReturn(serializer);
    // doNothing().when(gaService).post(new PageViewHit());
    when(gaService.post(new GoogleAnalyticsRequest())).thenReturn(new GoogleAnalyticsResponse());
    action.setServletRequest(request);
    action.setServletResponse(servletResponse);
    action.index();
    String monitoring = action.getVehicleMonitoring();
    assertTrue("Result XML does not match expected", monitoring.matches("(?s).*<ServiceDelivery><ResponseTimestamp>.+</ResponseTimestamp><VehicleMonitoringDelivery><ResponseTimestamp>.+</ResponseTimestamp><ValidUntil>.+</ValidUntil><VehicleActivity><MonitoredVehicleJourney><SituationRef><SituationSimpleRef>situation ref</SituationSimpleRef></SituationRef><VehicleLocation><Longitude>89.0</Longitude><Latitude>88.0</Latitude></VehicleLocation></MonitoredVehicleJourney></VehicleActivity></VehicleMonitoringDelivery><SituationExchangeDelivery><Situations><PtSituationElement><SituationNumber>1_1</SituationNumber><Summary xml:lang=\"EN\">summary</Summary><Description xml:lang=\"EN\">description</Description><Affects><VehicleJourneys><AffectedVehicleJourney><LineRef>1_100277</LineRef><DirectionRef>0</DirectionRef></AffectedVehicleJourney><AffectedVehicleJourney><LineRef>1_100277</LineRef><DirectionRef>1</DirectionRef></AffectedVehicleJourney><AffectedVehicleJourney><LineRef>1_100194</LineRef><DirectionRef>0</DirectionRef></AffectedVehicleJourney><AffectedVehicleJourney><LineRef>1_100194</LineRef><DirectionRef>1</DirectionRef></AffectedVehicleJourney></VehicleJourneys></Affects></PtSituationElement></Situations></SituationExchangeDelivery></ServiceDelivery></Siri>.*"));
}
Also used : SituationRefStructure(uk.org.siri.siri.SituationRefStructure) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Matchers.anyString(org.mockito.Matchers.anyString) LocationStructure(uk.org.siri.siri.LocationStructure) MonitoredVehicleJourney(uk.org.siri.siri.VehicleActivityStructure.MonitoredVehicleJourney) RouteBean(org.onebusaway.transit_data.model.RouteBean) GoogleAnalyticsRequest(com.brsanthu.googleanalytics.GoogleAnalyticsRequest) SiriXmlSerializer(org.onebusaway.transit_data_federation.siri.SiriXmlSerializer) VehicleActivityStructure(uk.org.siri.siri.VehicleActivityStructure) ServiceAlertBean(org.onebusaway.transit_data.model.service_alerts.ServiceAlertBean) GoogleAnalyticsResponse(com.brsanthu.googleanalytics.GoogleAnalyticsResponse) PrintWriter(java.io.PrintWriter) SituationSimpleRefStructure(uk.org.siri.siri.SituationSimpleRefStructure) Test(org.junit.Test)

Example 2 with MonitoredVehicleJourney

use of uk.org.siri.siri.VehicleActivityStructure.MonitoredVehicleJourney in project onebusaway-application-modules by camsys.

the class RealtimeServiceImpl method getVehicleActivityForVehicle.

@Override
public VehicleActivityStructure getVehicleActivityForVehicle(String vehicleId, int maximumOnwardCalls, long currentTime, String tripId) {
    TripForVehicleQueryBean query = new TripForVehicleQueryBean();
    query.setTime(new Date(currentTime));
    query.setVehicleId(vehicleId);
    TripDetailsInclusionBean inclusion = new TripDetailsInclusionBean();
    inclusion.setIncludeTripStatus(true);
    inclusion.setIncludeTripBean(true);
    query.setInclusion(inclusion);
    TripDetailsBean tripDetailsForCurrentTrip = _transitDataService.getTripDetailsForVehicleAndTime(query);
    if (tripDetailsForCurrentTrip == null) {
        TripDetailsQueryBean detailsQuery = new TripDetailsQueryBean();
        detailsQuery.setTime(currentTime);
        detailsQuery.setTripId(tripId);
        tripDetailsForCurrentTrip = _transitDataService.getSingleTripDetails(detailsQuery);
    }
    if (tripDetailsForCurrentTrip == null || !_presentationService.include(tripDetailsForCurrentTrip.getStatus()))
        return null;
    VehicleActivityStructure output = new VehicleActivityStructure();
    // Check for Realtime Data
    if (!tripDetailsForCurrentTrip.getStatus().isPredicted()) {
        output.setRecordedAtTime(new Date(getTime()));
    } else {
        output.setRecordedAtTime(new Date(tripDetailsForCurrentTrip.getStatus().getLastUpdateTime()));
    }
    List<TimepointPredictionRecord> timePredictionRecords = null;
    timePredictionRecords = _transitDataService.getPredictionRecordsForTrip(AgencyAndId.convertFromString(vehicleId).getAgencyId(), tripDetailsForCurrentTrip.getStatus());
    output.setMonitoredVehicleJourney(new MonitoredVehicleJourney());
    SiriSupport.fillMonitoredVehicleJourney(output.getMonitoredVehicleJourney(), tripDetailsForCurrentTrip.getTrip(), tripDetailsForCurrentTrip.getStatus(), null, OnwardCallsMode.VEHICLE_MONITORING, _presentationService, _transitDataService, maximumOnwardCalls, timePredictionRecords, tripDetailsForCurrentTrip.getStatus().isPredicted(), currentTime, false);
    return output;
}
Also used : TripForVehicleQueryBean(org.onebusaway.transit_data.model.trips.TripForVehicleQueryBean) TripDetailsQueryBean(org.onebusaway.transit_data.model.trips.TripDetailsQueryBean) TimepointPredictionRecord(org.onebusaway.realtime.api.TimepointPredictionRecord) TripDetailsInclusionBean(org.onebusaway.transit_data.model.trips.TripDetailsInclusionBean) VehicleActivityStructure(uk.org.siri.siri.VehicleActivityStructure) Date(java.util.Date) TripDetailsBean(org.onebusaway.transit_data.model.trips.TripDetailsBean) MonitoredVehicleJourney(uk.org.siri.siri.VehicleActivityStructure.MonitoredVehicleJourney)

Example 3 with MonitoredVehicleJourney

use of uk.org.siri.siri.VehicleActivityStructure.MonitoredVehicleJourney in project onebusaway-application-modules by camsys.

the class RealtimeServiceImpl method getVehicleActivityForRoute.

/**
 * SIRI METHODS
 */
@Override
public List<VehicleActivityStructure> getVehicleActivityForRoute(String routeId, String directionId, int maximumOnwardCalls, long currentTime, boolean showRawLocation) {
    List<VehicleActivityStructure> output = new ArrayList<VehicleActivityStructure>();
    ListBean<TripDetailsBean> trips = getAllTripsForRoute(routeId, currentTime);
    for (TripDetailsBean tripDetails : trips.getList()) {
        // filter out interlined routes
        if (routeId != null && !tripDetails.getTrip().getRoute().getId().equals(routeId))
            continue;
        // filtered out by user
        if (tripDetails.getTrip().getDirectionId() != null) {
            if (directionId != null && !tripDetails.getTrip().getDirectionId().equals(directionId)) {
                continue;
            }
        }
        if (!_presentationService.include(tripDetails.getStatus()))
            continue;
        VehicleActivityStructure activity = new VehicleActivityStructure();
        // Check for Realtime Data
        if (!tripDetails.getStatus().isPredicted()) {
            activity.setRecordedAtTime(new Date(getTime()));
        } else {
            activity.setRecordedAtTime(new Date(tripDetails.getStatus().getLastUpdateTime()));
        }
        List<TimepointPredictionRecord> timePredictionRecords = null;
        timePredictionRecords = _transitDataService.getPredictionRecordsForTrip(AgencyAndId.convertFromString(routeId).getAgencyId(), tripDetails.getStatus());
        activity.setMonitoredVehicleJourney(new MonitoredVehicleJourney());
        SiriSupport.fillMonitoredVehicleJourney(activity.getMonitoredVehicleJourney(), tripDetails.getTrip(), tripDetails.getStatus(), null, OnwardCallsMode.VEHICLE_MONITORING, _presentationService, _transitDataService, maximumOnwardCalls, timePredictionRecords, tripDetails.getStatus().isPredicted(), currentTime, showRawLocation);
        output.add(activity);
    }
    Collections.sort(output, new Comparator<VehicleActivityStructure>() {

        public int compare(VehicleActivityStructure arg0, VehicleActivityStructure arg1) {
            try {
                SiriExtensionWrapper wrapper0 = (SiriExtensionWrapper) arg0.getMonitoredVehicleJourney().getMonitoredCall().getExtensions().getAny();
                SiriExtensionWrapper wrapper1 = (SiriExtensionWrapper) arg1.getMonitoredVehicleJourney().getMonitoredCall().getExtensions().getAny();
                return wrapper0.getDistances().getDistanceFromCall().compareTo(wrapper1.getDistances().getDistanceFromCall());
            } catch (Exception e) {
                return -1;
            }
        }
    });
    return output;
}
Also used : ArrayList(java.util.ArrayList) SiriExtensionWrapper(org.onebusaway.transit_data_federation.siri.SiriExtensionWrapper) TimepointPredictionRecord(org.onebusaway.realtime.api.TimepointPredictionRecord) VehicleActivityStructure(uk.org.siri.siri.VehicleActivityStructure) TripDetailsBean(org.onebusaway.transit_data.model.trips.TripDetailsBean) Date(java.util.Date) MonitoredVehicleJourney(uk.org.siri.siri.VehicleActivityStructure.MonitoredVehicleJourney)

Example 4 with MonitoredVehicleJourney

use of uk.org.siri.siri.VehicleActivityStructure.MonitoredVehicleJourney 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

MonitoredVehicleJourney (uk.org.siri.siri.VehicleActivityStructure.MonitoredVehicleJourney)4 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 VehicleActivityStructure (uk.org.siri.siri.VehicleActivityStructure)3 TimepointPredictionRecord (org.onebusaway.realtime.api.TimepointPredictionRecord)2 TripDetailsBean (org.onebusaway.transit_data.model.trips.TripDetailsBean)2 GoogleAnalyticsRequest (com.brsanthu.googleanalytics.GoogleAnalyticsRequest)1 GoogleAnalyticsResponse (com.brsanthu.googleanalytics.GoogleAnalyticsResponse)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 PrintWriter (java.io.PrintWriter)1 Duration (javax.xml.datatype.Duration)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)1 VehicleLocationRecord (org.onebusaway.realtime.api.VehicleLocationRecord)1 RouteBean (org.onebusaway.transit_data.model.RouteBean)1 ServiceAlertBean (org.onebusaway.transit_data.model.service_alerts.ServiceAlertBean)1 TripDetailsInclusionBean (org.onebusaway.transit_data.model.trips.TripDetailsInclusionBean)1 TripDetailsQueryBean (org.onebusaway.transit_data.model.trips.TripDetailsQueryBean)1