aboutsummaryrefslogtreecommitdiff
path: root/src/Costasdev.Busurbano.Backend/Services/Providers/RenfeTransitProvider.cs
diff options
context:
space:
mode:
authorAriel Costas Guerrero <ariel@costas.dev>2025-12-07 23:33:10 +0100
committerAriel Costas Guerrero <ariel@costas.dev>2025-12-07 23:37:38 +0100
commita1d589c1a0d5a5010e5fe4e8a1ec403ffafb289f (patch)
tree870366d9ce178530b836086e432331f78ec4a07e /src/Costasdev.Busurbano.Backend/Services/Providers/RenfeTransitProvider.cs
parent5fa8d1ffeb4a3a0c5c6846de3986ec779a4fe564 (diff)
Implement Renfe data source
Diffstat (limited to 'src/Costasdev.Busurbano.Backend/Services/Providers/RenfeTransitProvider.cs')
-rw-r--r--src/Costasdev.Busurbano.Backend/Services/Providers/RenfeTransitProvider.cs76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/Costasdev.Busurbano.Backend/Services/Providers/RenfeTransitProvider.cs b/src/Costasdev.Busurbano.Backend/Services/Providers/RenfeTransitProvider.cs
new file mode 100644
index 0000000..55e880f
--- /dev/null
+++ b/src/Costasdev.Busurbano.Backend/Services/Providers/RenfeTransitProvider.cs
@@ -0,0 +1,76 @@
+using Costasdev.Busurbano.Backend.Configuration;
+using Costasdev.Busurbano.Backend.Extensions;
+using Costasdev.Busurbano.Backend.Types;
+using Microsoft.Extensions.Options;
+using SysFile = System.IO.File;
+
+namespace Costasdev.Busurbano.Backend.Services.Providers;
+
+public class RenfeTransitProvider : ITransitProvider
+{
+ private readonly AppConfiguration _configuration;
+ private readonly ILogger<RenfeTransitProvider> _logger;
+
+ public RenfeTransitProvider(IOptions<AppConfiguration> options, ILogger<RenfeTransitProvider> logger)
+ {
+ _configuration = options.Value;
+ _logger = logger;
+ }
+
+ public async Task<List<ConsolidatedCirculation>> GetCirculationsAsync(string stopId, DateTime nowLocal)
+ {
+ var todayDate = nowLocal.Date.ToString("yyyy-MM-dd");
+ var stopArrivals = await LoadStopArrivalsProto(stopId, todayDate);
+
+ if (stopArrivals == null)
+ {
+ return [];
+ }
+
+ var now = nowLocal.AddSeconds(60 - nowLocal.Second);
+ var scopeEnd = now.AddMinutes(300);
+
+ var scheduledWindow = stopArrivals.Arrivals
+ .Where(c => c.CallingDateTime(nowLocal.Date) != null)
+ .Where(c => c.CallingDateTime(nowLocal.Date)!.Value >= now && c.CallingDateTime(nowLocal.Date)!.Value <= scopeEnd)
+ .OrderBy(c => c.CallingDateTime(nowLocal.Date)!.Value);
+
+ var consolidatedCirculations = new List<ConsolidatedCirculation>();
+
+ foreach (var sched in scheduledWindow)
+ {
+ var minutes = (int)(sched.CallingDateTime(nowLocal.Date)!.Value - now).TotalMinutes;
+
+ consolidatedCirculations.Add(new ConsolidatedCirculation
+ {
+ Line = sched.Line,
+ Route = sched.Route,
+ Schedule = new ScheduleData
+ {
+ Running = sched.StartingDateTime(nowLocal.Date)!.Value <= now,
+ Minutes = minutes,
+ TripId = sched.TripId,
+ ServiceId = sched.ServiceId,
+ ShapeId = sched.ShapeId,
+ },
+ RealTime = null
+ });
+ }
+
+ return consolidatedCirculations;
+ }
+
+ private async Task<StopArrivals?> LoadStopArrivalsProto(string stopId, string dateString)
+ {
+ var file = Path.Combine(_configuration.RenfeScheduleBasePath, dateString, stopId + ".pb");
+ if (!SysFile.Exists(file))
+ {
+ _logger.LogWarning("Stop arrivals proto file not found: {File}", file);
+ return null;
+ }
+
+ var contents = await SysFile.ReadAllBytesAsync(file);
+ var stopArrivals = StopArrivals.Parser.ParseFrom(contents);
+ return stopArrivals;
+ }
+}