use of uk.org.siri.siri.MonitoredVehicleJourneyStructure in project onebusaway-application-modules by camsys.
the class StopMonitoringAction method index.
public DefaultHttpHeaders index() throws IOException {
processGoogleAnalytics();
long responseTimestamp = getTime();
_realtimeService.setTime(responseTimestamp);
String directionId = _request.getParameter("DirectionRef");
// 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>();
// Try to get the agency id passed by the user
String agencyId = _request.getParameter("OperatorRef");
if (agencyId != null) {
// The user provided an agency id so, use it
agencyIds.add(agencyId);
} else {
// They did not provide an agency id, so interpret that an any/all agencies.
Map<String, List<CoordinateBounds>> agencies = _transitDataService.getAgencyIdsWithCoverageArea();
agencyIds.addAll(agencies.keySet());
}
List<AgencyAndId> stopIds = new ArrayList<AgencyAndId>();
String stopIdsErrorString = "";
if (_request.getParameter("MonitoringRef") != null) {
try {
// If the user included an agency id as part of the stop id, ignore any OperatorRef arg
// or lack of OperatorRef arg and just use the included one.
AgencyAndId stopId = AgencyAndIdLibrary.convertFromString(_request.getParameter("MonitoringRef"));
if (isValidStop(stopId)) {
stopIds.add(stopId);
} else {
stopIdsErrorString += "No such stop: " + stopId.toString() + ".";
}
} catch (Exception e) {
// The user didn't provide an agency id in the MonitoringRef, so use our list of operator refs
for (String agency : agencyIds) {
AgencyAndId stopId = new AgencyAndId(agency, _request.getParameter("MonitoringRef"));
if (isValidStop(stopId)) {
stopIds.add(stopId);
} else {
stopIdsErrorString += "No such stop: " + stopId.toString() + ". ";
}
}
stopIdsErrorString = stopIdsErrorString.trim();
}
if (stopIds.size() > 0)
stopIdsErrorString = "";
} else {
stopIdsErrorString = "You must provide a MonitoringRef.";
}
List<AgencyAndId> routeIds = new ArrayList<AgencyAndId>();
String routeIdsErrorString = "";
if (_request.getParameter("LineRef") != null) {
try {
// Same as above for stop id
AgencyAndId routeId = AgencyAndIdLibrary.convertFromString(_request.getParameter("LineRef"));
if (isValidRoute(routeId)) {
routeIds.add(routeId);
} else {
routeIdsErrorString += "No such route: " + routeId.toString() + ".";
}
} catch (Exception e) {
// Same as above for stop id
for (String agency : agencyIds) {
AgencyAndId routeId = new AgencyAndId(agency, _request.getParameter("LineRef"));
if (isValidRoute(routeId)) {
routeIds.add(routeId);
} else {
routeIdsErrorString += "No such route: " + routeId.toString() + ". ";
}
}
routeIdsErrorString = routeIdsErrorString.trim();
}
if (routeIds.size() > 0)
routeIdsErrorString = "";
}
String detailLevel = _request.getParameter("StopMonitoringDetailLevel");
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;
}
}
int maximumStopVisits = Integer.MAX_VALUE;
try {
maximumStopVisits = Integer.parseInt(_request.getParameter("MaximumStopVisits"));
} catch (NumberFormatException e) {
maximumStopVisits = Integer.MAX_VALUE;
}
Integer minimumStopVisitsPerLine = null;
try {
minimumStopVisitsPerLine = Integer.parseInt(_request.getParameter("MinimumStopVisitsPerLine"));
} catch (NumberFormatException e) {
minimumStopVisitsPerLine = null;
}
// Monitored Stop Visits
List<MonitoredStopVisitStructure> visits = new ArrayList<MonitoredStopVisitStructure>();
Map<String, MonitoredStopVisitStructure> visitsMap = new HashMap<String, MonitoredStopVisitStructure>();
for (AgencyAndId stopId : stopIds) {
if (!stopId.hasValues())
continue;
// Stop ids can only be valid here because we only added valid ones to stopIds.
List<MonitoredStopVisitStructure> visitsForStop = _realtimeService.getMonitoredStopVisitsForStop(stopId.toString(), maximumOnwardCalls, responseTimestamp);
if (visitsForStop != null)
visits.addAll(visitsForStop);
}
List<MonitoredStopVisitStructure> filteredVisits = new ArrayList<MonitoredStopVisitStructure>();
Map<AgencyAndId, Integer> visitCountByLine = new HashMap<AgencyAndId, Integer>();
int visitCount = 0;
for (MonitoredStopVisitStructure visit : visits) {
MonitoredVehicleJourneyStructure journey = visit.getMonitoredVehicleJourney();
AgencyAndId thisRouteId = AgencyAndIdLibrary.convertFromString(journey.getLineRef().getValue());
String thisDirectionId = journey.getDirectionRef().getValue();
// user filtering
if (routeIds.size() > 0 && !routeIds.contains(thisRouteId))
continue;
if (thisDirectionId != null) {
if (directionId != null && !thisDirectionId.equals(directionId))
continue;
}
// visit count filters
Integer visitCountForThisLine = visitCountByLine.get(thisRouteId);
if (visitCountForThisLine == null) {
visitCountForThisLine = 0;
}
if (visitCount >= maximumStopVisits) {
if (minimumStopVisitsPerLine == null) {
break;
} else {
if (visitCountForThisLine >= minimumStopVisitsPerLine) {
continue;
}
}
}
// unique stops filters
if (visit.getMonitoredVehicleJourney() == null || visit.getMonitoredVehicleJourney().getVehicleRef() == null || StringUtils.isBlank(visit.getMonitoredVehicleJourney().getVehicleRef().getValue())) {
continue;
} else {
String visitKey = visit.getMonitoredVehicleJourney().getVehicleRef().getValue();
if (visitsMap.containsKey(visit.getMonitoredVehicleJourney().getVehicleRef().getValue())) {
if (visit.getMonitoredVehicleJourney().getProgressStatus() == null) {
visitsMap.remove(visitKey);
visitsMap.put(visitKey, visit);
}
continue;
} else {
visitsMap.put(visit.getMonitoredVehicleJourney().getVehicleRef().getValue(), visit);
}
}
filteredVisits.add(visit);
visitCount++;
visitCountForThisLine++;
visitCountByLine.put(thisRouteId, visitCountForThisLine);
}
visits = filteredVisits;
Exception error = null;
if (stopIds.size() == 0 || (_request.getParameter("LineRef") != null && routeIds.size() == 0)) {
String errorString = (stopIdsErrorString + " " + routeIdsErrorString).trim();
error = new Exception(errorString);
}
_response = generateSiriResponse(visits, stopIds, error, responseTimestamp);
try {
this._servletResponse.getWriter().write(getStopMonitoring());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
use of uk.org.siri.siri.MonitoredVehicleJourneyStructure 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.MonitoredVehicleJourneyStructure in project onebusaway-application-modules by camsys.
the class SearchResultFactoryImplTest method runGetStopResult.
// Support methods
private StopResult runGetStopResult(List<ServiceAlertBean> serviceAlerts) {
StopsForRouteBean stopsForRouteBean = mock(StopsForRouteBean.class);
List<StopGroupingBean> stopGroupingBeans = new ArrayList<StopGroupingBean>();
when(stopsForRouteBean.getStopGroupings()).thenReturn(stopGroupingBeans);
StopGroupingBean stopGroupingBean = mock(StopGroupingBean.class);
stopGroupingBeans.add(stopGroupingBean);
List<StopGroupBean> stopGroups = new ArrayList<StopGroupBean>();
StopGroupBean stopGroupBean = mock(StopGroupBean.class);
stopGroups.add(stopGroupBean);
when(stopGroupingBean.getStopGroups()).thenReturn(stopGroups);
List<String> stopIds = new ArrayList<String>();
when(stopGroupBean.getStopIds()).thenReturn(stopIds);
NameBean nameBean = mock(NameBean.class);
when(nameBean.getType()).thenReturn("destination");
when(stopGroupBean.getName()).thenReturn(nameBean);
List<String> stopGroupBeanStopIds = new ArrayList<String>();
stopGroupBeanStopIds.add(TEST_STOP_ID);
when(stopGroupBean.getStopIds()).thenReturn(stopGroupBeanStopIds);
when(stopGroupBean.getId()).thenReturn(TEST_STOP_ID);
stopIds.add(TEST_STOP_ID);
List<RouteBean> routeBeans = new ArrayList<RouteBean>();
routeBeans.add(createRouteBean());
StopBean stopBean = mock(StopBean.class);
when(stopBean.getId()).thenReturn(TEST_STOP_ID);
when(stopBean.getRoutes()).thenReturn(routeBeans);
List<MonitoredStopVisitStructure> monitoredStopVisits = new ArrayList<MonitoredStopVisitStructure>();
MonitoredStopVisitStructure monitoredStopVisitStructure = mock(MonitoredStopVisitStructure.class);
monitoredStopVisits.add(monitoredStopVisitStructure);
MonitoredVehicleJourneyStructure monVehJourney = mock(MonitoredVehicleJourneyStructure.class);
when(monitoredStopVisitStructure.getMonitoredVehicleJourney()).thenReturn(monVehJourney);
when(monitoredStopVisitStructure.getRecordedAtTime()).thenReturn(new Date(TEST_TIME));
LineRefStructure lineRefStructure = mock(LineRefStructure.class);
when(monVehJourney.getLineRef()).thenReturn(lineRefStructure);
when(lineRefStructure.getValue()).thenReturn(ROUTE_ID);
DirectionRefStructure directionRef = mock(DirectionRefStructure.class);
when(monVehJourney.getDirectionRef()).thenReturn(directionRef);
when(directionRef.getValue()).thenReturn(TEST_STOP_ID);
NaturalLanguageStringStructure natLangStrStructure = mock(NaturalLanguageStringStructure.class);
when(natLangStrStructure.getValue()).thenReturn(TEST_DESTINATION_NAME);
when(monVehJourney.getDestinationName()).thenReturn(natLangStrStructure);
MonitoredCallStructure monCall = mock(MonitoredCallStructure.class);
ExtensionsStructure extensions = mock(ExtensionsStructure.class);
SiriExtensionWrapper siriExtensionWrapper = mock(SiriExtensionWrapper.class);
SiriDistanceExtension distances = mock(SiriDistanceExtension.class);
when(distances.getPresentableDistance()).thenReturn(TEST_PRESENTABLE_DISTANCE);
when(siriExtensionWrapper.getDistances()).thenReturn(distances);
when(extensions.getAny()).thenReturn(siriExtensionWrapper);
when(monCall.getExtensions()).thenReturn(extensions);
when(monVehJourney.getMonitoredCall()).thenReturn(monCall);
when(_realtimeService.getMonitoredStopVisitsForStop(eq(TEST_STOP_ID), eq(0), anyLong())).thenReturn(monitoredStopVisits);
when(_transitDataService.getStopsForRoute(anyString())).thenReturn(stopsForRouteBean);
when(_realtimeService.getServiceAlertsForRouteAndDirection(ROUTE_ID, TEST_STOP_ID)).thenReturn(serviceAlerts);
SearchResultFactoryImpl srf = new SearchResultFactoryImpl(_transitDataService, _realtimeService, _configurationService);
Set<RouteBean> routeFilter = new HashSet<RouteBean>();
StopResult result = (StopResult) srf.getStopResult(stopBean, routeFilter);
return result;
}
use of uk.org.siri.siri.MonitoredVehicleJourneyStructure in project onebusaway-application-modules by camsys.
the class RealtimeServiceImpl method getMonitoredStopVisitsForStop.
@Override
public List<MonitoredStopVisitStructure> getMonitoredStopVisitsForStop(String stopId, int maximumOnwardCalls, long currentTime) {
List<MonitoredStopVisitStructure> output = new ArrayList<MonitoredStopVisitStructure>();
for (ArrivalAndDepartureBean adBean : getArrivalsAndDeparturesForStop(stopId, currentTime)) {
TripStatusBean statusBeanForCurrentTrip = adBean.getTripStatus();
TripBean tripBeanForAd = adBean.getTrip();
final RouteBean routeBean = tripBeanForAd.getRoute();
if (statusBeanForCurrentTrip == null) {
_log.debug("status drop");
continue;
}
if (!_presentationService.include(statusBeanForCurrentTrip) || !_presentationService.include(adBean, statusBeanForCurrentTrip)) {
_log.debug("presentation drop for vehicle=" + statusBeanForCurrentTrip.getVehicleId());
continue;
}
if (!_transitDataService.stopHasRevenueServiceOnRoute((routeBean.getAgency() != null ? routeBean.getAgency().getId() : null), stopId, routeBean.getId(), adBean.getTrip().getDirectionId())) {
_log.debug("non reveunue drop");
continue;
}
// Filter out if the vehicle has realtime information and is ahead of current stop
if (statusBeanForCurrentTrip.isPredicted() && !(adBean.hasPredictedArrivalTime() || adBean.hasPredictedDepartureTime())) {
_log.debug("no realtime drop");
continue;
}
if (statusBeanForCurrentTrip.getVehicleId() != null) {
_log.debug("valid vehicle " + statusBeanForCurrentTrip.getVehicleId());
}
MonitoredStopVisitStructure stopVisit = new MonitoredStopVisitStructure();
// Check for Realtime Data
if (!statusBeanForCurrentTrip.isPredicted()) {
stopVisit.setRecordedAtTime(new Date(getTime()));
} else {
stopVisit.setRecordedAtTime(new Date(statusBeanForCurrentTrip.getLastUpdateTime()));
}
List<TimepointPredictionRecord> timePredictionRecords = null;
timePredictionRecords = createTimePredictionRecordsForStop(adBean, stopId);
stopVisit.setMonitoredVehicleJourney(new MonitoredVehicleJourneyStructure());
SiriSupport.fillMonitoredVehicleJourney(stopVisit.getMonitoredVehicleJourney(), tripBeanForAd, statusBeanForCurrentTrip, adBean.getStop(), OnwardCallsMode.STOP_MONITORING, _presentationService, _transitDataService, maximumOnwardCalls, timePredictionRecords, statusBeanForCurrentTrip.isPredicted(), currentTime, false);
output.add(stopVisit);
}
Collections.sort(output, new Comparator<MonitoredStopVisitStructure>() {
public int compare(MonitoredStopVisitStructure arg0, MonitoredStopVisitStructure arg1) {
try {
Date expectedArrival0 = arg0.getMonitoredVehicleJourney().getMonitoredCall().getExpectedArrivalTime();
Date expectedArrival1 = arg1.getMonitoredVehicleJourney().getMonitoredCall().getExpectedArrivalTime();
return expectedArrival0.compareTo(expectedArrival1);
} catch (Exception e) {
return -1;
}
}
});
return output;
}
Aggregations