use of uk.org.siri.siri.VehicleActivityStructure in project onebusaway-application-modules by camsys.
the class VehicleMonitoringAction method index.
// @Override
public String index() {
processGoogleAnalytics();
long currentTimestamp = getTime();
_realtimeService.setTime(currentTimestamp);
String directionId = _request.getParameter("DirectionRef");
String tripId = _request.getParameter("TripId");
boolean showRawLocation = Boolean.valueOf(_request.getParameter("ShowRawLocation"));
// We need to support the user providing no agency id which means 'all agencies'.
// So, this array will hold a single agency if the user provides it or all
// agencies if the user provides none. We'll iterate over them later while
// querying for vehicles and routes
List<String> agencyIds = new ArrayList<String>();
String agencyId = _request.getParameter("OperatorRef");
if (agencyId != null) {
agencyIds.add(agencyId);
} else {
Map<String, List<CoordinateBounds>> agencies = _transitDataService.getAgencyIdsWithCoverageArea();
agencyIds.addAll(agencies.keySet());
}
List<AgencyAndId> vehicleIds = new ArrayList<AgencyAndId>();
if (_request.getParameter("VehicleRef") != null) {
try {
// If the user included an agency id as part of the vehicle id, ignore any OperatorRef arg
// or lack of OperatorRef arg and just use the included one.
AgencyAndId vehicleId = AgencyAndIdLibrary.convertFromString(_request.getParameter("VehicleRef"));
vehicleIds.add(vehicleId);
} catch (Exception e) {
// The user didn't provide an agency id in the VehicleRef, so use our list of operator refs
for (String agency : agencyIds) {
AgencyAndId vehicleId = new AgencyAndId(agency, _request.getParameter("VehicleRef"));
vehicleIds.add(vehicleId);
}
}
}
List<AgencyAndId> routeIds = new ArrayList<AgencyAndId>();
String routeIdErrorString = "";
if (_request.getParameter("LineRef") != null) {
try {
// Same as above for vehicle id
AgencyAndId routeId = AgencyAndIdLibrary.convertFromString(_request.getParameter("LineRef"));
if (isValidRoute(routeId)) {
routeIds.add(routeId);
} else {
routeIdErrorString += "No such route: " + routeId.toString() + ".";
}
} catch (Exception e) {
// Same as above for vehicle id
for (String agency : agencyIds) {
AgencyAndId routeId = new AgencyAndId(agency, _request.getParameter("LineRef"));
if (isValidRoute(routeId)) {
routeIds.add(routeId);
} else {
routeIdErrorString += "No such route: " + routeId.toString() + ". ";
}
}
}
}
String detailLevel = _request.getParameter("VehicleMonitoringDetailLevel");
int maximumOnwardCalls = 0;
if (detailLevel != null && detailLevel.equals("calls")) {
maximumOnwardCalls = Integer.MAX_VALUE;
try {
maximumOnwardCalls = Integer.parseInt(_request.getParameter("MaximumNumberOfCallsOnwards"));
} catch (NumberFormatException e) {
maximumOnwardCalls = Integer.MAX_VALUE;
}
}
// *** CASE 1: single vehicle, ignore any other filters
if (vehicleIds.size() > 0) {
List<VehicleActivityStructure> activities = new ArrayList<VehicleActivityStructure>();
for (AgencyAndId vehicleId : vehicleIds) {
VehicleActivityStructure activity = _realtimeService.getVehicleActivityForVehicle(vehicleId.toString(), maximumOnwardCalls, currentTimestamp, tripId);
if (activity != null) {
activities.add(activity);
}
}
// No vehicle id validation, so we pass null for error
_response = generateSiriResponse(activities, null, null, currentTimestamp);
// *** CASE 2: by route, using direction id, if provided
} else if (_request.getParameter("LineRef") != null) {
List<VehicleActivityStructure> activities = new ArrayList<VehicleActivityStructure>();
for (AgencyAndId routeId : routeIds) {
List<VehicleActivityStructure> activitiesForRoute = _realtimeService.getVehicleActivityForRoute(routeId.toString(), directionId, maximumOnwardCalls, currentTimestamp, showRawLocation);
if (activitiesForRoute != null) {
activities.addAll(activitiesForRoute);
}
}
if (vehicleIds.size() > 0) {
List<VehicleActivityStructure> filteredActivities = new ArrayList<VehicleActivityStructure>();
for (VehicleActivityStructure activity : activities) {
MonitoredVehicleJourneyStructure journey = activity.getMonitoredVehicleJourney();
AgencyAndId thisVehicleId = AgencyAndIdLibrary.convertFromString(journey.getVehicleRef().getValue());
// user filtering
if (!vehicleIds.contains(thisVehicleId))
continue;
filteredActivities.add(activity);
}
activities = filteredActivities;
}
Exception error = null;
if (_request.getParameter("LineRef") != null && routeIds.size() == 0) {
error = new Exception(routeIdErrorString.trim());
}
_response = generateSiriResponse(activities, routeIds, error, currentTimestamp);
// *** CASE 3: all vehicles
} else {
try {
int hashKey = _cacheService.hash(maximumOnwardCalls, agencyIds, _type);
List<VehicleActivityStructure> activities = new ArrayList<VehicleActivityStructure>();
if (!_cacheService.containsKey(hashKey)) {
for (String agency : agencyIds) {
ListBean<VehicleStatusBean> vehicles = _transitDataService.getAllVehiclesForAgency(agency, currentTimestamp);
for (VehicleStatusBean v : vehicles.getList()) {
VehicleActivityStructure activity = _realtimeService.getVehicleActivityForVehicle(v.getVehicleId(), maximumOnwardCalls, currentTimestamp, tripId);
if (activity != null) {
activities.add(activity);
}
}
}
// There is no input (route id) to validate, so pass null error
_response = generateSiriResponse(activities, null, null, currentTimestamp);
_cacheService.store(hashKey, getVehicleMonitoring());
} else {
_cachedResponse = _cacheService.retrieve(hashKey);
}
} catch (Exception e) {
_log.error("vm all broke:", e);
throw new RuntimeException(e);
}
}
try {
this._servletResponse.getWriter().write(getVehicleMonitoring());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
use of uk.org.siri.siri.VehicleActivityStructure 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>.*"));
}
use of uk.org.siri.siri.VehicleActivityStructure in project onebusaway-application-modules by camsys.
the class SearchResultFactoryImpl method getRouteResult.
@Override
public SearchResult getRouteResult(RouteBean routeBean) {
List<RouteDirection> directions = new ArrayList<RouteDirection>();
StopsForRouteBean stopsForRoute = _transitDataService.getStopsForRoute(routeBean.getId());
// create stop ID->stop bean map
Map<String, StopBean> stopIdToStopBeanMap = new HashMap<String, StopBean>();
for (StopBean stopBean : stopsForRoute.getStops()) {
stopIdToStopBeanMap.put(stopBean.getId(), stopBean);
}
// add stops in both directions
List<VehicleActivityStructure> journeyList = _realtimeService.getVehicleActivityForRoute(routeBean.getId(), null, 0, SystemTime.currentTimeMillis(), false);
Map<String, List<String>> stopIdToDistanceAwayStringMap = new HashMap<String, List<String>>();
Map<String, List<String>> stopIdToVehicleIdMap = new HashMap<String, List<String>>();
Map<String, Boolean> stopIdToRealtimeDataMap = new HashMap<String, Boolean>();
// build map of stop IDs to list of distance strings
for (VehicleActivityStructure journey : journeyList) {
// on detour?
MonitoredCallStructure monitoredCall = journey.getMonitoredVehicleJourney().getMonitoredCall();
if (monitoredCall == null) {
continue;
}
String stopId = monitoredCall.getStopPointRef().getValue();
fillDistanceAwayStringsList(journey.getMonitoredVehicleJourney(), journey.getRecordedAtTime(), stopId, stopIdToDistanceAwayStringMap);
fillVehicleIdsStringList(journey.getMonitoredVehicleJourney(), journey.getRecordedAtTime(), stopId, stopIdToVehicleIdMap);
fillRealtimeData(journey.getMonitoredVehicleJourney(), stopId, stopIdToRealtimeDataMap);
}
List<StopGroupingBean> stopGroupings = stopsForRoute.getStopGroupings();
for (StopGroupingBean stopGroupingBean : stopGroupings) {
for (StopGroupBean stopGroupBean : stopGroupingBean.getStopGroups()) {
NameBean name = stopGroupBean.getName();
String type = name.getType();
if (!type.equals("destination"))
continue;
// service in this direction
Boolean hasUpcomingScheduledService = _transitDataService.routeHasUpcomingScheduledService((routeBean.getAgency() != null ? routeBean.getAgency().getId() : null), SystemTime.currentTimeMillis(), routeBean.getId(), stopGroupBean.getId());
// if there are buses on route, always have "scheduled service"
Boolean routeHasVehiclesInService = _realtimeService.getVehiclesInServiceForRoute(routeBean.getId(), stopGroupBean.getId(), SystemTime.currentTimeMillis());
if (routeHasVehiclesInService) {
hasUpcomingScheduledService = true;
}
// stops in this direction
List<StopOnRoute> stopsOnRoute = null;
if (!stopGroupBean.getStopIds().isEmpty()) {
stopsOnRoute = new ArrayList<StopOnRoute>();
for (String stopId : stopGroupBean.getStopIds()) {
if (_transitDataService.stopHasRevenueServiceOnRoute((routeBean.getAgency() != null ? routeBean.getAgency().getId() : null), stopId, routeBean.getId(), stopGroupBean.getId())) {
stopsOnRoute.add(new StopOnRoute(stopIdToStopBeanMap.get(stopId), stopIdToDistanceAwayStringMap.get(stopId), stopIdToRealtimeDataMap.get(stopId), stopIdToVehicleIdMap.get(stopId)));
}
}
}
directions.add(new RouteDirection(stopGroupBean.getName().getName(), stopGroupBean, stopsOnRoute, hasUpcomingScheduledService, null));
}
}
// service alerts in this direction
Set<String> serviceAlertDescriptions = new HashSet<String>();
List<ServiceAlertBean> serviceAlertBeans = _realtimeService.getServiceAlertsForRoute(routeBean.getId());
populateServiceAlerts(serviceAlertDescriptions, serviceAlertBeans);
return new RouteResult(routeBean, directions, serviceAlertDescriptions);
}
use of uk.org.siri.siri.VehicleActivityStructure in project onebusaway-application-modules by camsys.
the class ServiceAlertsHelper method addSituationExchangeToServiceDelivery.
public void addSituationExchangeToServiceDelivery(ServiceDelivery serviceDelivery, List<VehicleActivityStructure> activities, TransitDataService transitDataService, List<AgencyAndId> routeIds) {
if (activities == null)
return;
Map<String, PtSituationElementStructure> ptSituationElements = new HashMap<String, PtSituationElementStructure>();
for (VehicleActivityStructure activity : activities) {
if (activity.getMonitoredVehicleJourney() != null) {
addSituationElement(transitDataService, ptSituationElements, activity.getMonitoredVehicleJourney().getSituationRef());
}
}
addPtSituationElementsToServiceDelivery(serviceDelivery, ptSituationElements);
if (routeIds == null)
return;
List<ServiceAlertBean> serviceAlerts = new ArrayList<ServiceAlertBean>();
for (AgencyAndId routeId : routeIds) {
SituationQueryBean query = new SituationQueryBean();
SituationQueryBean.AffectsBean affects = new SituationQueryBean.AffectsBean();
query.getAffects().add(affects);
affects.setRouteId(routeId.toString());
ListBean<ServiceAlertBean> serviceAlertsForRoute = transitDataService.getServiceAlerts(query);
if (serviceAlertsForRoute != null) {
serviceAlerts.addAll(serviceAlertsForRoute.getList());
}
}
if (serviceAlerts.size() == 0)
return;
addSituationExchangeToServiceDelivery(serviceDelivery, serviceAlerts);
}
use of uk.org.siri.siri.VehicleActivityStructure 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;
}
Aggregations