Search in sources :

Example 1 with EntityType

use of org.onebusaway.gtfs_realtime.archiver.service.GtfsRealtimeRetriever.EntityType in project onebusaway-application-modules by camsys.

the class GtfsRealtimePlaybackController method tripUpdates.

@RequestMapping(value = "/gtfs-realtime/{path:trip-updates|vehicle-positions}")
public void tripUpdates(ServletRequest request, HttpServletResponse response, @RequestParam(value = "key", required = true) String key, @RequestParam(value = "timestamp", required = false) Long timestampInSeconds, @RequestParam(value = "time", required = false) String simpleDate, @RequestParam(value = "interval", required = false, defaultValue = "30") long interval, @PathVariable String path) throws IOException {
    Status status = isAllowed(key);
    if (Status.RATE_EXCEEDED == status) {
        response.sendError(TOO_MANY_REQUESTS);
        return;
    }
    if (Status.AUTHORIZED != status) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    if (simpleDate != null) {
        Date parsed;
        try {
            parsed = DATE_FORMAT.parse(simpleDate);
            timestampInSeconds = parsed.getTime() / 1000;
        } catch (ParseException e) {
        // bury
        }
    }
    if (timestampInSeconds == null) {
        response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED, "time or timestamp parameters required");
        return;
    }
    EntityType type = path.equals("trip-updates") ? EntityType.TRIP : EntityType.VEHICLE;
    // will not create new session if time is the same
    Date requestedDate = new Date(timestampInSeconds * 1000);
    _timeService.setCurrentTime(key, requestedDate);
    Date endDate = _timeService.getCurrentTime(key);
    Date startDate = new Date((endDate.getTime() - (interval * 1000)));
    FeedMessage tripUpdates = _gtfsRealtimeRetriever.getFeedMessage(type, startDate, endDate);
    render(request, response, tripUpdates);
}
Also used : Status(org.onebusaway.users.services.ApiKeyPermissionService.Status) EntityType(org.onebusaway.gtfs_realtime.archiver.service.GtfsRealtimeRetriever.EntityType) FeedMessage(com.google.transit.realtime.GtfsRealtime.FeedMessage) ParseException(java.text.ParseException) Date(java.util.Date) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

FeedMessage (com.google.transit.realtime.GtfsRealtime.FeedMessage)1 ParseException (java.text.ParseException)1 Date (java.util.Date)1 EntityType (org.onebusaway.gtfs_realtime.archiver.service.GtfsRealtimeRetriever.EntityType)1 Status (org.onebusaway.users.services.ApiKeyPermissionService.Status)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1