public IHttpActionResult GetRouteSummary(long id) { if (DB.Routes.Any(x => x.Id == id)) { RouteSummary summary = new RouteSummary() { TotalReservations = DB.Reservations.Where(x => x.Route.Id == id && !x.Cancelled).Count() }; // List <int> bookedSeats = new List <int>(); foreach (var seat in DB.Reservations.Where(x => !x.Cancelled).Select(t => t.Seats)) { bookedSeats.AddRange(seat.Split(',').Select(x => int.Parse(x))); } summary.BookedSeats = bookedSeats.ToArray(); return(MapResponse(summary)); } else { return(NotFound()); } }
private static async Task TestRecordRouteAsync(Channel channel, int deltaLatitude = 5, int deltaLongitude = 5, CancellationToken cancellationToken = default) { var client = new RouteGuide.RouteGuideClient(channel); const int longitude = 34; const int latitude = 1; //var tasks = new List<Task>(deltaLatitude * deltaLongitude); using (AsyncClientStreamingCall <Point, RouteSummary> input = client.RecordRoute()) { for (int lat = latitude; lat < latitude + deltaLatitude; lat++) { for (int lo = longitude; lo < longitude + deltaLongitude; lo++) { Console.WriteLine($"queuing point {lat}:{lo}..."); //tasks.Add(input.RequestStream.WriteAsync(new Point() {Latitude = lat, Longitude = lo})); await input.RequestStream.WriteAsync(new Point() { Latitude = lat, Longitude = lo }).ConfigureAwait(false); } } //await Task.WhenAll(tasks).ConfigureAwait(false); Console.WriteLine($"all {deltaLongitude*deltaLatitude} points sent"); await input.RequestStream.CompleteAsync().ConfigureAwait(false); Console.WriteLine("request stream closed"); RouteSummary summary = await input.ResponseAsync.ConfigureAwait(false); Console.WriteLine($"Point count: {summary.PointCount}"); } }
/// <summary> /// Gets a stream of points, and responds with statistics about the "trip": number of points, /// number of known features visited, total distance traveled, and total time spent. /// </summary> public async Task <RouteSummary> RecordRoute(Grpc.Core.ServerCallContext context, Grpc.Core.IAsyncStreamReader <Point> requestStream) { int pointCount = 0; int featureCount = 0; int distance = 0; Point previous = null; var stopwatch = new Stopwatch(); stopwatch.Start(); while (await requestStream.MoveNext()) { var point = requestStream.Current; pointCount++; if (RouteGuideUtil.Exists(CheckFeature(point))) { featureCount++; } if (previous != null) { distance += (int)CalcDistance(previous, point); } previous = point; } stopwatch.Stop(); return(RouteSummary.CreateBuilder().SetPointCount(pointCount) .SetFeatureCount(featureCount).SetDistance(distance) .SetElapsedTime((int)(stopwatch.ElapsedMilliseconds / 1000)).Build()); }
public async Task <IActionResult> PutRouteSummary([FromRoute] int id, [FromBody] RouteSummary routeSummary) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != routeSummary.Id) { return(BadRequest()); } _context.Entry(routeSummary).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!RouteSummaryExists(id)) { return(NotFound()); } else { throw; } } return(Ok(_context.RouteSummaries.Find(id))); }
// GET: api/Routes public List <RouteSummary> GetRoutes() { var routes = db.Routes; var summaryRoutes = new List <RouteSummary>(); foreach (var route in routes) { var summaryRoute = new RouteSummary(route); summaryRoutes.Add(summaryRoute); } return(summaryRoutes); }
public async Task <IActionResult> PostRouteSummary([FromBody] RouteSummary routeSummary) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _context.RouteSummaries.Add(routeSummary); await _context.SaveChangesAsync(); return(CreatedAtAction("GetRouteSummary", new { id = routeSummary.Id }, routeSummary)); }
// GET: api/Routes public List <RouteSummary> GetRoutes() { using (var db = new TrolleyTracker.Models.TrolleyTrackerContext()) { var routes = db.Routes; var summaryRoutes = new List <RouteSummary>(); foreach (var route in routes) { var summaryRoute = new RouteSummary(route); summaryRoutes.Add(summaryRoute); } return(summaryRoutes); } }
private void AddRoute(RouteSummary route) { var routeHeader = new StackLayout(); routeHeader.SetValue(Grid.ColumnSpanProperty, 4); routeHeader.Orientation = StackOrientation.Horizontal; routeHeader.Margin = new Thickness(40, 5, 0, 0); var icon = new LagoVista.XPlat.Core.Icon(); icon.IconKey = "fa-map"; icon.Margin = new Thickness(0, 5, 0, 0); icon.FontSize = 16; icon.TextColor = Color.FromRgb(0xD4, 0x8D, 0x17); routeHeader.Children.Add(icon); var routeLabel = new Label() { Text = "Route: ", FontSize = 16, FontAttributes = FontAttributes.Bold, }; routeHeader.Children.Add(routeLabel); var routeName = new Label() { Text = $"{route.Name} ({route.MessageTypes.First().Name})", FontSize = 16, }; routeHeader.Children.Add(routeName); RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) }); routeHeader.SetValue(Grid.RowProperty, RowDefinitions.Count - 1); Children.Add(routeHeader); AddSectionHeader(50, "fa-list-ol", PlatformManagerResources.InstanceDetails_PipelineModules); foreach (var pipelienModule in route.PipelineModules) { AddItem(60, pipelienModule.Name, pipelienModule.Status.ToString(), pipelienModule.Type, PIPELINEMODULE_TYPE, pipelienModule.Id); } }
/// <summary> /// Client-streaming example. Sends numPoints randomly chosen points from features /// with a variable delay in between. Prints the statistics when they are sent from the server. /// </summary> public async Task RecordRoute(List <Feature> features, int numPoints) { try { Log("*** RecordRoute Client-streaming example. Sends numPoints randomly chosen points from features with a variable delay in between. Prints the statistics when they are sent from the server."); Log("*** RecordRoute"); using (var call = client.RecordRoute()) { // Send numPoints points randomly selected from the features list. StringBuilder numMsg = new StringBuilder(); Random rand = new Random(); for (int i = 0; i < numPoints; ++i) { int index = rand.Next(features.Count); Point point = features[index].Location; Log("Visiting point {0}, {1}", point.GetLatitude(), point.GetLongitude()); await call.RequestStream.WriteAsync(point); // A bit of delay before sending the next one. await Task.Delay(rand.Next(1000) + 500); } await call.RequestStream.CompleteAsync(); RouteSummary summary = await call.ResponseAsync; Log("Finished trip with {0} points. Passed {1} features. " + "Travelled {2} meters. It took {3} seconds.", summary.PointCount, summary.FeatureCount, summary.Distance, summary.ElapsedTime); Log("Finished RecordRoute"); } } catch (RpcException e) { Log("RPC failed", e); throw; } }
public override async Task <RouteSummary> RecordRoute(IAsyncStreamReader <Point> requestStream, ServerCallContext context) { Notify("record route..."); DateTime start = DateTime.Now; var summary = new RouteSummary(); bool hasNext = await requestStream.MoveNext(context.CancellationToken).ConfigureAwait(false); while (hasNext) { if (context.CancellationToken.IsCancellationRequested) { context.Status = Status.DefaultCancelled; return(DefaultRouteSummary); } var point = requestStream.Current; Notify($"receiving one new point {point.Latitude}:{point.Longitude}"); summary.PointCount++; hasNext = await requestStream.MoveNext(context.CancellationToken).ConfigureAwait(false); } summary.ElapsedTime = (int)(DateTime.Now - start).TotalMilliseconds; Notify("...route recorded"); return(summary); }
/// <summary> /// This method handles the task of calling the remote gRPC Service RecordRoute by passing a STREAM of /// Point Message Types. Upon completion of the asynchronous stream, the remote server calculates the distance /// between all the points and returns a single RouteSummary Message Type back. /// </summary> /// <param name="pointsOfInterest">An array of Routeguide Points</param> /// <returns></returns> public async Task RecordRoute(Routeguide.Point[] pointsOfInterest) { try { var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)); //Sending and Receiving will be sequential - send/stream all first, then receive var thisStream = _client.RecordRoute(cancellationToken: cts.Token); foreach (var t in pointsOfInterest) { await thisStream.RequestStream.WriteAsync(t); } await thisStream.RequestStream.CompleteAsync(); RouteSummary summary = await thisStream.ResponseAsync; var myResultSummary = summary.ToString(); if (!String.IsNullOrEmpty(myResultSummary)) { _myRouteGuideUiHandler.AddTextToUi(myResultSummary, false); } else { #if DEBUG Debug.Log("async Task RecordRoute empty"); #endif } } catch (RpcException e) { _myRouteGuideUiHandler.AddTextToUi("RecordRoute Service is unavailable. " + e.Message, false); } #if DEBUG Debug.Log("async Task RecordRoute Finished"); #endif }