Search in sources :

Example 11 with FeedEntity

use of com.google.transit.realtime.GtfsRealtime.FeedEntity in project onebusaway-application-modules by camsys.

the class FeedEntityConvertor method readAll.

public List<T> readAll(FeedMessage feedMessage) {
    List<T> updates = new ArrayList<T>();
    if (feedMessage == null) {
        return updates;
    }
    List<FeedEntity> entityList = feedMessage.getEntityList();
    long timestamp = feedMessage.getHeader().getTimestamp() * 1000;
    for (FeedEntity entity : entityList) {
        T model = readFeedEntity(entity, timestamp);
        if (model != null) {
            updates.add(model);
        }
    }
    return updates;
}
Also used : ArrayList(java.util.ArrayList) FeedEntity(com.google.transit.realtime.GtfsRealtime.FeedEntity)

Example 12 with FeedEntity

use of com.google.transit.realtime.GtfsRealtime.FeedEntity in project onebusaway-application-modules by camsys.

the class GtfsRealtimeSource method handleAlerts.

private void handleAlerts(FeedMessage alerts) {
    for (FeedEntity entity : alerts.getEntityList()) {
        Alert alert = entity.getAlert();
        if (alert == null) {
            _log.warn("epxected a FeedEntity with an Alert");
            continue;
        }
        AgencyAndId id = createId(entity.getId());
        if (entity.getIsDeleted()) {
            _alertsById.remove(id);
            _serviceAlertService.removeServiceAlert(id);
        } else {
            ServiceAlert.Builder serviceAlertBuilder = _alertLibrary.getAlertAsServiceAlert(id, alert, _alertAgencyIdMap);
            ServiceAlert serviceAlert = serviceAlertBuilder.build();
            ServiceAlert existingAlert = _alertsById.get(id);
            if (existingAlert == null || !existingAlert.equals(serviceAlert)) {
                _alertsById.put(id, serviceAlert);
                ServiceAlertRecord serviceAlertRecord = new ServiceAlertRecord();
                serviceAlertRecord.setAgencyId(_agencyIds.get(0));
                serviceAlertRecord.setActiveWindows(new HashSet<ServiceAlertTimeRange>());
                if (serviceAlert.getActiveWindowList() != null) {
                    for (ServiceAlerts.TimeRange timeRange : serviceAlert.getActiveWindowList()) {
                        ServiceAlertTimeRange serviceAlertTimeRange = new ServiceAlertTimeRange();
                        serviceAlertTimeRange.setFromValue(timeRange.getStart());
                        serviceAlertTimeRange.setToValue(timeRange.getEnd());
                        serviceAlertRecord.getActiveWindows().add(serviceAlertTimeRange);
                    }
                }
                serviceAlertRecord.setAllAffects(new HashSet<ServiceAlertsSituationAffectsClause>());
                if (serviceAlert.getAffectsList() != null) {
                    for (ServiceAlerts.Affects affects : serviceAlertBuilder.getAffectsList()) {
                        ServiceAlertsSituationAffectsClause serviceAlertsSituationAffectsClause = new ServiceAlertsSituationAffectsClause();
                        serviceAlertsSituationAffectsClause.setAgencyId(affects.getAgencyId());
                        serviceAlertsSituationAffectsClause.setApplicationId(affects.getApplicationId());
                        serviceAlertsSituationAffectsClause.setDirectionId(affects.getDirectionId());
                        serviceAlertsSituationAffectsClause.setRouteId(affects.getRouteId().getId());
                        serviceAlertsSituationAffectsClause.setStopId(affects.getStopId().getId());
                        serviceAlertsSituationAffectsClause.setTripId(affects.getTripId().getId());
                        serviceAlertRecord.getAllAffects().add(serviceAlertsSituationAffectsClause);
                    }
                }
                serviceAlertRecord.setCause(getECause(serviceAlert.getCause()));
                serviceAlertRecord.setConsequences(new HashSet<ServiceAlertSituationConsequenceClause>());
                if (serviceAlert.getConsequenceList() != null) {
                    for (ServiceAlerts.Consequence consequence : serviceAlert.getConsequenceList()) {
                        ServiceAlertSituationConsequenceClause serviceAlertSituationConsequenceClause = new ServiceAlertSituationConsequenceClause();
                        serviceAlertSituationConsequenceClause.setDetourPath(consequence.getDetourPath());
                        serviceAlertSituationConsequenceClause.setDetourStopIds(new HashSet<String>());
                        if (consequence.getDetourStopIdsList() != null) {
                            for (ServiceAlerts.Id stopId : consequence.getDetourStopIdsList()) {
                                serviceAlertSituationConsequenceClause.getDetourStopIds().add(stopId.getId());
                            }
                        }
                        serviceAlertRecord.getConsequences().add(serviceAlertSituationConsequenceClause);
                    }
                }
                serviceAlertRecord.setCreationTime(serviceAlert.getCreationTime());
                serviceAlertRecord.setDescriptions(new HashSet<ServiceAlertLocalizedString>());
                if (serviceAlert.getDescription() != null) {
                    for (ServiceAlerts.TranslatedString.Translation translation : serviceAlert.getDescription().getTranslationList()) {
                        ServiceAlertLocalizedString string = new ServiceAlertLocalizedString();
                        string.setValue(translation.getText());
                        string.setLanguage(translation.getLanguage());
                        serviceAlertRecord.getDescriptions().add(string);
                    }
                }
                serviceAlertRecord.setModifiedTime(serviceAlert.getModifiedTime());
                serviceAlertRecord.setPublicationWindows(new HashSet<ServiceAlertTimeRange>());
                serviceAlertRecord.setServiceAlertId(serviceAlert.getId().getId());
                serviceAlertRecord.setSeverity(getESeverity(serviceAlert.getSeverity()));
                serviceAlertRecord.setSummaries(new HashSet<ServiceAlertLocalizedString>());
                if (serviceAlert.getSummary() != null) {
                    for (ServiceAlerts.TranslatedString.Translation translation : serviceAlert.getSummary().getTranslationList()) {
                        ServiceAlertLocalizedString string = new ServiceAlertLocalizedString();
                        string.setValue(translation.getText());
                        string.setLanguage(translation.getLanguage());
                        serviceAlertRecord.getSummaries().add(string);
                    }
                }
                serviceAlertRecord.setUrls(new HashSet<ServiceAlertLocalizedString>());
                if (serviceAlert.getUrl() != null) {
                    for (ServiceAlerts.TranslatedString.Translation translation : serviceAlert.getUrl().getTranslationList()) {
                        ServiceAlertLocalizedString string = new ServiceAlertLocalizedString();
                        string.setValue(translation.getText());
                        string.setLanguage(translation.getLanguage());
                        serviceAlertRecord.getUrls().add(string);
                    }
                }
                _serviceAlertService.createOrUpdateServiceAlert(serviceAlertRecord);
            }
        }
    }
}
Also used : ServiceAlertsSituationAffectsClause(org.onebusaway.transit_data_federation.impl.service_alerts.ServiceAlertsSituationAffectsClause) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ServiceAlerts(org.onebusaway.transit_data_federation.services.service_alerts.ServiceAlerts) ServiceAlertLocalizedString(org.onebusaway.transit_data_federation.impl.service_alerts.ServiceAlertLocalizedString) ServiceAlertRecord(org.onebusaway.transit_data_federation.impl.service_alerts.ServiceAlertRecord) ServiceAlertTimeRange(org.onebusaway.transit_data_federation.impl.service_alerts.ServiceAlertTimeRange) ServiceAlertLocalizedString(org.onebusaway.transit_data_federation.impl.service_alerts.ServiceAlertLocalizedString) ServiceAlert(org.onebusaway.transit_data_federation.services.service_alerts.ServiceAlerts.ServiceAlert) ServiceAlert(org.onebusaway.transit_data_federation.services.service_alerts.ServiceAlerts.ServiceAlert) Alert(com.google.transit.realtime.GtfsRealtime.Alert) FeedEntity(com.google.transit.realtime.GtfsRealtime.FeedEntity) ServiceAlertSituationConsequenceClause(org.onebusaway.transit_data_federation.impl.service_alerts.ServiceAlertSituationConsequenceClause)

Example 13 with FeedEntity

use of com.google.transit.realtime.GtfsRealtime.FeedEntity in project onebusaway-application-modules by camsys.

the class GtfsRealtimeTripLibrary method groupTripUpdatesAndVehiclePositions.

public List<CombinedTripUpdatesAndVehiclePosition> groupTripUpdatesAndVehiclePositions(MonitoredResult result, FeedMessage tripUpdateMessage, FeedMessage vehiclePositionsMessage) {
    List<CombinedTripUpdatesAndVehiclePosition> updates = new ArrayList<CombinedTripUpdatesAndVehiclePosition>();
    Map<String, TripUpdate> bestTripByVehicleId = new HashMap<String, TripUpdate>();
    ListMultimap<String, TripUpdate> tripUpdatesByVehicleId = ArrayListMultimap.create();
    Map<String, VehiclePosition> vehiclePositionsByVehicleId = new HashMap<String, VehiclePosition>();
    ListMultimap<BlockDescriptor, TripUpdate> anonymousTripUpdatesByBlock = ArrayListMultimap.<BlockDescriptor, TripUpdate>create();
    Map<BlockDescriptor, VehiclePosition> anonymousVehiclePositionsByBlock = new HashMap<BlockDescriptor, VehiclePosition>();
    Set<BlockDescriptor> badAnonymousVehiclePositions = new HashSet<BlockDescriptor>();
    for (FeedEntity fe : tripUpdateMessage.getEntityList()) {
        if (!fe.hasTripUpdate()) {
            continue;
        }
        TripUpdate tu = fe.getTripUpdate();
        if (tu.hasVehicle() && tu.getVehicle().hasId() && StringUtils.isNotBlank(tu.getVehicle().getId())) {
            // Trip update has a vehicle ID - index by vehicle ID
            String vehicleId = tu.getVehicle().getId();
            tripUpdatesByVehicleId.put(vehicleId, tu);
            if (!bestTripByVehicleId.containsKey(vehicleId)) {
                bestTripByVehicleId.put(vehicleId, tu);
            } else {
                // upcoming merge will fix this
                _log.debug("Multiple TripUpdates for vehicle {}; taking best.", vehicleId);
                if (tripMoreAppropriate(tu, bestTripByVehicleId.get(vehicleId), vehicleId)) {
                    bestTripByVehicleId.put(vehicleId, tu);
                }
            }
        } else {
            /*
         * Trip update does not have a vehicle ID - index by TripDescriptor
         * (includes start date and time).
         */
            TripDescriptor td = tu.getTrip();
            long time = tu.hasTimestamp() ? tu.getTimestamp() * 1000 : currentTime();
            BlockDescriptor bd = getTripDescriptorAsBlockDescriptor(result, td, time);
            if (bd == null) {
                continue;
            }
            if (!anonymousTripUpdatesByBlock.containsKey(bd)) {
                anonymousTripUpdatesByBlock.put(bd, tu);
            } else {
                _log.warn("Multiple anonymous TripUpdates for trip {}; will not map to VehiclePosition.", td.getTripId());
                anonymousTripUpdatesByBlock.put(bd, tu);
            }
        }
    }
    for (FeedEntity fe : vehiclePositionsMessage.getEntityList()) {
        if (!fe.hasVehicle()) {
            continue;
        }
        VehiclePosition vp = fe.getVehicle();
        if (vp.hasVehicle() && vp.getVehicle().hasId()) {
            // Vehicle position has a vehicle ID - index by vehicle ID
            String vehicleId = vp.getVehicle().getId();
            if (!vehiclePositionsByVehicleId.containsKey(vehicleId)) {
                vehiclePositionsByVehicleId.put(vehicleId, vp);
            } else {
                _log.warn("Multiple updates for vehicle {}; taking newest.", vehicleId);
                VehiclePosition otherUpdate = vehiclePositionsByVehicleId.get(vehicleId);
                long otherTimestamp = otherUpdate.getTimestamp();
                if (vp.getTimestamp() > otherTimestamp) {
                    vehiclePositionsByVehicleId.put(vehicleId, vp);
                }
            }
        } else if (vp.hasTrip()) {
            /*
         * Vehicle position does not have vehicle ID but has TripDescriptor, so
         * use that, but only if there is only one.
         */
            TripDescriptor td = vp.getTrip();
            long time = vp.hasTimestamp() ? vp.getTimestamp() * 1000 : currentTime();
            BlockDescriptor bd = getTripDescriptorAsBlockDescriptor(result, td, time);
            if (bd == null) {
                continue;
            }
            if (!anonymousVehiclePositionsByBlock.containsKey(bd)) {
                anonymousVehiclePositionsByBlock.put(bd, vp);
            } else {
                /*
           * When we have multiple VehiclePositions for a block but no way to
           * uniquely distinguish them there is nothing useful or reasonable we
           * can do with the data.
           */
                _log.warn("Multiple anonymous VehiclePositions for trip {}; giving up.", td.getTripId());
                badAnonymousVehiclePositions.add(bd);
            }
        } else {
            /*
         * Pathological VehiclePosition contains no identifying information;
         * skip.
         */
            continue;
        }
    }
    // a block
    for (BlockDescriptor bd : badAnonymousVehiclePositions) {
        anonymousVehiclePositionsByBlock.remove(bd);
    }
    // Map updates by vehicle ID
    for (Map.Entry<String, Collection<TripUpdate>> e : tripUpdatesByVehicleId.asMap().entrySet()) {
        CombinedTripUpdatesAndVehiclePosition update = new CombinedTripUpdatesAndVehiclePosition();
        String vehicleId = e.getKey();
        Collection<TripUpdate> tripUpdates = e.getValue();
        TripUpdate tu = bestTripByVehicleId.get(vehicleId);
        long time = tu.hasTimestamp() ? tu.getTimestamp() * 1000 : currentTime();
        update.block = getTripDescriptorAsBlockDescriptor(result, tu.getTrip(), time);
        update.tripUpdates = new ArrayList<TripUpdate>(tripUpdates);
        update.bestTrip = tu.getTrip().getTripId();
        if (vehiclePositionsByVehicleId.containsKey(vehicleId)) {
            update.vehiclePosition = vehiclePositionsByVehicleId.get(vehicleId);
        }
        updates.add(update);
    }
    // Map anonymous updates by block descriptor
    for (Entry<BlockDescriptor, Collection<TripUpdate>> e : anonymousTripUpdatesByBlock.asMap().entrySet()) {
        CombinedTripUpdatesAndVehiclePosition update = new CombinedTripUpdatesAndVehiclePosition();
        BlockDescriptor bd = e.getKey();
        update.block = bd;
        update.tripUpdates = new ArrayList<TripUpdate>(e.getValue());
        if (update.tripUpdates.size() == 1 && anonymousVehiclePositionsByBlock.containsKey(bd)) {
            update.vehiclePosition = anonymousVehiclePositionsByBlock.get(bd);
        }
        updates.add(update);
    }
    // Set vehicle ID in block if possible
    for (CombinedTripUpdatesAndVehiclePosition update : updates) {
        String vehicleId = null;
        for (TripUpdate tu : update.tripUpdates) {
            if (tu.hasVehicle() && tu.getVehicle().hasId()) {
                vehicleId = tu.getVehicle().getId();
                break;
            }
        }
        if (vehicleId == null && update.vehiclePosition != null && update.vehiclePosition.hasVehicle() && update.vehiclePosition.getVehicle().hasId()) {
            vehicleId = update.vehiclePosition.getVehicle().getId();
        }
        if (vehicleId != null && update.block != null) {
            update.block.setVehicleId(vehicleId);
        }
    }
    return updates;
}
Also used : TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) OneBusAwayTripUpdate(com.google.transit.realtime.GtfsRealtimeOneBusAway.OneBusAwayTripUpdate) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VehiclePosition(com.google.transit.realtime.GtfsRealtime.VehiclePosition) TripDescriptor(com.google.transit.realtime.GtfsRealtime.TripDescriptor) Collection(java.util.Collection) FeedEntity(com.google.transit.realtime.GtfsRealtime.FeedEntity) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Aggregations

FeedEntity (com.google.transit.realtime.GtfsRealtime.FeedEntity)13 FeedMessage (com.google.transit.realtime.GtfsRealtime.FeedMessage)8 TripUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate)6 ArrayList (java.util.ArrayList)6 Test (org.junit.Test)5 Alert (com.google.transit.realtime.GtfsRealtime.Alert)3 TranslatedString (com.google.transit.realtime.GtfsRealtime.TranslatedString)3 InputStream (java.io.InputStream)3 Date (java.util.Date)3 ResponseBean (org.onebusaway.api.model.ResponseBean)3 ListBean (org.onebusaway.transit_data.model.ListBean)3 EntitySelector (com.google.transit.realtime.GtfsRealtime.EntitySelector)2 TimeRange (com.google.transit.realtime.GtfsRealtime.TimeRange)2 Translation (com.google.transit.realtime.GtfsRealtime.TranslatedString.Translation)2 VehiclePosition (com.google.transit.realtime.GtfsRealtime.VehiclePosition)2 FileInputStream (java.io.FileInputStream)2 RouteBean (org.onebusaway.transit_data.model.RouteBean)2 VehicleStatusBean (org.onebusaway.transit_data.model.VehicleStatusBean)2 TripBean (org.onebusaway.transit_data.model.trips.TripBean)2 TripStatusBean (org.onebusaway.transit_data.model.trips.TripStatusBean)2