private void GetStopTimes(BusSnapshotInfo status, out StopTimeInfo last, out StopTimeInfo next) { last = next = null; var times = StaticData.GetStopTimesByTripId(status.TripId); var stoptimes = new LinkedList <StopTimeInfo>(times.OrderBy(st => st.Sequence)); if (stoptimes == null || stoptimes.Count == 0) { return; } var busTime = status.AdjustedTimestamp; LinkedListNode <StopTimeInfo> node = stoptimes.First; while (node != null) { if (busTime < node.Value.Arrival) { next = node.Value; break; } last = node.Value; node = node.Next; } }
private void GetStopTimes(BusSnapshotInfo status, out StopTimeInfo last, out StopTimeInfo next) { last = next = null; if (_stoptimes == null) { return; } var busTime = status.AdjustedTimestamp; LinkedListNode <StopTimeInfo> node = _stoptimes.First; while (node != null) { if (busTime < node.Value.Arrival) { next = node.Value; break; } last = node.Value; node = node.Next; } }
public Task <Tuple <StopTimeInfo, StopTimeInfo> > GetStopTimes(BusSnapshotInfo status) { StopTimeInfo last = null; StopTimeInfo next = null; GetStopTimes(status, out last, out next); return(Task.FromResult(Tuple.Create(last, next))); }
public Task <Tuple <StopTimeInfo, StopTimeInfo> > GetStopTimes(BusSnapshotInfo status) { if (_stoptimes == null) { var times = StaticData.GetStopTimesByTripId((int)this.GetPrimaryKeyLong()); _stoptimes = new LinkedList <StopTimeInfo>(times.OrderBy(st => st.Sequence)); } StopTimeInfo last = null; StopTimeInfo next = null; GetStopTimes(status, out last, out next); return(Task.FromResult(Tuple.Create(last, next))); }
public override async Task <ExceptionInfo> StopHandler(StopTimeInfo request, ServerCallContext context) { try { Logger.Log(Level.Info, "Received stop event at time {0}", request.StopTime); await _driverBridge.DispatchStopEvent(new BridgeDriverStopped(new DateTime(request.StopTime))); } catch (Exception ex) { Logger.Log(Level.Error, "Driver stop handler error", ex); return(GrpcUtils.SerializeException(ex)); } finally { /* Do not await on shutdown async, which will cause a deadlock since * shutdown waits for this method to return.*/ _server.ShutdownAsync(); } Logger.Log(Level.Info, "Clean stop handler execution"); return(new ExceptionInfo() { NoError = true }); }
/// <summary> /// Finds the shortest path using A* algorithm /// </summary> /// <param name="graph">The graph.</param> /// <param name="source">The source.</param> /// <param name="destinationStop">Name of the destination.</param> /// <returns>Shortest path between source and destination</returns> public Path <StopTimeInfo> FindShortestPath(Graph <int, StopTimeInfo> graph, StopTimeInfo source, StopDto destinationStop) { // Initialize extended list var nodesAlreadyExtended = new List <GraphNode <int, StopTimeInfo> >(); var nodesToExtend = new Dictionary <int, GraphNode <int, StopTimeInfo> > { { source.Id, graph.Nodes[source.Id] } }; // Initialize distance lookups var cameFrom = new Dictionary <int, GraphNode <int, StopTimeInfo> >(); var nodeDistances = graph.Nodes.ToDictionary(k => k.Key, v => double.MaxValue); var distanceFromSource = new Dictionary <int, double>(nodeDistances) { [source.Id] = 0 }; var totalDistanceToDestination = new Dictionary <int, double>(nodeDistances) { [source.Id] = source.StopDto.GetDistanceTo(destinationStop) * DistanceEnhancementFactor }; // Look for paths while (nodesToExtend.Any()) { // Get node with minimum distance var currentNodeWithMinimumDistance = nodesToExtend.Aggregate((l, r) => totalDistanceToDestination[l.Key] < totalDistanceToDestination[r.Key] ? l : r).Value; // If destination is reached if (currentNodeWithMinimumDistance.Data.StopDto.Name.TrimToLower() == destinationStop.Name.TrimToLower() || currentNodeWithMinimumDistance.Id.Equals(destinationStop?.Id)) { // Reconstruct path return(ReconstructPath(currentNodeWithMinimumDistance, graph, cameFrom, source.StopDto.Name)); } nodesToExtend.Remove(currentNodeWithMinimumDistance.Id); nodesAlreadyExtended.Add(currentNodeWithMinimumDistance); foreach (var neighborEdge in currentNodeWithMinimumDistance.Neighbors) { var neighbor = graph.Nodes[neighborEdge.DestinationId]; // Ignore the neighbor which is already extended if (nodesAlreadyExtended.Contains(neighbor)) { continue; } // Calculate the distance from start to a neighbor var distanceToNeighbor = distanceFromSource[currentNodeWithMinimumDistance.Id] + neighborEdge.Cost; if (!nodesToExtend.ContainsKey(neighbor.Id)) { nodesToExtend.Add(neighbor.Id, neighbor); } // Skip neighbor if it is not better than the best minimum distance so far else if (distanceToNeighbor >= distanceFromSource[neighbor.Id]) { continue; } // This path is the best until now. Record it! cameFrom[neighbor.Id] = currentNodeWithMinimumDistance; distanceFromSource[neighbor.Id] = distanceToNeighbor; var distanceFromNeighborToDestination = neighbor.Data.StopDto.GetDistanceTo(destinationStop) * DistanceEnhancementFactor; totalDistanceToDestination[neighbor.Id] = distanceFromSource[neighbor.Id] + distanceFromNeighborToDestination; } } return(new Path <StopTimeInfo>()); }