Search in sources :

Example 6 with VehicleLocationCacheElements

use of org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements in project onebusaway-application-modules by camsys.

the class BlockLocationServiceImpl method getBlockLocationRecordsAsVehicleLocationRecords.

private List<VehicleLocationCacheElements> getBlockLocationRecordsAsVehicleLocationRecords(BlockInstance blockInstance, List<BlockLocationRecord> records) {
    List<VehicleLocationCacheElements> results = new ArrayList<VehicleLocationCacheElements>();
    for (BlockLocationRecord record : records) {
        VehicleLocationRecord vlr = new VehicleLocationRecord();
        vlr.setBlockId(blockInstance.getBlock().getBlock().getId());
        if (record.isLocationSet()) {
            vlr.setCurrentLocationLat(record.getLocationLat());
            vlr.setCurrentLocationLon(record.getLocationLon());
        }
        if (record.isOrientationSet())
            vlr.setCurrentOrientation(record.getOrientation());
        if (record.isDistanceAlongBlockSet())
            vlr.setDistanceAlongBlock(record.getDistanceAlongBlock());
        vlr.setPhase(record.getPhase());
        if (record.isScheduleDeviationSet())
            vlr.setScheduleDeviation(record.getScheduleDeviation());
        vlr.setServiceDate(record.getServiceDate());
        vlr.setStatus(record.getStatus());
        vlr.setTimeOfRecord(record.getTime());
        vlr.setVehicleId(record.getVehicleId());
        VehicleLocationCacheElement element = new VehicleLocationCacheElement(vlr, null, null);
        VehicleLocationCacheElements elements = new VehicleLocationCacheElements(blockInstance, element);
        results.add(elements);
    }
    return results;
}
Also used : VehicleLocationCacheElements(org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements) VehicleLocationCacheElement(org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElement) ArrayList(java.util.ArrayList) VehicleLocationRecord(org.onebusaway.realtime.api.VehicleLocationRecord)

Example 7 with VehicleLocationCacheElements

use of org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements in project onebusaway-application-modules by camsys.

the class BlockLocationServiceImpl method getBlockLocationRecordCollections.

private List<VehicleLocationCacheElements> getBlockLocationRecordCollections(RecordStrategy strategy, TargetTime time) {
    List<VehicleLocationCacheElements> entries = strategy.getRecordsFromCache();
    if (!entries.isEmpty()) {
        List<VehicleLocationCacheElements> inRange = new ArrayList<VehicleLocationCacheElements>();
        long offset = _predictionCacheMaxOffset * 1000;
        for (VehicleLocationCacheElements elements : entries) {
            if (elements.isEmpty())
                continue;
            Range range = elements.getTimeRange();
            long tFrom = (long) (range.getMin() - offset);
            long tTo = (long) (range.getMax() + offset);
            if (tFrom <= time.getCurrentTime() && time.getCurrentTime() <= tTo)
                inRange.add(elements);
        }
        if (!inRange.isEmpty())
            return inRange;
    }
    long offset = _blockLocationRecordCacheWindowSize * 1000 / 2;
    // We only consult persisted cache entries if the requested target time is
    // not within our current cache window
    boolean outOfRange = time.getTargetTime() + offset < time.getCurrentTime() || time.getCurrentTime() < time.getTargetTime() - offset;
    if (outOfRange && _persistBlockLocationRecords) {
        _blockLocationRecordPersistentStoreAccessCount.incrementAndGet();
        long fromTime = time.getTargetTime() - offset;
        long toTime = time.getTargetTime() + offset;
        List<BlockLocationRecord> predictions = strategy.getRecordsFromDao(fromTime, toTime);
        if (!predictions.isEmpty()) {
            Map<BlockLocationRecordKey, List<BlockLocationRecord>> recordsByKey = groupRecord(predictions);
            List<VehicleLocationCacheElements> allCollections = new ArrayList<VehicleLocationCacheElements>();
            for (Map.Entry<BlockLocationRecordKey, List<BlockLocationRecord>> entry : recordsByKey.entrySet()) {
                BlockLocationRecordKey key = entry.getKey();
                List<BlockLocationRecord> blockLocationRecords = entry.getValue();
                List<VehicleLocationCacheElements> someRecords = getBlockLocationRecordsAsVehicleLocationRecords(key.getBlockInstance(), blockLocationRecords);
                allCollections.addAll(someRecords);
            }
            return allCollections;
        }
    }
    return Collections.emptyList();
}
Also used : ArrayList(java.util.ArrayList) Range(org.onebusaway.collections.Range) VehicleLocationCacheElements(org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) FactoryMap(org.onebusaway.collections.FactoryMap) SortedMap(java.util.SortedMap) TreeMap(java.util.TreeMap)

Example 8 with VehicleLocationCacheElements

use of org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements in project onebusaway-application-modules by camsys.

the class BlockLocationServiceImpl method putBlockLocationRecord.

/**
 * We add the {@link BlockInstance} to the local cache and persist it to
 * a back-end data-store if necessary
 *
 * @param scheduledBlockLocation TODO
 * @param samples
 */
private void putBlockLocationRecord(BlockInstance blockInstance, VehicleLocationRecord record, ScheduledBlockLocation scheduledBlockLocation, ScheduleDeviationSamples samples) {
    // Cache the result
    VehicleLocationCacheElements elements = _cache.addRecord(blockInstance, record, scheduledBlockLocation, samples);
    if (!CollectionsLibrary.isEmpty(_blockLocationListeners)) {
        /**
         * We only fill in the block location if we have listeners
         */
        BlockLocation location = getBlockLocation(blockInstance, elements, scheduledBlockLocation, record.getTimeOfRecord());
        if (location != null) {
            for (BlockLocationListener listener : _blockLocationListeners) {
                listener.handleBlockLocation(location);
            }
        }
    }
    if (_persistBlockLocationRecords) {
        List<BlockLocationRecord> blockLocationRecords = getVehicleLocationRecordAsBlockLocationRecord(blockInstance, record, scheduledBlockLocation);
        addPredictionToPersistenceQueue(blockLocationRecords);
    }
}
Also used : VehicleLocationCacheElements(org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements) BlockLocationListener(org.onebusaway.transit_data_federation.services.realtime.BlockLocationListener) ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) BlockLocation(org.onebusaway.transit_data_federation.services.realtime.BlockLocation)

Example 9 with VehicleLocationCacheElements

use of org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements in project onebusaway-application-modules by camsys.

the class BlockLocationServiceImpl method getLocationsForBlockInstance.

@Override
public Map<AgencyAndId, List<BlockLocation>> getLocationsForBlockInstance(BlockInstance blockInstance, List<Date> times, long currentTime) {
    Map<AgencyAndId, List<BlockLocation>> locationsByVehicleId = new FactoryMap<AgencyAndId, List<BlockLocation>>(new ArrayList<BlockLocation>());
    for (Date time : times) {
        TargetTime tt = new TargetTime(time.getTime(), currentTime);
        List<VehicleLocationCacheElements> records = getBlockLocationRecordCollectionForBlock(blockInstance, tt);
        for (VehicleLocationCacheElements cacheRecord : records) {
            BlockLocation location = getBlockLocation(blockInstance, cacheRecord, null, time.getTime());
            if (location != null) {
                locationsByVehicleId.get(location.getVehicleId()).add(location);
            }
        }
    }
    return locationsByVehicleId;
}
Also used : FactoryMap(org.onebusaway.collections.FactoryMap) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) VehicleLocationCacheElements(org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements) List(java.util.List) ArrayList(java.util.ArrayList) ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) BlockLocation(org.onebusaway.transit_data_federation.services.realtime.BlockLocation) TargetTime(org.onebusaway.transit_data_federation.model.TargetTime) Date(java.util.Date)

Example 10 with VehicleLocationCacheElements

use of org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements in project onebusaway-application-modules by camsys.

the class BlockLocationServiceImpl method getLocationsForBlockInstance.

@Override
public List<BlockLocation> getLocationsForBlockInstance(BlockInstance blockInstance, TargetTime time) {
    List<VehicleLocationCacheElements> records = getBlockLocationRecordCollectionForBlock(blockInstance, time);
    List<BlockLocation> locations = new ArrayList<BlockLocation>();
    for (VehicleLocationCacheElements cacheRecord : records) {
        BlockLocation location = getBlockLocation(blockInstance, cacheRecord, null, time.getTargetTime());
        if (location != null)
            locations.add(location);
    }
    return locations;
}
Also used : VehicleLocationCacheElements(org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements) ArrayList(java.util.ArrayList) ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) BlockLocation(org.onebusaway.transit_data_federation.services.realtime.BlockLocation)

Aggregations

VehicleLocationCacheElements (org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements)11 ArrayList (java.util.ArrayList)6 ScheduledBlockLocation (org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation)4 BlockLocation (org.onebusaway.transit_data_federation.services.realtime.BlockLocation)4 VehicleLocationRecord (org.onebusaway.realtime.api.VehicleLocationRecord)3 BlockInstance (org.onebusaway.transit_data_federation.services.blocks.BlockInstance)3 List (java.util.List)2 Test (org.junit.Test)2 FactoryMap (org.onebusaway.collections.FactoryMap)2 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)2 BlockEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.BlockEntryImpl)2 TripEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl)2 VehicleLocationCacheElement (org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElement)2 BlockConfigurationEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry)2 Date (java.util.Date)1 Map (java.util.Map)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 Range (org.onebusaway.collections.Range)1 TargetTime (org.onebusaway.transit_data_federation.model.TargetTime)1