diff options
| author | Ariel Costas Guerrero <ariel@costas.dev> | 2025-11-14 17:33:58 +0100 |
|---|---|---|
| committer | Ariel Costas Guerrero <ariel@costas.dev> | 2025-11-14 17:33:58 +0100 |
| commit | 033df2ee521fb8b4a1e091a0ccdc82e142488685 (patch) | |
| tree | d1f0aa4de5a895ea601d1663d3b546c8c3b4a802 /src/Costasdev.Busurbano.Backend/Controllers | |
| parent | 52f342f9135264216cdbf3012ea115981d0bc294 (diff) | |
Add ShapeTraversalService for shape loading and bus position calculation
Diffstat (limited to 'src/Costasdev.Busurbano.Backend/Controllers')
| -rw-r--r-- | src/Costasdev.Busurbano.Backend/Controllers/VigoController.cs | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/src/Costasdev.Busurbano.Backend/Controllers/VigoController.cs b/src/Costasdev.Busurbano.Backend/Controllers/VigoController.cs index 84b0461..05a3025 100644 --- a/src/Costasdev.Busurbano.Backend/Controllers/VigoController.cs +++ b/src/Costasdev.Busurbano.Backend/Controllers/VigoController.cs @@ -2,6 +2,7 @@ using System.Globalization; using System.Text; using System.Text.Json; using Costasdev.Busurbano.Backend.Configuration; +using Costasdev.Busurbano.Backend.Services; using Costasdev.Busurbano.Backend.Types; using Costasdev.VigoTransitApi; using Microsoft.AspNetCore.Mvc; @@ -18,12 +19,14 @@ public class VigoController : ControllerBase private readonly ILogger<VigoController> _logger; private readonly VigoTransitApiClient _api; private readonly AppConfiguration _configuration; + private readonly ShapeTraversalService _shapeService; - public VigoController(HttpClient http, IOptions<AppConfiguration> options, ILogger<VigoController> logger) + public VigoController(HttpClient http, IOptions<AppConfiguration> options, ILogger<VigoController> logger, ShapeTraversalService shapeService) { _logger = logger; _api = new VigoTransitApiClient(http); _configuration = options.Value; + _shapeService = shapeService; } [HttpGet("GetStopEstimates")] @@ -93,6 +96,8 @@ public class VigoController : ControllerBase .Where(c => c.StartingDateTime() != null && c.CallingDateTime() != null) .ToList(); + var stopLocation = timetableTask.Result.Location; + var now = nowLocal.AddSeconds(60 - nowLocal.Second); // Define the scope end as the time of the last realtime arrival (no extra buffer) var lastEstimateArrivalMinutes = realTimeEstimates.Max(e => e.Minutes); @@ -206,13 +211,26 @@ public class VigoController : ControllerBase continue; } + var isRunning = closestCirculation.StartingDateTime()!.Value <= now; + Position? currentPosition = null; + + // Calculate bus position only for realtime trips that have already departed + if (isRunning && !string.IsNullOrEmpty(closestCirculation.ShapeId)) + { + var shape = await _shapeService.LoadShapeAsync(closestCirculation.ShapeId); + if (shape != null && stopLocation != null) + { + currentPosition = _shapeService.GetBusPosition(shape, stopLocation, estimate.Meters); + } + } + consolidatedCirculations.Add(new ConsolidatedCirculation { Line = estimate.Line, Route = estimate.Route, Schedule = new ScheduleData { - Running = closestCirculation.StartingDateTime()!.Value <= now, + Running = isRunning, Minutes = (int)(closestCirculation.CallingDateTime()!.Value - now).TotalMinutes, TripId = closestCirculation.TripId, ServiceId = closestCirculation.ServiceId, @@ -221,7 +239,8 @@ public class VigoController : ControllerBase { Minutes = estimate.Minutes, Distance = estimate.Meters - } + }, + CurrentPosition = currentPosition }); usedTripIds.Add(closestCirculation.TripId); @@ -280,6 +299,19 @@ public class VigoController : ControllerBase return stopArrivals; } + private async Task<Shape> LoadShapeProto(string shapeId) + { + var file = Path.Combine(_configuration.ScheduleBasePath, shapeId + ".pb"); + if (!SysFile.Exists(file)) + { + throw new FileNotFoundException(); + } + + var contents = await SysFile.ReadAllBytesAsync(file); + var shape = Shape.Parser.ParseFrom(contents); + return shape; + } + private async Task<List<ScheduledStop>> LoadTimetable(string stopId, string dateString) { var file = Path.Combine(_configuration.ScheduleBasePath, dateString, stopId + ".json"); |
