use of org.onebusaway.realtime.api.VehicleLocationRecord in project onebusaway-application-modules by camsys.
the class GtfsRealtimeServiceImpl method getVehiclePositions.
@Override
public FeedMessage getVehiclePositions() {
FeedMessage.Builder feedMessage = createFeedWithDefaultHeader();
List<VehicleStatus> statuses = _vehicleStatusService.getAllVehicleStatuses();
for (VehicleStatus status : statuses) {
VehicleLocationRecord record = status.getRecord();
VehiclePosition.Builder vehiclePosition = VehiclePosition.newBuilder();
if (record.isCurrentLocationSet()) {
Position.Builder position = Position.newBuilder();
position.setLatitude((float) record.getCurrentLocationLat());
position.setLongitude((float) record.getCurrentLocationLon());
vehiclePosition.setPosition(position);
}
VehicleDescriptor.Builder vehicleDescriptor = VehicleDescriptor.newBuilder();
vehicleDescriptor.setId(AgencyAndId.convertToString(record.getVehicleId()));
vehiclePosition.setVehicle(vehicleDescriptor);
if (record.getTimeOfLocationUpdate() != 0)
vehiclePosition.setTimestamp(record.getTimeOfLocationUpdate() / 1000);
else
vehiclePosition.setTimestamp(record.getTimeOfRecord() / 1000);
/**
* TODO: Block? Trip?
*/
FeedEntity.Builder feedEntity = FeedEntity.newBuilder();
feedEntity.setVehicle(vehiclePosition);
feedEntity.setId(vehicleDescriptor.getId());
feedMessage.addEntity(feedEntity);
}
return feedMessage.build();
}
use of org.onebusaway.realtime.api.VehicleLocationRecord in project onebusaway-application-modules by camsys.
the class GtfsRealtimeSource method handleCombinedUpdates.
private void handleCombinedUpdates(MonitoredResult result, List<CombinedTripUpdatesAndVehiclePosition> updates) {
Set<AgencyAndId> seenVehicles = new HashSet<AgencyAndId>();
for (CombinedTripUpdatesAndVehiclePosition update : updates) {
VehicleLocationRecord record = _tripsLibrary.createVehicleLocationRecordForUpdate(result, update);
if (record != null) {
if (record.getTripId() != null) {
// tripId will be null if block was matched
result.addUnmatchedTripId(record.getTripId().toString());
}
AgencyAndId vehicleId = record.getVehicleId();
// the TDS will discard these
if (blockNotActive(record)) {
_log.debug("discarding v: " + vehicleId + " as block not active");
continue;
}
seenVehicles.add(vehicleId);
Date timestamp = new Date(record.getTimeOfRecord());
Date prev = _lastVehicleUpdate.get(vehicleId);
if (prev == null || prev.before(timestamp)) {
_log.debug("matched vehicle " + vehicleId + " on block=" + record.getBlockId() + " with scheduleDeviation=" + record.getScheduleDeviation());
_vehicleLocationListener.handleVehicleLocationRecord(record);
_lastVehicleUpdate.put(vehicleId, timestamp);
}
}
}
Calendar c = Calendar.getInstance();
c.add(Calendar.MINUTE, -15);
Date staleRecordThreshold = c.getTime();
long newestUpdate = 0;
Iterator<Map.Entry<AgencyAndId, Date>> it = _lastVehicleUpdate.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<AgencyAndId, Date> entry = it.next();
AgencyAndId vehicleId = entry.getKey();
Date lastUpdateTime = entry.getValue();
if (lastUpdateTime != null && lastUpdateTime.getTime() > newestUpdate) {
newestUpdate = lastUpdateTime.getTime();
}
if (!seenVehicles.contains(vehicleId) && lastUpdateTime.before(staleRecordThreshold)) {
_log.debug("removing stale vehicleId=" + vehicleId);
it.remove();
}
}
// NOTE: this implies receiving stale updates is equivalent to not being updated at all
result.setLastUpdate(newestUpdate);
_log.info("Agency " + this.getAgencyIds().get(0) + " has active vehicles=" + seenVehicles.size() + " for updates=" + updates.size() + " with most recent timestamp " + new Date(newestUpdate));
}
use of org.onebusaway.realtime.api.VehicleLocationRecord in project onebusaway-application-modules by camsys.
the class GtfsRealtimeTripLibrary method createVehicleLocationRecordForUpdate.
public VehicleLocationRecord createVehicleLocationRecordForUpdate(MonitoredResult result, CombinedTripUpdatesAndVehiclePosition update) {
VehicleLocationRecord record = new VehicleLocationRecord();
// this is just the default -- if we have tripUpdates this will be re-written
record.setTimeOfRecord(currentTime());
BlockDescriptor blockDescriptor = update.block;
if (update.block == null)
return null;
String vehicleId = update.block.getVehicleId();
record.setBlockId(blockDescriptor.getBlockInstance().getBlock().getBlock().getId());
record.setStatus(blockDescriptor.getScheduleRelationship().toString());
applyTripUpdatesToRecord(result, blockDescriptor, update.tripUpdates, record, vehicleId, update.bestTrip);
if (update.vehiclePosition != null) {
applyVehiclePositionToRecord(result, blockDescriptor, update.vehiclePosition, record);
}
/**
* By default, we use the block id as the vehicle id
*/
record.setVehicleId(record.getBlockId());
if (result != null) {
if (record.getTripId() != null) {
result.addMatchedTripId(record.getTripId().toString());
} else if (record.getBlockId() != null) {
// here we take a matched block as if it were a trip
result.addMatchedTripId(record.getBlockId().toString());
} else {
// we don't have a tripId, use the BlockId instead
result.addMatchedTripId(record.getBlockId().toString());
}
}
if (blockDescriptor.getVehicleId() != null) {
String agencyId = record.getBlockId().getAgencyId();
record.setVehicleId(new AgencyAndId(agencyId, blockDescriptor.getVehicleId()));
}
return record;
}
use of org.onebusaway.realtime.api.VehicleLocationRecord in project onebusaway-application-modules by camsys.
the class BlockLocationServiceImpl method getScheduledBlockLocationForVehicleLocationCacheRecord.
private ScheduledBlockLocation getScheduledBlockLocationForVehicleLocationCacheRecord(BlockInstance blockInstance, VehicleLocationCacheElement cacheElement, long targetTime) {
VehicleLocationRecord record = cacheElement.getRecord();
ScheduledBlockLocation scheduledBlockLocation = cacheElement.getScheduledBlockLocation();
BlockConfigurationEntry blockConfig = blockInstance.getBlock();
long serviceDate = blockInstance.getServiceDate();
int scheduledTime = (int) ((targetTime - serviceDate) / 1000);
/**
* If location interpolation has been turned off, then we assume the vehicle
* is at its last known location, so we return that if it's been stored with
* the cache element.
*/
if (!_locationInterpolation && scheduledBlockLocation != null) {
return scheduledBlockLocation;
}
if (record.isScheduleDeviationSet()) {
/**
* Effective scheduled time is the point that a transit vehicle is at on
* its schedule, with schedule deviation taken into account. So if it's
* 100 minutes into the current service date and the bus is running 10
* minutes late, it's actually at the 90 minute point in its scheduled
* operation.
*/
int effectiveScheduledTime = (int) (scheduledTime - record.getScheduleDeviation());
if (scheduledBlockLocation != null && scheduledBlockLocation.getScheduledTime() <= effectiveScheduledTime) {
return _scheduledBlockLocationService.getScheduledBlockLocationFromScheduledTime(scheduledBlockLocation, effectiveScheduledTime);
}
return _scheduledBlockLocationService.getScheduledBlockLocationFromScheduledTime(blockConfig, effectiveScheduledTime);
}
if (record.isDistanceAlongBlockSet()) {
if ((_locationInterpolation || _distanceAlongBlockLocationInterpolation) && scheduledBlockLocation != null && scheduledBlockLocation.getDistanceAlongBlock() <= record.getDistanceAlongBlock()) {
int ellapsedTime = (int) ((targetTime - record.getTimeOfRecord()) / 1000);
if (ellapsedTime >= 0) {
int effectiveScheduledTime = scheduledBlockLocation.getScheduledTime() + ellapsedTime;
return _scheduledBlockLocationService.getScheduledBlockLocationFromScheduledTime(blockConfig, effectiveScheduledTime);
}
return _scheduledBlockLocationService.getScheduledBlockLocationFromDistanceAlongBlock(scheduledBlockLocation, record.getDistanceAlongBlock());
}
return _scheduledBlockLocationService.getScheduledBlockLocationFromDistanceAlongBlock(blockConfig, record.getDistanceAlongBlock());
}
return _scheduledBlockLocationService.getScheduledBlockLocationFromScheduledTime(blockConfig, scheduledTime);
}
use of org.onebusaway.realtime.api.VehicleLocationRecord in project onebusaway-application-modules by camsys.
the class VehicleStatusServiceImpl method getAllVehicleStatuses.
@Override
public List<VehicleStatus> getAllVehicleStatuses() {
ArrayList<VehicleStatus> statuses = new ArrayList<VehicleStatus>();
for (VehicleLocationRecord record : _vehicleRecordsById.values()) {
VehicleStatus status = new VehicleStatus();
status.setRecord(record);
statuses.add(status);
}
return statuses;
}
Aggregations