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;
}
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);
}
}
}
}
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;
}
Aggregations