aboutsummaryrefslogtreecommitdiff
path: root/src/Costasdev.Busurbano.Backend/Controllers
diff options
context:
space:
mode:
Diffstat (limited to 'src/Costasdev.Busurbano.Backend/Controllers')
-rw-r--r--src/Costasdev.Busurbano.Backend/Controllers/ArrivalsController.cs53
-rw-r--r--src/Costasdev.Busurbano.Backend/Controllers/TileController.cs62
2 files changed, 54 insertions, 61 deletions
diff --git a/src/Costasdev.Busurbano.Backend/Controllers/ArrivalsController.cs b/src/Costasdev.Busurbano.Backend/Controllers/ArrivalsController.cs
index 7158137..934935e 100644
--- a/src/Costasdev.Busurbano.Backend/Controllers/ArrivalsController.cs
+++ b/src/Costasdev.Busurbano.Backend/Controllers/ArrivalsController.cs
@@ -1,6 +1,7 @@
using System.Net;
using Costasdev.Busurbano.Backend.GraphClient;
using Costasdev.Busurbano.Backend.GraphClient.App;
+using Costasdev.Busurbano.Backend.Services;
using Costasdev.Busurbano.Backend.Types.Arrivals;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
@@ -14,16 +15,22 @@ public partial class ArrivalsController : ControllerBase
private readonly ILogger<ArrivalsController> _logger;
private readonly IMemoryCache _cache;
private readonly HttpClient _httpClient;
+ private readonly ArrivalsPipeline _pipeline;
+ private readonly FeedService _feedService;
public ArrivalsController(
ILogger<ArrivalsController> logger,
IMemoryCache cache,
- HttpClient httpClient
+ HttpClient httpClient,
+ ArrivalsPipeline pipeline,
+ FeedService feedService
)
{
_logger = logger;
_cache = cache;
_httpClient = httpClient;
+ _pipeline = pipeline;
+ _feedService = feedService;
}
[HttpGet("arrivals")]
@@ -37,11 +44,7 @@ public partial class ArrivalsController : ControllerBase
var todayLocal = nowLocal.Date;
var requestContent = ArrivalsAtStopContent.Query(
- new ArrivalsAtStopContent.Args(
- id,
- reduced ? 4 : 10,
- ShouldFetchPastArrivals(id)
- )
+ new ArrivalsAtStopContent.Args(id, reduced)
);
var request = new HttpRequestMessage(HttpMethod.Post, "http://100.67.54.115:3957/otp/gtfs/v1");
@@ -60,10 +63,26 @@ public partial class ArrivalsController : ControllerBase
}
var stop = responseBody.Data.Stop;
+ _logger.LogInformation("Fetched {Count} arrivals for stop {StopName} ({StopId})", stop.Arrivals.Count, stop.Name, id);
+
List<Arrival> arrivals = [];
foreach (var item in stop.Arrivals)
{
- var departureTime = todayLocal.AddSeconds(item.ScheduledDepartureSeconds);
+ if (item.PickupTypeParsed.Equals(ArrivalsAtStopResponse.PickupType.None))
+ {
+ continue;
+ }
+
+ if (item.Trip.Geometry?.Points != null)
+ {
+ _logger.LogDebug("Trip {TripId} has geometry", item.Trip.GtfsId);
+ }
+
+ // Calculate departure time using the service day in the feed's timezone (Europe/Madrid)
+ // This ensures we treat ScheduledDepartureSeconds as relative to the local midnight of the service day
+ var serviceDayLocal = TimeZoneInfo.ConvertTime(DateTimeOffset.FromUnixTimeSeconds(item.ServiceDay), tz);
+ var departureTime = serviceDayLocal.Date.AddSeconds(item.ScheduledDepartureSeconds);
+
var minutesToArrive = (int)(departureTime - nowLocal).TotalMinutes;
//var isRunning = departureTime < nowLocal;
@@ -89,17 +108,29 @@ public partial class ArrivalsController : ControllerBase
Estimate = new ArrivalDetails
{
Minutes = minutesToArrive,
- Precision = departureTime < nowLocal ? ArrivalPrecision.Past : ArrivalPrecision.Scheduled
- }
+ Precision = departureTime < nowLocal.AddMinutes(-1) ? ArrivalPrecision.Past : ArrivalPrecision.Scheduled
+ },
+ RawOtpTrip = item
};
arrivals.Add(arrival);
}
- return Ok(new StopArrivalsResponse
+ await _pipeline.ExecuteAsync(new ArrivalsContext
{
+ StopId = id,
StopCode = stop.Code,
- StopName = stop.Name,
+ IsReduced = reduced,
+ Arrivals = arrivals,
+ NowLocal = nowLocal
+ });
+
+ var feedId = id.Split(':')[0];
+
+ return Ok(new StopArrivalsResponse
+ {
+ StopCode = _feedService.NormalizeStopCode(feedId, stop.Code),
+ StopName = _feedService.NormalizeStopName(feedId, stop.Name),
Arrivals = arrivals
});
}
diff --git a/src/Costasdev.Busurbano.Backend/Controllers/TileController.cs b/src/Costasdev.Busurbano.Backend/Controllers/TileController.cs
index 6354d67..0e9d21b 100644
--- a/src/Costasdev.Busurbano.Backend/Controllers/TileController.cs
+++ b/src/Costasdev.Busurbano.Backend/Controllers/TileController.cs
@@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System.Text.Json;
using Costasdev.Busurbano.Backend.Helpers;
+using Costasdev.Busurbano.Backend.Services;
namespace Costasdev.Busurbano.Backend.Controllers;
@@ -20,29 +21,21 @@ public class TileController : ControllerBase
private readonly ILogger<TileController> _logger;
private readonly IMemoryCache _cache;
private readonly HttpClient _httpClient;
+ private readonly FeedService _feedService;
public TileController(
ILogger<TileController> logger,
IMemoryCache cache,
- HttpClient httpClient
+ HttpClient httpClient,
+ FeedService feedService
)
{
_logger = logger;
_cache = cache;
_httpClient = httpClient;
+ _feedService = feedService;
}
- private static readonly string[] HiddenStops =
- [
- "vitrasa:20223", // Castrelos (Pavillón - U1)
- "vitrasa:20146", // García Barbón, 7 (A, 18A)
- "vitrasa:20220", // COIA-SAMIL (15)
- "vitrasa:20001", // Samil por Beiramar (15B)
- "vitrasa:20002", // Samil por Torrecedeira (15C)
- "vitrasa:20144", // Samil por Coia (C3d, C3i)
- "vitrasa:20145" // Samil por Bouzs (C3d, C3i)
- ];
-
[HttpGet("stops/{z:int}/{x:int}/{y:int}")]
public async Task<IActionResult> Stops(int z, int x, int y)
{
@@ -97,24 +90,14 @@ public class TileController : ControllerBase
{
var idParts = stop.GtfsId.Split(':', 2);
string feedId = idParts[0];
- string codeWithinFeed = stop.Code ?? string.Empty;
+ string codeWithinFeed = _feedService.NormalizeStopCode(feedId, stop.Code ?? string.Empty);
- // TODO: Refactor this, maybe do it client-side or smth
- if (feedId == "vitrasa")
- {
- var digits = new string(codeWithinFeed.Where(char.IsDigit).ToArray());
- if (int.TryParse(digits, out int code))
- {
- codeWithinFeed = code.ToString();
- }
- }
-
- if (HiddenStops.Contains($"{feedId}:{codeWithinFeed}"))
+ if (_feedService.IsStopHidden($"{feedId}:{codeWithinFeed}"))
{
return;
}
- var (Color, TextColor) = GetFallbackColourForFeed(idParts[0]);
+ var (Color, TextColor) = _feedService.GetFallbackColourForFeed(idParts[0]);
var distinctRoutes = GetDistinctRoutes(feedId, stop.Routes ?? []);
Feature feature = new()
@@ -129,7 +112,7 @@ public class TileController : ControllerBase
// The public identifier, usually feed:code or feed:id, recognisable by users and in other systems
{ "code", $"{idParts[0]}:{codeWithinFeed}" },
// The name of the stop
- { "name", stop.Name },
+ { "name", _feedService.NormalizeStopName(feedId, stop.Name) },
// Routes
{ "routes", JsonSerializer
.Serialize(
@@ -176,21 +159,15 @@ public class TileController : ControllerBase
return File(ms.ToArray(), "application/x-protobuf");
}
- private static List<StopTileResponse.Route> GetDistinctRoutes(string feedId, List<StopTileResponse.Route> routes)
+ private List<StopTileResponse.Route> GetDistinctRoutes(string feedId, List<StopTileResponse.Route> routes)
{
List<StopTileResponse.Route> distinctRoutes = [];
HashSet<string> seen = new();
foreach (var route in routes)
{
- var seenId = route.ShortName;
- if (feedId == "xunta")
- {
- // For Xunta routes we take only the contract number (XG123, for example)
- seenId = seenId.Substring(0, 5);
-
- route.ShortName = seenId;
- }
+ var seenId = _feedService.NormalizeRouteShortName(feedId, route.ShortName ?? string.Empty);
+ route.ShortName = seenId;
if (seen.Contains(seenId))
{
@@ -206,19 +183,4 @@ public class TileController : ControllerBase
Comparer<string?>.Create(SortingHelper.SortRouteShortNames)
)];
}
-
- private static (string Color, string TextColor) GetFallbackColourForFeed(string feed)
- {
- return feed switch
- {
- "vitrasa" => ("#95D516", "#000000"),
- "santiago" => ("#508096", "#FFFFFF"),
- "coruna" => ("#E61C29", "#FFFFFF"),
- "xunta" => ("#007BC4", "#FFFFFF"),
- "renfe" => ("#870164", "#FFFFFF"),
- "feve" => ("#EE3D32", "#FFFFFF"),
- _ => ("#000000", "#FFFFFF"),
-
- };
- }
}