Search in sources :

Example 1 with VehicleLocationRecord

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();
}
Also used : VehicleLocationRecord(org.onebusaway.realtime.api.VehicleLocationRecord) VehicleStatus(org.onebusaway.transit_data_federation.services.realtime.VehicleStatus)

Example 2 with VehicleLocationRecord

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));
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Calendar(java.util.Calendar) VehicleLocationRecord(org.onebusaway.realtime.api.VehicleLocationRecord) Map(java.util.Map) HashMap(java.util.HashMap) Date(java.util.Date) HashSet(java.util.HashSet)

Example 3 with VehicleLocationRecord

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;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) VehicleLocationRecord(org.onebusaway.realtime.api.VehicleLocationRecord)

Example 4 with VehicleLocationRecord

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);
}
Also used : ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) VehicleLocationRecord(org.onebusaway.realtime.api.VehicleLocationRecord) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry) CoordinatePoint(org.onebusaway.geospatial.model.CoordinatePoint)

Example 5 with VehicleLocationRecord

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;
}
Also used : ArrayList(java.util.ArrayList) VehicleLocationRecord(org.onebusaway.realtime.api.VehicleLocationRecord) VehicleStatus(org.onebusaway.transit_data_federation.services.realtime.VehicleStatus)

Aggregations

VehicleLocationRecord (org.onebusaway.realtime.api.VehicleLocationRecord)34 BlockConfigurationEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry)17 BlockInstance (org.onebusaway.transit_data_federation.services.blocks.BlockInstance)16 BlockEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.BlockEntryImpl)15 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)11 TripUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate)10 Test (org.junit.Test)10 TripEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl)10 Date (java.util.Date)8 StopTimeUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate)7 ScheduledBlockLocation (org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation)7 BlockLocation (org.onebusaway.transit_data_federation.services.realtime.BlockLocation)6 BlockStopTimeEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockStopTimeEntry)6 ArrayList (java.util.ArrayList)5 CoordinatePoint (org.onebusaway.geospatial.model.CoordinatePoint)5 ScheduledBlockLocationServiceImpl (org.onebusaway.transit_data_federation.impl.blocks.ScheduledBlockLocationServiceImpl)5 VehicleLocationRecordCacheImpl (org.onebusaway.transit_data_federation.impl.realtime.VehicleLocationRecordCacheImpl)5 StopTimeInstance (org.onebusaway.transit_data_federation.model.StopTimeInstance)5 TargetTime (org.onebusaway.transit_data_federation.model.TargetTime)5 ArrivalAndDepartureInstance (org.onebusaway.transit_data_federation.services.realtime.ArrivalAndDepartureInstance)5