1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
using System.Net;
using Costasdev.Busurbano.Backend.Configuration;
using Costasdev.Busurbano.Backend.GraphClient;
using Costasdev.Busurbano.Backend.GraphClient.App;
using Costasdev.Busurbano.Backend.Helpers;
using Costasdev.Busurbano.Backend.Services;
using Costasdev.Busurbano.Backend.Types;
using Costasdev.Busurbano.Backend.Types.Arrivals;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
namespace Costasdev.Busurbano.Backend.Controllers;
[ApiController]
[Route("api/stops")]
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;
private readonly AppConfiguration _config;
public ArrivalsController(
ILogger<ArrivalsController> logger,
IMemoryCache cache,
HttpClient httpClient,
ArrivalsPipeline pipeline,
FeedService feedService,
IOptions<AppConfiguration> configOptions
)
{
_logger = logger;
_cache = cache;
_httpClient = httpClient;
_pipeline = pipeline;
_feedService = feedService;
_config = configOptions.Value;
}
[HttpGet("arrivals")]
public async Task<IActionResult> GetArrivals(
[FromQuery] string id,
[FromQuery] bool reduced
)
{
var tz = TimeZoneInfo.FindSystemTimeZoneById("Europe/Madrid");
var nowLocal = TimeZoneInfo.ConvertTime(DateTime.UtcNow, tz);
var todayLocal = nowLocal.Date;
var requestContent = ArrivalsAtStopContent.Query(
new ArrivalsAtStopContent.Args(id, reduced)
);
var request = new HttpRequestMessage(HttpMethod.Post, $"{_config.OpenTripPlannerBaseUrl}/gtfs/v1");
request.Content = JsonContent.Create(new GraphClientRequest
{
Query = requestContent
});
var response = await _httpClient.SendAsync(request);
var responseBody = await response.Content.ReadFromJsonAsync<GraphClientResponse<ArrivalsAtStopResponse>>();
if (responseBody is not { IsSuccess: true } || responseBody.Data?.Stop == null)
{
LogErrorFetchingStopData(response.StatusCode, await response.Content.ReadAsStringAsync());
return StatusCode(500, "Error fetching stop data");
}
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)
{
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;
Arrival arrival = new()
{
TripId = item.Trip.GtfsId,
Route = new RouteInfo
{
GtfsId = item.Trip.Route.GtfsId,
ShortName = item.Trip.RouteShortName,
Colour = item.Trip.Route.Color ?? "FFFFFF",
TextColour = item.Trip.Route.TextColor ?? "000000"
},
Headsign = new HeadsignInfo
{
Destination = item.Headsign
},
Estimate = new ArrivalDetails
{
Minutes = minutesToArrive,
Precision = departureTime < nowLocal.AddMinutes(-1) ? ArrivalPrecision.Past : ArrivalPrecision.Scheduled
},
RawOtpTrip = item
};
arrivals.Add(arrival);
}
await _pipeline.ExecuteAsync(new ArrivalsContext
{
StopId = id,
StopCode = stop.Code,
IsReduced = reduced,
Arrivals = arrivals,
NowLocal = nowLocal,
StopLocation = new Position { Latitude = stop.Lat, Longitude = stop.Lon }
});
var feedId = id.Split(':')[0];
// Time after an arrival's time to still include it in the response. This is useful without real-time data, for delayed buses.
var timeThreshold = GetThresholdForFeed(id);
return Ok(new StopArrivalsResponse
{
StopCode = _feedService.NormalizeStopCode(feedId, stop.Code),
StopName = _feedService.NormalizeStopName(feedId, stop.Name),
StopLocation = new Position
{
Latitude = stop.Lat,
Longitude = stop.Lon
},
Routes = [.. stop.Routes
.OrderBy(
r => r.ShortName,
Comparer<string?>.Create(SortingHelper.SortRouteShortNames)
)
.Select(r => new RouteInfo
{
GtfsId = r.GtfsId,
ShortName = _feedService.NormalizeRouteShortName(feedId, r.ShortName ?? ""),
Colour = r.Color ?? "FFFFFF",
TextColour = r.TextColor ?? "000000"
})],
Arrivals = [.. arrivals.Where(a => a.Estimate.Minutes >= timeThreshold)]
});
}
private static int GetThresholdForFeed(string id)
{
string feedId = id.Split(':', 2)[0];
if (feedId == "vitrasa" || feedId == "coruna")
{
return 0;
}
return -30;
}
[LoggerMessage(LogLevel.Error, "Error fetching stop data, received {statusCode} {responseBody}")]
partial void LogErrorFetchingStopData(HttpStatusCode statusCode, string responseBody);
}
|